// // Copyright 2025 DXOS.org // import { type Meta, type StoryObj } from '@storybook/react-vite'; import React, { forwardRef, useEffect, useMemo, useRef } from 'react'; import { findFirstFocusable, useFocusGroup } from '@dxos/react-focus'; import { withLayout, withTheme } from '@dxos/react-ui/testing'; import { mx } from '@dxos/ui-theme'; import { Checkbox } from '../components/Checkbox/Checkbox.tsx'; import * as Field from '../components/Field/Field.tsx'; import { Input } from '../components/Input/Input.tsx'; import * as ScrollArea from '../components/ScrollArea/ScrollArea.tsx'; import * as Hooks from '../hooks/Hooks.ts'; // TODO(burdon): Implement horizontal movement between columns when column is selected. // TODO(burdon): Prevent tab out of app. // `dx-focus-ring` is the app's focus affordance; without it a focusable div falls back to the // browser's own outline, which is what these blocks were drawing. const border = 'dx-focus-ring rounded-xs border border-separator-subtle'; /** Horizontal group over the columns; each column is one stop. */ const Board = forwardRef(({ columns }, ref) => { const { ref: focusGroupRef, ...focusGroupProps } = useFocusGroup({ axis: 'horizontal', memorizeCurrent: true, tabbable: true, }); return (
([ref, focusGroupRef])} tabIndex={0} {...focusGroupProps} className='flex dx-fill overflow-hidden dx-focus-ring rounded-xs' >
{columns.map((column) => ( ))}
); }); /** Vertical group over the items, entered with Enter and left with Escape. */ const Column = ({ items }: { items: string[] }) => { const { ref: focusGroupRef, ...focusGroupProps } = useFocusGroup({ axis: 'vertical', tabBehavior: 'limited', memorizeCurrent: true, }); return (
{items.map((item) => ( ))}
); }; /** One stop for the column's arrow keys, holding controls of its own. */ const Item = ({ value }: { value: string }) => { const { ref: focusGroupRef, ...focusGroupProps } = useFocusGroup({ tabBehavior: 'unlimited' }); return (
); }; const DefaultStory = () => { const columns = useMemo(() => { return [['A1', 'A2', 'A3'], ['B1'], ['C1', 'C2', 'C3', 'C4', 'C5', 'C6'], ['D1', 'D2']]; }, []); const ref = useRef(null); useEffect(() => { findFirstFocusable(ref.current)?.focus(); }, []); return ; }; const meta: Meta = { title: 'ui/react-ui-core/exemplars/focus', component: DefaultStory, decorators: [withTheme(), withLayout({ layout: 'fullscreen' })], parameters: { layout: 'fullscreen', }, }; export default meta; type Story = StoryObj; export const Default: Story = { args: {}, };