Skip to content

Commit 06be0e5

Browse files
feat(alert): add htmlAttributes property for passing attributes to buttons (#27862)
Issue number: N/A --------- ## What is the current behavior? Buttons containing only icons are not accessible as there is no way to pass an `aria-label` attribute (or any other html attribute). ## What is the new behavior? - Adds the `htmlAttributes` property on the `AlertButton` interface - Passes the `htmlAttributes` to the buttons - Adds a test to verify `aria-label` and `aria-labelled-by` are passed to the button ## Does this introduce a breaking change? - [ ] Yes - [x] No
1 parent 5ce4ec0 commit 06be0e5

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

core/src/components/alert/alert-interface.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export interface AlertButton {
4848
role?: 'cancel' | 'destructive' | string;
4949
cssClass?: string | string[];
5050
id?: string;
51+
htmlAttributes?: { [key: string]: any };
5152
// TODO(FW-2832): type
5253
handler?: (value: any) => AlertButtonOverlayHandler | Promise<AlertButtonOverlayHandler>;
5354
}

core/src/components/alert/alert.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ export class Alert implements ComponentInterface, OverlayInterface {
666666
<div class={alertButtonGroupClass}>
667667
{buttons.map((button) => (
668668
<button
669+
{...button.htmlAttributes}
669670
type="button"
670671
id={button.id}
671672
class={buttonClass(button)}

core/src/components/alert/test/a11y/alert.e2e.ts

+16
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,21 @@ configs({ directions: ['ltr'] }).forEach(({ config, title }) => {
6060
test('should allow for manually specifying aria attributes', async ({ page }) => {
6161
await testAria(page, 'customAria', 'Custom title', 'Custom description');
6262
});
63+
64+
test('should have aria-labelledby and aria-label added to the button when htmlAttributes is set', async ({
65+
page,
66+
}) => {
67+
const didPresent = await page.spyOnEvent('ionAlertDidPresent');
68+
69+
const button = page.locator('#ariaLabelButton');
70+
await button.click();
71+
72+
await didPresent.next();
73+
74+
const alertButton = page.locator('ion-alert .alert-button');
75+
76+
await expect(alertButton).toHaveAttribute('aria-labelledby', 'close-label');
77+
await expect(alertButton).toHaveAttribute('aria-label', 'close button');
78+
});
6379
});
6480
});

core/src/components/alert/test/a11y/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ <h1>Alert - A11y</h1>
2424
<ion-button id="noHeaders" expand="block" onclick="presentNoHeaders()">No Headers</ion-button>
2525
<ion-button id="noMessage" expand="block" onclick="presentNoMessage()">No Message</ion-button>
2626
<ion-button id="customAria" expand="block" onclick="presentCustomAria()">Custom Aria</ion-button>
27+
<ion-button id="ariaLabelButton" expand="block" onclick="presentAriaLabelButton()">Aria Label Button</ion-button>
2728
</main>
2829

2930
<script>
@@ -76,6 +77,23 @@ <h1>Alert - A11y</h1>
7677
},
7778
});
7879
}
80+
81+
function presentAriaLabelButton() {
82+
openAlert({
83+
header: 'Header',
84+
subHeader: 'Subtitle',
85+
message: 'This is an alert message with custom aria attributes passed to the button.',
86+
buttons: [
87+
{
88+
text: 'Close',
89+
htmlAttributes: {
90+
'aria-label': 'close button',
91+
'aria-labelledby': 'close-label',
92+
},
93+
},
94+
],
95+
});
96+
}
7997
</script>
8098
</body>
8199
</html>

0 commit comments

Comments
 (0)