// // Copyright 2026 DXOS.org // // @import-as-namespace import { Editable as EditablePrimitive, useEditableContext } from '@ark-ui/react/editable'; import React, { type ComponentPropsWithRef, type PropsWithChildren, forwardRef, useEffect, useRef } from 'react'; import { createContext } from '@dxos/react-hooks'; import { mx } from '@dxos/ui-theme'; import { type ThemedClassName } from '@dxos/ui-types'; import { recipes } from '../../recipes.ts'; import * as Icon from '../Icon/Icon.tsx'; import { type EditableActivationBinding, type UseEditableOptions, useEditable } from './useEditable.ts'; const EDITABLE_NAME = 'Editable.Root'; const EDITABLE_PREVIEW_NAME = 'Editable.Preview'; const EDITABLE_INPUT_NAME = 'Editable.Input'; // The keyboard door onto the preview depends on the activation gesture, which the root is given and the machine the // parts read does not expose. const [EditableActivationProvider, useEditableActivation] = createContext(EDITABLE_NAME); // A controlled field's submit never reaches the machine's commit callback, so Save commits through the hook. const [EditableCommitProvider, useEditableCommit] = createContext<{ commit: () => void }>(EDITABLE_NAME); // // Root // type EditableRootProps = ThemedClassName< PropsWithChildren, keyof UseEditableOptions | 'children'>> >; /** Seeds the machine (through `useEditable`) and lays Preview and Input into one grid cell, so the swap moves nothing. */ const EditableRoot = forwardRef( ( { children, classNames, value, defaultValue, onValueChange, activation, blurBehavior, disabled, placeholder, editing, onEditingChange, ...props }, forwardedRef, ) => { const { api, activationProps, commit } = useEditable({ value, defaultValue, onValueChange, activation, blurBehavior, disabled, placeholder, editing, onEditingChange, }); return ( {children} ); }, ); EditableRoot.displayName = EDITABLE_NAME; // // Preview // type EditablePreviewProps = ThemedClassName, 'children'>>; /** The static text and the activation affordance; hidden by the machine while editing. */ const EditablePreview = forwardRef(({ classNames, ...props }, forwardedRef) => { const { valueText, edit } = useEditableContext(); const { role, onKeyDown } = useEditableActivation(EDITABLE_PREVIEW_NAME); return ( {valueText} {/* The pencil is a door of its own: one click opens the field whatever gesture the preview takes (a double-click in a list, where a single click selects the row). */} { event.stopPropagation(); edit(); }} /> ); }); EditablePreview.displayName = EDITABLE_PREVIEW_NAME; // // Input // type EditableInputProps = ThemedClassName, 'value' | 'onChange' | 'placeholder'>>; /** The field shown while editing, focused with the caret at the end, with a save button at its end. */ const EditableInput = forwardRef(({ classNames, ...props }, forwardedRef) => { const { editing } = useEditableContext(); const { commit } = useEditableCommit(EDITABLE_INPUT_NAME); const localRef = useRef(null); // The machine focuses the input but leaves the caret where the browser puts it, which from the keyboard is the start. useEffect(() => { const element = localRef.current; if (editing && element) { element.focus(); const end = element.value.length; element.setSelectionRange(end, end); } }, [editing]); return ( <> { localRef.current = element; // Typed `unknown`: `ForwardedRef`'s callback is declared void, but React 19 may return a cleanup. const cleanup: unknown = typeof forwardedRef === 'function' ? forwardedRef(element) : undefined; if (forwardedRef && typeof forwardedRef !== 'function') { forwardedRef.current = element; } return () => { localRef.current = null; if (typeof cleanup === 'function') { cleanup(); } }; }} /> {/* Saves the edit, at the input's end in the same cell; the machine hides it while not editing. */} commit()} > ); }); EditableInput.displayName = EDITABLE_INPUT_NAME; export { EditableInput as Input, EditablePreview as Preview, EditableRoot as Root }; export type { EditableInputProps as InputProps, EditablePreviewProps as PreviewProps, EditableRootProps as RootProps }; export * from './useEditable.ts';