diff --git a/change/@fluentui-react-dialog-ac4ee2cd-8976-4c96-8d14-cf825f633e82.json b/change/@fluentui-react-dialog-ac4ee2cd-8976-4c96-8d14-cf825f633e82.json new file mode 100644 index 00000000000000..2fa4c231d3ae42 --- /dev/null +++ b/change/@fluentui-react-dialog-ac4ee2cd-8976-4c96-8d14-cf825f633e82.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "chore(react-dialog): removes document listener to Escape keydown", + "packageName": "@fluentui/react-dialog", + "email": "bernardo.sunderhus@gmail.com", + "dependentChangeType": "patch" +} diff --git a/packages/react-components/react-dialog/etc/react-dialog.api.md b/packages/react-components/react-dialog/etc/react-dialog.api.md index 2402f672ca2f13..5275014b440be5 100644 --- a/packages/react-components/react-dialog/etc/react-dialog.api.md +++ b/packages/react-components/react-dialog/etc/react-dialog.api.md @@ -71,14 +71,6 @@ export type DialogOpenChangeData = { type: 'escapeKeyDown'; open: boolean; event: React_2.KeyboardEvent; -} -/** -* document escape keydown defers from internal escape keydown events because of the synthetic event API -*/ -| { - type: 'documentEscapeKeyDown'; - open: boolean; - event: KeyboardEvent; } | { type: 'backdropClick'; open: boolean; @@ -90,7 +82,7 @@ export type DialogOpenChangeData = { }; // @public (undocumented) -export type DialogOpenChangeEvent = React_2.KeyboardEvent | React_2.MouseEvent | KeyboardEvent; +export type DialogOpenChangeEvent = React_2.KeyboardEvent | React_2.MouseEvent; // @public (undocumented) export type DialogProps = ComponentProps> & { diff --git a/packages/react-components/react-dialog/src/components/Dialog/Dialog.types.ts b/packages/react-components/react-dialog/src/components/Dialog/Dialog.types.ts index 1c279b8f799c40..aa4b4bc027cd71 100644 --- a/packages/react-components/react-dialog/src/components/Dialog/Dialog.types.ts +++ b/packages/react-components/react-dialog/src/components/Dialog/Dialog.types.ts @@ -12,14 +12,10 @@ export type DialogSlots = { backdrop?: Slot<'div'>; }; -export type DialogOpenChangeEvent = React.KeyboardEvent | React.MouseEvent | KeyboardEvent; +export type DialogOpenChangeEvent = React.KeyboardEvent | React.MouseEvent; export type DialogOpenChangeData = | { type: 'escapeKeyDown'; open: boolean; event: React.KeyboardEvent } - /** - * document escape keydown defers from internal escape keydown events because of the synthetic event API - */ - | { type: 'documentEscapeKeyDown'; open: boolean; event: KeyboardEvent } | { type: 'backdropClick'; open: boolean; event: React.MouseEvent } | { type: 'triggerClick'; open: boolean; event: React.MouseEvent }; diff --git a/packages/react-components/react-dialog/src/components/Dialog/useDialog.ts b/packages/react-components/react-dialog/src/components/Dialog/useDialog.ts index 2e8d8cec001aaa..774934236adc6f 100644 --- a/packages/react-components/react-dialog/src/components/Dialog/useDialog.ts +++ b/packages/react-components/react-dialog/src/components/Dialog/useDialog.ts @@ -8,7 +8,7 @@ import { } from '@fluentui/react-utilities'; import { useFocusFinders } from '@fluentui/react-tabster'; import { useFluent_unstable } from '@fluentui/react-shared-contexts'; -import { normalizeDefaultPrevented, isEscapeKeyDismiss, useDisableBodyScroll } from '../../utils'; +import { normalizeDefaultPrevented, useDisableBodyScroll } from '../../utils'; import type { DialogProps, DialogState, DialogModalType, DialogOpenChangeData } from './Dialog.types'; @@ -171,21 +171,6 @@ function useFocusFirstElement({ if (targetDocument.activeElement === trigger) { trigger.blur(); } - const listener = (event: KeyboardEvent) => { - if (isEscapeKeyDismiss(event, modalType)) { - requestOpenChange({ - event, - open: false, - type: 'documentEscapeKeyDown', - }); - trigger.focus(); - event.stopImmediatePropagation(); - } - }; - targetDocument.addEventListener('keydown', listener, { passive: false }); - return () => { - targetDocument.removeEventListener('keydown', listener); - }; } }, [findFirstFocusable, requestOpenChange, open, modalType, targetDocument]); diff --git a/packages/react-components/react-dialog/src/stories/Dialog/DialogNoFocusableElement.md b/packages/react-components/react-dialog/src/stories/Dialog/DialogNoFocusableElement.md new file mode 100644 index 00000000000000..2222080b4f8149 --- /dev/null +++ b/packages/react-components/react-dialog/src/stories/Dialog/DialogNoFocusableElement.md @@ -0,0 +1,3 @@ +A `Dialog` **should** always have at least one focusable element. +In the case no focusable element is present inside a `Dialog` the only method +that would close the `Dialog` would be clicking on the `backdrop` diff --git a/packages/react-components/react-dialog/src/stories/Dialog/DialogNoFocusableElement.stories.tsx b/packages/react-components/react-dialog/src/stories/Dialog/DialogNoFocusableElement.stories.tsx index 35f086724578ba..2f228e6e4142d1 100644 --- a/packages/react-components/react-dialog/src/stories/Dialog/DialogNoFocusableElement.stories.tsx +++ b/packages/react-components/react-dialog/src/stories/Dialog/DialogNoFocusableElement.stories.tsx @@ -2,6 +2,8 @@ import * as React from 'react'; import { Dialog, DialogTrigger, DialogSurface, DialogTitle, DialogBody } from '@fluentui/react-dialog'; import { Button } from '@fluentui/react-components'; +import story from './DialogNoFocusableElement.md'; + export const NoFocusableElement = () => { return ( <> @@ -12,8 +14,9 @@ export const NoFocusableElement = () => { Dialog Title -

⛔️ A Dialog without focusable elements is not recommended! ⛔️

-

Escape key and backdrop click still works to ensure this modal can be closed

+

⛔️ A Dialog without focusable elements is not recommended!

+

⛔️ Escape key doesn't work here

+

✅ Backdrop click still works to ensure this modal can be closed

@@ -24,8 +27,9 @@ export const NoFocusableElement = () => { Dialog Title -

⛔️ A Dialog without focusable elements is not recommended! ⛔️

-

⛔️ Escape key doesn't work, you're trapped! ⛔️

+

⛔️ A Dialog without focusable elements is not recommended!

+

⛔️ Escape key doesn't work

+

⛔️ you're trapped!

@@ -36,7 +40,7 @@ export const NoFocusableElement = () => { NoFocusableElement.parameters = { docs: { description: { - story: '', + story, }, }, };