An open source design system that's fully customizable and agent ready
TypeScript
fix(docsite): show DropdownMenuRadioGroup in properties preview (#5976)
홍규진84278073f7bd
diff --git a/.changeset/dropdown-menu-mount-open-focus.md b/.changeset/dropdown-menu-mount-open-focus.mdnew file mode 100644index 0000000000..aeb34154ee--- /dev/null+++ b/.changeset/dropdown-menu-mount-open-focus.md@@ -0,0 +1,9 @@+---+'@astryxdesign/core': patch+---++[fix] DropdownMenu: keep focus where it is when a controlled menu mounts already open (#5976)++Mounting with `isMenuOpen` true no longer moves focus into the first item; only an open that happens after mount does, per the input-modality rules. A page that renders a menu open on load no longer drops keyboard users into it, and ArrowDown on the focused trigger walks into an already-open menu instead of requiring a close and reopen.++@Kyujeniusdiff --git a/.changeset/dropdown-menu-radio-group-playground.md b/.changeset/dropdown-menu-radio-group-playground.mdnew file mode 100644index 0000000000..2b7dc05221--- /dev/null+++ b/.changeset/dropdown-menu-radio-group-playground.md@@ -0,0 +1,9 @@+---+'@astryxdesign/core': patch+---++[fix] DropdownMenuRadioGroup: render a working menu in the docsite properties preview (#5976)++Adds a `DropdownMenu` playground wrapper that opens on first load and two `DropdownMenuRadioItem` children, so the properties-tab preview shows selectable radio choices immediately instead of an empty container.++@Kyujeniusdiff --git a/apps/docsite/src/__tests__/data-extraction.test.ts b/apps/docsite/src/__tests__/data-extraction.test.tsindex 7c2f2ecbcc..bb310402ea 100644--- a/apps/docsite/src/__tests__/data-extraction.test.ts+++ b/apps/docsite/src/__tests__/data-extraction.test.ts@@ -413,6 +413,38 @@ describe('componentRegistry', () => {});});+ it('DropdownMenuRadioGroup declares a menu wrapper and radio choices for its preview (#5888)', () => {+ const core = components['@astryxdesign/core'];+ const radioGroup = core.find(c => c.name === 'DropdownMenuRadioGroup');++ expect(radioGroup).toBeDefined();+ // The menu opens on first load so both choices are visible immediately;+ // the preview bridges DropdownMenu's `onOpenChange` back to this prop so+ // selecting still closes the menu and the trigger reopens it.+ expect(radioGroup!.playground?.wrapper).toMatchObject({+ component: 'DropdownMenu',+ props: {+ button: {label: 'Sort'},+ presentation: 'popover',+ isMenuOpen: true,+ },+ });+ expect(radioGroup!.playground?.defaults).toMatchObject({+ value: 'newest',+ label: 'Sort by',+ children: [+ {+ __element: 'DropdownMenuRadioItem',+ props: {value: 'newest', label: 'Newest'},+ },+ {+ __element: 'DropdownMenuRadioItem',+ props: {value: 'oldest', label: 'Oldest'},+ },+ ],+ });+ });+it('Citation satisfies its required source prop via playground defaults', () => {const core = components['@astryxdesign/core'];const citation = core.find(c => c.name === 'Citation');diff --git a/apps/docsite/src/__tests__/interactive-preview.test.ts b/apps/docsite/src/__tests__/interactive-preview.test.tsindex d4aba86639..8ba30ab8cd 100644--- a/apps/docsite/src/__tests__/interactive-preview.test.ts+++ b/apps/docsite/src/__tests__/interactive-preview.test.ts@@ -4,9 +4,10 @@/*** @file InteractivePreview tests.- * @input InteractivePreviewStage with radio-item and Tokenizer playground previews- * @output Regression coverage for wrapper-owned selection state and for the- * controlled-value change bridge.+ * @input InteractivePreviewStage with radio-item, radio-group and Tokenizer+ * playground previews+ * @output Regression coverage for wrapper-owned selection and open state and+ * for the controlled-value change bridge.*/import {@@ -45,18 +46,34 @@ function MockButton({label, onClick}: {label: string; onClick?: () => void}) {return createElement('button', {onClick}, label);}+const MenuContext = createContext<{close: () => void}>({close: () => {}});++// Controlled like the real menu: renders its children only while open and+// reports every open/close through `onOpenChange`.function MockDropdownMenu({button,+ isMenuOpen = false,+ onOpenChange,children,}: {button: {label: string};+ isMenuOpen?: boolean;+ onOpenChange?: (isOpen: boolean) => void;children?: ReactNode;}) {return createElement(- 'div',- null,- createElement('button', null, button.label),- children,+ MenuContext.Provider,+ {value: {close: () => onOpenChange?.(false)}},+ createElement(+ 'div',+ null,+ createElement(+ 'button',+ {onClick: () => onOpenChange?.(!isMenuOpen)},+ button.label,+ ),+ isMenuOpen ? children : null,+ ),);}@@ -65,6 +82,7 @@ const RadioContext = createContext<{onChange: (value: string) => void;}>({onChange: () => {}});+// Selecting closes the enclosing menu, as `hasCloseOnSelect` does by default.function MockRadioGroup({value,onChange,@@ -76,9 +94,18 @@ function MockRadioGroup({label: string;children?: ReactNode;}) {+ const menu = useContext(MenuContext);return createElement(RadioContext.Provider,- {value: {value, onChange}},+ {+ value: {+ value,+ onChange: (next: string) => {+ onChange(next);+ menu.close();+ },+ },+ },createElement('div', {'aria-label': label, role: 'group'}, children),);}@@ -184,6 +211,68 @@ describe('InteractivePreviewStage', () => {expect(item).toHaveAttribute('aria-checked', 'true');});+ it('shows the radio choices on first load and keeps the menu interactive (#5888)', async () => {+ const user = userEvent.setup();+ const knobs = pickPrimaryProps('DropdownMenuRadioGroup', [+ {name: 'value', type: 'string | undefined', description: ''},+ {name: 'onChange', type: '(value: string) => void', description: ''},+ {name: 'label', type: 'string', description: ''},+ ]);++ function PreviewHarness() {+ const [value, setValue] = useState('newest');+ return createElement(InteractivePreviewStage, {+ name: 'DropdownMenuRadioGroup',+ state: {+ value,+ label: 'Sort by',+ children: [+ {+ __element: 'DropdownMenuRadioItem',+ props: {value: 'newest', label: 'Newest'},+ },+ {+ __element: 'DropdownMenuRadioItem',+ props: {value: 'oldest', label: 'Oldest'},+ },+ ],+ },+ knobs,+ playground: {+ wrapper: {+ component: 'DropdownMenu',+ props: {button: {label: 'Sort'}, isMenuOpen: true},+ },+ },+ onPropChange: (_propName: string, nextValue: unknown) =>+ setValue(nextValue as string),+ });+ }++ render(createElement(PreviewHarness));++ // Both choices are visible without opening the menu first.+ expect(screen.getByRole('menuitemradio', {name: 'Newest'})).toHaveAttribute(+ 'aria-checked',+ 'true',+ );+ const oldest = screen.getByRole('menuitemradio', {name: 'Oldest'});+ expect(oldest).toHaveAttribute('aria-checked', 'false');++ // Selecting commits the value and closes the menu through the wrapper's+ // bridged `onOpenChange`; the trigger reopens it on the new selection.+ await user.click(oldest);+ expect(+ screen.queryByRole('menuitemradio', {name: 'Oldest'}),+ ).not.toBeInTheDocument();++ await user.click(screen.getByRole('button', {name: 'Sort'}));+ expect(screen.getByRole('menuitemradio', {name: 'Oldest'})).toHaveAttribute(+ 'aria-checked',+ 'true',+ );+ });+it('drops a removed Tokenizer token from the rendered preview (#5981)', async () => {const user = userEvent.setup();const knobs = pickPrimaryProps('Tokenizer', [diff --git a/apps/docsite/src/components/component-detail/InteractivePreview.tsx b/apps/docsite/src/components/component-detail/InteractivePreview.tsxindex 96f4f10113..1d18ef87b2 100644--- a/apps/docsite/src/components/component-detail/InteractivePreview.tsx+++ b/apps/docsite/src/components/component-detail/InteractivePreview.tsx@@ -266,6 +266,9 @@ export function InteractivePreviewStage({const overlayControl = getOverlayPreviewControl(playground);const WrapperComponent = wrapper ? getComponent(wrapper.component) : null;const [wrapperValue, setWrapperValue] = useState<unknown>(undefined);+ const [isWrapperMenuOpen, setIsWrapperMenuOpen] = useState<+ boolean | undefined+ >(undefined);const wrapperProps = useMemo(() => {const resolved = wrapper?.props@@ -287,8 +290,15 @@ export function InteractivePreviewStage({resolved.onChange(newVal);}},+ // A menu wrapper seeded open (`isMenuOpen`) is controlled, so bridge+ // its open pair too: selecting still closes it and the trigger reopens+ // it (#5888).+ ...(typeof resolved.isMenuOpen === 'boolean' && {+ isMenuOpen: isWrapperMenuOpen ?? resolved.isMenuOpen,+ onOpenChange: setIsWrapperMenuOpen,+ }),};- }, [wrapper, wrapperValue, state, onPropChange]);+ }, [wrapper, wrapperValue, isWrapperMenuOpen, state, onPropChange]);const renderPreview = useCallback((rendered: ReactNode): ReactNode => {diff --git a/apps/storybook/stories/DropdownMenu.stories.tsx b/apps/storybook/stories/DropdownMenu.stories.tsxindex f9a4791e65..929055dcf5 100644--- a/apps/storybook/stories/DropdownMenu.stories.tsx+++ b/apps/storybook/stories/DropdownMenu.stories.tsx@@ -317,6 +317,32 @@ export const Controlled: Story = {},};+// Mounted already open: the menu is visible on first render, but focus stays+// where it was. Only an open the user initiates moves focus into the first+// item; from the focused trigger, ArrowDown walks into the open menu (#5976).+export const MountedOpen: Story = {+ render: function MountedOpenStory() {+ const [isOpen, setIsOpen] = useState(true);+ return (+ <DropdownMenu+ button={{label: 'Sort'}}+ isMenuOpen={isOpen}+ onOpenChange={setIsOpen}>+ <DropdownMenuItem label="Newest" />+ <DropdownMenuItem label="Oldest" />+ </DropdownMenu>+ );+ },+ parameters: {+ docs: {+ description: {+ story:+ 'A controlled menu mounted with `isMenuOpen` true renders open without taking focus; `document.activeElement` stays on the page until the user opens the menu themselves. Tab to the trigger, then ArrowDown enters the already-open menu at the first item.',+ },+ },+ },+};+// Custom menu widthexport const CustomWidth: Story = {render: () => (diff --git a/packages/core/src/DropdownMenu/DropdownMenu.doc.mjs b/packages/core/src/DropdownMenu/DropdownMenu.doc.mjsindex a409f8ce1e..5b3a95477f 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.doc.mjs+++ b/packages/core/src/DropdownMenu/DropdownMenu.doc.mjs@@ -200,7 +200,8 @@ export const docs = {{name: 'isMenuOpen',type: 'boolean',- description: 'Controlled open state for the menu.',+ description:+ 'Controlled open state for the menu. Mounting with true renders the menu open without moving focus into it; focus moves to the first item only when the menu opens after mount.',},{name: 'onOpenChange',diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsxindex d0500a54e1..bdfc0c4989 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx@@ -907,6 +907,144 @@ describe('DropdownMenu controlled mode', () => {expect(HTMLElement.prototype.showPopover).toHaveBeenCalled();});+ it('does not move focus into a menu that mounts already open', () => {+ const raf = vi+ .spyOn(window, 'requestAnimationFrame')+ .mockImplementation(callback => {+ callback(0);+ return 0;+ });++ try {+ render(+ <DropdownMenu+ button={{label: 'Sort'}}+ isMenuOpen+ onOpenChange={vi.fn()}>+ <DropdownMenuItem label="Newest" />+ <DropdownMenuItem label="Oldest" />+ </DropdownMenu>,+ );++ // Nobody opened the menu, so nobody is dropped into it (#5976).+ expect(+ screen.getByRole('menuitem', {name: 'Newest', hidden: true}),+ ).toBeInTheDocument();+ expect(document.body).toHaveFocus();+ } finally {+ raf.mockRestore();+ }+ });++ it('still focuses the first item when a mounted menu opens later', async () => {+ const raf = vi+ .spyOn(window, 'requestAnimationFrame')+ .mockImplementation(callback => {+ callback(0);+ return 0;+ });++ try {+ const {rerender} = render(+ <DropdownMenu+ button={{label: 'Sort'}}+ isMenuOpen={false}+ onOpenChange={vi.fn()}>+ <DropdownMenuItem label="Newest" />+ <DropdownMenuItem label="Oldest" />+ </DropdownMenu>,+ );++ rerender(+ <DropdownMenu+ button={{label: 'Sort'}}+ isMenuOpen+ onOpenChange={vi.fn()}>+ <DropdownMenuItem label="Newest" />+ <DropdownMenuItem label="Oldest" />+ </DropdownMenu>,+ );++ // A programmatic open after mount keeps the first-item focus (#4594).+ await waitFor(() =>+ expect(+ screen.getByRole('menuitem', {name: 'Newest', hidden: true}),+ ).toHaveFocus(),+ );+ } finally {+ raf.mockRestore();+ }+ });++ it('walks into a mounted-open menu with ArrowDown from the focused trigger', () => {+ const raf = vi+ .spyOn(window, 'requestAnimationFrame')+ .mockImplementation(callback => {+ callback(0);+ return 0;+ });++ try {+ render(+ <DropdownMenu+ button={{label: 'Sort'}}+ isMenuOpen+ onOpenChange={vi.fn()}>+ <DropdownMenuItem label="Newest" />+ <DropdownMenuItem label="Oldest" />+ </DropdownMenu>,+ );++ // The items sit outside the tab order, so the trigger is the only+ // keyboard way in; ArrowDown must not require closing and reopening.+ const trigger = screen.getByRole('button', {name: /Sort/});+ trigger.focus();+ fireEvent.keyDown(trigger, {key: 'ArrowDown'});++ expect(+ screen.getByRole('menuitem', {name: 'Newest', hidden: true}),+ ).toHaveFocus();+ expect(trigger).toHaveAttribute('aria-expanded', 'true');+ } finally {+ raf.mockRestore();+ }+ });++ it('focuses the first item once a mounted-open menu is closed and reopened', async () => {+ const raf = vi+ .spyOn(window, 'requestAnimationFrame')+ .mockImplementation(callback => {+ callback(0);+ return 0;+ });++ try {+ const menu = (isMenuOpen: boolean) => (+ <DropdownMenu+ button={{label: 'Sort'}}+ isMenuOpen={isMenuOpen}+ onOpenChange={vi.fn()}>+ <DropdownMenuItem label="Newest" />+ <DropdownMenuItem label="Oldest" />+ </DropdownMenu>+ );+ const {rerender} = render(menu(true));+ expect(document.body).toHaveFocus();++ // The mount-open exemption ends with the first close; the next open is+ // a real one and lands on the first item again.+ rerender(menu(false));+ rerender(menu(true));+ await waitFor(() =>+ expect(+ screen.getByRole('menuitem', {name: 'Newest', hidden: true}),+ ).toHaveFocus(),+ );+ } finally {+ raf.mockRestore();+ }+ });+it('calls onOpenChange when button is clicked', async () => {const user = userEvent.setup();const handleToggle = vi.fn();diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsxindex e84eafb795..78bfc6d013 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx@@ -688,14 +688,25 @@ function DropdownMenuPopover({),});+ // Mounting already open (`isMenuOpen` true on the first render) is not an+ // open anyone asked for, so it must not move focus into the menu — that+ // drops keyboard users mid-page (#5976). The flag is decided once at mount+ // rather than per effect run, because the layer may defer the first show+ // until its popover element mounts; it clears once the consumer closes the+ // menu, so every later open follows the modality rules above.+ const isMountedOpenRef = useRef(isControlled && controlledIsOpen === true);+// Sync controlled open state → popover.useEffect(() => {if (isControlled) {if (controlledIsOpen && !popover.isOpen) {- shouldFocusOnOpenRef.current = true;+ shouldFocusOnOpenRef.current = !isMountedOpenRef.current;popover.show();- } else if (!controlledIsOpen && popover.isOpen) {- popover.hide();+ } else if (!controlledIsOpen) {+ isMountedOpenRef.current = false;+ if (popover.isOpen) {+ popover.hide();+ }}}}, [controlledIsOpen, isControlled, popover]);@@ -809,10 +820,21 @@ function DropdownMenuPopover({e.preventDefault();openAndFocus();}+ return;+ }+ // Open with focus still on the trigger — a menu that mounted open, or+ // a pointer open followed by Shift+Tab — has nothing in the tab order+ // to reach its items, so ArrowDown walks in the way a keyboard open+ // would land (#5976). Enter/Space keep toggling the menu closed, and+ // once focus is inside, key events go to the menu container instead.+ if (e.key === 'ArrowDown') {+ e.preventDefault();+ if (!focusFirst()) {+ listRef.current?.focus();+ }}- // When open, key events go to the menu container via useListFocus},- [popover.isOpen, openAndFocus],+ [popover.isOpen, openAndFocus, focusFirst, listRef],);// Icon-onlydiff --git a/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjs b/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjsindex 1182567998..83254cf30a 100644--- a/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjs+++ b/packages/core/src/DropdownMenu/DropdownMenuRadioGroup.doc.mjs@@ -9,6 +9,33 @@ export const docs = {isHiddenFromOverview: true,description:'A single-select group of radio menu items (role="group" of menuitemradio). Owns the selected value and lays its items out with the menu\'s inter-item gap.',+ playground: {+ wrapper: {+ component: 'DropdownMenu',+ // Open on first load so the radio choices are visible without a click;+ // the preview bridges `onOpenChange` back so selecting still closes the+ // menu and the trigger reopens it.+ props: {+ button: {label: 'Sort'},+ presentation: 'popover',+ isMenuOpen: true,+ },+ },+ defaults: {+ value: 'newest',+ label: 'Sort by',+ children: [+ {+ __element: 'DropdownMenuRadioItem',+ props: {value: 'newest', label: 'Newest'},+ },+ {+ __element: 'DropdownMenuRadioItem',+ props: {value: 'oldest', label: 'Oldest'},+ },+ ],+ },+ },props: [{name: 'value',