MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useDialog

Function useDialog

packages/react-aria/src/dialog/useDialog.ts:57–150  ·  view source on GitHub ↗
(
  props: AriaDialogProps,
  ref: RefObject<FocusableElement | null>
)

Source from the content-addressed store, hash-verified

55 * A dialog is an overlay shown above other content in an application.
56 */
57export function useDialog(
58 props: AriaDialogProps,
59 ref: RefObject<FocusableElement | null>
60): DialogAria {
61 let {role = 'dialog', isEntering} = props;
62 let titleId: string | undefined = useSlotId();
63 titleId = props['aria-label'] ? undefined : titleId;
64
65 let contentId: string | undefined = useSlotId();
66 contentId = role === 'alertdialog' && !props['aria-describedby'] ? contentId : undefined;
67
68 let isRefocusing = useRef(false);
69
70 // Focus the dialog itself on mount (or once it has entered), unless a child element is already focused.
71 useEffect(() => {
72 if (!isEntering && ref.current && !isFocusWithin(ref.current)) {
73 focusSafely(ref.current);
74
75 // Safari on iOS does not move the VoiceOver cursor to the dialog
76 // or announce that it has opened until it has rendered. A workaround
77 // is to wait for half a second, then blur and re-focus the dialog.
78 let timeout = setTimeout(() => {
79 // Check that the dialog is still focused, or focused was lost to the body.
80 if (getActiveElement() === ref.current || getActiveElement() === document.body) {
81 isRefocusing.current = true;
82 if (ref.current) {
83 ref.current.blur();
84 focusSafely(ref.current);
85 }
86 isRefocusing.current = false;
87 }
88 }, 500);
89
90 return () => {
91 clearTimeout(timeout);
92 };
93 }
94 }, [ref, isEntering]);
95
96 useOverlayFocusContain();
97
98 // Warn in dev mode if the dialog has no accessible title.
99 // This catches a common mistake where useDialog and useOverlayTriggerState
100 // are used in the same component, causing the title element to not be
101 // in the DOM when useSlotId queries for it.
102 // Check the DOM element directly since aria-labelledby may be added by
103 // wrapper components (e.g. RAC Dialog uses trigger ID as a fallback).
104 let hasWarned = useRef(false);
105 useEffect(() => {
106 if (process.env.NODE_ENV !== 'production' && !hasWarned.current && ref.current) {
107 let el = ref.current;
108 let hasAriaLabel = el.hasAttribute('aria-label');
109 let hasAriaLabelledby = el.hasAttribute('aria-labelledby');
110 if (!hasAriaLabel && !hasAriaLabelledby) {
111 console.warn(
112 'A dialog must have a title for accessibility. ' +
113 'Either provide an aria-label or aria-labelledby prop, or render a heading element inside the dialog.'
114 );

Callers 8

ExampleFunction · 0.90
AlertDialogExampleFunction · 0.90
RegularDialogExampleFunction · 0.90
Dialog.tsxFile · 0.90
ComboBoxTrayFunction · 0.90
SearchAutocompleteTrayFunction · 0.90
Dialog.tsxFile · 0.90
DialogFunction · 0.90

Calls 7

useSlotIdFunction · 0.90
isFocusWithinFunction · 0.90
focusSafelyFunction · 0.90
getActiveElementFunction · 0.90
useOverlayFocusContainFunction · 0.90
filterDOMPropsFunction · 0.90
hasAttributeMethod · 0.80

Tested by 3

ExampleFunction · 0.72
AlertDialogExampleFunction · 0.72
RegularDialogExampleFunction · 0.72