Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): DropdownMenu bottom-sheet trigger forwards the caller's ref (#8898)

Parinith13f3867ed10f
diff --git a/.changeset/dropdown-menu-sheet-trigger-ref.md b/.changeset/dropdown-menu-sheet-trigger-ref.md
new file mode 100644
index 0000000000..8dcefe9da9
--- /dev/null
+++ b/.changeset/dropdown-menu-sheet-trigger-ref.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `DropdownMenu` and `MoreMenu` now forward the caller's ref to the trigger button in the bottom-sheet presentation (`architecture:public-component-api` INV8). Before, `presentation="bottom-sheet"`, or `presentation="adaptive"` on a compact touch screen, replaced the `button.ref` (and the `ref` that `MoreMenu` passes) with an internal ref, so a caller's ref stayed empty. The popover presentation already forwarded it. Apps need no code change.
+
+@parinith-web
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
index 2b7ab6da0a..e98af28772 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -127,6 +127,47 @@ describe('DropdownMenu', () => {
).not.toBeInTheDocument();
});
+ it("forwards the button's ref object to the popover trigger", () => {
+ const ref = createRef<HTMLButtonElement>();
+ render(
+ <DropdownMenu
+ button={{label: 'Project actions', ref}}
+ items={[{label: 'Edit project', onClick: vi.fn()}]}
+ />,
+ );
+ expect(ref.current).toBe(
+ screen.getByRole('button', {name: /Project actions/}),
+ );
+ });
+
+ it("forwards the button's ref object to the bottom-sheet trigger", () => {
+ const ref = createRef<HTMLButtonElement>();
+ render(
+ <DropdownMenu
+ button={{label: 'Project actions', ref}}
+ presentation="bottom-sheet"
+ items={[{label: 'Edit project', onClick: vi.fn()}]}
+ />,
+ );
+ expect(ref.current).toBe(
+ screen.getByRole('button', {name: /Project actions/}),
+ );
+ });
+
+ it("forwards the button's callback ref to the bottom-sheet trigger", () => {
+ const ref = vi.fn();
+ render(
+ <DropdownMenu
+ button={{label: 'Project actions', ref}}
+ presentation="bottom-sheet"
+ items={[{label: 'Edit project', onClick: vi.fn()}]}
+ />,
+ );
+ expect(ref).toHaveBeenCalledWith(
+ screen.getByRole('button', {name: /Project actions/}),
+ );
+ });
+
it('renders data-driven actions in a bottom sheet when requested', async () => {
const user = userEvent.setup();
const onEdit = vi.fn();
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx
index 31367307bc..dc6ff5e6e4 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx
@@ -67,6 +67,7 @@ import {
type DropdownMenuSurfaceValue,
} from './DropdownMenuContext';
import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';
+import {useMergedRefs} from '../hooks/useMergedRefs';
import {useListFocus} from '../hooks/useListFocus';
import {useMenuPress} from '../hooks/useMenuPress';
import type {MenuPressPointerType} from '../hooks/menuPressGesture';
@@ -621,6 +622,11 @@ function DropdownMenuBottomSheet({
const button = buttonFromProps ?? {label: t(DEFAULT_BUTTON_I18N_KEY)};
const backLabel = t('@astryx.dropdownMenu.back');
const buttonRef = useRef<HTMLElement>(null);
+ // The caller's `button.ref` rides along with the sheet's own trigger ref.
+ const triggerRef = useMergedRefs<HTMLButtonElement>(
+ buttonRef as React.Ref<HTMLButtonElement>,
+ button.ref,
+ );
const triggerId = useId();
const sheetId = useId();
const actionListRef = useRef<HTMLDivElement>(null);
@@ -806,7 +812,7 @@ function DropdownMenuBottomSheet({
) : (
<Button
{...button}
- ref={buttonRef as React.Ref<HTMLButtonElement>}
+ ref={triggerRef}
xstyle={[
isOpen && styles.triggerOpen,
button.xstyle,
diff --git a/packages/core/src/MoreMenu/MoreMenu.test.tsx b/packages/core/src/MoreMenu/MoreMenu.test.tsx
index 829ab539e4..8d37cf7b01 100644
--- a/packages/core/src/MoreMenu/MoreMenu.test.tsx
+++ b/packages/core/src/MoreMenu/MoreMenu.test.tsx
@@ -9,6 +9,7 @@
* SYNC: When MoreMenu.tsx changes, update tests to match new behavior
*/
+import {createRef} from 'react';
import {describe, it, expect, vi, beforeAll, beforeEach} from 'vitest';
import {render, screen, fireEvent, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
@@ -256,6 +257,16 @@ describe('MoreMenu', () => {
expect(handleEdit).not.toHaveBeenCalled();
});
+ it('forwards its ref to the bottom-sheet trigger', () => {
+ const ref = createRef<HTMLButtonElement>();
+ render(
+ <MoreMenu items={defaultItems} ref={ref} presentation="bottom-sheet" />,
+ );
+ expect(ref.current).toBe(
+ screen.getByRole('button', {name: 'More options'}),
+ );
+ });
+
it('supports forwardRef', () => {
const ref = vi.fn();
render(<MoreMenu items={defaultItems} ref={ref} />);