diff --git a/src/components/CloseableContext.ts b/src/components/CloseableContext.ts index c161d55d..9dcb7d76 100644 --- a/src/components/CloseableContext.ts +++ b/src/components/CloseableContext.ts @@ -1,10 +1,13 @@ import { createContext } from 'preact'; -/** - * Provide a close handler to descendants. This can allow, e.g., button-like - * components to correctly close a parent dialog or panel. - */ export type CloseableInfo = { + /** The close button title. Defaults to "Close" */ + title?: string; + + /** + * Provide a close handler to descendants. This can allow, e.g., button-like + * components to correctly close a parent dialog or panel. + */ onClose: (() => void) | undefined; }; diff --git a/src/components/feedback/Dialog.tsx b/src/components/feedback/Dialog.tsx index 0744c2d7..ffa1412f 100644 --- a/src/components/feedback/Dialog.tsx +++ b/src/components/feedback/Dialog.tsx @@ -33,6 +33,9 @@ type ComponentProps = { */ onClose?: () => void; + /** Accessible title for the close button */ + closeTitle?: string; + /** * Element that should take focus when the Dialog is first rendered. When not * provided ("auto"), the dialog's outer element will take focus. Setting this @@ -89,6 +92,7 @@ export default function Dialog({ classes, elementRef, onClose, + closeTitle, ...rest }: DialogProps) { @@ -227,6 +231,7 @@ export default function Dialog({ // Provide a close handler to descendant components const closeableContext: CloseableInfo = { onClose: onClose ? closeHandler : undefined, + title: closeTitle, }; return ( diff --git a/src/components/input/CloseButton.tsx b/src/components/input/CloseButton.tsx index 81edd47f..52c497f5 100644 --- a/src/components/input/CloseButton.tsx +++ b/src/components/input/CloseButton.tsx @@ -22,14 +22,14 @@ export default function CloseButton({ classes, elementRef, - title = 'Close', - + title, onClick, ...iconButtonProps }: CloseButtonProps) { const closeableContext = useContext(CloseableContext); - // Any provided `onClick` is prioritized, but also check to see if there is a - // close handler provided by a `CloseableContext` + // Provided `title` and `onClick` are prioritized, but fall back to values + // from the `CloseableContext` + const buttonTitle = title ?? closeableContext?.title ?? 'Close'; const closeHandler = onClick ?? closeableContext?.onClose; return ( @@ -37,7 +37,7 @@ export default function CloseButton({ data-component="CloseButton" elementRef={downcastRef(elementRef)} icon={CancelIcon} - title={title} + title={buttonTitle} classes={classes} {...iconButtonProps} onClick={closeHandler} diff --git a/src/pattern-library/components/patterns/feedback/DialogPage.tsx b/src/pattern-library/components/patterns/feedback/DialogPage.tsx index acf93d12..05b9bf99 100644 --- a/src/pattern-library/components/patterns/feedback/DialogPage.tsx +++ b/src/pattern-library/components/patterns/feedback/DialogPage.tsx @@ -225,7 +225,6 @@ export default function DialogPage() { buttons={} icon={EditIcon} initialFocus={inputRef} - onClose={() => {}} title="Basic dialog" >

@@ -256,11 +255,7 @@ export default function DialogPage() { used.

- {}} - variant="custom" - transitionComponent={Slider} - > +
Custom dialog content
@@ -327,7 +322,6 @@ export default function DialogPage() { > {}} title="Dialog that closes when there are external clicks" >

This dialog will close if you click outside of it

@@ -354,7 +348,7 @@ export default function DialogPage() { title="Dialog with close-on-Escape behavior" withSource > - {}} title="Close on ESC"> +

This dialog will close if you press Escape.

@@ -380,11 +374,7 @@ export default function DialogPage() { title="Dialog that closes on external focus events" withSource > - {}} - title="Close on Away Focus" - > +

This dialog will close if you focus outside of it

@@ -442,6 +432,29 @@ export default function DialogPage() { + + + + Determine the accessible title to be set on the close button + when onClose has also been provided. + + + string + + + {`'Close'`} + + + + +

+ This dialog has a custom title on the close button. Hover over + it to see it. +

+
+
+
+ @@ -460,12 +473,7 @@ export default function DialogPage() { title="Dialog with focus restoration on close" withSource > - {}} - title="Restore focus" - > +

This dialog will restore focus to the previously-focused element on close. @@ -553,7 +561,6 @@ export default function DialogPage() { buttons={} icon={EditIcon} initialFocus={inputRef} - onClose={() => {}} title="Basic dialog" >

This is a basic ModalDialog.

@@ -584,7 +591,6 @@ export default function DialogPage() { } - onClose={() => {}} title="Modal with long content" > @@ -602,7 +608,6 @@ export default function DialogPage() { } classes="h-[25rem]" - onClose={() => {}} title="Modal with tabs and data table" scrollable={false} > @@ -684,7 +689,6 @@ export default function DialogPage() { title="Modal with a fixed height" buttons={} classes="h-[25rem]" - onClose={() => {}} >

This ModalDialog has a height of 25rem. @@ -700,7 +704,6 @@ export default function DialogPage() { title="ModalDialog with a fixed height" buttons={} classes="h-[25rem]" - onClose={() => {}} >

This ModalDialog has a height of 25rem and long @@ -717,7 +720,6 @@ export default function DialogPage() { } - onClose={() => {}} >

@@ -735,7 +737,6 @@ export default function DialogPage() { } - onClose={() => {}} >

@@ -851,7 +852,6 @@ export default function DialogPage() { } - onClose={() => {}} title="Small modal" size="sm" > @@ -862,7 +862,6 @@ export default function DialogPage() { } - onClose={() => {}} title="Medium-size modal" size="md" > @@ -873,7 +872,6 @@ export default function DialogPage() { } - onClose={() => {}} title="Wide modal" size="lg" > @@ -885,7 +883,6 @@ export default function DialogPage() { } classes="w-[40em] h-[80vh] top-[10vh]" - onClose={() => {}} title="Custom-size modal" size="custom" >