Code Monkey home page Code Monkey logo

Comments (10)

kitten avatar kitten commented on June 11, 2024

I haven't been able to create a reproduction for this, so I unfortunately do have to ask you whether you can create a quick CodeSandbox that can reproduce this reliably, with some "click instructions". I've been trying all sorts of combinations of selecting, clicking, deselecting, clicking, etc in Firefox and Chrome, and can't quite get it to reproduce this reliably.

from use-editable.

carlesnunez avatar carlesnunez commented on June 11, 2024

I've found a slot to test it. The error occurs when a change is happens on the onClick event of an element. Here you have a case:
https://codesandbox.io/s/use-editable-forked-j9itq?file=/src/Edit.js

And a video with reproduce steps:

Screen.Recording.2021-08-13.at.03.11.47.PM.mp4

from use-editable.

carlesnunez avatar carlesnunez commented on June 11, 2024

Hello, any updates in this one? cc: @kitten

from use-editable.

carlesnunez avatar carlesnunez commented on June 11, 2024

I've been investigating a bit by my own and find out that the problem might be related with this code:

  useLayoutEffect(() => {
    state.onChange = onChange;

    if (!elementRef.current || opts!.disabled) return;

    state.disconnected = false;
    state.observer.observe(elementRef.current, observerSettings);
    if (state.position) {
      const { position, extent } = state.position;
      setCurrentRange(
        makeRange(elementRef.current, position, position + extent)
      );
    }

    return () => {
      state.observer.disconnect();
    };
  });

Commenting this piece of code makes the bug disappear, but OFC I assume that this is necessary.

cc: @kitten

from use-editable.

Ontopic avatar Ontopic commented on June 11, 2024

Your sandbox seems to be using Slate..?

from use-editable.

carlesnunez avatar carlesnunez commented on June 11, 2024

Hello @Ontopic I've fixed the codesandbox, sorry and THX for the heads up!:
https://codesandbox.io/s/use-editable-forked-j9itq?file=/src/Edit.js

from use-editable.

Ontopic avatar Ontopic commented on June 11, 2024

I had a quick look, what I think might be worth exploring is your direct call to setSelection; that rerenders your component (since it changes the useState value). So when you click, it shortly moves the cursor to the right position, but immediately rerenders and "resets" the cursor. Will have a better look later. Let me know if you find anything.

from use-editable.

Ontopic avatar Ontopic commented on June 11, 2024

Couldn't resist quickly trying out not putting select in a useState.

import React, { useState, useRef, useCallback } from "react";
import { useEditable } from "use-editable";

export default () => {
  const [txt, setTxt] = useState("Text");
  const editorRef = useRef(null);
  // const [selected, setSelected] = useState(false);
  const status = useRef({
    selected: false
  })
 
  const onEditableChange = useCallback((value) => {
    console.log(`"${value}"`)
    setTxt(value.slice(0, -1))
  }, [])

  const Edit = useEditable(editorRef, onEditableChange, {
    disabled: false,
    indentation: 2
  })

  return (
    <div
      className="App"
      ref={editorRef}
      // onClick={() => setSelected(!selected)}
      onClick={() => (status.current.selected = !status.current.selected)}
      
      style={{ backgroundColor: status.current.selected ? "gray" : "white" }}
    >
      {txt}
    </div>
  );
};

This at least fixes your bug, but selection is only shown when rerendered (so after setTxt is called). You can work around this in several ways, but I don't think your selected state should go through an onClick anyway. What if the editable is focused on page load, f.e., or entered through keyboard navigation? Trying with onFocus and onBlur would be more stable.

from use-editable.

carlesnunez avatar carlesnunez commented on June 11, 2024

I'm pretty sure that this shouldn't happen no matter which implementation do we have. Using a reference to avoid a re-render onClick IMHO is not a realistic solution that scales well. I've been trying older versions of the library and it seems that in previous versions this bug wasn't happening.

from use-editable.

carlesnunez avatar carlesnunez commented on June 11, 2024

A pr has been oppened.

from use-editable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.