-
Notifications
You must be signed in to change notification settings - Fork 414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Controlled input loses cursor under ShadowRoot in React 18 #597
Comments
Reagent's built-in workaround for controlled inputs doesn't work when we use the new `react.dom.client` API to render to a shadow root. This makes the cursor skip to the end whenever you try to insert a character into a text input field. This article describes the bug in some detail: https://github.com/reagent-project/reagent/blob/master/doc/ControlledInputs.md It also offers a workaround. Basically, for our :input components, instead of the :value prop (a controlled input), we use :default-value (uncontrolled). Then, we have to force the :input component to remount when we dispatch an event which would update the :default-value. That allows us to "control" the value via a subscription, while keeping the healthy cursor behavior of an "uncontrolled" input. So far, the only input we actually need to control is the new app-db editor. This is not a good smell, and could be a pitfall for future development. I think it's tolerable for now, though. I hope reagent can fix this issue - it seems really complicated. reagent-project/reagent#597
Not sure if this fixes the issue but that code doesn't seem quite right. It's creating a new input element every render cycle instead of updating an existing one. You could try adding a Lastly it seems like it's better to use (defn repro []
(with-let [text (r/atom "")]
[:input {:on-change #(reset! text (.. % -target -value))
:value @text}])) Not sure that will entirely fix it, but it's a place to start. |
Hey @jaidetree, thanks for taking a look. Were you able to run the repro without bugs? I think your hypothesis might be incorrect, though. As far as I know, |
@kimo-k Just note about
Reagents controlled input cursor hacks are a huge mess and our async rendering batching for ratom changes is really problematic with React 18 update batching so there is probably no easy fix for this. |
This controlled-input cursor bug appears when I try to use the new
react.dom.client
API to render an:input
component within a shadow root. The text cursor skips to the end every time I insert a character.The same component works fine with the
react.dom
API.Is this expected, and is there anything I can do? I'm reticent to go with the react-native workaround, since my codebase is pretty large, with everything inside a shadow root (i.e. re-frame-10x).
Minimal repro: master...kimo-k:reagent:shadow-root-controlled-input-bug
The text was updated successfully, but these errors were encountered: