From 50c20411dee9b55a903381a59ce54cc2c0e53eed Mon Sep 17 00:00:00 2001 From: agent Date: Wed, 18 Nov 2026 05:18:24 +0000 Subject: [PATCH 1/9] Attach the Thaana keyboard through a callback ref so it follows the element (#66, 3.1) Co-Authored-By: Claude Opus 5.5 --- .changeset/thaana-follows-element.md | 5 +++ docs/guides/thaana-input.md | 4 +-- docs/text-inputs/thaana-toggle.md | 3 +- .../onu/src/components/choice/_combobox.tsx | 3 +- .../components/choice/account-switcher.tsx | 3 +- .../src/components/choice/dual-listbox.tsx | 3 +- .../onu/src/components/inputs/tags-input.tsx | 10 ++++-- .../src/components/inputs/text-input.test.tsx | 8 +++++ .../onu/src/components/inputs/text-input.tsx | 11 +++++-- .../onu/src/components/inputs/textarea.tsx | 5 ++- .../onu/src/components/listing/search-box.tsx | 4 +-- .../overlays/command-palette.test.tsx | 20 +++++++++++ .../components/overlays/command-palette.tsx | 9 +++-- packages/onu/src/hooks/use-merged-ref.ts | 7 ++-- packages/onu/src/maps/_place-search.tsx | 3 +- packages/onu/src/thaana/thaana.test.tsx | 27 ++++++++++++--- packages/onu/src/thaana/use-thaana-field.ts | 17 ++++------ packages/onu/src/thaana/use-thaana-input.ts | 33 +++++++++---------- .../text-inputs/thaana-toggle.demo.tsx | 3 +- 19 files changed, 117 insertions(+), 61 deletions(-) create mode 100644 .changeset/thaana-follows-element.md diff --git a/.changeset/thaana-follows-element.md b/.changeset/thaana-follows-element.md new file mode 100644 index 000000000..f5ead5077 --- /dev/null +++ b/.changeset/thaana-follows-element.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +The Thaana keyboard now attaches to fields that mount later or get replaced, so `CommandPalette thaana` types Thaana and a `TextInput` keeps typing Thaana after an `endAddon` appears. `useThaanaInput(options)` and `useThaanaField(thaana)` now return a callback `ref` to put on the element instead of taking a ref object. diff --git a/docs/guides/thaana-input.md b/docs/guides/thaana-input.md index 8504c9923..ac1a13dd2 100644 --- a/docs/guides/thaana-input.md +++ b/docs/guides/thaana-input.md @@ -44,8 +44,8 @@ Pass your own map with `createKeymap(latinKeys, thaanaCharacters)`. import { useThaanaInput, attachThaanaKeyboard, transliterate } from '@javaabu/onu/thaana'; function MyField() { - const ref = useRef(null); - const { enabled, toggle } = useThaanaInput(ref, { keymap: 'phonetic' }); + // `ref` is a callback ref: the keyboard follows the element, even one that mounts later. + const { ref, enabled, toggle } = useThaanaInput({ keymap: 'phonetic' }); return ; } diff --git a/docs/text-inputs/thaana-toggle.md b/docs/text-inputs/thaana-toggle.md index 9182d78eb..02b026aca 100644 --- a/docs/text-inputs/thaana-toggle.md +++ b/docs/text-inputs/thaana-toggle.md @@ -10,8 +10,7 @@ import { ThaanaToggle } from "@javaabu/onu" import { useThaanaInput } from "@javaabu/onu/thaana" function MyField() { - const ref = useRef(null) - const { enabled, toggle } = useThaanaInput(ref) + const { ref, enabled, toggle } = useThaanaInput() return (
diff --git a/packages/onu/src/components/choice/_combobox.tsx b/packages/onu/src/components/choice/_combobox.tsx index aba1e042c..6bb5c0606 100644 --- a/packages/onu/src/components/choice/_combobox.tsx +++ b/packages/onu/src/components/choice/_combobox.tsx @@ -138,8 +138,7 @@ function SearchField({ className?: string onKeyDown?: React.KeyboardEventHandler }) { - const ref = React.useRef(null) - const { fieldProps } = useThaanaField(ref, thaana) + const { ref, fieldProps } = useThaanaField(thaana) return (
diff --git a/packages/onu/src/components/choice/account-switcher.tsx b/packages/onu/src/components/choice/account-switcher.tsx index 492da295b..3bf7ffca8 100644 --- a/packages/onu/src/components/choice/account-switcher.tsx +++ b/packages/onu/src/components/choice/account-switcher.tsx @@ -110,8 +110,7 @@ function SearchInput({ placeholder: string thaana?: ThaanaProp }) { - const ref = React.useRef(null) - const { fieldProps } = useThaanaField(ref, thaana) + const { ref, fieldProps } = useThaanaField(thaana) return (
diff --git a/packages/onu/src/components/choice/dual-listbox.tsx b/packages/onu/src/components/choice/dual-listbox.tsx index aaee1cf72..df1cf55f8 100644 --- a/packages/onu/src/components/choice/dual-listbox.tsx +++ b/packages/onu/src/components/choice/dual-listbox.tsx @@ -55,8 +55,7 @@ function FilterInput({ disabled?: boolean className?: string }) { - const ref = React.useRef(null) - const { fieldProps } = useThaanaField(ref, thaana) + const { ref, fieldProps } = useThaanaField(thaana) return (
(null) const anchorRef = React.useRef(null) - const merged = useMergedRef(local, ref) const [tags, setTags] = useControllableState(valueProp, defaultValue, onValueChange) const [text, setText] = React.useState("") const [announce, setAnnounce] = React.useState(null) - const { fieldProps, active: thaanaActive, enabled: thaanaOn, toggle } = useThaanaField(local, thaana) + const { + ref: thaanaRef, + fieldProps, + active: thaanaActive, + enabled: thaanaOn, + toggle, + } = useThaanaField(thaana) + const merged = useMergedRef(local, thaanaRef, ref) const { name, ...control } = useFormFieldControl({ ...props, disabled: disabledProp }) const disabled = !!control.disabled const full = max !== undefined && tags.length >= max diff --git a/packages/onu/src/components/inputs/text-input.test.tsx b/packages/onu/src/components/inputs/text-input.test.tsx index 517c22572..a3ced178e 100644 --- a/packages/onu/src/components/inputs/text-input.test.tsx +++ b/packages/onu/src/components/inputs/text-input.test.tsx @@ -70,6 +70,14 @@ describe("TextInput", () => { expect(input).toHaveValue("ަހމަދު") }) + it("keeps typing Thaana when an end addon replaces the input node", () => { + const { rerender } = renderWithOnu() + rerender() + const input = screen.getByRole("textbox") + typeThaana(input, "ahmadu") + expect(input).toHaveValue("ަހމަދު") + }) + it("toggles Thaana with the toggle button and announces it", async () => { renderWithOnu() const toggle = screen.getByRole("button", { name: /Thaana and Latin/ }) diff --git a/packages/onu/src/components/inputs/text-input.tsx b/packages/onu/src/components/inputs/text-input.tsx index 9b7749877..bbce0739c 100644 --- a/packages/onu/src/components/inputs/text-input.tsx +++ b/packages/onu/src/components/inputs/text-input.tsx @@ -94,10 +94,15 @@ export function TextInput({ ref, ...props }: TextInputProps) { - const local = React.useRef(null) - const merged = useMergedRef(local, ref) const latin = props.type === "email" || props.type === "url" - const { fieldProps, active, enabled, toggle } = useThaanaField(local, latin ? undefined : thaana) + const { + ref: thaanaRef, + fieldProps, + active, + enabled, + toggle, + } = useThaanaField(latin ? undefined : thaana) + const merged = useMergedRef(thaanaRef, ref) const controlProps = useFormFieldControl(latin ? latinProps(props, urlProtocol) : props) const showToggle = active && thaanaToggle const end = diff --git a/packages/onu/src/components/inputs/textarea.tsx b/packages/onu/src/components/inputs/textarea.tsx index 685b386d4..0151d5b23 100644 --- a/packages/onu/src/components/inputs/textarea.tsx +++ b/packages/onu/src/components/inputs/textarea.tsx @@ -31,9 +31,8 @@ export function Textarea({ onChange, ...props }: TextareaProps) { - const local = React.useRef(null) - const merged = useMergedRef(local, ref) - const { fieldProps, active, enabled, toggle } = useThaanaField(local, thaana) + const { ref: thaanaRef, fieldProps, active, enabled, toggle } = useThaanaField(thaana) + const merged = useMergedRef(thaanaRef, ref) const controlProps = useFormFieldControl(props) const [typed, setTyped] = React.useState(() => String(props.defaultValue ?? "").length) const count = props.value !== undefined ? String(props.value).length : typed diff --git a/packages/onu/src/components/listing/search-box.tsx b/packages/onu/src/components/listing/search-box.tsx index 8d546e89c..92c06ba21 100644 --- a/packages/onu/src/components/listing/search-box.tsx +++ b/packages/onu/src/components/listing/search-box.tsx @@ -63,8 +63,8 @@ export function SearchBox({ }: SearchBoxProps) { const { t } = useTranslation() const local = React.useRef(null) - const merged = useMergedRef(local, ref) - const { fieldProps } = useThaanaField(local, thaana) + const { ref: thaanaRef, fieldProps } = useThaanaField(thaana) + const merged = useMergedRef(local, thaanaRef, ref) const controlProps = useFormFieldControl(props) const [applied, setApplied] = React.useState(value ?? defaultValue) diff --git a/packages/onu/src/components/overlays/command-palette.test.tsx b/packages/onu/src/components/overlays/command-palette.test.tsx index e4070a25c..2cabf397f 100644 --- a/packages/onu/src/components/overlays/command-palette.test.tsx +++ b/packages/onu/src/components/overlays/command-palette.test.tsx @@ -274,6 +274,26 @@ describe("CommandPalette", () => { expect(input).toHaveAttribute("lang", "dv") expect(input).toHaveAttribute("dir", "rtl") expect(screen.getByRole("button", { name: /Thaana and Latin/ })).toBeInTheDocument() + fireEvent( + input, + new InputEvent("beforeinput", { inputType: "insertText", data: "k", cancelable: true, bubbles: true }), + ) + expect(input).toHaveValue("ކ") + }) + + it("types Thaana each time the palette opens", async () => { + renderWithOnu() + for (let i = 0; i < 2; i++) { + fireEvent.keyDown(document, { key: "k", ctrlKey: true }) + const input = await screen.findByRole("combobox") + fireEvent( + input, + new InputEvent("beforeinput", { inputType: "insertText", data: "k", cancelable: true, bubbles: true }), + ) + expect(input).toHaveValue("ކ") + fireEvent.keyDown(input, { key: "Escape" }) + await waitFor(() => expect(screen.queryByRole("combobox")).not.toBeInTheDocument()) + } }) it("closes from the phone close button", async () => { diff --git a/packages/onu/src/components/overlays/command-palette.tsx b/packages/onu/src/components/overlays/command-palette.tsx index d1f6e1b14..6b330d238 100644 --- a/packages/onu/src/components/overlays/command-palette.tsx +++ b/packages/onu/src/components/overlays/command-palette.tsx @@ -175,8 +175,13 @@ export function CommandPalette({ const [open, setOpenState] = useControllableState(openProp, defaultOpen, onOpenChange) const [query, setQuery] = React.useState("") const [known, setKnown] = React.useState>({}) - const inputRef = React.useRef(null) - const { fieldProps, active: thaanaActive, enabled: thaanaEnabled, toggle } = useThaanaField(inputRef, thaana) + const { + ref: inputRef, + fieldProps, + active: thaanaActive, + enabled: thaanaEnabled, + toggle, + } = useThaanaField(thaana) const recentKey = (typeof recent === "object" && recent.storageKey) || DEFAULT_RECENT_KEY const maxRecent = (typeof recent === "object" && recent.max) || 5 diff --git a/packages/onu/src/hooks/use-merged-ref.ts b/packages/onu/src/hooks/use-merged-ref.ts index e0025bb4a..ef8b51f91 100644 --- a/packages/onu/src/hooks/use-merged-ref.ts +++ b/packages/onu/src/hooks/use-merged-ref.ts @@ -7,13 +7,14 @@ function setRef(ref: React.Ref | undefined, node: T | null) { else if (ref) (ref as React.RefObject).current = node } -/** Merges a forwarded ref with a local one. */ -export function useMergedRef(a: React.Ref | undefined, b?: React.Ref) { +/** Merges a forwarded ref with local ones. */ +export function useMergedRef(a: React.Ref | undefined, b?: React.Ref, c?: React.Ref) { return React.useCallback( (node: T | null) => { setRef(a, node) setRef(b, node) + setRef(c, node) }, - [a, b], + [a, b, c], ) } diff --git a/packages/onu/src/maps/_place-search.tsx b/packages/onu/src/maps/_place-search.tsx index 48247366e..faa01a799 100644 --- a/packages/onu/src/maps/_place-search.tsx +++ b/packages/onu/src/maps/_place-search.tsx @@ -87,8 +87,7 @@ export function PlaceSearchBox({ "aria-label": ariaLabel, }: PlaceSearchBoxProps) { const { t } = useTranslation() - const inputRef = React.useRef(null) - const { fieldProps } = useThaanaField(inputRef, thaana) + const { ref: inputRef, fieldProps } = useThaanaField(thaana) const { query, setQuery, results, status, reset, retry } = usePlaceSearchBox({ search, minChars, debounce }) const [open, setOpen] = React.useState(false) const [active, setActive] = React.useState(-1) diff --git a/packages/onu/src/thaana/thaana.test.tsx b/packages/onu/src/thaana/thaana.test.tsx index 94668cf67..cf0f99ddb 100644 --- a/packages/onu/src/thaana/thaana.test.tsx +++ b/packages/onu/src/thaana/thaana.test.tsx @@ -258,8 +258,7 @@ describe("insertText", () => { describe("useThaanaInput", () => { function Field(props: { enabled?: boolean; onEnabledChange?: (v: boolean) => void; defaultEnabled?: boolean }) { - const ref = React.useRef(null) - const { enabled, toggle } = useThaanaInput(ref, { + const { ref, enabled, toggle } = useThaanaInput({ ...props, toggleShortcut: { code: "KeyT", altKey: true, shiftKey: true }, }) @@ -296,12 +295,30 @@ describe("useThaanaInput", () => { rerender() expect(getByRole("button")).toHaveTextContent("on") }) + + it("follows the element when it mounts later or is replaced", () => { + function Late({ show, swap }: { show: boolean; swap?: boolean }) { + const { ref } = useThaanaInput() + if (!show) return null + return swap ?