Skip to main content

Guides

Resting caret

Let a native host quiet a focused editor while the person reads, without losing their place. Install the separate lib-use-resting-caret helper for use with TextField, TextArea or Composer. Omit this helper on ordinary web pages.

Permission comes from the host

Enable this only in a native host that can meet the whole contract below. Do not copy the demo's simulated permission into an app.

  • Keep enabled false when assistive technology or voice input is on, or its state is unknown. Do not infer this permission from reduced motion.
  • Supply subscribeWake(wake), returning an unsubscribe function. Call wake() synchronously before opening a native editing menu, delivering dictation, inserting from a character palette or forwarding other native input.
  • Supply onHeld(held). While held, intercept native input-method keys before the engine receives them, request a wake and forward the keys only after onHeld(false). Include dead keys, modifier keys and the Globe key.
  • Pass occluded when the host knows the window is covered or minimised.
  • Keep both callbacks stable. Change enabled to revoke permission.

Engines that support caret-animation: manual keep their own steady caret without changing editability.native-animation Other engines temporarily change the editor's editability, not just its caret colour. That fallback is why a host bridge is required: input that bypasses page events needs to wake the editor first. A browser page cannot establish that contract by itself.

import { useState } from "react";
import { TextArea } from "@/components/ouro/text-area";
import { useRestingCaret } from "@/lib/ouro/use-resting-caret";

function Draft({ readOnly, disabled }: { readOnly: boolean; disabled: boolean }) {
  const [box, setBox] = useState<HTMLInputElement | HTMLTextAreaElement | HTMLDivElement | null>(null);
  useRestingCaret(box, {
    enabled: host.caretRestPermitted,
    occluded: host.windowCovered,
    subscribeWake: host.subscribeCaretWake,
    onHeld: host.setCaretHeld,
  }, readOnly, disabled);
  return <TextArea ref={setBox} label="Draft" autoGrow readOnly={readOnly} disabled={disabled} />;
}

These are methods supplied by your host, not browser APIs. For an app without this bridge, omit useRestingCaret.

Pass the same readOnly and disabled state to the hook and the field: while held, the hook's own read-only state cannot identify a new caller-owned restriction. Use the state setter as the callback ref, not an object ref, so the hook can follow a replacement editor. Pass ref={setBox} to TextField or Composer too; Composer's rich editor uses a div rather than a textarea.ref When changing a TextField's native type, pass that value as the hook's fifth argument too.

The helper is separate from the components' props. The item tests exercise the helper with each actual editor, including plain-to-rich replacement.ref

A quiet place to return to

After thirty seconds without editing input, the focused editor shows a steady caret for a collapsed selection.idle Typing wakes it in capture phase, before the page's input handlers run; pointer movement and scrolling do not wake it.idle

A composing word stays editable, and changing the host's permission to false releases a hold. A caller's own readOnly remains the caller's.safety Native input types that expose no text selection keep their own caret.native

Page visibility and the host's cover signal also control the hold. A hidden page draws no replacement caret; an unfocused window keeps the engine's own caret behaviour.visibility The editor's data-caret-rest attribute exposes the current reason for inspection.

The fallback's steady caret is decoration, hidden from assistive technology. Text selection, including its direction, stays in the original control.selection Waking from the fallback starts a new native typing run; do not promise a single undo group across a rest.

Try Try quiet reading in the TextArea demo, then type without clicking the field again. Do not use this simulated host as an accessibility detector.

Prior art

Borrow the idle hold and host-wake contract from a consuming app, with permission kept explicit rather than enabled for the open web. Adopt the CSS caret-animation: manual property where supported: the engine owns its caret, with no replacement to position.native-animationcss

Claim sources