Skip to content

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

TypeScript

fix(Drawer): a scrimless drawer's keyboard open lets its first control's tooltip show (#9685)

Cindy Zhang35f558e74fc4
diff --git a/.changeset/drawer-keyboard-open-tooltip.md b/.changeset/drawer-keyboard-open-tooltip.md
new file mode 100644
index 0000000000..42e7bdcd11
--- /dev/null
+++ b/.changeset/drawer-keyboard-open-tooltip.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Opening a `Drawer` without its scrim from the keyboard no longer throws an `InvalidStateError` when its first control has a tooltip, such as a `DrawerHeader` close button. The tooltip now opens once the drawer is shown.
+
+@cixzhang @duytbp
diff --git a/packages/core/src/Drawer/Drawer.test.tsx b/packages/core/src/Drawer/Drawer.test.tsx
index d31f3819a2..ae374a1710 100644
--- a/packages/core/src/Drawer/Drawer.test.tsx
+++ b/packages/core/src/Drawer/Drawer.test.tsx
@@ -10,13 +10,14 @@
*/
import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
-import {render, screen, fireEvent, act} from '@testing-library/react';
+import {render, screen, fireEvent, act, waitFor} from '@testing-library/react';
import {createContext, createRef, use, useState} from 'react';
import * as stylex from '@stylexjs/stylex';
import {Drawer} from './Drawer';
import {DrawerHeader} from './DrawerHeader';
import {Button} from '../Button';
import {ButtonGroup} from '../ButtonGroup';
+import {Tooltip} from '../Tooltip/Tooltip';
import {layerTextReset} from '../Layer/layerTextReset.stylex';
import {layerStructureReset} from '../Layer/layerStructureReset.stylex';
@@ -1369,3 +1370,77 @@ describe('Drawer', () => {
});
});
});
+
+describe('Drawer without a scrim, opened from the keyboard', () => {
+ it('lets a focused control open its tooltip after the show, with no error', async () => {
+ // A <dialog>'s showPopover() runs the dialog focusing steps and focuses
+ // its first control inside the show. After a key press that focus is
+ // :focus-visible, so the control's tooltip asks to show while the
+ // drawer's show is still running, which the browser refuses with an
+ // InvalidStateError. This stand-in refuses the same way.
+ let showing = 0;
+ const tooltipShows: HTMLElement[] = [];
+ HTMLElement.prototype.showPopover = vi.fn(function (this: HTMLElement) {
+ if (showing > 0) {
+ throw new DOMException(
+ 'Invalid to show a popover during another show operation',
+ 'InvalidStateError',
+ );
+ }
+ showing++;
+ try {
+ this.setAttribute('open', '');
+ if (this instanceof HTMLDialogElement) {
+ this.querySelector<HTMLElement>('button')?.focus();
+ } else {
+ tooltipShows.push(this);
+ }
+ } finally {
+ showing--;
+ }
+ });
+ const matches = Element.prototype.matches;
+ vi.spyOn(Element.prototype, 'matches').mockImplementation(function (
+ this: Element,
+ selector: string,
+ ) {
+ if (selector === ':focus-visible') {
+ return true;
+ }
+ return matches.call(this, selector);
+ });
+ const errors: unknown[] = [];
+ const onError = (event: ErrorEvent) => {
+ errors.push(event.error);
+ event.preventDefault();
+ };
+ window.addEventListener('error', onError);
+ try {
+ const {rerender} = render(
+ <Drawer
+ isOpen={false}
+ onOpenChange={() => {}}
+ label="Filters"
+ hasScrim={false}>
+ <Tooltip content="Close the filters">
+ <button type="button">Close</button>
+ </Tooltip>
+ </Drawer>,
+ );
+ rerender(
+ <Drawer isOpen onOpenChange={() => {}} label="Filters" hasScrim={false}>
+ <Tooltip content="Close the filters">
+ <button type="button">Close</button>
+ </Tooltip>
+ </Drawer>,
+ );
+ expect(document.activeElement).toBe(
+ screen.getByRole('button', {name: 'Close', hidden: true}),
+ );
+ await waitFor(() => expect(tooltipShows.length).toBeGreaterThan(0));
+ expect(errors).toEqual([]);
+ } finally {
+ window.removeEventListener('error', onError);
+ }
+ });
+});
diff --git a/packages/core/src/Drawer/__tests__/DrawerKeyboardOpen.a11y.chromium.spec.ts b/packages/core/src/Drawer/__tests__/DrawerKeyboardOpen.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..8ad0337cab
--- /dev/null
+++ b/packages/core/src/Drawer/__tests__/DrawerKeyboardOpen.a11y.chromium.spec.ts
@@ -0,0 +1,61 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+// @pr-a11y scope core/Drawer core/Layer core/Tooltip
+
+/**
+ * @file DrawerKeyboardOpen.a11y.chromium.spec.ts
+ * @input The built Core/Drawer Scrim story; real Chromium keyboard input
+ * @output Browser evidence that opening the scrimless drawer from the keyboard
+ * throws nothing, and that the focused close button's tooltip opens
+ * @position Chromium proof for the scrimless Drawer's show running as a layer
+ * popover operation. jsdom has no dialog focusing steps and no refusal of a
+ * nested popover show, so Drawer.test.tsx uses stand-ins for both.
+ */
+
+import {expect, test} from '@playwright/test';
+import {holdMotionStill} from '@astryxdesign/a11y-spec/chromium';
+import {
+ DEFAULT_STORYBOOK_DIR,
+ serveStorybook,
+ type StaticServer,
+} from '@astryxdesign/a11y-spec/storybook';
+
+// The Chromium that CI installs accepts a show inside another show unless
+// these features are on. With them, the base throws and the tooltip stays
+// closed.
+test.use({
+ launchOptions: {
+ args: [
+ '--enable-blink-features=PopoverHintNewBehavior,PopoverHintNestedShowException',
+ ],
+ },
+});
+
+let storybook: StaticServer;
+test.beforeAll(async () => {
+ storybook = await serveStorybook(
+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,
+ );
+});
+test.afterAll(async () => {
+ await storybook?.close();
+});
+
+test('opening the scrimless drawer from the keyboard throws nothing and shows the close tooltip', async ({
+ page,
+}) => {
+ const errors: string[] = [];
+ page.on('pageerror', error => errors.push(error.message));
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=core-drawer--scrim&viewMode=story`,
+ );
+ await holdMotionStill(page);
+ await page.getByRole('button', {name: 'Without scrim'}).focus();
+ await page.keyboard.press('Enter');
+
+ const drawer = page.getByRole('dialog', {name: 'Non-modal details'});
+ await expect(drawer).toBeVisible();
+ // The dialog's show focused its first control, the close button, from a
+ // key press, so its tooltip opens once the show has ended.
+ await expect(page.getByRole('tooltip')).toBeVisible();
+ expect(errors).toEqual([]);
+});
diff --git a/packages/core/src/Drawer/useDrawerDialogPresence.ts b/packages/core/src/Drawer/useDrawerDialogPresence.ts
index a7c006a7ea..673bff98e1 100644
--- a/packages/core/src/Drawer/useDrawerDialogPresence.ts
+++ b/packages/core/src/Drawer/useDrawerDialogPresence.ts
@@ -33,6 +33,7 @@ import {
} from 'react';
import {flushSync} from 'react-dom';
import {notifyTopLayerEntry} from '../Layer/modalOutlet';
+import {runPopoverOperation} from '../Layer/popoverOperations';
/** Slack past the computed transition before the backstop gives up waiting. */
const EXIT_BACKSTOP_BUFFER_MS = 50;
@@ -71,7 +72,10 @@ function showDrawerHost(dialog: HTMLDialogElement, isModal: boolean): void {
if (isModal) {
dialog.showModal();
} else if (typeof dialog.showPopover === 'function') {
- dialog.showPopover();
+ // The dialog's show focuses its first control. A tooltip on that control
+ // then asks to show. The browser refuses a show inside another show.
+ // Counting this show makes the tooltip wait until it ends.
+ runPopoverOperation(dialog.ownerDocument, () => dialog.showPopover());
// The drawer is now the newest top-layer entry: global hosts shown before
// it (the toast viewport) re-enter above it, as they do above a modal.
notifyTopLayerEntry();