Comments (10)
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.
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.
Hello, any updates in this one? cc: @kitten
from use-editable.
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.
Your sandbox seems to be using Slate..?
from use-editable.
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.
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.
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.
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.
A pr has been oppened.
from use-editable.
Related Issues (20)
- Contribution HOT 3
- Feature Suggestion: Disable line break HOT 2
- Deleting character in empty field throws error HOT 1
- Feature Request: Select All HOT 3
- Setting contentEditable without setAttribute does not set the attribute in js-dom HOT 4
- [Feature Request] Provide (x, y) coordinates HOT 1
- UseEditable keeps `state.position` after blur, and steals focus on rerender HOT 4
- Example code seems to be broken HOT 1
- delete long content (childrenElementCount =800) is very slow (takes ~ 6 sec) HOT 1
- Dropdown-example HOT 2
- Uncaught DOMException: Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index.
- Support meta data over data attributes
- First selection + delete/backspace press does not work HOT 2
- Edit.insert does not trigger onChange
- Element loses focus on first state change in React 18 HOT 5
- No parent error. HOT 2
- SSR is not working due to MutationObserver HOT 5
- onSelect event is not firing in Chrome & Safari HOT 2
- /n line added to contentEditable HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from use-editable.