{"version":3,"file":"chunk-Focus.mjs","names":[],"sources":["../../src/components/Focus/FocusContext.ts","../../src/components/Focus/Focus.tsx"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\nimport { createContext, useContext } from 'react';\n\n// Kept out of `Focus.tsx`: react-refresh only fast-refreshes a module whose exports are all components.\n\n/** A state a member reports to its group, shown as the group's ring colour. */\nexport type FocusState = 'active' | 'error';\n\nexport type FocusContextValue = {\n  /** Called by a member (e.g. a drop target) to colour the group's ring. */\n  setFocus?: (state: FocusState | undefined) => void;\n  /** True while any element inside the group has DOM focus. */\n  groupHasFocus?: boolean;\n};\n\nexport const FocusContext = createContext<FocusContextValue>({});\n\n/** The enclosing `Focus.Group`'s state; empty outside one. */\nexport const useFocus = () => useContext(FocusContext);\n","//\n// Copyright 2026 DXOS.org\n//\n\n// @import-as-namespace\n\nimport { ark } from '@ark-ui/react/factory';\nimport React, {\n  type FocusEvent,\n  type KeyboardEvent,\n  type MouseEvent,\n  useCallback,\n  useMemo,\n  useRef,\n  useState,\n} from 'react';\n\nimport { useFocusGroup } from '@dxos/react-focus';\nimport { useComposedRefs } from '@dxos/react-hooks';\nimport { type Axis } from '@dxos/ui-types';\n\nimport { recipes } from '../../recipes.ts';\nimport { composableProps, slottable } from '../../util/slots.ts';\nimport { FocusContext, type FocusState } from './FocusContext.ts';\n\n//\n// Group\n//\n\ntype FocusGroupProps = {\n  /** `grid` moves to the member on screen in the arrow's direction; `grid-linear` does so for up and down only. */\n  orientation?: Axis | 'grid' | 'grid-linear';\n  /** Names the members at any depth, instead of the group's top-level focusables (e.g. a board's cards). */\n  itemSelector?: string;\n  /** A separator-coloured border while unfocused (e.g. a grid cell's edge). */\n  border?: boolean;\n  onKeyDown?: (event: KeyboardEvent<HTMLDivElement>) => void;\n};\n\n/**\n * One `Tab` stop over focusable children: arrow keys move between them, `Enter` moves into the group and `Escape`\n * leaves it. Members report a state (`useFocus().setFocus`) that colours the group's ring.\n */\nconst FocusGroup = slottable<HTMLDivElement, FocusGroupProps>(\n  ({ children, asChild, orientation = 'vertical', itemSelector, border, onKeyDown, ...props }, forwardedRef) => {\n    const rootRef = useRef<HTMLDivElement>(null);\n    const {\n      ref: focusGroupRef,\n      onKeyDown: onFocusGroupKeyDown,\n      onFocus: onFocusGroupFocus,\n      ...focusGroupAttrs\n    } = useFocusGroup({ axis: orientation, itemSelector, tabBehavior: 'limited-trap-focus', memorizeCurrent: true });\n    const [state, setState] = useState<FocusState | undefined>();\n    const [groupHasFocus, setGroupHasFocus] = useState(false);\n    const context = useMemo(() => ({ setFocus: setState, groupHasFocus }), [groupHasFocus]);\n\n    const handleKeyDown = useCallback(\n      (event: KeyboardEvent<HTMLDivElement>) => {\n        onFocusGroupKeyDown(event);\n        onKeyDown?.(event);\n      },\n      [onFocusGroupKeyDown, onKeyDown],\n    );\n\n    const handleFocus = useCallback(\n      (event: FocusEvent<HTMLDivElement>) => {\n        onFocusGroupFocus(event);\n        setGroupHasFocus(true);\n      },\n      [onFocusGroupFocus],\n    );\n\n    // Focus moving between members bubbles a blur too; only focus leaving the group clears it.\n    const handleBlur = useCallback((event: FocusEvent<HTMLDivElement>) => {\n      if (!(event.relatedTarget instanceof Node) || !rootRef.current?.contains(event.relatedTarget)) {\n        setGroupHasFocus(false);\n      }\n    }, []);\n\n    const { className, ...rest } = composableProps(props, { classNames: recipes.focusGroup() });\n    return (\n      <FocusContext.Provider value={context}>\n        <ark.div\n          asChild={asChild}\n          {...rest}\n          tabIndex={0}\n          {...focusGroupAttrs}\n          data-scope='focus'\n          data-part='group'\n          data-orientation={orientation}\n          data-border={border ? '' : undefined}\n          data-focus-state={state}\n          className={className}\n          onFocus={handleFocus}\n          onBlur={handleBlur}\n          onKeyDown={handleKeyDown}\n          ref={useComposedRefs<HTMLDivElement>(rootRef, forwardedRef, focusGroupRef)}\n        >\n          {children}\n        </ark.div>\n      </FocusContext.Provider>\n    );\n  },\n);\n\nFocusGroup.displayName = 'Focus.Group';\n\n//\n// Item\n//\n\ntype FocusItemProps = {\n  /** `aria-current`: the item is where the user is (\"you are here\"), set by the caller; focus alone does not set it. */\n  current?: boolean;\n  /**\n   * `aria-selected`: the item is selected in its collection, drawn by the shared selected edge (`focus.css`). A selectable\n   * item is an `option` unless it names its own role (e.g. `gridcell`); its collection must be the matching `listbox` or\n   * `grid`.\n   */\n  selected?: boolean;\n  /** A separator-coloured border while unfocused. */\n  border?: boolean;\n  /** Called on click and on `Enter`. */\n  onCurrentChange?: () => void;\n};\n\n/**\n * A member of a `Focus.Group`, moved between as one unit: its own controls are not arrow targets, `Enter` selects it\n * rather than moving into it, and `Escape` returns focus to it.\n */\nconst FocusItem = slottable<HTMLDivElement, FocusItemProps>(\n  (\n    { children, asChild, current, selected, border, onCurrentChange, onClick, onFocus, onBlur, ...props },\n    forwardedRef,\n  ) => {\n    const {\n      ref: focusGroupRef,\n      onKeyDown: onFocusGroupKeyDown,\n      onFocus: _onFocusGroupFocus,\n      ...focusGroupAttrs\n    } = useFocusGroup({ tabBehavior: 'unlimited', ignoreKeys: ['Enter'] });\n\n    const handleClick = useCallback(\n      (event: MouseEvent<HTMLDivElement>) => {\n        onCurrentChange?.();\n        onClick?.(event);\n      },\n      [onCurrentChange, onClick],\n    );\n\n    const handleKeyDown = useCallback(\n      (event: KeyboardEvent<HTMLDivElement>) => {\n        onFocusGroupKeyDown(event);\n        if (event.key === 'Enter') {\n          onCurrentChange?.();\n        }\n      },\n      [onFocusGroupKeyDown, onCurrentChange],\n    );\n\n    const { className, ...rest } = composableProps(props, { classNames: recipes.focusItem() });\n    return (\n      <ark.div\n        asChild={asChild}\n        {...rest}\n        tabIndex={0}\n        {...focusGroupAttrs}\n        data-scope='focus'\n        data-part='item'\n        data-border={border ? '' : undefined}\n        // Focus is drawn by the focus ring; `current` and `selected` are the caller's states, never inferred from it.\n        aria-current={current || undefined}\n        // `aria-selected` is only valid on a selectable role, so a selectable item without one is an option.\n        role={rest.role ?? (selected === undefined ? undefined : 'option')}\n        aria-selected={selected}\n        className={className}\n        onClick={handleClick}\n        onKeyDown={handleKeyDown}\n        onFocus={onFocus}\n        onBlur={onBlur}\n        ref={useComposedRefs<HTMLDivElement>(forwardedRef, focusGroupRef)}\n      >\n        {children}\n      </ark.div>\n    );\n  },\n);\n\nFocusItem.displayName = 'Focus.Item';\n\n//\n// ItemBinding\n//\n\n/**\n * A collection item's selection and activation, as a selection hook (`useListSelection`) gives it. Every collection\n * (a list, a masonry, a mosaic stack or board) takes one per item as `itemProps(id)`, so they select alike.\n */\ntype FocusItemBinding = {\n  'aria-selected'?: boolean;\n  'onClick'?: (event: MouseEvent) => void;\n  'onFocus'?: (event: FocusEvent) => void;\n  /** Clicked, or Enter pressed while the item itself has focus (e.g. to open it). */\n  'onActivate'?: (event: MouseEvent | KeyboardEvent) => void;\n};\n\n/** The item element's props for a binding: activation fires on click and on the item's own Enter, not a control's. */\nconst bindItem = (binding: FocusItemBinding | undefined) => ({\n  'aria-selected': binding?.['aria-selected'],\n  'onClick': (event: MouseEvent) => {\n    binding?.onClick?.(event);\n    binding?.onActivate?.(event);\n  },\n  'onFocus': binding?.onFocus,\n  'onKeyDown': (event: KeyboardEvent) => {\n    if (event.key === 'Enter' && event.target === event.currentTarget) {\n      binding?.onActivate?.(event);\n    }\n  },\n});\n\nexport type { FocusGroupProps as GroupProps, FocusItemBinding as ItemBinding, FocusItemProps as ItemProps };\n\nexport { FocusGroup as Group, FocusItem as Item, bindItem };\nexport { type FocusContextValue, type FocusState, useFocus } from './FocusContext.ts';\n"],"mappings":";;;;;;;;;AAkBA,IAAa,eAAe,cAAiC,CAAC,CAAC;;AAG/D,IAAa,iBAAiB,WAAW,YAAY;;;;;;;;;;;;;ACsBrD,IAAM,aAAa,WAChB,EAAE,UAAU,SAAS,cAAc,YAAY,cAAc,QAAQ,WAAW,GAAG,SAAS,iBAAiB;CAC5G,MAAM,UAAU,OAAuB,IAAI;CAC3C,MAAM,EACJ,KAAK,eACL,WAAW,qBACX,SAAS,mBACT,GAAG,oBAC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