Skip to content

Commit 894b79b

Browse files
authored
fix: Use consistent close modal label across components (#3527)
1 parent c3f0d49 commit 894b79b

File tree

16 files changed

+104
-5
lines changed

16 files changed

+104
-5
lines changed

src/__tests__/snapshot-tests/__snapshots__/documenter.test.ts.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5844,6 +5844,11 @@ The object should contain, among others:
58445844
"optional": true,
58455845
"type": "string",
58465846
},
5847+
{
5848+
"name": "preferencesModalCloseAriaLabel",
5849+
"optional": true,
5850+
"type": "string",
5851+
},
58475852
{
58485853
"name": "preferencesModalConfirm",
58495854
"optional": true,
@@ -6295,6 +6300,13 @@ The values for all configured preferences are present even if the user didn't ch
62956300
"optional": true,
62966301
"type": "string",
62976302
},
6303+
{
6304+
"description": "Adds an aria-label to the close button, for accessibility.",
6305+
"i18nTag": true,
6306+
"name": "closeAriaLabel",
6307+
"optional": true,
6308+
"type": "string",
6309+
},
62986310
{
62996311
"description": "Label of the confirm button in the modal footer.",
63006312
"i18nTag": true,
@@ -16950,6 +16962,11 @@ exports[`Documenter definition for split-panel matches the snapshot: split-panel
1695016962
"optional": true,
1695116963
"type": "string",
1695216964
},
16965+
{
16966+
"name": "preferencesCloseAriaLabel",
16967+
"optional": true,
16968+
"type": "string",
16969+
},
1695316970
{
1695416971
"name": "preferencesConfirm",
1695516972
"optional": true,

src/app-layout/__tests__/__snapshots__/widget-contract-split-panel.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ Map {
309309
"closeButtonAriaLabel": undefined,
310310
"openButtonAriaLabel": undefined,
311311
"preferencesCancel": undefined,
312+
"preferencesCloseAriaLabel": undefined,
312313
"preferencesConfirm": undefined,
313314
"preferencesPositionBottom": undefined,
314315
"preferencesPositionDescription": undefined,
@@ -1075,6 +1076,7 @@ Map {
10751076
"closeButtonAriaLabel": undefined,
10761077
"openButtonAriaLabel": undefined,
10771078
"preferencesCancel": undefined,
1079+
"preferencesCloseAriaLabel": undefined,
10781080
"preferencesConfirm": undefined,
10791081
"preferencesPositionBottom": undefined,
10801082
"preferencesPositionDescription": undefined,

src/code-editor/__tests__/code-editor.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,9 @@ describe('Code editor component', () => {
675675
'i18nStrings.preferencesModalThemeFilteringAriaLabel': 'Custom theme filter',
676676
'i18nStrings.preferencesModalThemeFilteringPlaceholder': 'Custom theme filter placeholder',
677677
},
678+
modal: {
679+
closeAriaLabel: 'Custom modal close',
680+
},
678681
}}
679682
>
680683
<CodeEditor {...defaultProps} themes={{ light: ['One'], dark: ['Two'] }} i18nStrings={undefined} />
@@ -683,6 +686,7 @@ describe('Code editor component', () => {
683686
const wrapper = createWrapper(container).findCodeEditor()!;
684687
wrapper.findSettingsButton()!.click();
685688
const modal = createWrapper().findModal()!;
689+
expect(modal.findDismissButton().getElement()).toHaveAccessibleName('Custom modal close');
686690
expect(modal.findHeader().getElement()).toHaveTextContent('Custom modal header');
687691
expect(modal.findFooter()!.findSpaceBetween()!.find(':nth-child(1)')!.findButton()!.getElement()).toHaveTextContent(
688692
'Custom cancel'

src/code-editor/__tests__/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const i18nStrings: CodeEditorProps.I18nStrings = {
1919

2020
preferencesModalHeader: 'Preferences',
2121
preferencesModalCancel: 'Cancel',
22+
preferencesModalCloseAriaLabel: 'Close',
2223
preferencesModalConfirm: 'Confirm',
2324
preferencesModalWrapLines: 'Wrap lines',
2425
preferencesModalTheme: 'Theme',

src/code-editor/__tests__/preferences-modal.test.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,28 @@ afterEach(() => {
2323
});
2424

2525
test('should not render modal when preferences are not displayed', () => {
26-
const { wrapper } = renderCodeEditor({});
26+
const { wrapper } = renderCodeEditor();
2727
expect(createWrapper().findModal()).toBeFalsy();
2828
wrapper.findSettingsButton()!.click();
2929
expect(createWrapper().findModal()).toBeTruthy();
3030
cancelPreferences();
3131
expect(createWrapper().findModal()).toBeFalsy();
3232
});
3333

34+
test('should use preferencesModalCloseAriaLabel when provided', () => {
35+
const { wrapper } = renderCodeEditor({
36+
i18nStrings: { preferencesModalCloseAriaLabel: 'Close', preferencesModalCancel: 'Cancel' },
37+
});
38+
wrapper.findSettingsButton()!.click();
39+
expect(createWrapper().findModal()!.findDismissButton()!.getElement()).toHaveAccessibleName('Close');
40+
});
41+
42+
test("should use preferencesModalCancel when preferencesModalCloseAriaLabel isn't provided", () => {
43+
const { wrapper } = renderCodeEditor({ i18nStrings: { ...i18nStrings, preferencesModalCloseAriaLabel: undefined } });
44+
wrapper.findSettingsButton()!.click();
45+
expect(createWrapper().findModal()!.findDismissButton()!.getElement()).toHaveAccessibleName('Cancel');
46+
});
47+
3448
test('should change wrap lines preference via modal', () => {
3549
const onPreferencesChange = jest.fn();
3650
const { wrapper } = renderCodeEditor({ onPreferencesChange: event => onPreferencesChange(event.detail) });

src/code-editor/index.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const CodeEditor = forwardRef((props: CodeEditorProps, ref: React.Ref<CodeEditor
7272
const isRefresh = useVisualRefresh();
7373
const baseProps = getBaseProps(rest);
7474
const i18n = useInternalI18n('code-editor');
75+
const i18nModal = useInternalI18n('modal');
7576

7677
const errorsTabRef = useRef<HTMLButtonElement>(null);
7778
const warningsTabRef = useRef<HTMLButtonElement>(null);
@@ -291,6 +292,9 @@ const CodeEditor = forwardRef((props: CodeEditorProps, ref: React.Ref<CodeEditor
291292
header: i18n('i18nStrings.preferencesModalHeader', i18nStrings?.preferencesModalHeader),
292293
cancel: i18n('i18nStrings.preferencesModalCancel', i18nStrings?.preferencesModalCancel),
293294
confirm: i18n('i18nStrings.preferencesModalConfirm', i18nStrings?.preferencesModalConfirm),
295+
close:
296+
i18nModal('closeAriaLabel', i18nStrings?.preferencesModalCloseAriaLabel) ||
297+
i18nStrings?.preferencesModalCancel,
294298
wrapLines: i18n('i18nStrings.preferencesModalWrapLines', i18nStrings?.preferencesModalWrapLines),
295299
theme: i18n('i18nStrings.preferencesModalTheme', i18nStrings?.preferencesModalTheme),
296300
lightThemes: i18n('i18nStrings.preferencesModalLightThemes', i18nStrings?.preferencesModalLightThemes),

src/code-editor/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ export namespace CodeEditorProps {
159159
preferencesModalHeader?: string;
160160
preferencesModalCancel?: string;
161161
preferencesModalConfirm?: string;
162+
preferencesModalCloseAriaLabel?: string;
162163
preferencesModalWrapLines?: string;
163164
preferencesModalTheme?: string;
164165
preferencesModalLightThemes?: string;

src/code-editor/preferences-modal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface PreferencesModali18nStrings {
1919
header?: string;
2020
cancel?: string;
2121
confirm?: string;
22+
close?: string;
2223
wrapLines?: string;
2324
theme?: string;
2425
lightThemes?: string;
@@ -76,7 +77,7 @@ export default (props: PreferencesModalProps) => {
7677
removeModalRoot={props.removeModalRoot}
7778
onDismiss={props.onDismiss}
7879
header={props.i18nStrings.header}
79-
closeAriaLabel={props.i18nStrings.cancel}
80+
closeAriaLabel={props.i18nStrings.close}
8081
footer={
8182
<InternalBox float="right">
8283
<InternalSpaceBetween direction="horizontal" size="xs">

src/collection-preferences/__tests__/collection-preferences.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@ describe('i18n', () => {
223223
expect(footerItems.find(':nth-child(2)')!.findButton()!.getElement()).toHaveTextContent('Custom confirm');
224224
});
225225

226+
test('correctly provides closeAriaLabel to the dismiss button', () => {
227+
const { container } = render(<CollectionPreferences closeAriaLabel="Close" />);
228+
const wrapper = createWrapper(container).findCollectionPreferences()!;
229+
wrapper.findTriggerButton().click();
230+
expect(wrapper.findModal()!.findDismissButton()!.getElement()).toHaveAccessibleName('Close');
231+
});
232+
233+
test("uses cancelLabel if closeAriaLabel isn't provided", () => {
234+
const { container } = render(<CollectionPreferences cancelLabel="Cancel" />);
235+
const wrapper = createWrapper(container).findCollectionPreferences()!;
236+
wrapper.findTriggerButton().click();
237+
expect(wrapper.findModal()!.findDismissButton()!.getElement()).toHaveAccessibleName('Cancel');
238+
});
239+
226240
test('supports using preference labels and descriptions from i18n provider', () => {
227241
const { container } = render(
228242
<TestI18nProvider
@@ -244,6 +258,9 @@ describe('i18n', () => {
244258
'contentDisplayPreference.liveAnnouncementDndItemCommitted':
245259
'{isInitialPosition, select, true {Item moved back to its original position {initialPosition} of {total}} false {Item moved from position {initialPosition} to position {finalPosition} of {total}} other {}}',
246260
},
261+
modal: {
262+
closeAriaLabel: 'Custom modal close',
263+
},
247264
}}
248265
>
249266
<CollectionPreferences
@@ -269,6 +286,7 @@ describe('i18n', () => {
269286
const wrapper = createWrapper(container).findCollectionPreferences()!;
270287
wrapper.findTriggerButton().click();
271288
const modal = wrapper.findModal()!;
289+
expect(modal.findDismissButton().getElement()).toHaveAccessibleName('Custom modal close');
272290
expect(modal.findPageSizePreference()!.findTitle().getElement()).toHaveTextContent('Custom page size');
273291
expect(modal.findWrapLinesPreference()!.findLabel().getElement()).toHaveTextContent('Custom wrap lines');
274292
expect(modal.findWrapLinesPreference()!.findDescription()!.getElement()).toHaveTextContent(

src/collection-preferences/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default function CollectionPreferences({
4747
title,
4848
confirmLabel,
4949
cancelLabel,
50+
closeAriaLabel,
5051
disabled = false,
5152
onConfirm,
5253
onCancel,
@@ -206,7 +207,7 @@ export default function CollectionPreferences({
206207
</InternalSpaceBetween>
207208
</InternalBox>
208209
}
209-
closeAriaLabel={cancelLabel}
210+
closeAriaLabel={closeAriaLabel || cancelLabel}
210211
size={hasContentOnTheLeft && hasContentOnTheRight ? 'large' : 'medium'}
211212
onDismiss={onCancelListener}
212213
>

0 commit comments

Comments
 (0)