Comments (4)
After looking into this a bit. It appears the issue is a result of this useLayoutEffect.
When the current range is set, it draws the focus of the browser automatically. Then it is focused yet again with the call indicated by the OP.
Since no dependency array is passed in, the effect will get called on every react render. As far as I can tell, that is unnecessary. You should be able to declare the dependency array as follows:
[elementRef, onChange, opts, state, state.disconnected, state.observer, state.position]
@kitten I believe the above catches all scenarios in which the position needs to be set, etc. without taking focus from other elements unnecessarily but you could probably better say whether that is true or not.
I'm happy to create a PR if that makes things any easier for you.
from use-editable.
+1, this library is great, thank you for providing it.
I wanted to mention that I've noticed this behavior as well. I found that if I click outside any editable areas, then focus isn't stolen - but if I go directly from my editable <pre>
area that's controlled by this hook, to a normal text area that isn't controlled by this hook, I see the behavior described here.
from use-editable.
I have solve this focus issue by handling element's focus with a local state and using useEditable
's disable
option:
function EditableText({ as, text, onChange }) {
const editorRef = useRef(null)
const [hasFocus, setHasFocus] = useState(false)
useEditable(editorRef, onChange, { disabled: !hasFocus })
return React.createElement(
as,
{
ref: editorRef,
onClick: () => setHasFocus(true),
onBlur: () => setHasFocus(false),
},
text
)
}
// (EditableText is used in this way)
const [value, setValue] = useState()
const onChange = useCallback((text) => {
setValue(text)
}, [])
<EditableText as="h1" text={value} onChange={onChange} />
from use-editable.
It looks like the above works as long as opts
is not changing. We could either switch to only using opts.disabled
in the dependencies array (which might need eslint-disable-next-line react-hooks/exhaustive-deps
) or it would be an expectation for end users to ensure that opts is memoized with something along the lines of this:
const options = useMemo(() => ({
disabled: disabled,
}), [disabled]);
useEditable(editorRef, onEditableChange, options);
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
- Bug: Caret not positioning correctly when not clicking directly on the text HOT 10
- [Feature Request] Provide (x, y) coordinates HOT 1
- 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.