From b02692a6826229e9fc0e29007096b30d8ff9b82b Mon Sep 17 00:00:00 2001 From: Maria Hutt Date: Fri, 4 Dec 2026 19:53:38 -0800 Subject: [PATCH] fix(alert): render the cancel button last when buttons stack When alert buttons stack, buttons with role "cancel" render last in ios and md, matching native iOS and the Material Design stacked layout. Inline buttons keep their order. --- core/src/components/alert/alert.tsx | 19 ++++- .../components/alert/test/basic/alert.e2e.ts | 69 +++++++++++++++++++ .../components/alert/test/basic/index.html | 3 +- 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/core/src/components/alert/alert.tsx b/core/src/components/alert/alert.tsx index 6ab7d683eec..7a8e11d58b8 100644 --- a/core/src/components/alert/alert.tsx +++ b/core/src/components/alert/alert.tsx @@ -846,12 +846,27 @@ export class Alert implements ComponentInterface, OverlayInterface { } private renderAlertButtons() { - const buttons = this.processedButtons; + const { processedButtons, isButtonGroupWrapped } = this; const mode = getIonMode(this); + const isStacked = processedButtons.length > 2 || isButtonGroupWrapped; const alertButtonGroupClass = { 'alert-button-group': true, - 'alert-button-group-vertical': buttons.length > 2 || this.isButtonGroupWrapped, + 'alert-button-group-vertical': isStacked, }; + + /** + * Stacked buttons render the cancel button last, below the other + * actions, which keep the order they were passed in. Reorder the + * DOM rather than use CSS `order` so the tab order matches what + * is on screen. + */ + const buttons = isStacked + ? [ + ...processedButtons.filter((button) => button.role !== 'cancel'), + ...processedButtons.filter((button) => button.role === 'cancel'), + ] + : processedButtons; + return (
(this.buttonGroupEl = el)}> {buttons.map((button) => ( diff --git a/core/src/components/alert/test/basic/alert.e2e.ts b/core/src/components/alert/test/basic/alert.e2e.ts index c04029be359..c1446b210f5 100644 --- a/core/src/components/alert/test/basic/alert.e2e.ts +++ b/core/src/components/alert/test/basic/alert.e2e.ts @@ -124,6 +124,75 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ config, title }) => }); }); +/** + * This behavior does not vary across directions. + */ +configs({ directions: ['ltr'] }).forEach(({ config, title }) => { + test.describe(title('alert: stacked button order'), () => { + const openAlert = async (page: E2EPage, buttons: unknown[]) => { + test.info().annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/26367', + }); + + await page.setContent( + ` + + + + `, + config + ); + + const ionAlertDidPresent = await page.spyOnEvent('ionAlertDidPresent'); + await page.locator('ion-alert').evaluate((el: HTMLIonAlertElement) => el.present()); + await ionAlertDidPresent.next(); + }; + + /** + * Returns the button labels from top to bottom, and checks + * that the DOM order matches so tab order follows the screen. + */ + const getStackedOrder = async (page: E2EPage) => { + const buttons = page.locator('ion-alert .alert-button'); + const domOrder = await buttons.allTextContents(); + const tops = await buttons.evaluateAll((els) => els.map((el) => el.getBoundingClientRect().top)); + expect(tops).toEqual([...tops].sort((a, b) => a - b)); + return domOrder; + }; + + test('should render the cancel button last when two buttons wrap', async ({ page }) => { + await openAlert(page, [ + { text: 'Cancel Cancel Cancel Cancel', role: 'cancel' }, + { text: 'Confirm Confirm Confirm Confirm' }, + ]); + + await expect(page.locator('ion-alert .alert-button-group')).toHaveClass(/alert-button-group-vertical/); + expect(await getStackedOrder(page)).toEqual(['Confirm Confirm Confirm Confirm', 'Cancel Cancel Cancel Cancel']); + }); + + test('should render the cancel button last when there are more than two buttons', async ({ page }) => { + await openAlert(page, [ + { text: 'Cancel', role: 'cancel' }, + { text: 'Regular' }, + { text: 'Delete', role: 'destructive' }, + ]); + + expect(await getStackedOrder(page)).toEqual(['Regular', 'Delete', 'Cancel']); + }); + + test('should keep the given order when two buttons do not wrap', async ({ page }) => { + await openAlert(page, [{ text: 'Cancel', role: 'cancel' }, { text: 'OK' }]); + + await expect(page.locator('ion-alert .alert-button-group')).not.toHaveClass(/alert-button-group-vertical/); + expect(await page.locator('ion-alert .alert-button').allTextContents()).toEqual(['Cancel', 'OK']); + }); + }); +}); + configs({ palettes: ['light', 'dark'] }).forEach(({ config, screenshot, title }) => { test.describe(title('should not have visual regressions'), () => { test('more than two buttons', async ({ page }) => { diff --git a/core/src/components/alert/test/basic/index.html b/core/src/components/alert/test/basic/index.html index 8c163252e46..e4ceec3d1ab 100644 --- a/core/src/components/alert/test/basic/index.html +++ b/core/src/components/alert/test/basic/index.html @@ -143,7 +143,8 @@ header: 'Alert!', buttons: [ { - text: 'Action Action Action Action', + text: 'Cancel Cancel Cancel Cancel', + role: 'cancel', }, { text: 'Action',