|
| 1 | +import { focusSafely } from "@solid-aria/focus"; |
| 2 | +import { AriaLabelingProps, DOMElements, DOMProps } from "@solid-aria/types"; |
| 3 | +import { createSlotId, filterDOMProps } from "@solid-aria/utils"; |
| 4 | +import { Accessor, createEffect, createMemo, JSX, onCleanup } from "solid-js"; |
| 5 | + |
| 6 | +export interface AriaDialogProps extends DOMProps, AriaLabelingProps { |
| 7 | + /** |
| 8 | + * The accessibility role for the dialog. |
| 9 | + * @default 'dialog' |
| 10 | + */ |
| 11 | + role?: "dialog" | "alertdialog"; |
| 12 | +} |
| 13 | + |
| 14 | +export interface DialogAria< |
| 15 | + DialogElementType extends DOMElements, |
| 16 | + TitleElementType extends DOMElements |
| 17 | +> { |
| 18 | + /** |
| 19 | + * Props for the dialog container element. |
| 20 | + */ |
| 21 | + dialogProps: Accessor<JSX.IntrinsicElements[DialogElementType]>; |
| 22 | + |
| 23 | + /** |
| 24 | + * Props for the dialog title element. |
| 25 | + */ |
| 26 | + titleProps: Accessor<JSX.IntrinsicElements[TitleElementType]>; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Provides the behavior and accessibility implementation for a dialog component. |
| 31 | + * A dialog is an overlay shown above other content in an application. |
| 32 | + */ |
| 33 | +export function createDialog< |
| 34 | + DialogElementType extends DOMElements = "div", |
| 35 | + TitleElementType extends DOMElements = "h3", |
| 36 | + RefElement extends HTMLElement = HTMLDivElement |
| 37 | +>( |
| 38 | + props: AriaDialogProps, |
| 39 | + ref: Accessor<RefElement | undefined> |
| 40 | +): DialogAria<DialogElementType, TitleElementType> { |
| 41 | + const defaultTitleId = createSlotId(); |
| 42 | + |
| 43 | + const titleId = createMemo(() => { |
| 44 | + return props["aria-label"] ? undefined : defaultTitleId(); |
| 45 | + }); |
| 46 | + |
| 47 | + // Focus the dialog itself on mount, unless a child element is already focused. |
| 48 | + createEffect(() => { |
| 49 | + const dialogEl = ref(); |
| 50 | + |
| 51 | + if (dialogEl && !dialogEl.contains(document.activeElement)) { |
| 52 | + focusSafely(dialogEl); |
| 53 | + |
| 54 | + // Safari on iOS does not move the VoiceOver cursor to the dialog |
| 55 | + // or announce that it has opened until it has rendered. A workaround |
| 56 | + // is to wait for half a second, then blur and re-focus the dialog. |
| 57 | + const timeoutId = setTimeout(() => { |
| 58 | + if (document.activeElement === dialogEl) { |
| 59 | + dialogEl.blur(); |
| 60 | + focusSafely(dialogEl); |
| 61 | + } |
| 62 | + }, 500); |
| 63 | + |
| 64 | + onCleanup(() => { |
| 65 | + clearTimeout(timeoutId); |
| 66 | + }); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + const domProps = createMemo(() => filterDOMProps(props, { labelable: true })); |
| 71 | + |
| 72 | + // We do not use aria-modal due to a Safari bug which forces the first focusable element to be focused |
| 73 | + // on mount when inside an iframe, no matter which element we programmatically focus. |
| 74 | + // See https://bugs.webkit.org/show_bug.cgi?id=211934. |
| 75 | + // useModal sets aria-hidden on all elements outside the dialog, so the dialog will behave as a modal |
| 76 | + // even without aria-modal on the dialog itself. |
| 77 | + const dialogProps = createMemo(() => ({ |
| 78 | + ...domProps(), |
| 79 | + role: props.role ?? "dialog", |
| 80 | + tabIndex: -1, |
| 81 | + "aria-labelledby": props["aria-labelledby"] || titleId() |
| 82 | + })); |
| 83 | + |
| 84 | + const titleProps = createMemo(() => ({ |
| 85 | + id: titleId() |
| 86 | + })); |
| 87 | + |
| 88 | + return { dialogProps, titleProps }; |
| 89 | +} |
0 commit comments