Skip to content

Commit 94a2cd1

Browse files
authored
Merge pull request #627 from GaiwanTeam/master
Fix cursor jumping for inputs inside shadow dom
2 parents 589e5a6 + b87e3af commit 94a2cd1

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/reagent/impl/input.cljs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,25 @@
1717

1818
(def ^:dynamic *force-set-dom-value* false)
1919

20+
(defn get-true-active-element []
21+
;; For controlled inputs `input-node-set-value` tries to do some heurestics to
22+
;; preserve the cursor position. But before doing so, it tries to find the active
23+
;; element in the document.
24+
;;
25+
;; Unfortunately, if an input element is inside a shadow dom, the
26+
;; activeElement points to the shadow root instead of the input element. This
27+
;; messes up the cursor perservation logic.
28+
;;
29+
;; But the saving grace here is that active input element can still be found
30+
;; by querying for the `.activeElement`` on the shadow-root.
31+
(let [active-element (.-activeElement js/document)]
32+
(if-let [shadow-root (.-shadowRoot active-element)]
33+
(.-activeElement shadow-root)
34+
active-element)))
35+
2036
(defn input-node-set-value
2137
[node rendered-value dom-value ^clj component {:keys [on-write]}]
22-
(if (or (not (and (identical? node (.-activeElement js/document))
38+
(if (or (not (and (identical? node (get-true-active-element))
2339
(has-selection-api? (.-type node))
2440
(string? rendered-value)
2541
(string? dom-value)))

0 commit comments

Comments
 (0)