diff --git a/demo/e2e/animation.spec.ts b/demo/e2e/animation.spec.ts index 229bed64..88acfd91 100644 --- a/demo/e2e/animation.spec.ts +++ b/demo/e2e/animation.spec.ts @@ -104,6 +104,17 @@ test.describe('Animation Tests', () => { await expect(shade).toHaveCount(0); }); + test('foldable page transition keeps its shade outside the control rail', async ({ page }) => { + await page.goto('/main/index', { waitUntil: 'networkidle' }); + await page.locator('ion-app').evaluate((app) => app.classList.add('ios-theme-enable-foldable')); + await page.getByRole('button', { name: 'button', exact: true }).click(); + const shade = page.locator('.ios-transition-shade'); + await expect(shade).toBeVisible(); + await page.evaluate(() => document.getAnimations().forEach((animation) => animation.pause())); + await expect(shade).toHaveCSS('clip-path', 'inset(0px 80px 0px 0px)'); + await page.evaluate(() => document.getAnimations().forEach((animation) => animation.play())); + }); + test('runs and completes the iOS popover animations', async ({ page }) => { await page.goto('/main/index/popover', { waitUntil: 'networkidle' }); await clearAnimationCalls(page); diff --git a/demo/e2e/foldable-back-button.spec.ts b/demo/e2e/foldable-back-button.spec.ts index 40020f82..357821b9 100644 --- a/demo/e2e/foldable-back-button.spec.ts +++ b/demo/e2e/foldable-back-button.spec.ts @@ -60,6 +60,73 @@ test('Native UI Shell suspension synchronously restores and resumes foldable own await expect(projection).toBeVisible(); }); +test('a stale suspension lease cannot re-hide Web controls after shell teardown', async ({ page }) => { + await page.goto('/main/index/button'); + await page.locator('ion-app').evaluate((element) => element.classList.add('ios-theme-enable-foldable')); + const source = page.locator('app-button ion-header ion-back-button').first(); + await expect(page.locator('ion-app > ion-back-button.ios-theme-foldable-back-button-projection')).toBeVisible(); + + await page.evaluate(async () => { + const shell = (window as any).nativeUIShell; + const lease = await shell.suspend(); + await shell.destroy(); + await lease.resume(); + }); + await expect(source).toBeVisible(); + await expect(page.locator('html')).not.toHaveClass(/ios-theme-native-ui-shell-prehide/); +}); + +test('Web toolbar projection switches at WillLeave and restores on cancellation', async ({ page }) => { + await page.goto('/main/index/button'); + await page.locator('ion-app').evaluate((app) => app.classList.add('ios-theme-enable-foldable')); + const projection = page.locator('ion-app > ion-back-button.ios-theme-foldable-back-button-projection'); + await expect(projection).toBeVisible(); + const routedPage = page.locator('app-button.ion-page'); + + await routedPage.evaluate((element) => { + element.dispatchEvent(new CustomEvent('ionViewWillLeave', { bubbles: true })); + }); + await expect(projection).toHaveCount(0); + await routedPage.evaluate((element) => element.dispatchEvent(new CustomEvent('ionViewDidLeave', { bubbles: true }))); + await page.waitForTimeout(150); + await expect(projection).toHaveCount(0); + + await routedPage.evaluate((element) => { + element.classList.add('ion-page-invisible'); + element.dispatchEvent(new CustomEvent('ionViewWillEnter', { bubbles: true })); + }); + await expect(projection).toBeVisible(); + await routedPage.evaluate((element) => { + element.classList.remove('ion-page-invisible'); + element.dispatchEvent(new CustomEvent('ionViewDidEnter', { bubbles: true })); + }); + + await routedPage.evaluate((element) => element.dispatchEvent(new CustomEvent('ionViewWillLeave', { bubbles: true }))); + await expect(projection).toHaveCount(0); + await routedPage.evaluate((element) => element.dispatchEvent(new Event('iosThemeFoldableTransitionCanceled', { bubbles: true }))); + await expect(projection).toBeVisible(); +}); + +test('turning foldable on during a transition honors its success or cancellation', async ({ page }) => { + await page.goto('/main/index/button'); + const app = page.locator('ion-app'); + await app.evaluate((element) => element.classList.remove('ios-theme-enable-foldable')); + const routedPage = page.locator('app-button.ion-page'); + const projection = page.locator('ion-app > ion-back-button.ios-theme-foldable-back-button-projection'); + + await routedPage.evaluate((element) => element.dispatchEvent(new CustomEvent('ionViewWillLeave', { bubbles: true }))); + await app.evaluate((element) => element.classList.add('ios-theme-enable-foldable')); + await expect(projection).toHaveCount(0); + await routedPage.evaluate((element) => element.dispatchEvent(new Event('iosThemeFoldableTransitionCanceled', { bubbles: true }))); + await expect(projection).toBeVisible(); + + await app.evaluate((element) => element.classList.remove('ios-theme-enable-foldable')); + await routedPage.evaluate((element) => element.dispatchEvent(new CustomEvent('ionViewWillLeave', { bubbles: true }))); + await app.evaluate((element) => element.classList.add('ios-theme-enable-foldable')); + await routedPage.evaluate((element) => element.dispatchEvent(new CustomEvent('ionViewDidLeave', { bubbles: true }))); + await expect(projection).toHaveCount(0); +}); + test('foldable projection respects shell opt-out and iOS mode', async ({ page }) => { await page.goto('/main/index/button'); const app = page.locator('ion-app'); diff --git a/demo/e2e/foldable-layout.spec.ts b/demo/e2e/foldable-layout.spec.ts index 3dcba1ff..14b6919c 100644 --- a/demo/e2e/foldable-layout.spec.ts +++ b/demo/e2e/foldable-layout.spec.ts @@ -2,6 +2,7 @@ import { expect, test } from '@playwright/test'; for (const direction of ['ltr', 'rtl'] as const) { test(`menus respect foldable safe-area insets in ${direction}`, async ({ page }) => { + await page.setViewportSize({ width: 390, height: 844 }); await page.goto('/main/index', { waitUntil: 'networkidle' }); const menu = page.locator('ion-menu'); @@ -11,8 +12,11 @@ for (const direction of ['ltr', 'rtl'] as const) { app.classList.add('ios-theme-enable-foldable'); element.side = direction === 'ltr' ? 'end' : 'start'; await new Promise(requestAnimationFrame); - const defaultBounds = element.getBoundingClientRect(); - const defaultRightOffset = innerWidth - defaultBounds.right; + const container = element.shadowRoot!.querySelector('[part~="container"]')!; + await element.open(false); + const defaultHostBounds = element.getBoundingClientRect(); + const defaultRightOffset = innerWidth - container.getBoundingClientRect().right; + await element.close(false); app.style.setProperty('--ios-theme-foldable-safe-area-left', '76px'); app.style.setProperty('--ios-theme-foldable-safe-area-right', '84px'); app.style.setProperty('--ion-safe-area-left', '76px'); @@ -21,9 +25,17 @@ for (const direction of ['ltr', 'rtl'] as const) { for (const side of ['start', 'end'] as const) { element.side = side; await new Promise(requestAnimationFrame); + await element.open(false); const bounds = element.getBoundingClientRect(); + const containerBounds = container.getBoundingClientRect(); const physicalSide = side === 'start' ? (direction === 'ltr' ? 'left' : 'right') : direction === 'ltr' ? 'right' : 'left'; - offsets.push({ side, physicalSide, offset: physicalSide === 'left' ? bounds.left : innerWidth - bounds.right }); + offsets.push({ + side, + physicalSide, + hostWidthPreserved: bounds.width > 0 && bounds.width === defaultHostBounds.width, + offset: physicalSide === 'left' ? containerBounds.left : innerWidth - containerBounds.right, + }); + await element.close(false); } const contentStyle = getComputedStyle(element.querySelector('ion-content')!); @@ -38,6 +50,7 @@ for (const direction of ['ltr', 'rtl'] as const) { await new Promise(requestAnimationFrame); if (!modalContent.classList.contains('ios')) throw new Error('Expected an iOS ion-content fixture'); return { + defaultHostWidthPositive: defaultHostBounds.width > 0, defaultRightOffset, offsets, safeAreaLeft: contentStyle.getPropertyValue('--ion-safe-area-left').trim(), @@ -46,16 +59,19 @@ for (const direction of ['ltr', 'rtl'] as const) { }; }, direction); - expect(result.defaultRightOffset).toBe(80); + expect({ hostWidthPositive: result.defaultHostWidthPositive, containerRight: result.defaultRightOffset }).toEqual({ + hostWidthPositive: true, + containerRight: 80, + }); expect(result.offsets).toEqual( direction === 'ltr' ? [ - { side: 'start', physicalSide: 'left', offset: 76 }, - { side: 'end', physicalSide: 'right', offset: 84 }, + { side: 'start', physicalSide: 'left', hostWidthPreserved: true, offset: 76 }, + { side: 'end', physicalSide: 'right', hostWidthPreserved: true, offset: 84 }, ] : [ - { side: 'start', physicalSide: 'right', offset: 84 }, - { side: 'end', physicalSide: 'left', offset: 76 }, + { side: 'start', physicalSide: 'right', hostWidthPreserved: true, offset: 84 }, + { side: 'end', physicalSide: 'left', hostWidthPreserved: true, offset: 76 }, ], ); expect(result.safeAreaLeft).toBe('0px'); diff --git a/demo/e2e/native-ui-shell.spec.ts b/demo/e2e/native-ui-shell.spec.ts index 18bc72cb..a2cd7d08 100644 --- a/demo/e2e/native-ui-shell.spec.ts +++ b/demo/e2e/native-ui-shell.spec.ts @@ -6,8 +6,8 @@ import * as overlayTypes from '../src/app/overlay-types'; const importer = new NodePackageImporter(resolve(__dirname, '../../')); -const mockNative = async (page: Page, fail = false) => { - await page.addInitScript((fail) => { +const mockNative = async (page: Page, fail = false, foldableRail = true) => { + const script = ([fail, foldableRail]: readonly [boolean, boolean]) => { const state = { updates: [] as any[], sequence: 0, @@ -15,6 +15,7 @@ const mockNative = async (page: Page, fail = false) => { hang: false, rejectInactiveSearch: false, rejectAllSearch: false, + rejectControlLabel: '', activate: (_event: any) => {}, search: (_event: any) => {}, metrics: (_event: any) => {}, @@ -37,7 +38,7 @@ const mockNative = async (page: Page, fail = false) => { }, ], nativePromise: async (_plugin: string, method: string, options: any) => { - if (method === 'configure') return { supported: true }; + if (method === 'configure') return { supported: true, foldableRail }; if (method === 'getWebViewMetrics') return { radius: 0 }; state.updates.push(method === 'clear' ? { ...options, controls: [] } : options); if (state.hang && method === 'update') await new Promise(() => {}); @@ -51,6 +52,11 @@ const mockNative = async (page: Page, fail = false) => { ?.filter((control: any) => control.search && (state.rejectAllSearch || control.search.available === false)) .map((control: any) => control.id) : [], + rejectedControls: state.rejectControlLabel + ? options.controls + ?.filter((control: any) => control.items.some((item: any) => item.accessibilityLabel === state.rejectControlLabel)) + .map((control: any) => control.id) + : [], }; }, nativeCallback: (_plugin: string, method: string, options: any, callback: (event: any) => void) => { @@ -63,7 +69,8 @@ const mockNative = async (page: Page, fail = false) => { }, }, }); - }, fail); + }; + await page.addInitScript(script, [fail, foldableRail] as const); }; const activate = (page: Page, label: string, duplicate = false) => @@ -324,7 +331,7 @@ test('unsupported search morph releases and restores a native fixed-slot FAB int await expect(page.locator('ion-tab-bar')).toHaveAttribute('data-native-ui-shell', ''); }); -test('foldable tabs stay in the web layer instead of using horizontal native projection', async ({ page }) => { +test('foldable tabs request native adaptive rail placement', async ({ page }) => { await mockNative(page); await page.goto('/main/index'); const app = page.locator('ion-app'); @@ -332,20 +339,23 @@ test('foldable tabs stay in the web layer instead of using horizontal native pro await expect(bar).toHaveAttribute('data-native-ui-shell', ''); await app.evaluate((element) => element.classList.add('ios-theme-enable-foldable')); - await expect(bar).not.toHaveAttribute('data-native-ui-shell'); - await expect(bar).not.toHaveClass(/ios27-enable-gesture/); + await expect(bar).toHaveAttribute('data-native-ui-shell', ''); await expect .poll(() => - page.evaluate(() => (window as any).__nativeUIShell.updates.at(-1).controls.some((control: any) => control.kind === 'ion-tab-bar')), + page.evaluate(() => + (window as any).__nativeUIShell.updates + .at(-1) + .controls.some((control: any) => control.kind === 'ion-tab-bar' && control.placement === 'foldable-rail'), + ), ) - .toBe(false); + .toBe(true); await app.evaluate((element) => element.classList.remove('ios-theme-enable-foldable')); await expect(bar).toHaveAttribute('data-native-ui-shell', ''); await expect(bar).toHaveClass(/ios27-enable-gesture/); }); -test('foldable back navigation hands ownership between native and Web projection', async ({ page }) => { +test('foldable back navigation and toolbar slots request native rail placement', async ({ page }) => { await mockNative(page); await page.goto('/main/index/native-ui-shell'); const app = page.locator('ion-app'); @@ -354,20 +364,384 @@ test('foldable back navigation hands ownership between native and Web projection await expect(source).toHaveAttribute('data-native-ui-shell', ''); await app.evaluate((element) => element.classList.add('ios-theme-enable-foldable')); - await expect(projection).toHaveCount(1); + await expect(projection).toHaveCount(0); + await expect(source).toHaveAttribute('data-native-ui-shell', ''); + await expect + .poll(() => + page.evaluate( + () => + (window as any).__nativeUIShell.updates + .at(-1) + .controls.some( + (control: any) => + control.kind === 'ion-back-button' && control.placement === 'foldable-rail' && control.toolbarSlot === undefined, + ) && + !(window as any).__nativeUIShell.updates + .at(-1) + .controls.some((control: any) => control.items.some((item: any) => item.label === 'Cancel')), + ), + ) + .toBe(true); await expect .poll(() => page.evaluate(() => - (window as any).__nativeUIShell.updates.at(-1).controls.some((control: any) => control.kind === 'ion-back-button'), + (window as any).__nativeUIShell.updates + .at(-1) + .controls.some( + (control: any) => + control.kind === 'ion-button' && + control.placement === 'foldable-rail' && + control.toolbarSlot === 'end' && + control.items.some((item: any) => item.accessibilityLabel === 'Save'), + ), ), ) - .toBe(false); + .toBe(true); + await expect + .poll(() => + page.evaluate( + () => + (window as any).__nativeUIShell.updates.at(-1).controls.find((control: any) => control.kind === 'ion-menu-button')?.toolbarSlot, + ), + ) + .toBe('start'); await app.evaluate((element) => element.classList.remove('ios-theme-enable-foldable')); await expect(projection).toHaveCount(0); await expect(source).toHaveAttribute('data-native-ui-shell', ''); }); +test('native foldable toolbar returns with Index after a pushed page', async ({ page }) => { + await mockNative(page); + await page.goto('/main/index'); + await page.locator('ion-app').evaluate((app) => app.classList.add('ios-theme-enable-foldable')); + const hasIndexActions = () => + page.evaluate(() => { + const controls = (window as any).__nativeUIShell.updates.at(-1).controls; + return controls.some( + (control: any) => control.placement === 'foldable-rail' && control.items.some((item: any) => item.accessibilityLabel === 'GitHub'), + ); + }); + await expect.poll(hasIndexActions).toBe(true); + await page.getByRole('button', { name: 'button', exact: true }).click(); + await expect(page).toHaveURL(/\/main\/index\/button$/); + await expect + .poll(() => + page.evaluate(() => { + const state = (window as any).__nativeUIShell; + const snapshot = state.updates.at(-1); + return !!snapshot.controls.find((control: any) => control.kind === 'ion-back-button')?.items[0]; + }), + ) + .toBe(true); + await page.evaluate(() => { + const state = (window as any).__nativeUIShell; + const snapshot = state.updates.at(-1); + const back = snapshot.controls.find((control: any) => control.kind === 'ion-back-button').items[0]; + state.activate({ id: back.id, revision: snapshot.revision, sequence: ++state.sequence }); + }); + await expect(page).toHaveURL(/\/main\/index$/); + await expect.poll(hasIndexActions).toBe(true); +}); + +test('native foldable actions follow WillEnter and stay enabled during navigation', async ({ page }) => { + await mockNative(page); + await page.goto('/main/index/native-ui-shell'); + await page.locator('ion-app').evaluate((app) => app.classList.add('ios-theme-enable-foldable')); + const source = page.locator('app-native-ui-shell ion-back-button'); + await expect(source).toHaveAttribute('data-native-ui-shell', ''); + const routedPage = page.locator('app-native-ui-shell.ion-page'); + + await routedPage.evaluate((element) => element.dispatchEvent(new CustomEvent('ionViewWillLeave', { bubbles: true }))); + await expect(source).not.toHaveAttribute('data-native-ui-shell', ''); + await routedPage.evaluate((element) => element.dispatchEvent(new CustomEvent('ionViewDidLeave', { bubbles: true }))); + await expect(source).not.toHaveAttribute('data-native-ui-shell', ''); + await page.waitForTimeout(150); + await expect(source).not.toHaveAttribute('data-native-ui-shell', ''); + + await routedPage.evaluate((element) => { + element.classList.add('ion-page-invisible'); + element.style.pointerEvents = 'none'; + element.dispatchEvent(new CustomEvent('ionViewWillEnter', { bubbles: true })); + }); + await expect(source).toHaveAttribute('data-native-ui-shell', ''); + const saveDisabled = () => + page.evaluate( + () => + (window as any).__nativeUIShell.updates + .findLast((update: any) => + update.controls.some((control: any) => control.items.some((item: any) => item.accessibilityLabel === 'Save')), + ) + .controls.flatMap((control: any) => control.items) + .find((item: any) => item.accessibilityLabel === 'Save').disabled, + ); + await expect.poll(saveDisabled).toBe(false); + await page.addStyleTag({ content: '.author-no-pointer { pointer-events: none }' }); + const save = page.locator('app-native-ui-shell ion-button[type=submit]'); + for (const target of [save, save.locator('xpath=..'), save.locator('xpath=../..')]) { + await target.evaluate((element) => element.classList.add('author-no-pointer')); + await expect.poll(saveDisabled).toBe(true); + await target.evaluate((element) => element.classList.remove('author-no-pointer')); + await expect.poll(saveDisabled).toBe(false); + } + await activate(page, 'Save'); + await expect(page.locator('app-native-ui-shell [data-save-count]')).toHaveText('1'); + await routedPage.evaluate((element) => { + element.classList.remove('ion-page-invisible'); + element.dispatchEvent(new CustomEvent('ionViewDidEnter', { bubbles: true })); + }); + await expect.poll(saveDisabled).toBe(false); + await save.evaluate((element) => element.classList.add('author-no-pointer')); + await expect.poll(saveDisabled).toBe(true); + await save.evaluate((element) => element.classList.remove('author-no-pointer')); + await routedPage.evaluate((element) => (element.style.pointerEvents = '')); + await routedPage.evaluate((element) => element.dispatchEvent(new CustomEvent('ionViewWillLeave', { bubbles: true }))); + await expect(source).not.toHaveAttribute('data-native-ui-shell', ''); + await routedPage.evaluate((element) => element.dispatchEvent(new Event('iosThemeFoldableTransitionCanceled', { bubbles: true }))); + await expect(source).toHaveAttribute('data-native-ui-shell', ''); +}); + +test('foldable toolbar sources are hidden before ownership and restored with their lifecycle', async ({ page }) => { + await mockNative(page); + await page.goto('/main/index/native-ui-shell'); + await page.locator('ion-app').evaluate((element) => element.classList.add('ios-theme-enable-foldable')); + const source = page.locator('app-native-ui-shell ion-button[type=submit]'); + await expect(source).toHaveAttribute('data-native-ui-shell', ''); + await expect(source).toHaveClass(/ios-theme-native-ui-shell-prehidden/); + + const prehidden = await source.evaluate((element) => { + element.removeAttribute('data-native-ui-shell'); + const style = getComputedStyle(element); + return { position: style.position, visibility: style.visibility }; + }); + expect(prehidden).toEqual({ position: 'absolute', visibility: 'hidden' }); + + const earlyBack = await page.locator('app-native-ui-shell ion-back-button').evaluate((element) => { + element.removeAttribute('data-native-ui-shell'); + element.classList.remove('ios-theme-native-ui-shell-prehidden'); + const style = getComputedStyle(element); + return { position: style.position, visibility: style.visibility }; + }); + expect(earlyBack).toEqual({ position: 'absolute', visibility: 'hidden' }); + + const customBack = page.locator('app-native-ui-shell ion-back-button'); + await customBack.evaluate((element: HTMLIonBackButtonElement) => (element.color = 'primary')); + await expect(customBack).not.toHaveAttribute('data-native-ui-shell', ''); + await expect(customBack).toHaveCSS('visibility', 'visible'); + const webOnlyBacks = await page.locator('app-native-ui-shell').evaluate((host) => { + const content = host.querySelector('ion-content')!; + const nested = document.createElement('ion-header'); + nested.innerHTML = ''; + content.append(nested); + const condensed = document.createElement('ion-header'); + condensed.setAttribute('collapse', 'condense'); + condensed.innerHTML = ''; + host.append(condensed); + return [nested.querySelector('ion-back-button')!, condensed.querySelector('ion-back-button')!].map( + (back) => getComputedStyle(back).visibility, + ); + }); + expect(webOnlyBacks).toEqual(['visible', 'visible']); + + const lateBackInitially = await page.locator('ion-app').evaluate((root) => { + const header = document.createElement('ion-header'); + header.innerHTML = ''; + root.append(header); + const back = header.querySelector('ion-back-button')!; + back.setAttribute('data-late-back', ''); + return back.classList.contains('ios-theme-foldable-back-web-owned'); + }); + expect(lateBackInitially).toBe(false); + const lateBack = page.locator('ion-back-button[data-late-back]'); + await expect(lateBack).toHaveClass(/ios-theme-native-ui-shell-prehidden/); + await expect(lateBack).toHaveAttribute('data-native-ui-shell', ''); + await page.waitForTimeout(1600); // Past the unhydrated readiness timeout. + await expect(lateBack).toHaveAttribute('data-native-ui-shell', ''); + await expect(lateBack).not.toHaveClass(/ios-theme-foldable-back-web-owned/); + await lateBack.evaluate((element) => element.closest('ion-header')?.remove()); + + const lateAction = page.locator('app-native-ui-shell ion-button[data-late-action]'); + await page + .locator('app-native-ui-shell ion-toolbar') + .first() + .evaluate((toolbar) => { + const button = document.createElement('ion-button'); + button.setAttribute('data-late-action', ''); + button.setAttribute('aria-label', 'Late action'); + button.innerHTML = ''; + toolbar.append(button); + }); + await expect(lateAction).toHaveAttribute('data-native-ui-shell', ''); + + await page.addStyleTag({ content: '.author-hidden { visibility: hidden !important }' }); + await source.evaluate((element) => element.classList.add('author-hidden')); + await expect(source).not.toHaveAttribute('data-native-ui-shell', ''); + await source.evaluate((element) => element.classList.remove('author-hidden')); + await expect(source).toHaveAttribute('data-native-ui-shell', ''); + + await source.evaluate((element) => element.classList.add('ios-theme-shell-disabled')); + await expect(source).toHaveCSS('visibility', 'visible'); + + await page.evaluate(() => (window as any).nativeUIShell.destroy()); + await expect(source).not.toHaveClass(/ios-theme-native-ui-shell-prehidden/); + await expect(source).toHaveCSS('visibility', 'visible'); +}); + +test('rejected foldable control returns to an operable Web source', async ({ page }) => { + await mockNative(page); + await page.goto('/main/index/native-ui-shell'); + await page.locator('ion-app').evaluate((element) => element.classList.add('ios-theme-enable-foldable')); + const save = page.locator('app-native-ui-shell ion-button[type=submit]'); + await expect(save).toHaveAttribute('data-native-ui-shell', ''); + await page.evaluate(() => { + (window as any).__nativeUIShell.rejectControlLabel = 'Save'; + window.dispatchEvent(new Event('nativeUIShellRefresh')); + }); + await expect(save).not.toHaveAttribute('data-native-ui-shell', ''); + await expect(save).not.toHaveClass(/ios-theme-native-ui-shell-prehidden/); + await expect(save).toHaveCSS('visibility', 'visible'); + await save.click(); + await expect(page.locator('[data-save-count]')).toHaveText('1'); +}); + +test('foldable rail remains native while its Ionic menu is open', async ({ page }) => { + await page.setViewportSize({ width: 390, height: 844 }); + await mockNative(page); + await page.goto('/main/index/native-ui-shell'); + await page.locator('app-native-ui-shell ion-menu-button').evaluate((element: HTMLIonMenuButtonElement) => (element.autoHide = false)); + await page.locator('ion-app').evaluate((element) => element.classList.add('ios-theme-enable-foldable')); + const menu = page.locator('ion-menu'); + const menuSource = page.locator('app-native-ui-shell ion-menu-button').locator('..'); + const backSource = page.locator('app-native-ui-shell ion-back-button'); + const saveSource = page.locator('app-native-ui-shell ion-button[type=submit]'); + const cancelSource = page.locator('app-native-ui-shell ion-button').filter({ hasText: 'Cancel' }); + const actionGroup = page.locator('app-native-ui-shell ion-buttons[data-glass-group]'); + const tabs = page.locator('ion-tab-bar'); + + await actionGroup.evaluate((element) => { + const cancel = document.createElement('ion-button') as HTMLIonButtonElement; + cancel.classList.add('ios'); + cancel.fill = 'clear'; + cancel.textContent = 'Cancel mixed action'; + element.prepend(cancel); + }); + const mixedCancel = actionGroup.locator('ion-button').filter({ hasText: 'Cancel mixed action' }); + const mixedIcons = actionGroup.locator('ion-button').filter({ has: page.locator('ion-icon') }); + await expect(actionGroup).not.toHaveAttribute('data-native-ui-shell'); + await expect(mixedIcons.nth(0)).toHaveAttribute('data-native-ui-shell', ''); + await expect(mixedIcons.nth(1)).toHaveAttribute('data-native-ui-shell', ''); + await expect(mixedCancel).not.toHaveAttribute('data-native-ui-shell'); + await expect(mixedCancel).toBeVisible(); + await expect + .poll(() => + page.evaluate(() => { + const controls = (window as any).__nativeUIShell.updates.at(-1)?.controls ?? []; + const demoActions = (control: any) => + control.items.filter((item: any) => ['GitHub', 'Refresh'].includes(item.accessibilityLabel)).length; + return { + groups: controls.filter((control: any) => control.kind === 'ion-buttons' && demoActions(control) === 2).length, + individuals: controls.filter((control: any) => control.kind === 'ion-button' && demoActions(control) > 0).length, + }; + }), + ) + .toEqual({ groups: 1, individuals: 0 }); + const nativeDisabled = () => + page.evaluate(() => { + const items = (window as any).__nativeUIShell.updates.at(-1).controls.flatMap((control: any) => control.items); + return { + save: items.find((item: any) => item.accessibilityLabel === 'Save')?.disabled, + actions: items.filter((item: any) => ['GitHub', 'Refresh'].includes(item.accessibilityLabel)).map((item: any) => item.disabled), + }; + }); + await actionGroup.evaluate((element: HTMLElement) => (element.style.pointerEvents = 'none')); + await activate(page, 'menu'); + await expect(menu).toHaveClass(/show-menu/); + await expect.poll(nativeDisabled).toEqual({ save: false, actions: [true, true] }); + await actionGroup.evaluate((element: HTMLElement) => (element.style.pointerEvents = '')); + await expect.poll(nativeDisabled).toEqual({ save: false, actions: [false, false] }); + await mixedCancel.evaluate((element) => element.remove()); + await expect(actionGroup).toHaveAttribute('data-native-ui-shell', ''); + await saveSource.evaluate((element: HTMLElement) => (element.style.pointerEvents = 'none')); + await actionGroup.evaluate((element: HTMLElement) => (element.style.pointerEvents = 'none')); + await expect.poll(nativeDisabled).toEqual({ save: true, actions: [true, true] }); + await expect(menu).toHaveClass(/show-menu/); + await activate(page, 'Save'); + await expect(page.locator('[data-save-count]')).toHaveText('0'); + await saveSource.evaluate((element: HTMLElement) => (element.style.pointerEvents = '')); + await actionGroup.evaluate((element: HTMLElement) => (element.style.pointerEvents = '')); + await expect.poll(nativeDisabled).toEqual({ save: false, actions: [false, false] }); + await expect.poll(() => cancelSource.evaluate((element) => getComputedStyle(element).pointerEvents)).toBe('none'); + for (const source of [menuSource, backSource, saveSource, tabs]) await expect(source).toHaveAttribute('data-native-ui-shell', ''); + await expect(cancelSource).toBeVisible(); + await activate(page, 'Save'); + await expect(page.locator('[data-save-count]')).toHaveText('1'); + // Placement is selected when this page enters, not re-evaluated from later content. + await cancelSource.evaluate((element) => { + element.setAttribute('data-morphed-cancel', ''); + element.textContent = ''; + const icon = document.createElement('ion-icon'); + icon.setAttribute('slot', 'icon-only'); + icon.setAttribute('name', 'checkmark'); + element.append(icon); + }); + const morphedCancel = page.locator('app-native-ui-shell ion-button[data-morphed-cancel]'); + await expect(morphedCancel).not.toHaveAttribute('data-native-ui-shell', ''); + await expect(morphedCancel).not.toHaveClass(/ios-theme-native-ui-shell-prehidden/); + await expect(morphedCancel).toBeVisible(); +}); + +test('foldable controls stay operable on Web when the native side rail is unavailable', async ({ page }) => { + await page.setViewportSize({ width: 390, height: 844 }); + await mockNative(page, false, false); + await page.goto('/main/index/native-ui-shell'); + await page.locator('app-native-ui-shell ion-menu-button').evaluate((element: HTMLIonMenuButtonElement) => (element.autoHide = false)); + await page.locator('ion-app').evaluate((element) => element.classList.add('ios-theme-enable-foldable')); + + const projection = page.locator('ion-app > ion-back-button.ios-theme-foldable-back-button-projection'); + const menuProjection = page.locator('ion-app > ion-menu-button.ios-theme-foldable-toolbar-projection'); + const saveProjection = page.locator('ion-app > ion-button.ios-theme-foldable-toolbar-projection[aria-label=Save]'); + await expect(projection).toBeVisible(); + await expect(menuProjection).toBeVisible(); + await expect(saveProjection).toBeVisible(); + await expect(page.locator('ion-tab-bar')).toBeVisible(); + await page.addStyleTag({ content: '.author-hidden { visibility: hidden !important }' }); + const saveSource = page.locator('app-native-ui-shell ion-button[type=submit]'); + await saveSource.evaluate((element) => element.classList.add('author-hidden')); + await expect(saveProjection).toHaveCount(0); + await saveSource.evaluate((element) => element.classList.remove('author-hidden')); + await expect(saveProjection).toBeVisible(); + await expect.poll(() => page.evaluate(() => (window as any).nativeUIShell.getStatus().projected)).toBeGreaterThan(0); + await expect + .poll(() => + page.evaluate(() => + (window as any).__nativeUIShell.updates.every((snapshot: any) => + snapshot.controls.every((control: any) => control.placement !== 'foldable-rail'), + ), + ), + ) + .toBe(true); + + await menuProjection.click(); + await expect(page.locator('ion-menu')).toHaveClass(/show-menu/); + await expect(projection).toBeVisible(); + await expect(menuProjection).toBeVisible(); + await expect(saveProjection).toBeVisible(); + await expect(page.locator('ion-tab-bar')).toBeVisible(); + await saveProjection.click(); + await expect(page.locator('[data-save-count]')).toHaveText('1'); + + await page.evaluate(() => { + const outlet = document.querySelector('ion-tabs ion-router-outlet')!; + (window as any).__foldableBackCloneMoved = false; + new MutationObserver(() => { + if (outlet.querySelector(':scope > ion-back-button.ion-cloned-element')) (window as any).__foldableBackCloneMoved = true; + }).observe(outlet, { childList: true }); + }); + await projection.click(); + await expect(page).toHaveURL(/\/main\/index$/); + expect(await page.evaluate(() => (window as any).__foldableBackCloneMoved)).toBe(false); +}); + test('native click preserves external form submit, disabled, and duplicate protection', async ({ page }) => { await mockNative(page); await page.goto('/main/index/native-ui-shell'); @@ -387,15 +761,20 @@ test('native click preserves external form submit, disabled, and duplicate prote expect(await page.evaluate(() => (window as any).__nativeUIShell.updates.length)).toBe(count); }); -test('ancestor display/theme aliases and non-glass fills restore Web', async ({ page }) => { +test('ancestor display, element opt-out aliases and non-glass fills restore Web', async ({ page }) => { await mockNative(page); await page.goto('/main/index/native-ui-shell'); const button = page.locator('app-native-ui-shell ion-button[type=submit]'); + const toolbar = button.locator('xpath=ancestor::ion-toolbar[1]'); await expect(button).toHaveAttribute('data-native-ui-shell', ''); - for (const toggle of ['Parent hidden', 'Theme disabled']) { - await page.getByRole('button', { name: `${toggle}: false`, exact: true }).click(); + await page.getByRole('button', { name: 'Parent hidden: false', exact: true }).click(); + await expect(button).not.toHaveAttribute('data-native-ui-shell'); + await page.getByRole('button', { name: 'Parent hidden: true', exact: true }).click(); + await expect(button).toHaveAttribute('data-native-ui-shell', ''); + for (const name of ['ios-theme-disabled', 'ios26-disabled', 'ionic-theme-disabled']) { + await toolbar.evaluate((element, name) => element.classList.add(name), name); await expect(button).not.toHaveAttribute('data-native-ui-shell'); - await page.getByRole('button', { name: `${toggle}: true`, exact: true }).click(); + await toolbar.evaluate((element, name) => element.classList.remove(name), name); await expect(button).toHaveAttribute('data-native-ui-shell', ''); } for (const name of ['ios-theme-disabled', 'ios26-disabled', 'ionic-theme-disabled']) { @@ -404,6 +783,11 @@ test('ancestor display/theme aliases and non-glass fills restore Web', async ({ await button.evaluate((element, name) => element.classList.remove(name), name); await expect(button).toHaveAttribute('data-native-ui-shell', ''); } + await page + .locator('app-native-ui-shell ion-buttons[slot=end] ion-button') + .filter({ hasText: 'Cancel' }) + .evaluate((element) => element.remove()); + await expect(button).toHaveAttribute('data-native-ui-shell', ''); for (const fill of ['clear', 'solid', 'outline']) { await page.getByRole('button', { name: `fill: ${fill}`, exact: true }).click(); await expect(button).not.toHaveAttribute('data-native-ui-shell'); @@ -719,6 +1103,16 @@ test('clear ion-buttons share one glass surface and keep independent actions', a const native = await projectedGroup(); expect(native.items).toHaveLength(2); expect(native.items.map((item: any) => item.accessibilityLabel)).toEqual(['GitHub', 'Refresh']); + const githubName = () => + page.evaluate( + (id) => + (window as any).__nativeUIShell.updates.at(-1).controls.find((control: any) => control.id === id)?.items[0]?.accessibilityLabel, + native.id, + ); + await github.locator('ion-icon').evaluate((icon) => icon.setAttribute('aria-hidden', 'true')); + await expect.poll(githubName).not.toBe('GitHub'); + await github.locator('ion-icon').evaluate((icon) => icon.removeAttribute('aria-hidden')); + await expect.poll(githubName).toBe('GitHub'); for (const item of native.items) expect(item.icon).toMatch(/^iVBOR/); await activate(page, 'GitHub', true); await expect(page.locator('ion-title').filter({ hasText: 'Actions:' })).toHaveText('Actions: 1 / 0'); @@ -775,7 +1169,7 @@ test('theme-disabled ion-buttons project eligible buttons independently', async await expect .poll(() => page.evaluate(() => { - const controls = (window as any).__nativeUIShell.updates.at(-1).controls; + const controls = (window as any).__nativeUIShell.updates.at(-1)?.controls ?? []; const isDemoAction = (control: any) => control.items.some((item: any) => ['GitHub', 'Refresh'].includes(item.accessibilityLabel)); return { buttons: controls.filter((control: any) => control.kind === 'ion-button' && isDemoAction(control)).length, @@ -1071,6 +1465,7 @@ test('menu button toggles its Ionic menu and follows autoHide, disabled and spli await page.goto('/main/index/native-ui-shell'); const menu = page.locator('ion-menu'); const button = page.locator('app-native-ui-shell ion-menu-button'); + await button.evaluate((element: HTMLIonMenuButtonElement) => (element.autoHide = true)); const surface = button.locator('..'); await expect(surface).toHaveAttribute('data-native-ui-shell', ''); await expect diff --git a/demo/ios/NativeUIShellTests/NativeUIShellTests.swift b/demo/ios/NativeUIShellTests/NativeUIShellTests.swift index bd6650ea..cd3fcfd8 100644 --- a/demo/ios/NativeUIShellTests/NativeUIShellTests.swift +++ b/demo/ios/NativeUIShellTests/NativeUIShellTests.swift @@ -108,6 +108,8 @@ final class NativeUIShellTests: XCTestCase { entry.tap() let save = nativeButton(app, label: "Save") XCTAssertTrue(save.waitForExistence(timeout: 10), app.debugDescription) + XCTAssertFalse(nativeButton(app, label: "Cancel").exists, "Text-only toolbar actions must remain in Web") + XCTAssertTrue(app.webViews.buttons["Cancel"].firstMatch.exists, app.debugDescription) XCUIDevice.shared.press(.home) XCTAssertTrue(app.wait(for: .runningBackground, timeout: 5)) app.activate() @@ -249,6 +251,72 @@ final class NativeUIShellTests: XCTestCase { capture("native-tab-settings") } + func testNativeFoldableRail() throws { + let app = XCUIApplication(bundleIdentifier: "io.ionic.theme.ios27") + app.launch() + let toggle = app.webViews.switches["Foldable Mode"] + XCTAssertTrue(toggle.waitForExistence(timeout: 15), app.debugDescription) + if toggle.value as? String == "0" { toggle.tap() } + + let index = app.buttons["Index"] + let library = app.buttons["Library"] + XCTAssertTrue(index.waitForExistence(timeout: 10), app.debugDescription) + XCTAssertTrue(library.waitForExistence(timeout: 10), app.debugDescription) + assertOnFoldableRail(index, in: app) + library.tap() + XCTAssertTrue(app.webViews.staticTexts["Library"].firstMatch.waitForExistence(timeout: 5), "The native tab did not project its activation to Web\n" + app.debugDescription) + XCTAssertTrue(library.isSelected, app.debugDescription) + index.tap() + XCTAssertTrue(toggle.waitForExistence(timeout: 5), app.debugDescription) + capture("native-foldable-index") + openPage(app, name: "native-ui-shell") + let save = nativeButton(app, label: "Save") + XCTAssertTrue(save.waitForExistence(timeout: 10), app.debugDescription) + save.tap() + XCTAssertTrue(savedOnce(app).waitForExistence(timeout: 5), app.debugDescription) + let github = app.buttons["GitHub"] + XCTAssertTrue(github.waitForExistence(timeout: 5), app.debugDescription) + github.tap() + XCTAssertTrue(app.webViews.staticTexts["Actions: 1 / 0"].waitForExistence(timeout: 5), app.debugDescription) + let refresh = app.buttons["Refresh"] + XCTAssertTrue(refresh.waitForExistence(timeout: 5), app.debugDescription) + refresh.tap() + XCTAssertTrue(app.webViews.staticTexts["Actions: 1 / 1"].waitForExistence(timeout: 5), app.debugDescription) + capture("native-foldable-toolbar") + + let back = app.buttons["BackButton"] + XCTAssertTrue(back.waitForExistence(timeout: 5), app.debugDescription) + assertOnFoldableRail(back, in: app) + back.tap() + XCTAssertTrue(toggle.waitForExistence(timeout: 10), app.debugDescription) + XCTAssertTrue(nativeButton(app, label: "GitHub").waitForExistence(timeout: 10), "Index toolbar did not return after native back\n" + app.debugDescription) + openPage(app, name: "native-ui-shell") + XCTAssertTrue(back.waitForExistence(timeout: 5), app.debugDescription) + let pageMenu = nativeButton(app, label: "menu") + XCTAssertTrue(pageMenu.waitForExistence(timeout: 5), app.debugDescription) + pageMenu.tap() + let menuLink = app.webViews.links["Docs"] + XCTAssertTrue(menuLink.waitForExistence(timeout: 5), app.debugDescription) + capture("native-foldable-menu-open") + for control in [pageMenu, back, save, library] { + XCTAssertTrue(control.waitForExistence(timeout: 5), "Every foldable rail control must remain native while the menu is open\n" + app.debugDescription) + assertOnFoldableRail(control, in: app) + XCTAssertTrue(control.isEnabled, "Foldable rail controls must remain enabled while Ionic disables the covered page\n" + app.debugDescription) + } + save.tap() + app.coordinate(withNormalizedOffset: CGVector(dx: 0.81, dy: 0.55)).tap() + XCTAssertTrue(menuLink.waitForNonExistence(timeout: 5), "The projected Ionic menu button did not close its menu\n" + app.debugDescription) + XCTAssertTrue(savedOnce(app).waitForExistence(timeout: 5), "The native control stopped projecting actions while the menu was open\n" + app.debugDescription) + capture("native-foldable-after-menu") + } + + private func assertOnFoldableRail(_ element: XCUIElement, in app: XCUIApplication, + file: StaticString = #filePath, line: UInt = #line) { + XCTAssertGreaterThan(element.frame.midX, app.frame.width * 0.8, + "Expected native control on the physical right rail, got \(element.frame) in \(app.frame)", + file: file, line: line) + } + func testNativeShellPageAuditAndRepeatedNavigation() throws { executionTimeAllowance = 900 let app = XCUIApplication(bundleIdentifier: "io.ionic.theme.ios27") diff --git a/demo/package-lock.json b/demo/package-lock.json index 82ac6ad5..d386d48d 100644 --- a/demo/package-lock.json +++ b/demo/package-lock.json @@ -56,13 +56,13 @@ }, "..": { "name": "@rdlabo/ionic-theme-ios27", - "version": "1.0.1", + "version": "1.0.4", "bundleDependencies": [ "@rdlabo/ionic-theme-utils" ], "license": "MIT", "dependencies": { - "@rdlabo/ionic-theme-utils": "git+https://github.com/rdlabo-dev/ionic-theme-utils.git#e82be4389d9209750229f5fee990046b5b1217da" + "@rdlabo/ionic-theme-utils": "git+https://github.com/rdlabo-dev/ionic-theme-utils.git#44b1535387c326ed69fc239d38dbdb9776b629c2" }, "devDependencies": { "@capacitor/core": "^8.5.2", diff --git a/demo/src/app/docs/docs-content.generated.ts b/demo/src/app/docs/docs-content.generated.ts index 88e3180a..dab40a24 100644 --- a/demo/src/app/docs/docs-content.generated.ts +++ b/demo/src/app/docs/docs-content.generated.ts @@ -1,3 +1,3 @@ // Generated from docs/special-markup.md. Do not edit directly. export const docsContentHtml = - '

Special markup and classes

\n

Most Ionic markup works without changes. The combinations below are explicit opt-ins provided by the theme.

\n

Primary submit buttons

\n

Solid submit buttons use the Ionic color's contrast value for their foreground. Their directional edge treatment follows the iOS 27 prominent-button appearance and does not require an additional brightness color.

\n
<ion-button type="submit" color="primary">Submit</ion-button>\n<ion-button class="button-submit" fill="solid" color="primary">Continue</ion-button>
\n\n
\n
Preview
\nSubmit\nContinue\n
\n\n

Use .button-submit when the button needs the same treatment but cannot use type="submit".

\n

Preferred overlay actions

\n

For iOS alerts and action sheets, set role: 'preferred' on a button to give it a filled --ion-color-primary background and --ion-color-primary-contrast text and icons. While pressed, the background uses --ion-color-primary-shade. This is a theme convention using Ionic's custom button roles; it does not automatically select or invoke the action. Dismissal reports the role as preferred.

\n
buttons: [\n  { text: \'Cancel\', role: \'cancel\' },\n  { text: \'Continue\', role: \'preferred\' },\n];
\n\n

Buttons with no role or default keep the normal text color. cancel retains Ionic's cancellation behavior, selected remains a selection state, and destructive uses --ios-theme-destructive-color. An existing confirm role is not treated as preferred. Use preferred for the recommended action, not simply any action that confirms a choice.

\n

Floating iPad sheets

\n

Set expandToScroll: false on a sheet modal to use floating lower corners and a 20px bottom gap on iPad. Ionic then sizes the visible page at each breakpoint, so the theme can style it with CSS alone. Content scrolls within the current breakpoint; dragging the handle still resizes the sheet. With the default expandToScroll: true, the sheet keeps Ionic's bottom-attached layout and scroll-to-expand behavior.

\n

Tab bar position

\n

Add one of tab-bar-position-start, tab-bar-position-center, or tab-bar-position-end to an iOS ion-tab-bar to position the whole bar within its safe area. These classes work with both slot="top" and slot="bottom" and preserve the bar's width and press animation. Start and end follow the text direction (reversed in RTL). Without a class, the existing placement is unchanged.

\n
<ion-tab-bar slot="bottom" class="tab-bar-position-center">\n  <ion-tab-button tab="home">Home</ion-tab-button>\n  <ion-tab-button tab="settings">Settings</ion-tab-button>\n</ion-tab-bar>
\n\n

These classes do not reposition a separate ion-fab; leave room for it when choosing the bar's position.

\n

Foldable layouts

\n

To simulate the iPhone Duo layout on the web, add .ios-theme-enable-foldable to the active ion-app. Use body only when the application has no ion-app root:

\n
<ion-app class="ios-theme-enable-foldable">...</ion-app>
\n\n

The class reserves 80px on the physical right by default, matching the system navigation region measured in the iPhone Duo Simulator. The physical left defaults to 0px. Override --ios-theme-foldable-safe-area-left or --ios-theme-foldable-safe-area-right when simulating a different foldable layout.

\n

This keeps routers and component backgrounds full-viewport. ion-content moves its scroll foreground, ion-toolbar moves its container foreground, and ion-fab adjusts only when it is placed beside the system UI. The corresponding Ionic safe-area variable is reset inside those foreground components so descendants do not add the inset again.

\n

ion-menu, ion-modal, and ion-popover are handled as separate surfaces: their internal foreground components do not receive the main-page conversion and retain Ionic's standard safe-area handling. A menu presented beside the system UI is positioned after the corresponding inset; a menu from the opposite side is unchanged. Left and right remain physical coordinates in RTL, while Ionic's side="start" and side="end" values remain logical.

\n

The foldable values are web-layout simulation inputs. They are independent from Ionic's normal iPhone safe-area variables and do not change ordinary iPhone layouts unless the opt-in class is present.

\n

When the app contains ion-tabs, foldable mode moves its iOS tab bar into the physical right-side reserved region and aligns it above the bottom safe area. The Ionic slot value does not select a different foldable position. The stable rail is icon-only, matching a four-tab SwiftUI TabView on iPhone Duo. While the user presses and drags across the rail, every icon-and-label tab reveals its label so the pending destination stays identifiable. The tab bar remains in the web layer and receives pointer input in the simulated system region. Use ion-menu when navigation should become a sidebar; foldable mode does not convert tabs into a menu.

\n

When enableNativeUIShell() is initialized at application startup, foldable mode uses the same document-level projection lifecycle for fixed-toolbar navigation and actions. The active ion-back-button and ion-button or ion-menu-button controls that contain an icon remain the sources of truth, are hidden while owned, and are represented by interactive Web clones in the physical right-side system rail. Buttons in the same ion-buttons group share a vertical rail group. An icon-and-text button is shown as its icon while retaining its accessible label; a text-only button stays in the original toolbar. Menus, modals, and popovers retain their own toolbar layout.

\n

These Web projections also work when no ion-tabs exists and when native projection is unavailable. Disabling foldable mode or leaving the page removes the clones and restores their sources. Override --ios-theme-foldable-toolbar-top when the simulated system controls use a different vertical layout.

\n

Two-line inset list items

\n

Place an unslotted ion-label immediately alongside an unslotted ion-note to render a two-line item. When using the iOS-style inset-list background, wrap the items in ion-item-group; keep ion-list-header outside the group.

\n
<ion-list inset="true">\n  <ion-list-header>\n    <ion-label>Connections</ion-label>\n  </ion-list-header>\n  <ion-item-group>\n    <ion-item>\n      <ion-label>Network &amp; internet</ion-label>\n      <ion-note>Mobile, Wi-Fi, hotspot</ion-note>\n    </ion-item>\n  </ion-item-group>\n</ion-list>
\n\n
\n
Preview
\n\n \n Connections\n \n \n \n Network & internet\n Mobile, Wi-Fi, hotspot\n \n \n\n
\n\n

Use slot="end" on ion-note when you want the standard trailing-note layout instead.

\n

Inset-list section headers

\n

Add .item-group-header to an ion-item-group to create the centered icon, title, and description used at the top of the component demo pages.

\n

This is an introductory group. Place regular list items in a separate ion-item-group that follows it.

\n
<ion-list inset="true">\n  <ion-item-group class="item-group-header">\n    <ion-item>\n      <ion-label>\n        <ion-icon name="list" style="background: var(--ion-color-primary)"></ion-icon>\n        <h2>Lists</h2>\n        <ion-text>Inset-list examples</ion-text>\n      </ion-label>\n    </ion-item>\n  </ion-item-group>\n  <ion-item-group>\n    <ion-item><ion-label>First item</ion-label></ion-item>\n  </ion-item-group>\n</ion-list>
\n\n
\n
Preview
\n\n \n \n \n \n

Lists

\n Inset-list examples\n
\n
\n
\n \n First item\n \n
\n
\n\n

Full-width segments

\n

Add .segment-style-glass to give a segment the same glass surface and selected indicator treatment as the tab bar. The class preserves the segment's existing dimensions and text colors, supports scrollable segments, and respects Ionic's public --background property.

\n
<ion-segment class="segment-style-glass" value="available">\n  <ion-segment-button value="available">Available</ion-segment-button>\n  <ion-segment-button value="away">Away</ion-segment-button>\n</ion-segment>
\n\n

For colored segments, use Ionic's color property (for example, color="primary" or color="secondary"). Ionic uses the palette's base color for the softly tinted track while keeping the selected surface and labels neutral. A surrounding colored toolbar only supplies colors when the segment has no color of its own. The optional moving glass inherits the selected surface color, and custom Ionic palettes work without additional registration.

\n

Add .segment-expand when segment buttons should divide the available width evenly. The class also changes the Liquid Glass effect sizing when registerSegmentEffect is used.

\n

Segments use a 32px minimum height in content and a 48px minimum height inside ion-toolbar. .segment-expand keeps the compact 32px layout in a toolbar. Compact segments retain Ionic's flat background and indicator colors; only the regular toolbar variant has a glass container and scales its outer container while pressed. Content and expanded segments keep their outer bounds. The optional moving glass lens is independent of the container's background.

\n
<ion-segment class="segment-expand" value="new">\n  <ion-segment-button value="new"><ion-label>New</ion-label></ion-segment-button>\n  <ion-segment-button value="replied"><ion-label>Replied</ion-label></ion-segment-button>\n</ion-segment>
\n\n
\n
Preview
\n\n New\n Replied\n\n
\n\n

Classic search bar in a condense header

\n

The theme gives iOS search bars the iOS 27 appearance by default. Add .searchbar-classic to the search field shown beneath a large title in an ion-header with collapse="condense". It uses the conventional filled iOS appearance and collapses with the large title instead of remaining in the fixed header.

\n

Place it in a toolbar with a color, such as color="light"; the classic background is derived from that color's contrast value.

\n

The example uses Ionic's standard collapsible large-title structure. Scroll the preview to collapse the large title and reveal the fixed header.

\n
<div class="ion-page">\n  <ion-header translucent="true">\n    <ion-toolbar color="light">\n      <ion-title>Search</ion-title>\n    </ion-toolbar>\n  </ion-header>\n  <ion-content color="light" fullscreen="true">\n    <ion-header collapse="condense">\n      <ion-toolbar color="light">\n        <ion-title size="large">Search</ion-title>\n      </ion-toolbar>\n      <ion-toolbar color="light">\n        <ion-searchbar class="searchbar-classic" placeholder="Filter results"></ion-searchbar>\n      </ion-toolbar>\n    </ion-header>\n    <ion-list inset="true">\n      <ion-item-group>\n        <ion-item><ion-label>Recent item 1</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 2</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 3</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 4</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 5</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 6</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 7</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 8</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 9</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 10</ion-label></ion-item>\n      </ion-item-group>\n    </ion-list>\n  </ion-content>\n</div>
\n\n
\n
Preview
\n
\n \n \n Search\n \n \n \n \n \n Search\n \n \n \n \n \n \n \n Recent item 1\n Recent item 2\n Recent item 3\n Recent item 4\n Recent item 5\n Recent item 6\n Recent item 7\n Recent item 8\n Recent item 9\n Recent item 10\n \n \n \n
\n
\n\n

The .ion-page wrapper makes this embedded preview behave like a complete routed page. An application using ion-router-outlet normally receives that page container automatically. The inset list and its items only provide enough content to demonstrate scrolling; they are not required by .searchbar-classic.

\n

Search-bar toolbars

\n

Add .toolbar-searchbar when an ion-toolbar combines a search bar with start or end buttons. The class centers the slotted controls and adjusts the spacing around the search field.

\n
<ion-toolbar class="toolbar-searchbar">\n  <ion-buttons slot="start">\n    <ion-button>Cancel</ion-button>\n  </ion-buttons>\n  <ion-searchbar></ion-searchbar>\n</ion-toolbar>
\n\n
\n
Preview
\n\n \n Cancel\n \n \n\n
\n\n

Opting out

\n

Add .ios-theme-disabled to an individual Ionic component when it must retain Ionic's standard iOS styling.

\n

.ios26-disabled is deprecated but remains supported as an alias with the same behavior. Use .ios-theme-disabled for new code.

\n
<ion-button>iOS 27 theme</ion-button> <ion-button class="ios-theme-disabled">Standard Ionic button</ion-button>
\n\n
\n
Preview
\niOS 27 theme Standard Ionic button\n
\n\n

For the background model behind inset lists, see Using ion-item-group.

\n'; + '

Special markup and classes

\n

Most Ionic markup works without changes. The combinations below are explicit opt-ins provided by the theme.

\n

Primary submit buttons

\n

Solid submit buttons use the Ionic color's contrast value for their foreground. Their directional edge treatment follows the iOS 27 prominent-button appearance and does not require an additional brightness color.

\n
<ion-button type="submit" color="primary">Submit</ion-button>\n<ion-button class="button-submit" fill="solid" color="primary">Continue</ion-button>
\n\n
\n
Preview
\nSubmit\nContinue\n
\n\n

Use .button-submit when the button needs the same treatment but cannot use type="submit".

\n

Preferred overlay actions

\n

For iOS alerts and action sheets, set role: 'preferred' on a button to give it a filled --ion-color-primary background and --ion-color-primary-contrast text and icons. While pressed, the background uses --ion-color-primary-shade. This is a theme convention using Ionic's custom button roles; it does not automatically select or invoke the action. Dismissal reports the role as preferred.

\n
buttons: [\n  { text: \'Cancel\', role: \'cancel\' },\n  { text: \'Continue\', role: \'preferred\' },\n];
\n\n

Buttons with no role or default keep the normal text color. cancel retains Ionic's cancellation behavior, selected remains a selection state, and destructive uses --ios-theme-destructive-color. An existing confirm role is not treated as preferred. Use preferred for the recommended action, not simply any action that confirms a choice.

\n

Floating iPad sheets

\n

Set expandToScroll: false on a sheet modal to use floating lower corners and a 20px bottom gap on iPad. Ionic then sizes the visible page at each breakpoint, so the theme can style it with CSS alone. Content scrolls within the current breakpoint; dragging the handle still resizes the sheet. With the default expandToScroll: true, the sheet keeps Ionic's bottom-attached layout and scroll-to-expand behavior.

\n

Tab bar position

\n

Add one of tab-bar-position-start, tab-bar-position-center, or tab-bar-position-end to an iOS ion-tab-bar to position the whole bar within its safe area. These classes work with both slot="top" and slot="bottom" and preserve the bar's width and press animation. Start and end follow the text direction (reversed in RTL). Without a class, the existing placement is unchanged.

\n
<ion-tab-bar slot="bottom" class="tab-bar-position-center">\n  <ion-tab-button tab="home">Home</ion-tab-button>\n  <ion-tab-button tab="settings">Settings</ion-tab-button>\n</ion-tab-bar>
\n\n

These classes do not reposition a separate ion-fab; leave room for it when choosing the bar's position.

\n

Foldable layouts

\n

To simulate the iPhone Duo layout on the web, add .ios-theme-enable-foldable to the active ion-app. Use body only when the application has no ion-app root:

\n
<ion-app class="ios-theme-enable-foldable">...</ion-app>
\n\n

The class reserves 80px on the physical right by default, matching the system navigation region measured in the iPhone Duo Simulator. The physical left defaults to 0px. Override --ios-theme-foldable-safe-area-left or --ios-theme-foldable-safe-area-right when simulating a different foldable layout.

\n

This keeps routers and component backgrounds full-viewport. ion-content moves its scroll foreground, ion-toolbar moves its container foreground, and ion-fab adjusts only when it is placed beside the system UI. The corresponding Ionic safe-area variable is reset inside those foreground components so descendants do not add the inset again.

\n

ion-menu, ion-modal, and ion-popover are handled as separate surfaces: their internal foreground components do not receive the main-page conversion and retain Ionic's standard safe-area handling. A menu presented beside the system UI keeps Ionic's full-viewport animation host and offsets only its visible container by the corresponding inset; a menu from the opposite side is unchanged. Left and right remain physical coordinates in RTL, while Ionic's side="start" and side="end" values remain logical.

\n

The foldable values are web-layout simulation inputs. They are independent from Ionic's normal iPhone safe-area variables and do not change ordinary iPhone layouts unless the opt-in class is present.

\n

When the app contains ion-tabs, foldable mode moves its iOS tab bar into the physical right-side reserved region and aligns it above the bottom safe area. The Ionic slot value does not select a different foldable position. Without Native UI Shell, the stable Web rail is icon-only, matching a four-tab SwiftUI TabView on iPhone Duo. While the user presses and drags across that rail, every icon-and-label tab reveals its label so the pending destination stays identifiable. The Web tab bar receives pointer input in the simulated system region. Use ion-menu when navigation should become a sidebar; foldable mode does not convert tabs into a menu.

\n

On supported iOS versions, initializing enableNativeUIShell() at application startup hands eligible foldable tabs, back navigation, menu buttons, and fixed-toolbar actions to a native SwiftUI TabView and toolbar. The Ionic controls remain the sources of labels, icons, selected/disabled state, form submission, routing, and click handlers while SwiftUI owns adaptive placement and interaction. Fixed-toolbar actions need an icon or SVG, no direct text node, and standard fill="default" or fill="clear" to be eligible for the side rail. Text-only actions stay in the original Web toolbar. Placement is chosen when a routed page enters; changing an existing button's content does not move it between the toolbar and rail until the page leaves and re-enters. Menus, modals, and popovers retain their own toolbar layout.

\n

On Web, Android, older iOS, or when native projection is unavailable during setup, the Web tab bar and fixed-toolbar clones remain the fallback. Those projections also work when no ion-tabs exists; text-only actions stay in the original Web toolbar. Disabling foldable mode or leaving the page removes the native ownership or Web clones and restores their sources. Override --ios-theme-foldable-toolbar-top when the simulated system controls use a different vertical layout.

\n

Two-line inset list items

\n

Place an unslotted ion-label immediately alongside an unslotted ion-note to render a two-line item. When using the iOS-style inset-list background, wrap the items in ion-item-group; keep ion-list-header outside the group.

\n
<ion-list inset="true">\n  <ion-list-header>\n    <ion-label>Connections</ion-label>\n  </ion-list-header>\n  <ion-item-group>\n    <ion-item>\n      <ion-label>Network &amp; internet</ion-label>\n      <ion-note>Mobile, Wi-Fi, hotspot</ion-note>\n    </ion-item>\n  </ion-item-group>\n</ion-list>
\n\n
\n
Preview
\n\n \n Connections\n \n \n \n Network & internet\n Mobile, Wi-Fi, hotspot\n \n \n\n
\n\n

Use slot="end" on ion-note when you want the standard trailing-note layout instead.

\n

Inset-list section headers

\n

Add .item-group-header to an ion-item-group to create the centered icon, title, and description used at the top of the component demo pages.

\n

This is an introductory group. Place regular list items in a separate ion-item-group that follows it.

\n
<ion-list inset="true">\n  <ion-item-group class="item-group-header">\n    <ion-item>\n      <ion-label>\n        <ion-icon name="list" style="background: var(--ion-color-primary)"></ion-icon>\n        <h2>Lists</h2>\n        <ion-text>Inset-list examples</ion-text>\n      </ion-label>\n    </ion-item>\n  </ion-item-group>\n  <ion-item-group>\n    <ion-item><ion-label>First item</ion-label></ion-item>\n  </ion-item-group>\n</ion-list>
\n\n
\n
Preview
\n\n \n \n \n \n

Lists

\n Inset-list examples\n
\n
\n
\n \n First item\n \n
\n
\n\n

Full-width segments

\n

Add .segment-style-glass to give a segment the same glass surface and selected indicator treatment as the tab bar. The class preserves the segment's existing dimensions and text colors, supports scrollable segments, and respects Ionic's public --background property.

\n
<ion-segment class="segment-style-glass" value="available">\n  <ion-segment-button value="available">Available</ion-segment-button>\n  <ion-segment-button value="away">Away</ion-segment-button>\n</ion-segment>
\n\n

For colored segments, use Ionic's color property (for example, color="primary" or color="secondary"). Ionic uses the palette's base color for the softly tinted track while keeping the selected surface and labels neutral. A surrounding colored toolbar only supplies colors when the segment has no color of its own. The optional moving glass inherits the selected surface color, and custom Ionic palettes work without additional registration.

\n

Add .segment-expand when segment buttons should divide the available width evenly. The class also changes the Liquid Glass effect sizing when registerSegmentEffect is used.

\n

Segments use a 32px minimum height in content and a 48px minimum height inside ion-toolbar. .segment-expand keeps the compact 32px layout in a toolbar. Compact segments retain Ionic's flat background and indicator colors; only the regular toolbar variant has a glass container and scales its outer container while pressed. Content and expanded segments keep their outer bounds. The optional moving glass lens is independent of the container's background.

\n
<ion-segment class="segment-expand" value="new">\n  <ion-segment-button value="new"><ion-label>New</ion-label></ion-segment-button>\n  <ion-segment-button value="replied"><ion-label>Replied</ion-label></ion-segment-button>\n</ion-segment>
\n\n
\n
Preview
\n\n New\n Replied\n\n
\n\n

Classic search bar in a condense header

\n

The theme gives iOS search bars the iOS 27 appearance by default. Add .searchbar-classic to the search field shown beneath a large title in an ion-header with collapse="condense". It uses the conventional filled iOS appearance and collapses with the large title instead of remaining in the fixed header.

\n

Place it in a toolbar with a color, such as color="light"; the classic background is derived from that color's contrast value.

\n

The example uses Ionic's standard collapsible large-title structure. Scroll the preview to collapse the large title and reveal the fixed header.

\n
<div class="ion-page">\n  <ion-header translucent="true">\n    <ion-toolbar color="light">\n      <ion-title>Search</ion-title>\n    </ion-toolbar>\n  </ion-header>\n  <ion-content color="light" fullscreen="true">\n    <ion-header collapse="condense">\n      <ion-toolbar color="light">\n        <ion-title size="large">Search</ion-title>\n      </ion-toolbar>\n      <ion-toolbar color="light">\n        <ion-searchbar class="searchbar-classic" placeholder="Filter results"></ion-searchbar>\n      </ion-toolbar>\n    </ion-header>\n    <ion-list inset="true">\n      <ion-item-group>\n        <ion-item><ion-label>Recent item 1</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 2</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 3</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 4</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 5</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 6</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 7</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 8</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 9</ion-label></ion-item>\n        <ion-item><ion-label>Recent item 10</ion-label></ion-item>\n      </ion-item-group>\n    </ion-list>\n  </ion-content>\n</div>
\n\n
\n
Preview
\n
\n \n \n Search\n \n \n \n \n \n Search\n \n \n \n \n \n \n \n Recent item 1\n Recent item 2\n Recent item 3\n Recent item 4\n Recent item 5\n Recent item 6\n Recent item 7\n Recent item 8\n Recent item 9\n Recent item 10\n \n \n \n
\n
\n\n

The .ion-page wrapper makes this embedded preview behave like a complete routed page. An application using ion-router-outlet normally receives that page container automatically. The inset list and its items only provide enough content to demonstrate scrolling; they are not required by .searchbar-classic.

\n

Search-bar toolbars

\n

Add .toolbar-searchbar when an ion-toolbar combines a search bar with start or end buttons. The class centers the slotted controls and adjusts the spacing around the search field.

\n
<ion-toolbar class="toolbar-searchbar">\n  <ion-buttons slot="start">\n    <ion-button>Cancel</ion-button>\n  </ion-buttons>\n  <ion-searchbar></ion-searchbar>\n</ion-toolbar>
\n\n
\n
Preview
\n\n \n Cancel\n \n \n\n
\n\n

Opting out

\n

Add .ios-theme-disabled to an individual Ionic component when it must retain Ionic's standard iOS styling.

\n

.ios26-disabled is deprecated but remains supported as an alias with the same behavior. Use .ios-theme-disabled for new code.

\n
<ion-button>iOS 27 theme</ion-button> <ion-button class="ios-theme-disabled">Standard Ionic button</ion-button>
\n\n
\n
Preview
\niOS 27 theme Standard Ionic button\n
\n\n

For the background model behind inset lists, see Using ion-item-group.

\n'; diff --git a/demo/src/app/index/index-page.component.html b/demo/src/app/index/index-page.component.html index 6e5045b4..2ba70cad 100644 --- a/demo/src/app/index/index-page.component.html +++ b/demo/src/app/index/index-page.component.html @@ -6,9 +6,11 @@ Index + - diff --git a/demo/src/app/native-ui-shell/native-ui-shell.page.html b/demo/src/app/native-ui-shell/native-ui-shell.page.html index bcdbbd6f..f405bda9 100644 --- a/demo/src/app/native-ui-shell/native-ui-shell.page.html +++ b/demo/src/app/native-ui-shell/native-ui-shell.page.html @@ -18,11 +18,11 @@ - + Actions: {{ github() }} / {{ refreshes() }} - - + + @@ -33,10 +33,14 @@
-

Save count: {{ saves() }}

+

+ Save count: {{ saves() }} +

Experimental: On supported iOS versions, fixed glass controls are rendered natively.

-

Selection: {{ selected() }} / Change count: {{ changes() }}

+

+ Selection: {{ selected() }} / Change count: {{ changes() }} +

Scroll the background to compare transparency and refraction in the top-right button.

@@ -44,13 +48,13 @@ @for (value of fills; track value) { - + }
@for (band of bands; track band) { -
- {{ band }} — GLASS / Glass / 123456789 -
+
+ {{ band }} — GLASS / Glass / 123456789 +
}
diff --git a/demo/src/native-ui-shell-transition.spec.ts b/demo/src/native-ui-shell-transition.spec.ts index 6dc9b608..60cfd220 100644 --- a/demo/src/native-ui-shell-transition.spec.ts +++ b/demo/src/native-ui-shell-transition.spec.ts @@ -1,6 +1,7 @@ import { expect, test } from 'vitest'; import { connectNativeUIShellTransition, + FOLDABLE_TRANSITION_CANCELED, getNativeSearchBindings, registerNativeSearch, setNativeUIShellIntegration, @@ -16,14 +17,18 @@ const fixture = () => { const pending = new Promise((resolve) => { ready = resolve; }); + let finish!: Parameters[0]; setNativeUIShellIntegration(doc, { suspend: async () => { await pending; - return () => calls.push('resume'); + return (canceled = false) => calls.push(`resume:${canceled}`); }, }); const animation = { - onFinish: () => animation, + onFinish: (callback: typeof finish) => { + finish = callback; + return animation; + }, play: async () => { calls.push('play'); }, @@ -45,7 +50,7 @@ const fixture = () => { }, } as unknown as Animation; connectNativeUIShellTransition(animation, page); - return { animation, calls, ready }; + return { animation, calls, ready, finish: (step: 0 | 1) => finish(step, animation) }; }; test('normal transition waits for native suspension', async () => { @@ -54,11 +59,11 @@ test('normal transition waits for native suspension', async () => { expect(calls).toEqual([]); ready(); await played; - expect(calls).toEqual(['play', 'resume']); + expect(calls).toEqual(['play', 'resume:false']); }); test('interactive cancellation queued during suspension keeps the last progress', async () => { - const { animation, calls, ready } = fixture(); + const { animation, calls, ready, finish } = fixture(); animation.progressStart(true); animation.progressStep(0.2); animation.progressStep(0.4); @@ -66,8 +71,10 @@ test('interactive cancellation queued during suspension keeps the last progress' expect(calls).toEqual([]); ready(); await expect.poll(() => calls).toEqual(['start', 'step:0.4', 'end:0']); + finish(0); + expect(calls).toEqual(['start', 'step:0.4', 'end:0', 'resume:true']); animation.destroy(); - expect(calls).toEqual(['start', 'step:0.4', 'end:0', 'resume', 'destroy']); + expect(calls).toEqual(['start', 'step:0.4', 'end:0', 'resume:true', 'destroy']); }); test('destroy during bridge preparation cannot start an abandoned transition', async () => { @@ -76,7 +83,7 @@ test('destroy during bridge preparation cannot start an abandoned transition', a animation.destroy(); ready(); await played; - expect(calls).toEqual(['destroy', 'resume']); + expect(calls).toEqual(['destroy', 'resume:true']); }); test('without native enablement the animation remains untouched', () => { @@ -86,6 +93,30 @@ test('without native enablement the animation remains untouched', () => { expect(animation.play).toBe(play); }); +test('a canceled Web transition releases the still-active leaving page', () => { + const doc = document.implementation.createHTMLDocument(); + const app = doc.createElement('ion-app'); + doc.body.append(app); + const entering = doc.createElement('main'); + const leaving = doc.createElement('main'); + app.append(entering, leaving); + let finish!: Parameters[0]; + const animation = { onFinish: (callback: typeof finish) => (finish = callback) } as unknown as Animation; + let cancellations = 0; + let canceledEntering: HTMLElement | undefined; + leaving.addEventListener(FOLDABLE_TRANSITION_CANCELED, (event) => { + cancellations++; + canceledEntering = (event as CustomEvent<{ entering: HTMLElement }>).detail.entering; + }); + + connectNativeUIShellTransition(animation, entering, leaving); + finish(1, animation); + expect(cancellations).toBe(0); + finish(0, animation); + expect(cancellations).toBe(1); + expect(canceledEntering).toBe(entering); +}); + test('server rendering has no DOM side effects', async () => { await expect(configureNativeTransition()).resolves.toEqual({ radius: 0 }); const handle = await enableNativeUIShell(); diff --git a/docs/native-ui-shell.md b/docs/native-ui-shell.md index afb3f7ac..25435640 100644 --- a/docs/native-ui-shell.md +++ b/docs/native-ui-shell.md @@ -6,7 +6,7 @@ title: Native UI Shell (Experimental) Native UI Shell is experimental. Its API and supported controls may change. -Native UI Shell gives an Ionic app native navigation and action controls around its Web content. The optional Capacitor iOS plugin renders supported fixed Ionic controls with UIKit and the system's Liquid Glass material. Page content, scrolling, application state and routing remain in Ionic's WebView. +Native UI Shell gives an Ionic app native navigation and action controls around its Web content. The optional Capacitor iOS plugin renders supported fixed Ionic controls with UIKit or SwiftUI and the system's Liquid Glass material. Page content, scrolling, application state and routing remain in Ionic's WebView. ## Background @@ -167,6 +167,14 @@ Suspensions are nestable and `resume()` is idempotent. Native projection resumes The native material and control appearance follow the running iOS version; an iOS 26 device does not acquire iOS 27's appearance merely by installing this theme. +## Foldable controls + +On supported iOS versions, adding `.ios-theme-enable-foldable` changes only controls that the system relocates into the physical side rail. Native UI Shell presents eligible tabs, back navigation, menu buttons, and toolbar actions through a SwiftUI `TabView` and toolbar only when iOS reports a physical right-side safe area large enough for that rail. SwiftUI owns their adaptive placement and Liquid Glass appearance; Ionic remains the source of labels, icons, selected/disabled state, routing, form submission, and click handlers. + +The SwiftUI surface is clipped and hit-tested to the system rail. Web content remains visible and interactive outside that physical region. The runtime optimistically updates tab selection before forwarding the action to the original `ion-tab-button`, using the same event and stale-revision protection as the other native controls. Menus, modals, and popovers remain independent surfaces and are not moved into the main-page rail. + +The foldable exception accepts standard `fill="default"` and `fill="clear"` fixed-toolbar buttons because SwiftUI determines their compact representation. Solid, outline, custom-color, scrolling, and explicitly opted-out controls remain on the Web. On Web, Android, older iOS, or when native projection is unavailable during setup, the existing foldable Web projection remains the fallback. + ## Native UI Shell API The generated reference below documents the handle returned by `enableNativeUIShell()`. The underlying Capacitor bridge and its control-snapshot protocol are implementation details. diff --git a/docs/special-markup.md b/docs/special-markup.md index 5ca190a3..343ec4f6 100644 --- a/docs/special-markup.md +++ b/docs/special-markup.md @@ -59,15 +59,15 @@ The class reserves `80px` on the physical right by default, matching the system This keeps routers and component backgrounds full-viewport. `ion-content` moves its scroll foreground, `ion-toolbar` moves its container foreground, and `ion-fab` adjusts only when it is placed beside the system UI. The corresponding Ionic safe-area variable is reset inside those foreground components so descendants do not add the inset again. -`ion-menu`, `ion-modal`, and `ion-popover` are handled as separate surfaces: their internal foreground components do not receive the main-page conversion and retain Ionic's standard safe-area handling. A menu presented beside the system UI is positioned after the corresponding inset; a menu from the opposite side is unchanged. Left and right remain physical coordinates in RTL, while Ionic's `side="start"` and `side="end"` values remain logical. +`ion-menu`, `ion-modal`, and `ion-popover` are handled as separate surfaces: their internal foreground components do not receive the main-page conversion and retain Ionic's standard safe-area handling. A menu presented beside the system UI keeps Ionic's full-viewport animation host and offsets only its visible container by the corresponding inset; a menu from the opposite side is unchanged. Left and right remain physical coordinates in RTL, while Ionic's `side="start"` and `side="end"` values remain logical. The foldable values are web-layout simulation inputs. They are independent from Ionic's normal iPhone safe-area variables and do not change ordinary iPhone layouts unless the opt-in class is present. -When the app contains `ion-tabs`, foldable mode moves its iOS tab bar into the physical right-side reserved region and aligns it above the bottom safe area. The Ionic `slot` value does not select a different foldable position. The stable rail is icon-only, matching a four-tab SwiftUI `TabView` on iPhone Duo. While the user presses and drags across the rail, every icon-and-label tab reveals its label so the pending destination stays identifiable. The tab bar remains in the web layer and receives pointer input in the simulated system region. Use `ion-menu` when navigation should become a sidebar; foldable mode does not convert tabs into a menu. +When the app contains `ion-tabs`, foldable mode moves its iOS tab bar into the physical right-side reserved region and aligns it above the bottom safe area. The Ionic `slot` value does not select a different foldable position. Without Native UI Shell, the stable Web rail is icon-only, matching a four-tab SwiftUI `TabView` on iPhone Duo. While the user presses and drags across that rail, every icon-and-label tab reveals its label so the pending destination stays identifiable. The Web tab bar receives pointer input in the simulated system region. Use `ion-menu` when navigation should become a sidebar; foldable mode does not convert tabs into a menu. -When `enableNativeUIShell()` is initialized at application startup, foldable mode uses the same document-level projection lifecycle for fixed-toolbar navigation and actions. The active `ion-back-button` and `ion-button` or `ion-menu-button` controls that contain an icon remain the sources of truth, are hidden while owned, and are represented by interactive Web clones in the physical right-side system rail. Buttons in the same `ion-buttons` group share a vertical rail group. An icon-and-text button is shown as its icon while retaining its accessible label; a text-only button stays in the original toolbar. Menus, modals, and popovers retain their own toolbar layout. +On supported iOS versions, initializing `enableNativeUIShell()` at application startup hands eligible foldable tabs, back navigation, menu buttons, and fixed-toolbar actions to a native SwiftUI `TabView` and toolbar. The Ionic controls remain the sources of labels, icons, selected/disabled state, form submission, routing, and click handlers while SwiftUI owns adaptive placement and interaction. Fixed-toolbar actions need an icon or SVG, no direct text node, and standard `fill="default"` or `fill="clear"` to be eligible for the side rail. Text-only actions stay in the original Web toolbar. Placement is chosen when a routed page enters; changing an existing button's content does not move it between the toolbar and rail until the page leaves and re-enters. Menus, modals, and popovers retain their own toolbar layout. -These Web projections also work when no `ion-tabs` exists and when native projection is unavailable. Disabling foldable mode or leaving the page removes the clones and restores their sources. Override `--ios-theme-foldable-toolbar-top` when the simulated system controls use a different vertical layout. +On Web, Android, older iOS, or when native projection is unavailable during setup, the Web tab bar and fixed-toolbar clones remain the fallback. Those projections also work when no `ion-tabs` exists; text-only actions stay in the original Web toolbar. Disabling foldable mode or leaving the page removes the native ownership or Web clones and restores their sources. Override `--ios-theme-foldable-toolbar-top` when the simulated system controls use a different vertical layout. ## Two-line inset list items diff --git a/ios/Sources/IonicNativeUIShellPlugin/Components/ShellFoldableRail.swift b/ios/Sources/IonicNativeUIShellPlugin/Components/ShellFoldableRail.swift new file mode 100644 index 00000000..e49f904d --- /dev/null +++ b/ios/Sources/IonicNativeUIShellPlugin/Components/ShellFoldableRail.swift @@ -0,0 +1,332 @@ +import Combine +import SwiftUI +import UIKit + +protocol ShellFoldableRailControlling: AnyObject { + var view: UIView { get } + func attach(to owner: UIViewController, in parent: UIView) + func apply(_ controls: [ShellControl], rendering: ShellRendering) + func detach() +} + +@available(iOS 26.0, *) +final class ShellFoldableRailModel: ObservableObject { + struct Item: Identifiable { + let id: String + let label: String + let accessibilityLabel: String + let image: UIImage? + let badge: ShellBadge? + let disabled: Bool + var selected: Bool + } + + struct Group: Identifiable { + let id: String + let items: [Item] + let slot: ShellControl.ToolbarSlot? + } + + @Published var back: Item? + @Published var groups: [Group] = [] + @Published var tabs: [Item] = [] + @Published var selection = "" + var activate: (String) -> Void = { _ in } + private var domSelection = "" + private var pendingSelection: ShellTabBar.PendingSelection? + private var pendingExpiryWork: DispatchWorkItem? + + func apply(_ controls: [ShellControl], rendering: ShellRendering, + now: CFAbsoluteTime = CFAbsoluteTimeGetCurrent()) { + func item(_ source: ShellItem) -> Item { + Item(id: source.id, label: source.content.label, + accessibilityLabel: source.content.accessibilityLabel, + image: rendering.image(source.content), badge: source.content.badge, disabled: source.content.disabled, + selected: source.content.selected) + } + withAnimation(.smooth(duration: 0.3)) { + back = controls.first(where: { $0.kind == .backButton })?.items.first.map(item) + groups = controls.compactMap { control in + guard [.button, .buttons, .menuButton].contains(control.kind) else { return nil } + return Group(id: control.id, items: control.items.map(item), slot: control.toolbarSlot) + } + } + tabs = controls.first(where: { $0.kind == .tabBar })?.items.map(item) ?? [] + domSelection = tabs.first(where: \.selected)?.id ?? "" + reconcileSelection(now: now) + } + + func select(_ id: String, now: CFAbsoluteTime = CFAbsoluteTimeGetCurrent(), ttl: CFTimeInterval = 1) { + guard let item = tabs.first(where: { $0.id == id }), !item.disabled else { return } + pendingSelection = .init(id: id, until: now + ttl) + selection = id + pendingExpiryWork?.cancel() + let work = DispatchWorkItem { [weak self] in self?.reconcileSelection() } + pendingExpiryWork = work + DispatchQueue.main.asyncAfter(deadline: .now() + ttl + 0.02, execute: work) + activate(id) + } + + private func reconcileSelection(now: CFAbsoluteTime = CFAbsoluteTimeGetCurrent()) { + guard let pending = pendingSelection else { selection = domSelection; return } + let item = tabs.first(where: { $0.id == pending.id }) + if item == nil || item!.disabled || now >= pending.until || domSelection == pending.id { + pendingSelection = nil + pendingExpiryWork?.cancel() + pendingExpiryWork = nil + selection = domSelection + } else { + selection = pending.id + } + } +} + +@available(iOS 26.0, *) +private struct ShellFoldableLabel: View { + let item: ShellFoldableRailModel.Item + + var body: some View { + if let image = item.image { + Label { + Text(item.label.isEmpty ? item.accessibilityLabel : item.label) + } icon: { + Image(uiImage: image) + } + } else { + Text(item.label) + } + } +} + +@available(iOS 26.0, *) +private func foldableButton(_ item: ShellFoldableRailModel.Item, model: ShellFoldableRailModel) -> some View { + Button { model.activate(item.id) } label: { ShellFoldableLabel(item: item) } + .disabled(item.disabled) + .accessibilityLabel(item.accessibilityLabel) + .accessibilityIdentifier(item.id) +} + +@available(iOS 26.0, *) +private func foldableBackButton(_ item: ShellFoldableRailModel.Item, model: ShellFoldableRailModel) -> some View { + Button { model.activate(item.id) } label: { Image(systemName: "chevron.backward") } + .disabled(item.disabled) + .accessibilityLabel(item.accessibilityLabel) + .accessibilityIdentifier("BackButton") +} + +@available(iOS 26.0, *) +private struct ShellFoldableBadge: ViewModifier { + let badge: ShellBadge? + + @ViewBuilder func body(content: Content) -> some View { + if let badge { content.badge(badge.value) } + else { content } + } +} + +@available(iOS 26.0, *) +private struct ShellFoldableRailView: View { + @ObservedObject var model: ShellFoldableRailModel + + var body: some View { + Group { + if model.tabs.isEmpty { + ShellFoldablePage(model: model) + } else { + TabView(selection: Binding(get: { model.selection }, set: { model.select($0) })) { + ForEach(model.tabs) { item in + ShellFoldablePage(model: model) + .tag(item.id) + .tabItem { ShellFoldableLabel(item: item) } + .modifier(ShellFoldableBadge(badge: item.badge)) + .disabled(item.disabled) + .accessibilityLabel(item.accessibilityLabel) + .accessibilityIdentifier(item.id) + } + } + } + } + .modifier(ShellFoldableCompression()) + .background(Color.clear) + } +} + +@available(iOS 26.0, *) +private struct ShellFoldablePage: View { + @ObservedObject var model: ShellFoldableRailModel + + var body: some View { + NavigationStack { + Color.clear.modifier(ShellFoldableToolbarAdapter(model: model)) + } + } +} + +@available(iOS 26.0, *) +private struct ShellFoldableCompression: ViewModifier { + @ViewBuilder func body(content: Content) -> some View { + if #available(iOS 27.1, *) { + content.toolbarVerticalCompressionBehavior(.prefersToolbarItems) + } else { + content + } + } +} + +@available(iOS 26.0, *) +private struct ShellFoldableToolbarAdapter: ViewModifier { + @ObservedObject var model: ShellFoldableRailModel + + @ViewBuilder func body(content: Content) -> some View { + if #available(iOS 27.1, *) { + content.modifier(ShellFoldableToolbar(model: model)) + } else { + content.modifier(ShellFoldableLegacyToolbar(model: model)) + } + } +} + +@available(iOS 27.1, *) +private struct ShellFoldableToolbar: ViewModifier { + @ObservedObject var model: ShellFoldableRailModel + + func body(content: Content) -> some View { + content.toolbar { + if let back = model.back { + ToolbarItem(placement: .navigation) { + foldableBackButton(back, model: model) + } + .axisBehavior(.verticalPreferred) + } + ForEach(model.groups.filter { $0.slot == .start }) { group in + ToolbarItemGroup(placement: .topBarLeading) { + ForEach(group.items) { item in + foldableButton(item, model: model) + } + } + .axisBehavior(.verticalPreferred) + } + ForEach(model.groups.filter { $0.slot != .start }) { group in + ToolbarItemGroup(placement: .topBarTrailing) { + ForEach(group.items) { item in + foldableButton(item, model: model) + } + } + .axisBehavior(.verticalPreferred) + } + } + } +} + +@available(iOS 26.0, *) +private struct ShellFoldableLegacyToolbar: ViewModifier { + @ObservedObject var model: ShellFoldableRailModel + + func body(content: Content) -> some View { + content.toolbar { + if let back = model.back { + ToolbarItem(placement: .navigation) { + foldableBackButton(back, model: model) + } + } + ForEach(model.groups) { group in + ToolbarItemGroup(placement: .primaryAction) { + ForEach(group.items) { item in + foldableButton(item, model: model) + } + } + } + } + } +} + +@available(iOS 26.0, *) +final class ShellFoldableRailController: ShellFoldableRailControlling { + private final class TransparentHostingController: UIHostingController { + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + makeFullSizeSurfacesTransparent(in: view) + } + + private func makeFullSizeSurfacesTransparent(in surface: UIView) { + let frame = surface.convert(surface.bounds, to: view) + let railWidth = view.safeAreaInsets.right > 0 ? view.safeAreaInsets.right : 80 + guard !(surface is UIVisualEffectView) else { return } + let coversHost = frame.insetBy(dx: -1, dy: -1).contains(view.bounds) + let coversRail = frame.minY <= 1 && frame.maxY >= view.bounds.maxY - 1 && + frame.minX <= view.bounds.maxX - railWidth + 1 && frame.maxX >= view.bounds.maxX - 1 + // SwiftUI may add an opaque backing behind the rail controls. + // Keep that backing clear without touching the glass controls or materials. + if coversHost || coversRail { + surface.backgroundColor = .clear + surface.isOpaque = false + } + surface.subviews.forEach(makeFullSizeSurfacesTransparent) + } + } + + private final class RailContainer: UIView { + private let railMask = CAShapeLayer() + + private var railWidth: CGFloat { safeAreaInsets.right > 0 ? safeAreaInsets.right : 80 } + + override init(frame: CGRect) { + super.init(frame: frame) + layer.mask = railMask + } + + required init?(coder: NSCoder) { nil } + + override func layoutSubviews() { + super.layoutSubviews() + railMask.path = UIBezierPath(rect: CGRect(x: bounds.maxX - railWidth, y: 0, + width: railWidth, height: bounds.height)).cgPath + } + + override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { + point.x >= bounds.maxX - railWidth && super.point(inside: point, with: event) + } + } + + private let model = ShellFoldableRailModel() + private lazy var controller = TransparentHostingController(rootView: ShellFoldableRailView(model: model)) + private let container = RailContainer() + private weak var owner: UIViewController? + + var view: UIView { container } + + init(activate: @escaping (String) -> Void) { + model.activate = activate + container.backgroundColor = .clear + controller.view.backgroundColor = .clear + controller.view.isOpaque = false + } + + func attach(to owner: UIViewController, in parent: UIView) { + guard self.owner !== owner || container.superview !== parent else { return } + detach() + self.owner = owner + owner.addChild(controller) + container.frame = parent.bounds + container.autoresizingMask = [.flexibleWidth, .flexibleHeight] + controller.view.frame = container.bounds + controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] + container.addSubview(controller.view) + parent.addSubview(container) + controller.didMove(toParent: owner) + } + + func apply(_ controls: [ShellControl], rendering: ShellRendering) { + model.apply(controls, rendering: rendering) + controller.overrideUserInterfaceStyle = controls.contains(where: \.dark) ? .dark : .light + } + + func detach() { + guard controller.parent != nil else { return } + controller.willMove(toParent: nil) + container.removeFromSuperview() + controller.view.removeFromSuperview() + controller.removeFromParent() + owner = nil + } +} diff --git a/ios/Sources/IonicNativeUIShellPlugin/IonicNativeUIShellPlugin.swift b/ios/Sources/IonicNativeUIShellPlugin/IonicNativeUIShellPlugin.swift index 11cfd077..291a2d41 100644 --- a/ios/Sources/IonicNativeUIShellPlugin/IonicNativeUIShellPlugin.swift +++ b/ios/Sources/IonicNativeUIShellPlugin/IonicNativeUIShellPlugin.swift @@ -13,6 +13,7 @@ public class IonicNativeUIShellPlugin: CAPPlugin, CAPBridgedPlugin, UITabBarDele CAPPluginMethod(name: "clear", returnType: CAPPluginReturnPromise) ] private var host: ShellHost? + private var foldableRail: ShellFoldableRailControlling? private var controls: [String: UIView] = [:] private var searchControllers: [String: UIViewController] = [:] private var fingerprints: [String: ShellControl] = [:] @@ -40,7 +41,10 @@ public class IonicNativeUIShellPlugin: CAPPlugin, CAPBridgedPlugin, UITabBarDele self.keyboardVisible = !overlap.isNull && overlap.width > 0 && overlap.height > 0 } } - if !keyboard { self.host?.isHidden = true } + if !keyboard { + self.host?.isHidden = true + self.foldableRail?.view.isHidden = true + } var searchOwnsKeyboard = false if #available(iOS 26.0, *) { self.searchControllers.values.forEach { @@ -105,6 +109,11 @@ public class IonicNativeUIShellPlugin: CAPPlugin, CAPBridgedPlugin, UITabBarDele self?.removeControls() self?.revision = 0 if #available(iOS 26.0, *) { + self?.bridge?.webView?.layoutIfNeeded() + // A foldable rail exists only when the system reserves enough of + // the physical right edge to host its adaptive controls. Ordinary + // iPhone/iPad safe areas must keep using the Web projection. + let foldableRail = (self?.bridge?.webView?.safeAreaInsets.right ?? 0) >= 70 // Ionic already paints the header edge; a second native effect can // add a dark scrim when the OS and Web themes differ. if let effect = self?.bridge?.webView?.scrollView.topEdgeEffect { @@ -112,7 +121,7 @@ public class IonicNativeUIShellPlugin: CAPPlugin, CAPBridgedPlugin, UITabBarDele effect.isHidden = true self?.restoreTopEdge = { [weak effect] in effect?.isHidden = hidden } } - call.resolve(["supported": true]) + call.resolve(["supported": true, "foldableRail": foldableRail]) } else { call.resolve(["supported": false]) } } @@ -146,6 +155,8 @@ public class IonicNativeUIShellPlugin: CAPPlugin, CAPBridgedPlugin, UITabBarDele Array(controls.keys).forEach { removeControl($0, duration: duration) } host?.removeFromSuperview() host = nil + foldableRail?.detach() + foldableRail = nil rendering.clear() pendingTabSelections.removeAll() pendingTabExpiryWorks.values.forEach { $0.cancel() } @@ -194,10 +205,11 @@ public class IonicNativeUIShellPlugin: CAPPlugin, CAPBridgedPlugin, UITabBarDele } let duration = ShellCrossfade.duration(snapshot.transitionDuration) let existing = Set(self.controls.keys) - let snapshots = snapshot.controls + let foldable = snapshot.controls.filter { $0.placement == .foldableRail } + let snapshots = snapshot.controls.filter { $0.placement != .foldableRail } let width = snapshot.viewportWidth self.revision = next - if snapshots.isEmpty { + if snapshots.isEmpty && foldable.isEmpty { self.removeControls(duration: duration) call.resolve(["revision": next]); return } @@ -216,6 +228,19 @@ public class IonicNativeUIShellPlugin: CAPPlugin, CAPBridgedPlugin, UITabBarDele var fabs: [(ShellFab, ShellControl)] = [] var searches: [(ShellSearchController, ShellControl, CGRect, CGRect, UIView?, Bool)] = [] var rejectedSearches: [String] = [] + if foldable.isEmpty || self.keyboardVisible { + self.foldableRail?.detach() + self.foldableRail = nil + if self.keyboardVisible { rejectedControls.append(contentsOf: foldable.map(\.id)) } + } else if let owner = self.bridge?.viewController { + let rail = self.foldableRail ?? ShellFoldableRailController(activate: { [weak self] id in self?.activate(id) }) + self.foldableRail = rail + rail.attach(to: owner, in: owner.view) + rail.apply(foldable, rendering: self.rendering) + rail.view.isHidden = false + } else { + rejectedControls.append(contentsOf: foldable.map(\.id)) + } UIView.performWithoutAnimation { if host.superview !== parent { parent.addSubview(host) } for node in snapshots { diff --git a/ios/Sources/IonicNativeUIShellPlugin/Shared/ShellSnapshot.swift b/ios/Sources/IonicNativeUIShellPlugin/Shared/ShellSnapshot.swift index 75e74adf..ab211732 100644 --- a/ios/Sources/IonicNativeUIShellPlugin/Shared/ShellSnapshot.swift +++ b/ios/Sources/IonicNativeUIShellPlugin/Shared/ShellSnapshot.swift @@ -31,8 +31,12 @@ struct ShellFrame: Decodable, Equatable { } struct ShellControl: Decodable, Equatable { + enum Placement: String, Decodable { case foldableRail = "foldable-rail" } + enum ToolbarSlot: String, Decodable { case start, end } let id: String let kind: ShellComponent + let placement: Placement? + let toolbarSlot: ToolbarSlot? let frame: ShellFrame let items: [ShellItem] let dark: Bool @@ -40,12 +44,14 @@ struct ShellControl: Decodable, Equatable { let tabBarAnchor: ShellTabBar.Anchor? let search: ShellSearch? - private enum CodingKeys: String, CodingKey { case id, kind, items, dark, rtl, tabBarAnchor, search } + private enum CodingKeys: String, CodingKey { case id, kind, placement, toolbarSlot, items, dark, rtl, tabBarAnchor, search } init(from decoder: Decoder) throws { let values = try decoder.container(keyedBy: CodingKeys.self) id = try values.decode(String.self, forKey: .id) kind = try values.decode(ShellComponent.self, forKey: .kind) + placement = try values.decodeIfPresent(Placement.self, forKey: .placement) + toolbarSlot = try values.decodeIfPresent(ToolbarSlot.self, forKey: .toolbarSlot) frame = try ShellFrame(from: decoder) items = try values.decode([ShellItem].self, forKey: .items) dark = try values.decode(Bool.self, forKey: .dark) @@ -57,6 +63,7 @@ struct ShellControl: Decodable, Equatable { var isValid: Bool { ShellComponents.supported.contains(kind) && !id.isEmpty && frame.isValid && !items.isEmpty && items.count <= 30 && (!ShellButton.kinds.contains(kind) || items.count == 1) && + (toolbarSlot == nil || (placement == .foldableRail && [.button, .buttons, .menuButton].contains(kind))) && Set(items.map(\.id)).count == items.count && items.allSatisfy(\.isValid) && (tabBarAnchor.map { kind == .tabBar && $0.isValid } ?? true) && (search.map { kind == .tabBar && $0.isValid } ?? true) diff --git a/ios/Tests/IonicNativeUIShellPluginTests/ShellSnapshotTests.swift b/ios/Tests/IonicNativeUIShellPluginTests/ShellSnapshotTests.swift index c804b0e8..970b7c82 100644 --- a/ios/Tests/IonicNativeUIShellPluginTests/ShellSnapshotTests.swift +++ b/ios/Tests/IonicNativeUIShellPluginTests/ShellSnapshotTests.swift @@ -37,6 +37,35 @@ final class ShellSnapshotTests: XCTestCase { } } + @MainActor func testFoldableTabOptimismWaitsForWebAndRollsBackWhenStale() throws { + guard #available(iOS 26.0, *) else { throw XCTSkip("Requires SwiftUI adaptive tabs") } + func tabs(_ selected: String, includeRight: Bool = true) throws -> [ShellControl] { + var items = [item(["id": "left", "selected": selected == "left"])] + if includeRight { + items.append(item(["id": "right", "selected": selected == "right", + "badge": ["value": "3", "color": "rgb(255, 0, 0)", "textColor": "rgb(255, 255, 255)"]])) + } + return try decode([control(["kind": "ion-tab-bar", "items": items])]).controls + } + let model = ShellFoldableRailModel() + let rendering = ShellRendering() + model.apply(try tabs("left"), rendering: rendering, now: 100) + XCTAssertEqual(model.tabs.last?.badge?.value, "3") + model.select("right", now: 100, ttl: 10) + model.apply(try tabs("left"), rendering: rendering, now: 101) + XCTAssertEqual(model.selection, "right", "a stale Web echo must not undo the optimistic selection") + model.apply(try tabs("right"), rendering: rendering, now: 102) + XCTAssertEqual(model.selection, "right", "the matching Web echo confirms the selection") + + model.apply(try tabs("left"), rendering: rendering, now: 200) + model.select("right", now: 200, ttl: 10) + model.apply(try tabs("left"), rendering: rendering, now: 211) + XCTAssertEqual(model.selection, "left", "an expired selection rolls back to Web state") + model.select("right", now: 220, ttl: 10) + model.apply(try tabs("left", includeRight: false), rendering: rendering, now: 221) + XCTAssertEqual(model.selection, "left", "a removed target rolls back immediately") + } + @MainActor func testSegmentSelectionEchoPreservesNativeViewsAndActionsUseUpdatedItems() throws { guard #available(iOS 26.0, *) else { return } func node(_ items: [JSObject]) throws -> ShellControl { diff --git a/package-lock.json b/package-lock.json index 6eb32d17..3d353f09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ ], "license": "MIT", "dependencies": { - "@rdlabo/ionic-theme-utils": "git+https://github.com/rdlabo-dev/ionic-theme-utils.git#30acb040153797407a5fe27f0db6c69a3410d855" + "@rdlabo/ionic-theme-utils": "git+https://github.com/rdlabo-dev/ionic-theme-utils.git#44b1535387c326ed69fc239d38dbdb9776b629c2" }, "devDependencies": { "@capacitor/core": "^8.5.2", @@ -2259,7 +2259,7 @@ }, "node_modules/@rdlabo/ionic-theme-utils": { "version": "0.1.0", - "resolved": "git+https://github.com/rdlabo-dev/ionic-theme-utils.git#30acb040153797407a5fe27f0db6c69a3410d855", + "resolved": "git+https://github.com/rdlabo-dev/ionic-theme-utils.git#44b1535387c326ed69fc239d38dbdb9776b629c2", "integrity": "sha512-W4MFC4reLS4cK23hqWrWEf88d1Gx3mRfkkd2eihzjaGm+jndS+SdBmGmcefuaUXzL0J6lDtBPMWEkn78IOI+LA==", "inBundle": true, "license": "MIT", diff --git a/package.json b/package.json index 4523ee81..abf394ee 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ }, "homepage": "https://docs.rdlabo.dev/projects/ionic-theme-ios27", "dependencies": { - "@rdlabo/ionic-theme-utils": "git+https://github.com/rdlabo-dev/ionic-theme-utils.git#30acb040153797407a5fe27f0db6c69a3410d855" + "@rdlabo/ionic-theme-utils": "git+https://github.com/rdlabo-dev/ionic-theme-utils.git#44b1535387c326ed69fc239d38dbdb9776b629c2" }, "bundleDependencies": [ "@rdlabo/ionic-theme-utils" diff --git a/src/native-integration/index.ts b/src/native-integration/index.ts index 84144b2f..1355497c 100644 --- a/src/native-integration/index.ts +++ b/src/native-integration/index.ts @@ -2,7 +2,7 @@ import type { Animation } from '@ionic/core'; export interface NativeUIShellIntegration { - suspend(scopes: HTMLElement[]): Promise<() => void>; + suspend(scopes: HTMLElement[]): Promise<(canceled?: boolean) => void>; search?(binding: NativeSearchBinding, active: boolean, focus?: boolean): Promise; } @@ -32,6 +32,7 @@ export const requestNativeSearch = async (binding: NativeSearchBinding, active: (await runtimes.get(binding.tabBar.ownerDocument)?.search?.(binding, active, focus)) ?? false; const runtimes = new WeakMap(); +export const FOLDABLE_TRANSITION_CANCELED = 'iosThemeFoldableTransitionCanceled'; export const setNativeUIShellIntegration = (doc: Document, runtime?: NativeUIShellIntegration) => { if (runtime) runtimes.set(doc, runtime); @@ -40,11 +41,15 @@ export const setNativeUIShellIntegration = (doc: Document, runtime?: NativeUIShe export const isNativeUIShell = (element: HTMLElement) => element.hasAttribute('data-native-ui-shell'); -export const suspendNativeUIShell = async (scopes: HTMLElement[]): Promise<() => void> => +export const suspendNativeUIShell = async (scopes: HTMLElement[]): Promise<(canceled?: boolean) => void> => (await runtimes.get(scopes[0]?.ownerDocument)?.suspend(scopes)) ?? (() => {}); /** Ionic write hooks cannot await the bridge. Gate playback, including interactive playback. */ export const connectNativeUIShellTransition = (animation: Animation, entering: HTMLElement, leaving?: HTMLElement) => { + if (leaving) + animation.onFinish((step) => { + if (step === 0) leaving.dispatchEvent(new CustomEvent(FOLDABLE_TRANSITION_CANCELED, { bubbles: true, detail: { entering } })); + }); if (!runtimes.has(entering.ownerDocument)) return; const scopes = leaving ? [entering, leaving] : [entering]; const play = animation.play.bind(animation); @@ -52,7 +57,7 @@ export const connectNativeUIShellTransition = (animation: Animation, entering: H const progressStep = animation.progressStep.bind(animation); const progressEnd = animation.progressEnd.bind(animation); const destroy = animation.destroy.bind(animation); - let release: (() => void) | undefined; + let release: ((canceled?: boolean) => void) | undefined; let preparation: Promise | undefined; let disposed = false; let interactiveReady = false; @@ -60,15 +65,15 @@ export const connectNativeUIShellTransition = (animation: Animation, entering: H let end: Parameters | undefined; const prepare = () => (preparation ??= suspendNativeUIShell(scopes).then((resume) => { - if (disposed) resume(); + if (disposed) resume(true); else release = resume; })); - const finish = () => { - release?.(); + const finish = (canceled = false) => { + release?.(canceled); release = undefined; preparation = undefined; }; - animation.onFinish(finish); + animation.onFinish((step) => finish(step === 0)); animation.play = async (options) => { await prepare(); if (disposed) return; @@ -103,7 +108,7 @@ export const connectNativeUIShellTransition = (animation: Animation, entering: H }; animation.destroy = (...args) => { disposed = true; - finish(); + finish(true); return destroy(...args); }; }; diff --git a/src/native/components/index.ts b/src/native/components/index.ts index 2d414108..33a9cf52 100644 --- a/src/native/components/index.ts +++ b/src/native/components/index.ts @@ -5,7 +5,7 @@ import * as menuButton from './ion-menu-button'; import * as tabBar from './ion-tab-bar'; import * as segment from './ion-segment'; import * as fab from './ion-fab'; -import { isDisabledButtonGroupChild, visible } from '../shared/dom'; +import { isDisabledButtonGroupChild, isFoldableRailSource, visible } from '../shared/dom'; import type { Candidate, Identify } from '../shared/candidate'; // Static composition only. Each component declares its own tag, discovery and reader. @@ -27,13 +27,17 @@ export const motionSelector = [ ...components.filter((component) => 'tracksMotion' in component && component.tracksMotion).map((component) => component.tag), ].join(', '); +export const isFoldableRailCandidate = isFoldableRailSource; + export const readCandidate = (element: HTMLElement, id: Identify): Candidate | undefined => { - if (!element.classList.contains('ios') || !visible(element) || element.closest('ion-modal, ion-popover')) return; + const foldable = isFoldableRailCandidate(element); + if (!element.classList.contains('ios') || !visible(element, foldable) || element.closest('ion-modal, ion-popover')) return; const style = getComputedStyle(element); if ( !isDisabledButtonGroupChild(element) && !style.getPropertyValue('--ios-theme-glass-background-rgb').trim() && - !style.getPropertyValue('--ios26-glass-background-rgb').trim() + !style.getPropertyValue('--ios26-glass-background-rgb').trim() && + !foldable ) return; if (element.contains(element.ownerDocument.activeElement)) return; diff --git a/src/native/components/ion-button.ts b/src/native/components/ion-button.ts index 369bbc62..d81cd9eb 100644 --- a/src/native/components/ion-button.ts +++ b/src/native/components/ion-button.ts @@ -1,12 +1,23 @@ import { createCandidate, appendItem } from '../shared/candidate'; import type { Candidate, Identify } from '../shared/candidate'; -import { inFixedToolbar } from '../shared/dom'; +import { inFixedToolbar, isFoldableToolbarAction, isFoldableToolbarGroup } from '../shared/dom'; export const tag = 'ion-button'; export const read = (element: HTMLElement, id: Identify): Candidate | undefined => { const button = element as HTMLIonButtonElement; - if (!inFixedToolbar(element) || button.fill !== 'default' || button.classList.contains('ion-color')) return; + const foldable = + !element.closest('ion-menu, ion-modal, ion-popover') && !!element.closest(':is(ion-app, body).ios-theme-enable-foldable'); + if (foldable && element.parentElement && isFoldableToolbarGroup(element.parentElement)) return; + const fill = button.fill ?? 'default'; + if ( + !inFixedToolbar(element) || + (foldable && !isFoldableToolbarAction(element)) || + (!foldable && fill !== 'default') || + (foldable && !['default', 'clear'].includes(fill)) || + button.classList.contains('ion-color') + ) + return; const candidate = createCandidate(element, tag, id); return appendItem(candidate, element, id) ? candidate : undefined; }; diff --git a/src/native/components/ion-buttons.ts b/src/native/components/ion-buttons.ts index 0dd49d66..1912eea0 100644 --- a/src/native/components/ion-buttons.ts +++ b/src/native/components/ion-buttons.ts @@ -1,6 +1,6 @@ import { createCandidate, appendItem } from '../shared/candidate'; import type { Candidate, Identify } from '../shared/candidate'; -import { inFixedToolbar } from '../shared/dom'; +import { foldableToolbarActions, inFixedToolbar, isFoldableToolbarGroup } from '../shared/dom'; import * as menuButton from './ion-menu-button'; export const tag = 'ion-buttons'; @@ -8,18 +8,24 @@ export const tracksMotion = true; export const read = (element: HTMLElement, id: Identify): Candidate | undefined => { if (!inFixedToolbar(element)) return; - const children = Array.from(element.children) as HTMLElement[]; + let children = Array.from(element.children) as HTMLElement[]; if (children.length === 1) return menuButton.read(element, id); + const foldable = + !element.closest('ion-menu, ion-modal, ion-popover') && !!element.closest(':is(ion-app, body).ios-theme-enable-foldable'); + if (foldable && !isFoldableToolbarGroup(element)) return; + if (foldable) children = foldableToolbarActions(element); if ( !children.length || children.some( (child) => !child.matches(`${menuButton.tag}.ios`) && - (!child.matches('ion-button.ios.button-clear') || (child as HTMLIonButtonElement).fill !== 'clear'), + (!child.matches(`ion-button.ios${foldable ? '' : '.button-clear'}`) || + !(foldable ? ['default', 'clear'] : ['clear']).includes((child as HTMLIonButtonElement).fill ?? 'default')), ) ) return; const candidate = createCandidate(element, tag, id); + if (foldable && children.length !== element.children.length) candidate.sources = children; for (const child of children) { const supported = child.matches(menuButton.tag) ? menuButton.append(candidate, child as HTMLIonMenuButtonElement, id) diff --git a/src/native/components/ion-tab-bar.ts b/src/native/components/ion-tab-bar.ts index 79893e16..cf4ea438 100644 --- a/src/native/components/ion-tab-bar.ts +++ b/src/native/components/ion-tab-bar.ts @@ -7,11 +7,6 @@ export const tracksMotion = true; export const read = (element: HTMLElement, id: Identify): Candidate | undefined => { if (element.closest('ion-content')) return; - if ( - (element.closest('ion-app') ?? element.ownerDocument.body).classList.contains('ios-theme-enable-foldable') && - !element.closest('ion-menu, ion-modal, ion-popover') - ) - return; const children = Array.from(element.querySelectorAll(':scope > ion-tab-button:not(.ion-cloned-element)')); if (!children.length) return; // Only equal items with Ionic's default layout map to UIKit's adaptive tabs. diff --git a/src/native/definitions.ts b/src/native/definitions.ts index 1cd38532..0e6d2ce2 100644 --- a/src/native/definitions.ts +++ b/src/native/definitions.ts @@ -83,6 +83,10 @@ export interface ShellItem extends Frame { export interface ShellControl extends Frame { id: string; kind: NativeUIShellComponent; + /** Lets the native host own adaptive placement instead of mirroring the DOM frame. */ + placement?: 'foldable-rail'; + /** Logical Ionic toolbar slot, preserved when projecting to the foldable rail. */ + toolbarSlot?: 'start' | 'end'; items: ShellItem[]; dark: boolean; rtl: boolean; @@ -131,7 +135,7 @@ export interface WebViewMetrics { } export interface NativeUIShellPlugin { - configure(): Promise<{ supported: boolean }>; + configure(): Promise<{ supported: boolean; foldableRail?: boolean }>; getWebViewMetrics(): Promise; update(snapshot: ShellSnapshot): Promise<{ revision: number; rejectedSearches?: string[]; rejectedControls?: string[] }>; clear(options: { revision: number }): Promise; diff --git a/src/native/foldable-web.ts b/src/native/foldable-web.ts index e5336838..823ce9c2 100644 --- a/src/native/foldable-web.ts +++ b/src/native/foldable-web.ts @@ -1,5 +1,19 @@ import type { NativeUIShellHandle, NativeUIShellOptions, NativeUIShellStatus } from './definitions'; -import { isExcluded, isShellDisabled, marker, unprojected } from './shared/dom'; +import { FOLDABLE_TRANSITION_CANCELED } from '../native-integration'; +import { + activateProjectedElement, + createFoldablePageState, + foldableEnteringPage, + foldableToolbarActions, + isExcluded, + isFoldableToolbarGroup, + isShellDisabled, + marker, + prehideOnlyMutation, + prehiddenClass, + unprojected, + withoutPrehide, +} from './shared/dom'; const backProjectionClass = 'ios-theme-foldable-back-button-projection'; const toolbarProjectionClass = 'ios-theme-foldable-toolbar-projection'; @@ -36,6 +50,7 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel let frame = 0; let updates = 0; let observingFoldable = false; + const foldablePages = createFoldablePageState(); let sourceObserver: MutationObserver | undefined; let waiters: (() => void)[] = []; const listeners = new AbortController(); @@ -44,11 +59,12 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel [backSource, ...toolbarProjections.flatMap(({ actions }) => actions.map(({ source }) => source))].filter( (source): source is HTMLElement => !!source, ); - const isRendered = (element: HTMLElement) => { - const style = getComputedStyle(element); - const rect = element.getBoundingClientRect(); - return element.isConnected && style.display !== 'none' && style.visibility === 'visible' && rect.width > 0 && rect.height > 0; - }; + const isRendered = (element: HTMLElement) => + withoutPrehide(element, () => { + const style = getComputedStyle(element); + const rect = element.getBoundingClientRect(); + return element.isConnected && style.display !== 'none' && style.visibility === 'visible' && rect.width > 0 && rect.height > 0; + }); const inEligibleToolbar = (element: HTMLElement) => { const currentRoot = foldableRoot(); const toolbar = element.closest('ion-toolbar'); @@ -60,20 +76,14 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel !!edge?.matches('ion-header, ion-footer') && !element.closest('ion-content') && !edge.hasAttribute('collapse') && - !isExcluded(element) && + !isExcluded(element, foldableEnteringPage(element)) && !isShellDisabled(element) && - !element.closest('ion-menu, ion-modal, ion-popover, .ion-page-hidden, .ion-page-invisible') + !foldablePages.isDeparted(element) && + !element.closest('ion-menu, ion-modal, ion-popover, .ion-page-hidden') ); }; const isEligibleBack = (element: HTMLIonBackButtonElement) => inEligibleToolbar(element) && unprojected(projectedSources(), () => isRendered(element)); - const isToolbarAction = (element: HTMLElement) => { - if (!element.matches('ion-button.ios, ion-menu-button.ios') || isExcluded(element) || isShellDisabled(element)) return false; - if (element.matches('ion-menu-button')) return true; - return !!element.querySelector('ion-icon, svg'); - }; - const toolbarActions = (element: HTMLIonButtonsElement) => - Array.from(element.children).filter((child): child is HTMLElement => child instanceof HTMLElement && isToolbarAction(child)); const pageOrder = (element: Element) => Array.from(doc.querySelectorAll('.ion-page')).indexOf(element.closest('.ion-page')!); const findBack = () => { const candidates = Array.from(doc.querySelectorAll(`ion-back-button:not(.${backProjectionClass})`)).filter( @@ -88,10 +98,9 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel const findToolbarGroups = () => { const candidates = Array.from(doc.querySelectorAll(`ion-buttons.ios:not(.${toolbarProjectionClass})`)).flatMap( (group): ToolbarSource[] => { - const actions = toolbarActions(group).filter((action) => unprojected(projectedSources(), () => isRendered(action))); + const actions = foldableToolbarActions(group).filter((action) => unprojected(projectedSources(), () => isRendered(action))); if (!actions.length) return []; - if (group.matches('.ionic-theme-disabled, .ios-theme-disabled, .ios26-disabled')) - return actions.filter(inEligibleToolbar).map((action) => ({ group, actions: [action] })); + if (!isFoldableToolbarGroup(group)) return actions.filter(inEligibleToolbar).map((action) => ({ group, actions: [action] })); return inEligibleToolbar(group) && unprojected(projectedSources(), () => isRendered(group)) ? [{ group, actions }] : []; }, ); @@ -131,6 +140,7 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel if (!['class', marker].includes(attribute.name) && !original.hasAttribute(attribute.name)) target.removeAttribute(attribute.name); for (const attribute of Array.from(original.attributes)) if (!['id', 'slot', marker, 'aria-hidden'].includes(attribute.name)) target.setAttribute(attribute.name, attribute.value); + target.classList.remove(prehiddenClass); }; const syncBack = (target: HTMLIonBackButtonElement, original: HTMLIonBackButtonElement) => { copyAttributes(target, original); @@ -191,7 +201,7 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel (event) => { event.preventDefault(); event.stopImmediatePropagation(); - if (backSource && backSource === findBack()) backSource.click(); + if (backSource && backSource === findBack()) activateProjectedElement(backSource); }, { capture: true }, ); @@ -215,7 +225,7 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel (event) => { event.preventDefault(); event.stopImmediatePropagation(); - if (isCurrentToolbarAction(source)) source.click(); + if (isCurrentToolbarAction(source)) activateProjectedElement(source); }, { capture: true }, ); @@ -231,9 +241,19 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel topOffset += actions.length * toolbarControlSize + toolbarControlGap; } if (nextBack || toolbarProjections.length) root.classList.add(readyClass); - sourceObserver = new MutationObserver(schedule); - if (nextBack?.shadowRoot) sourceObserver.observe(nextBack.shadowRoot, { subtree: true, childList: true, attributes: true }); - for (const { group } of groups) sourceObserver.observe(group, { subtree: true, childList: true, attributes: true }); + sourceObserver = new MutationObserver((records) => { + if ( + records.some( + (record) => + record.type !== 'attributes' || (![marker, 'aria-hidden'].includes(record.attributeName ?? '') && !prehideOnlyMutation(record)), + ) + ) + schedule(); + }); + if (nextBack?.shadowRoot) + sourceObserver.observe(nextBack.shadowRoot, { subtree: true, childList: true, attributes: true, attributeOldValue: true }); + for (const { group } of groups) + sourceObserver.observe(group, { subtree: true, childList: true, attributes: true, attributeOldValue: true }); }; const performUpdate = () => { frame = 0; @@ -245,6 +265,7 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel subtree: true, childList: true, attributes: true, + attributeOldValue: true, attributeFilter: observingFoldable ? undefined : ['class'], }); } @@ -293,13 +314,32 @@ export const createFoldableWebProjection = (doc: Document, options: NativeUIShel )), ); if ( - (observingFoldable && records.some((record) => record.attributeName !== marker && !insideProjection(record.target))) || - (!observingFoldable && foldableChanged) + (observingFoldable && + records.some((record) => record.attributeName !== marker && !prehideOnlyMutation(record) && !insideProjection(record.target))) || + (!observingFoldable && foldableChanged && records.some((record) => !prehideOnlyMutation(record))) ) schedule(); }); - observer.observe(doc.documentElement, { subtree: true, childList: true, attributes: true, attributeFilter: ['class'] }); - for (const name of ['ionViewDidEnter', 'ionViewDidLeave', 'ionModalWillPresent', 'ionModalDidDismiss']) + observer.observe(doc.documentElement, { + subtree: true, + childList: true, + attributes: true, + attributeOldValue: true, + attributeFilter: ['class'], + }); + const pageLifecycle = (event: Event) => { + const page = event.target; + if (!(page instanceof HTMLElement) || !page.matches('.ion-page')) return; + if (event.type === FOLDABLE_TRANSITION_CANCELED) { + const entering = (event as CustomEvent<{ entering?: HTMLElement }>).detail?.entering; + foldablePages.cancel(entering, page); + } else foldablePages.lifecycle(event); + if (foldableRoot()) schedule(); + }; + for (const name of ['ionViewWillEnter', 'ionViewWillLeave', 'ionViewDidEnter', 'ionViewDidLeave']) + doc.addEventListener(name, pageLifecycle, { capture: true, signal: listeners.signal }); + doc.addEventListener(FOLDABLE_TRANSITION_CANCELED, pageLifecycle, { capture: true, signal: listeners.signal }); + for (const name of ['ionModalWillPresent', 'ionModalDidDismiss']) doc.addEventListener(name, schedule, { capture: true, signal: listeners.signal }); if (options.controls === undefined || options.controls.toolbar === true) schedule(); diff --git a/src/native/index.ts b/src/native/index.ts index 5fb9cfb7..e030ee80 100644 --- a/src/native/index.ts +++ b/src/native/index.ts @@ -4,6 +4,7 @@ import type { NativeUIShellHandle, NativeUIShellOptions, NativeUIShellPlugin, We import { bindMetricsLifecycle } from './lifecycle'; import { createRuntime } from './runtime'; import { createFoldableWebProjection } from './foldable-web'; +import { prehideFoldableToolbarSources } from './prehide'; export type { NativeUIShellComponent, NativeUIShellControls, @@ -22,6 +23,27 @@ const web = (reason: string): NativeUIShellHandle => ({ destroy: async () => {}, }); +const combine = (native: NativeUIShellHandle, fallback: NativeUIShellHandle): NativeUIShellHandle => ({ + getStatus() { + const nativeStatus = native.getStatus(); + const fallbackStatus = fallback.getStatus(); + return { + ...nativeStatus, + state: + nativeStatus.state === 'stopped' && fallbackStatus.state === 'stopped' ? 'stopped' : nativeStatus.projected > 0 ? 'native' : 'web', + projected: nativeStatus.projected + fallbackStatus.projected, + updates: nativeStatus.updates + fallbackStatus.updates, + }; + }, + async suspend() { + const [nativeLease, fallbackLease] = await Promise.all([native.suspend(), fallback.suspend()]); + return { resume: async () => void (await Promise.all([nativeLease.resume(), fallbackLease.resume()])) }; + }, + async destroy() { + await Promise.all([native.destroy(), fallback.destroy()]); + }, +}); + /** Reads the current native WebView geometry and applies it to page transitions. */ export const configureNativeTransition = async (): Promise => { const metrics = typeof document !== 'undefined' && Capacitor.getPlatform() === 'ios' ? await plugin.getWebViewMetrics() : { radius: 0 }; @@ -42,49 +64,51 @@ export const enableNativeUIShell = (options: NativeUIShellOptions = {}): Promise : Promise.resolve(web('Disabled')); } if (typeof document === 'undefined') return Promise.resolve(web('Requires a document')); + const stopPrehide = + !active && (options.controls === undefined || options.controls.toolbar === true) ? prehideFoldableToolbarSources(document) : undefined; return (active ??= (async () => { - const foldableWeb = createFoldableWebProjection(document, options); - if (Capacitor.getPlatform() !== 'ios') return resetOnDestroy(withReason(foldableWeb, 'Requires Capacitor iOS')); + if (Capacitor.getPlatform() !== 'ios') + return resetOnDestroy(withReason(createFoldableWebProjection(document, options), 'Requires Capacitor iOS'), stopPrehide); let runtime: NativeUIShellHandle | undefined; try { await configureNativeTransition().catch(() => undefined); - if (!(await plugin.configure()).supported) { - return resetOnDestroy(withReason(foldableWeb, 'Requires iOS 26 or later')); + const capabilities = await plugin.configure(); + if (!capabilities.supported) { + return resetOnDestroy(withReason(createFoldableWebProjection(document, options), 'Requires iOS 26 or later'), stopPrehide); } - runtime = await createRuntime(document, plugin, options); + runtime = await createRuntime(document, plugin, options, capabilities.foldableRail === true); + if (capabilities.foldableRail !== true) runtime = combine(runtime, createFoldableWebProjection(document, options)); runtime = await bindMetricsLifecycle( runtime, () => plugin.addListener('webViewMetricsChange', (metrics) => setConfig({ radius: metrics.radius })), () => (active = undefined), ); - return resetOnDestroy(combine(runtime, foldableWeb)); + return resetOnDestroy(runtime, stopPrehide); } catch (error) { await runtime?.destroy(); - return resetOnDestroy(withReason(foldableWeb, error instanceof Error ? error.message : String(error))); + return resetOnDestroy( + withReason(createFoldableWebProjection(document, options), error instanceof Error ? error.message : String(error)), + stopPrehide, + ); } })()); }; -const combine = (native: NativeUIShellHandle, foldableWeb: NativeUIShellHandle): NativeUIShellHandle => ({ - getStatus: () => { - const nativeStatus = native.getStatus(); - const webStatus = foldableWeb.getStatus(); - return { ...nativeStatus, projected: nativeStatus.projected + webStatus.projected }; - }, +const resetOnDestroy = (handle: NativeUIShellHandle, prehide?: ReturnType): NativeUIShellHandle => ({ + getStatus: handle.getStatus, async suspend() { - const [nativeLease, webLease] = await Promise.all([native.suspend(), foldableWeb.suspend()]); - return { resume: async () => void (await Promise.all([nativeLease.resume(), webLease.resume()])) }; - }, - async destroy() { - await Promise.all([native.destroy(), foldableWeb.destroy()]); + const lease = await handle.suspend(); + const resumePrehide = prehide?.suspend(); + return { + async resume() { + await lease.resume(); + resumePrehide?.(); + }, + }; }, -}); - -const resetOnDestroy = (handle: NativeUIShellHandle): NativeUIShellHandle => ({ - getStatus: handle.getStatus, - suspend: handle.suspend, async destroy() { await handle.destroy(); + prehide?.stop(); active = undefined; }, }); diff --git a/src/native/prehide.ts b/src/native/prehide.ts new file mode 100644 index 00000000..0687079b --- /dev/null +++ b/src/native/prehide.ts @@ -0,0 +1,220 @@ +import { LIFECYCLE_DID_LEAVE, LIFECYCLE_WILL_ENTER } from '@ionic/core'; +import { + clearFoldablePlacement, + foldableBackWebClass, + foldableRailOwned, + inFixedToolbar, + isFoldableToolbarActionShape, + isPermanentlyExcluded, + isShellDisabled, + prehiddenClass, + prehideRootClass, + rejectedClass, + setFoldablePlacement, +} from './shared/dom'; + +const overlays = 'ion-menu, ion-modal, ion-popover'; +const sourceSelector = 'ion-back-button, ion-menu-button, ion-buttons, ion-button'; +const backSupported = (element: HTMLElement): boolean => { + const back = element as HTMLIonBackButtonElement; + return back.icon === undefined && back.color === undefined && !!back.shadowRoot; +}; +const eligible = (element: HTMLElement): boolean => + inFixedToolbar(element) && !isPermanentlyExcluded(element) && !isShellDisabled(element) && !element.closest(overlays); + +/** Capture toolbar placement once per routed-page epoch, before Ionic paints its transition. */ +export const prehideFoldableToolbarSources = (doc: Document): { suspend: () => () => void; stop: () => void } => { + doc.documentElement.classList.add(prehideRootClass); + const scopes = new Map>(); + const pendingBacks = new Map }>(); + const listeners = new AbortController(); + const root = () => doc.querySelector(':is(ion-app, body).ios-theme-enable-foldable'); + let suspended = 0; + let stopped = false; + const routedPage = (element: HTMLElement) => element.closest('.ion-page:not(ion-app, body)'); + const release = (scope: HTMLElement) => { + for (const [element, pending] of pendingBacks) { + if (pending.scope !== scope) continue; + clearTimeout(pending.timer); + pendingBacks.delete(element); + } + const owned = scopes.get(scope); + if (!owned) return; + owned.forEach((element) => { + element.classList.remove(prehiddenClass); + element.classList.remove(foldableBackWebClass); + clearFoldablePlacement(element); + }); + scopes.delete(scope); + }; + const capture = (scope: HTMLElement) => { + if (!root()?.contains(scope) || scope.closest(overlays)) return; + const owned = scopes.get(scope) ?? new Set(); + const place = (element: HTMLElement, rail: boolean) => { + if (owned.has(element)) return; + setFoldablePlacement(element, rail); + if (element.matches('ion-back-button')) element.classList.toggle(foldableBackWebClass, !rail); + owned.add(element); + }; + scope.querySelectorAll(sourceSelector).forEach((element) => { + if (element.closest(overlays) || (scope.matches('.ion-page') && routedPage(element) !== scope)) return; + if (element.matches('ion-buttons')) { + const children = Array.from(element.children).filter((child): child is HTMLElement => child instanceof HTMLElement); + const actions = children.filter((child) => eligible(child) && isFoldableToolbarActionShape(child)); + const group = + eligible(element) && + actions.length === children.length && + (actions.length > 1 || (actions.length === 1 && actions[0].matches('ion-menu-button'))); + const freshGroup = !owned.has(element); + place(element, group); + children.forEach((child) => { + if (!child.matches('ion-back-button')) place(child, freshGroup && actions.includes(child)); + }); + } else if (element.matches('ion-back-button') || !element.parentElement?.matches('ion-buttons')) { + if (owned.has(element)) return; + if ( + element.matches('ion-back-button') && + eligible(element) && + (element as HTMLIonBackButtonElement).icon === undefined && + (element as HTMLIonBackButtonElement).color === undefined && + !element.shadowRoot && + !element.classList.contains('hydrated') + ) { + if (!pendingBacks.has(element)) { + const timer = setTimeout(() => { + if (pendingBacks.get(element)?.scope !== scope) return; + pendingBacks.delete(element); + // A custom element that never hydrates must remain usable on Web. + place(element, false); + reconcile(); + }, 1500); + pendingBacks.set(element, { scope, timer }); + } + return; + } + const pending = pendingBacks.get(element); + if (pending) { + clearTimeout(pending.timer); + pendingBacks.delete(element); + } + place( + element, + eligible(element) && (element.matches('ion-back-button') ? backSupported(element) : isFoldableToolbarActionShape(element)), + ); + } + }); + scopes.set(scope, owned); + }; + const reconcile = () => { + const foldable = root(); + if (!foldable) { + Array.from(scopes.keys()).forEach(release); + return; + } + for (const scope of scopes.keys()) if (!scope.isConnected) release(scope); + for (const [element, pending] of pendingBacks) if (element.shadowRoot || element.classList.contains('hydrated')) capture(pending.scope); + // Ionic inserts the destination as invisible before WillEnter. Hide its + // sources in that same mutation microtask; capture new DOM identities on active pages too. + foldable.querySelectorAll('.ion-page:not(ion-app, body, .ion-page-hidden)').forEach(capture); + // Root toolbars can mount after startup; each new DOM identity is captured once. + foldable.querySelectorAll(sourceSelector).forEach((element) => { + if (routedPage(element) || element.closest(overlays)) return; + const scope = element.closest('ion-toolbar'); + if (scope) capture(scope); + }); + for (const owned of scopes.values()) { + for (const element of owned) { + const groupOwnsChildren = + element.parentElement?.matches('ion-buttons') && + foldableRailOwned(element.parentElement) && + Array.from(element.parentElement.children).every((child) => child instanceof HTMLElement && foldableRailOwned(child)); + const hide = + !suspended && + foldableRailOwned(element) && + !groupOwnsChildren && + element.isConnected && + !element.closest(`.${rejectedClass}`) && + !isPermanentlyExcluded(element) && + !isShellDisabled(element) && + (!element.matches('ion-back-button') || backSupported(element) || !element.classList.contains('hydrated')) && + (!element.matches('ion-buttons') || + Array.from(element.children).every((child) => child instanceof HTMLElement && foldableRailOwned(child))); + element.classList.toggle(prehiddenClass, hide); + if (element.matches('ion-back-button')) element.classList.toggle(foldableBackWebClass, !hide); + } + } + }; + const onWillEnter = (event: Event) => { + const page = event.target; + if (page instanceof HTMLElement && page.matches('.ion-page')) { + release(page); // Cached pages begin a new placement epoch. + capture(page); + reconcile(); + } + }; + const onDidLeave = (event: Event) => { + const page = event.target; + if (page instanceof HTMLElement) release(page); + }; + doc.addEventListener(LIFECYCLE_WILL_ENTER, onWillEnter, { capture: true, signal: listeners.signal }); + doc.addEventListener(LIFECYCLE_DID_LEAVE, onDidLeave, { capture: true, signal: listeners.signal }); + reconcile(); + const mutationRelevant = (record: MutationRecord) => { + if (record.type === 'childList') + return [...Array.from(record.addedNodes), ...Array.from(record.removedNodes)].some( + (node) => + node instanceof Element && + (node.matches(`${sourceSelector}, .ion-page, ion-toolbar`) || !!node.querySelector(`${sourceSelector}, .ion-page, ion-toolbar`)), + ); + if (record.attributeName !== 'class') return true; + const current = (record.target as Element).className; + const previous = record.oldValue ?? ''; + const withoutOwned = (value: string) => + value + .split(/\s+/) + .filter((name) => name && ![prehiddenClass, prehideRootClass, foldableBackWebClass].includes(name)) + .join(' '); + return typeof current !== 'string' || withoutOwned(previous) !== withoutOwned(current); + }; + let scheduled = false; + const observer = new (doc.defaultView?.MutationObserver ?? MutationObserver)((records) => { + if (scheduled || !records.some(mutationRelevant)) return; + scheduled = true; + queueMicrotask(() => { + scheduled = false; + reconcile(); + }); + }); + observer.observe(doc.documentElement, { + subtree: true, + childList: true, + attributes: true, + attributeOldValue: true, + attributeFilter: ['class', 'hidden', 'inert', 'icon', 'color'], + }); + return { + suspend: () => { + if (stopped) return () => {}; + suspended++; + doc.documentElement.classList.remove(prehideRootClass); + reconcile(); + let resumed = false; + return () => { + if (resumed || stopped) return; + resumed = true; + if (--suspended === 0) { + doc.documentElement.classList.add(prehideRootClass); + reconcile(); + } + }; + }, + stop: () => { + if (stopped) return; + stopped = true; + observer.disconnect(); + listeners.abort(); + doc.documentElement.classList.remove(prehideRootClass); + Array.from(scopes.keys()).forEach(release); + }, + }; +}; diff --git a/src/native/runtime.ts b/src/native/runtime.ts index 4955d114..61c205dc 100644 --- a/src/native/runtime.ts +++ b/src/native/runtime.ts @@ -1,6 +1,6 @@ import type { PluginListenerHandle } from '@capacitor/core'; import { LIFECYCLE_WILL_ENTER, LIFECYCLE_WILL_LEAVE, LIFECYCLE_DID_ENTER, LIFECYCLE_DID_LEAVE } from '@ionic/core'; -import { getNativeSearchBindings, setNativeUIShellIntegration } from '../native-integration'; +import { FOLDABLE_TRANSITION_CANCELED, getNativeSearchBindings, setNativeUIShellIntegration } from '../native-integration'; import { createSearchSupport } from './components/searchable-tabs'; import type { ShellActivation, @@ -10,8 +10,16 @@ import type { NativeUIShellPlugin, NativeUIShellStatus, } from './definitions'; -import { readCandidate, selector, shadowSelector, motionSelector } from './components'; -import { marker, unprojected } from './shared/dom'; +import { readCandidate, selector, shadowSelector, motionSelector, isFoldableRailCandidate } from './components'; +import { + activateProjectedElement, + createFoldablePageState, + isFoldableRailSource, + marker, + prehideOnlyMutation, + rejectedClass, + unprojected, +} from './shared/dom'; import { createIconRenderer } from './shared/icons'; import type { Candidate } from './shared/candidate'; import { CSS_MOTION_EVENTS } from './shared/events'; @@ -19,6 +27,8 @@ import { createCrossfade, fadeMarker } from './shared/crossfade'; const overlays = 'ion-modal, ion-popover, ion-alert, ion-action-sheet, ion-loading, ion-picker, ion-toast, ion-menu'; const overlayNames = ['Modal', 'Popover', 'Alert', 'ActionSheet', 'Loading', 'Picker', 'Toast']; +const foldableRailMarker = 'data-native-ui-shell-foldable-rail'; +const foldableRailMemberMarker = 'data-native-ui-shell-foldable-rail-member'; // A failed bridge must not leave the source inaccessible indefinitely. const bounded = (promise: Promise): Promise => @@ -31,6 +41,7 @@ export const createRuntime = async ( doc: Document, plugin: NativeUIShellPlugin, options: NativeUIShellOptions = {}, + nativeFoldableRail = true, ): Promise => { const win = doc.defaultView!; const icons = createIconRenderer(); @@ -38,8 +49,11 @@ export const createRuntime = async ( const ids = new WeakMap(); let rejected = new WeakMap(); const sources = new Map(); + const foldableRailOwners = new Set(); + const foldableRailMembers = new Set(); const suspended = new Set(); const pages = new Set(); + const foldablePages = createFoldablePageState(); const presented = new Set(); const manualSuspensions = new Set(); const moving = new Map>(); @@ -95,13 +109,22 @@ export const createRuntime = async ( const style = doc.createElement('style'); const hidden = `[${marker}]:not([${fadeMarker}])`; style.textContent = `${hidden}, ${hidden} *, ${hidden}::before, ${hidden}::after, ${hidden}::part(native) { visibility: hidden !important; } - [${marker}], [${marker}] * { pointer-events: none !important; }`; + [${marker}], [${marker}] * { pointer-events: none !important; } + /* Ionic disables the covered page while a menu is open. Foldable rail + controls remain outside that page; zero specificity preserves any + pointer-events rule supplied by the application itself. */ + :where(.menu-content-open) :where([${foldableRailMarker}]) { + pointer-events: auto; + } + :where(.menu-content-open) :where([${foldableRailMarker}]) > :where(:not([${foldableRailMemberMarker}])) { + pointer-events: none; + }`; const restore = (element: HTMLElement) => { lastSnapshot = ''; search.release(element); element.removeAttribute(marker); - if (!stopped) crossfade.play(element, false, handoffInstant); + if (!stopped) crossfade.play(element, false, handoffInstant || isFoldableRailSource(element)); if (element.getAttribute('aria-hidden') === 'true') { const previous = sources.get(element); if (previous == null) element.removeAttribute('aria-hidden'); @@ -110,7 +133,13 @@ export const createRuntime = async ( sources.delete(element); element.dispatchEvent(new CustomEvent('nativeUIShellChange')); }; - const restoreAll = () => Array.from(sources.keys()).forEach(restore); + const restoreAll = () => { + Array.from(sources.keys()).forEach(restore); + foldableRailOwners.forEach((element) => element.removeAttribute(foldableRailMarker)); + foldableRailOwners.clear(); + foldableRailMembers.forEach((element) => element.removeAttribute(foldableRailMemberMarker)); + foldableRailMembers.clear(); + }; const finishWaiters = () => { const current = waiters; waiters = []; @@ -123,14 +152,36 @@ export const createRuntime = async ( }; const search = createSearchSupport(doc, id, schedule); const candidateSources = (candidate: Candidate) => candidate.sources ?? [candidate.element]; + const setRejected = (element: HTMLElement, value: boolean) => { + element.classList.toggle(rejectedClass, value); + }; + const measuringPointerPages = new WeakSet(); const readEnabledCandidate = (element: HTMLElement): Candidate | undefined => { - const candidate = readCandidate(element, id); - if ( - candidate && - ['ion-back-button', 'ion-buttons', 'ion-menu-button'].includes(candidate.control.kind) && - element.closest(':is(ion-app, body).ios-theme-enable-foldable') - ) - return undefined; + const pointerPage = isFoldableRailCandidate(element) ? element.closest('.ion-page') : undefined; + let candidate: Candidate | undefined; + if (pointerPage && getComputedStyle(pointerPage).pointerEvents === 'none') { + const previous = pointerPage.style.getPropertyValue('pointer-events'); + const priority = pointerPage.style.getPropertyPriority('pointer-events'); + const hadStyle = pointerPage.hasAttribute('style'); + measuringPointerPages.add(pointerPage); + pointerPage.style.setProperty('pointer-events', 'auto', 'important'); + try { + candidate = readCandidate(element, id); + } finally { + if (previous) pointerPage.style.setProperty('pointer-events', previous, priority); + else pointerPage.style.removeProperty('pointer-events'); + if (!hadStyle && !pointerPage.style.length) pointerPage.removeAttribute('style'); + win.setTimeout(() => measuringPointerPages.delete(pointerPage), 0); + } + } else candidate = readCandidate(element, id); + if (candidate && isFoldableRailCandidate(element)) { + if (!nativeFoldableRail) return undefined; + candidate.control.placement = 'foldable-rail'; + if (['ion-button', 'ion-buttons', 'ion-menu-button'].includes(candidate.control.kind)) { + const slot = (element.matches('ion-buttons') ? element : (element.closest('ion-buttons') ?? element)).getAttribute('slot'); + if (slot === 'start' || slot === 'end') candidate.control.toolbarSlot = slot; + } + } return candidate && controlEnabled(candidate) ? candidate : undefined; }; const flush = async () => { @@ -143,17 +194,22 @@ export const createRuntime = async ( } }; const blocked = (element: HTMLElement) => - Array.from(suspended).some((scopes) => scopes.some((scope) => scope.contains(element))) || - Array.from(pages).some((page) => page.contains(element)) || - Array.from(moving.keys()).some((surface) => surface.contains(element)); + foldablePages.isDeparted(element) || + (!isFoldableRailSource(element) && + (Array.from(suspended).some((scopes) => scopes.some((scope) => scope.contains(element))) || + Array.from(pages).some((scope) => scope.contains(element)) || + Array.from(moving.keys()).some((surface) => surface.contains(element)))); const painted = () => new Promise((resolve) => win.requestAnimationFrame(() => win.requestAnimationFrame(() => resolve()))); - const overlayOpen = () => { + const overlayOpen = (includeMenu = true) => { for (const element of presented) if (!element.isConnected) presented.delete(element); + const presentedOverlayOpen = Array.from(presented).some((element) => includeMenu || !element.matches('ion-menu')); return ( manualSuspensions.size > 0 || - presented.size > 0 || + presentedOverlayOpen || Array.from(doc.querySelectorAll(overlays)).some( - (element) => (element as Element & { presented?: boolean }).presented || element.classList.contains('show-menu'), + (element) => + (includeMenu || !element.matches('ion-menu')) && + ((element as Element & { presented?: boolean }).presented || element.classList.contains('show-menu')), ) ); }; @@ -164,19 +220,21 @@ export const createRuntime = async ( search.keepSearchTabsVisible(); for (const page of pages) if (!page.isConnected) pages.delete(page); for (const surface of moving.keys()) if (!surface.isConnected) moving.delete(surface); - if (doc.hidden || overlayOpen()) return []; + if (doc.hidden || overlayOpen(false)) return []; if (win.visualViewport && (win.visualViewport.scale !== 1 || win.visualViewport.offsetTop !== 0) && !search.hasActive()) return []; - return unprojected(sources.keys(), () => + const menuOpen = overlayOpen(); + const candidates = unprojected(sources.keys(), () => search .decorate( Array.from(doc.querySelectorAll(selector)) - .filter((element) => !blocked(element)) + .filter((element) => !foldablePages.isDeparted(element) && (!blocked(element) || (menuOpen && isFoldableRailSource(element)))) .map(readEnabledCandidate) .filter((candidate): candidate is Candidate => !!candidate), blocked, ) .filter((candidate) => !rejected.has(candidate.element) || rejected.get(candidate.element) !== signature(candidate)), ); + return menuOpen ? candidates.filter((candidate) => isFoldableRailCandidate(candidate.element)) : candidates; }; const observe = () => { const wanted = new Set(); @@ -245,7 +303,7 @@ export const createRuntime = async ( removed.forEach(restore); // The outgoing tab is no longer visible, so waiting two frames only leaves its // native snapshot over the destination. Stack transitions still need the paint. - if (!handoffInstant) { + if (!handoffInstant && removed.some((element) => !isFoldableRailSource(element))) { await painted(); if (stopped || dirty) return; } @@ -274,6 +332,7 @@ export const createRuntime = async ( for (const candidate of candidates) { if (result.rejectedControls?.includes(candidate.control.id)) { rejected.set(candidate.element, signatures.get(candidate.element)!); + setRejected(candidate.element, true); dirty = true; } } @@ -282,6 +341,15 @@ export const createRuntime = async ( const current = dirty ? new Map(currentCandidates.map((candidate) => [candidate.element, signature(candidate)])) : signatures; const currentSources = new Set(currentCandidates.flatMap(candidateSources)); const accepted = candidates.filter((candidate) => current.get(candidate.element) === signatures.get(candidate.element)); + accepted.forEach((candidate) => setRejected(candidate.element, false)); + const acceptedFoldableRailOwners = new Set( + accepted.filter((candidate) => candidate.control.placement === 'foldable-rail').map((candidate) => candidate.element), + ); + const acceptedFoldableRailMembers = new Set( + accepted + .filter((candidate) => candidate.control.placement === 'foldable-rail') + .flatMap((candidate) => Array.from(candidate.actions.values())), + ); const invalidated = candidates.length !== accepted.length; acceptedRevision = result.revision; lastSnapshot = invalidated ? '' : serialized; @@ -291,10 +359,28 @@ export const createRuntime = async ( // Keep an existing cover while its content catches up. Only an ineligible // source needs to return to Web; new sources still require an exact ack. for (const element of sources.keys()) if (!currentSources.has(element)) restore(element); + for (const element of foldableRailOwners) { + if (acceptedFoldableRailOwners.has(element)) continue; + element.removeAttribute(foldableRailMarker); + foldableRailOwners.delete(element); + } + for (const element of acceptedFoldableRailOwners) { + element.setAttribute(foldableRailMarker, ''); + foldableRailOwners.add(element); + } + for (const element of foldableRailMembers) { + if (acceptedFoldableRailMembers.has(element)) continue; + element.removeAttribute(foldableRailMemberMarker); + foldableRailMembers.delete(element); + } + for (const element of acceptedFoldableRailMembers) { + element.setAttribute(foldableRailMemberMarker, ''); + foldableRailMembers.add(element); + } for (const element of accepted.flatMap(candidateSources)) { if (!sources.has(element)) { sources.set(element, element.getAttribute('aria-hidden')); - crossfade.play(element, true, handoffInstant); + crossfade.play(element, true, handoffInstant || isFoldableRailSource(element)); element.setAttribute(marker, ''); element.setAttribute('aria-hidden', 'true'); element.dispatchEvent(new CustomEvent('nativeUIShellChange')); @@ -326,13 +412,21 @@ export const createRuntime = async ( else finishWaiters(); } }; - const observation: MutationObserverInit = { subtree: true, childList: true, characterData: true, attributes: true }; + const observation: MutationObserverInit = { + subtree: true, + childList: true, + characterData: true, + attributes: true, + attributeOldValue: true, + }; const observer = new MutationObserver((records) => { if ( records.some( (record) => record.attributeName !== marker && record.attributeName !== fadeMarker && + !prehideOnlyMutation(record) && + !(record.attributeName === 'style' && measuringPointerPages.has(record.target as HTMLElement)) && !( record.attributeName === 'aria-hidden' && sources.has(record.target as HTMLElement) && @@ -347,6 +441,7 @@ export const createRuntime = async ( target.addEventListener(name, callback, { capture: true, signal: listeners.signal }); const pageWill: EventListener = (event) => { const page = event.target as HTMLElement; + foldablePages.lifecycle(event); getNativeSearchBindings(doc) .filter((binding) => page.contains(binding.footer)) .forEach((binding) => search.retire(binding)); @@ -357,7 +452,9 @@ export const createRuntime = async ( schedule(); }; const pageDid: EventListener = (event) => { - pages.delete(event.target as HTMLElement); + const page = event.target as HTMLElement; + foldablePages.lifecycle(event); + pages.delete(page); schedule(); if (tabSwitchHandoff && pages.size === 0) endTabSwitchHandoff(); }; @@ -417,6 +514,14 @@ export const createRuntime = async ( for (const name of Object.values(CSS_MOTION_EVENTS)) on(doc, name, motion); for (const name of [LIFECYCLE_WILL_ENTER, LIFECYCLE_WILL_LEAVE]) on(doc, name, pageWill); for (const name of [LIFECYCLE_DID_ENTER, LIFECYCLE_DID_LEAVE]) on(doc, name, pageDid); + on(doc, FOLDABLE_TRANSITION_CANCELED, (event) => { + const leaving = event.target as HTMLElement; + const entering = (event as CustomEvent<{ entering?: HTMLElement }>).detail?.entering; + foldablePages.cancel(entering, leaving); + pages.delete(leaving); + if (entering) pages.delete(entering); + schedule(); + }); on(doc, 'ionTabsWillChange', () => { // Vanilla ion-tabs dispatches DOM events; @ionic/angular uses EventEmitters instead. armTabSwitchHandoff(); @@ -439,12 +544,16 @@ export const createRuntime = async ( schedule(); }); } + const eventMenu = (event: Event) => + event.composedPath().find((target): target is HTMLElement => target instanceof HTMLElement && target.matches('ion-menu')); on(doc, 'ionWillOpen', (event) => { - presented.add(event.target as HTMLElement); + const menu = eventMenu(event); + if (menu) presented.add(menu); schedule(); }); on(doc, 'ionDidClose', (event) => { - presented.delete(event.target as HTMLElement); + const menu = eventMenu(event); + if (menu) presented.delete(menu); schedule(); }); for (const name of [ @@ -522,6 +631,7 @@ export const createRuntime = async ( /* Always restore the Web, even after bridge loss. */ } style.remove(); + doc.querySelectorAll(`.${rejectedClass}`).forEach((element) => setRejected(element, false)); icons.clear(); pages.clear(); presented.clear(); @@ -540,7 +650,7 @@ export const createRuntime = async ( event.revision < acceptedRevision || event.revision > revision || event.sequence <= lastSequence || - overlayOpen() + overlayOpen(false) ) return; // Native may send input before update() resolves on the JS bridge. @@ -561,7 +671,7 @@ export const createRuntime = async ( candidate?.control.search && [candidate.control.search.trigger.id, candidate.control.search.closeId].includes(event.id); if (!searchAction && (!item || item.disabled || item.visible === false)) return; // The original Ionic host owns form submission, routerLink and selection events. - element.click(); + activateProjectedElement(element); lastSnapshot = ''; // Reconcile even if Ionic rejects the proposed native selection. schedule(); }; @@ -597,9 +707,14 @@ export const createRuntime = async ( await flush(); // Source DOM has been restored before Ionic starts moving it. await new Promise((resolve) => win.requestAnimationFrame(() => resolve())); - return () => { + return (canceled = false) => { suspended.delete(scopes); - scopes.forEach((scope) => pages.delete(scope)); // interactive cancellation has no didLeave. + if (canceled || !scopes.some((scope) => scope.closest(':is(ion-app, body).ios-theme-enable-foldable'))) { + scopes.forEach((scope) => pages.delete(scope)); // Preserve ordinary iPhone handoff; cancellation has no DidLeave. + if (canceled) { + foldablePages.cancel(scopes[0], scopes[1]); // The entering page is abandoned; the leaving page stays active. + } + } schedule(); }; }, diff --git a/src/native/shared/candidate.ts b/src/native/shared/candidate.ts index 22b00dd7..7bb93ae5 100644 --- a/src/native/shared/candidate.ts +++ b/src/native/shared/candidate.ts @@ -1,5 +1,5 @@ import type { ShellControl, ShellItem, NativeUIShellComponent } from '../definitions'; -import { frame, isDark, text, visible } from './dom'; +import { frame, isDark, isFoldableRailSource, text, visible } from './dom'; import { iconSource } from './icons'; export interface Candidate { @@ -39,7 +39,11 @@ export const appendItem = ( content: ParentNode = child, label = text(child), ): ShellItem | undefined => { - if (!visible(child) || child.querySelector('input, button, a, img, canvas, video, ion-spinner, ion-avatar')) return; + if ( + !visible(child, isFoldableRailSource(candidate.element)) || + child.querySelector('input, button, a, img, canvas, video, ion-spinner, ion-avatar') + ) + return; const icons = Array.from(content.querySelectorAll('ion-icon')); const directSVGs = Array.from(content.querySelectorAll('svg')); if (icons.length + directSVGs.length > 1) return undefined; @@ -54,7 +58,11 @@ export const appendItem = ( id: id(child), ...frame(child.getBoundingClientRect(), candidate.element.getBoundingClientRect()), label, - accessibilityLabel: child.getAttribute('aria-label') ?? native?.getAttribute('aria-label') ?? label, + accessibilityLabel: + child.getAttribute('aria-label') ?? + native?.getAttribute('aria-label') ?? + (icons[0]?.getAttribute('aria-hidden') === 'true' ? null : icons[0]?.getAttribute('aria-label')) ?? + label, disabled: !!(child as ItemElement).disabled || !!(candidate.element as ItemElement).disabled || diff --git a/src/native/shared/dom.ts b/src/native/shared/dom.ts index 48df88fe..53b7acaf 100644 --- a/src/native/shared/dom.ts +++ b/src/native/shared/dom.ts @@ -3,24 +3,144 @@ import { fadeMarker } from './crossfade'; import type { Frame } from '../definitions'; export const marker = 'data-native-ui-shell'; +export const prehiddenClass = 'ios-theme-native-ui-shell-prehidden'; +export const prehideRootClass = 'ios-theme-native-ui-shell-prehide'; +export const rejectedClass = 'ios-theme-native-ui-shell-rejected'; +export const foldableBackWebClass = 'ios-theme-foldable-back-web-owned'; +const prehideClasses = new Set([prehiddenClass, prehideRootClass]); +export const prehideOnlyMutation = (record: MutationRecord): boolean => { + if (record.attributeName !== 'class' || record.oldValue === null) return false; + const withoutPrehide = (value: string) => + value + .split(/\s+/) + .filter((name) => name && !prehideClasses.has(name)) + .join(' '); + return withoutPrehide(record.oldValue) === withoutPrehide((record.target as Element).getAttribute('class') ?? ''); +}; + +export const withoutPrehide = (element: HTMLElement, read: () => T): T => { + const changed: HTMLElement[] = []; + for (let current: HTMLElement | null = element; current; current = current.parentElement) { + if (current.classList.contains(prehiddenClass)) { + current.classList.remove(prehiddenClass); + changed.push(current); + } + } + const root = element.ownerDocument.documentElement; + if (element.matches('ion-back-button') && root.classList.contains(prehideRootClass)) { + root.classList.remove(prehideRootClass); + changed.push(root); + } + try { + return read(); + } finally { + changed.forEach((current) => current.classList.add(current === root ? prehideRootClass : prehiddenClass)); + } +}; export const isDark = (style: CSSStyleDeclaration): boolean => style.getPropertyValue('--ios27-color-scheme').trim() === 'dark'; -export const excluded = - '.ionic-theme-disabled, .ios-theme-disabled, .ios26-disabled, .ion-page-hidden, .ion-page-invisible, .ion-cloned-element, [hidden], [inert]'; +const permanentlyExcluded = '.ionic-theme-disabled, .ios-theme-disabled, .ios26-disabled, .ion-cloned-element, [hidden], [inert]'; +export const excluded = `${permanentlyExcluded}, .ion-page-hidden, .ion-page-invisible`; +const enteringPages = new WeakSet(); +export const setFoldableEnteringPage = (page: HTMLElement, entering: boolean): void => { + if (entering) enteringPages.add(page); + else enteringPages.delete(page); +}; +export const foldableEnteringPage = (element: HTMLElement): HTMLElement | undefined => { + const page = element.closest('.ion-page-invisible'); + return page && enteringPages.has(page) && page.closest(':is(ion-app, body).ios-theme-enable-foldable') ? page : undefined; +}; +export const createFoldablePageState = () => { + const departed = new WeakSet(); + return { + isDeparted(element: HTMLElement): boolean { + const page = element.closest('.ion-page'); + return !!page && departed.has(page) && !!element.closest(':is(ion-app, body).ios-theme-enable-foldable'); + }, + lifecycle(event: Event): void { + const page = event.target; + if (!(page instanceof HTMLElement) || !page.matches('.ion-page')) return; + const entering = event.type === 'ionViewWillEnter'; + if (entering || event.type === 'ionViewDidEnter') departed.delete(page); + else if (event.type === 'ionViewWillLeave' || event.type === 'ionViewDidLeave') departed.add(page); + setFoldableEnteringPage(page, entering); + }, + cancel(entering?: HTMLElement, leaving?: HTMLElement): void { + if (entering) { + departed.add(entering); + setFoldableEnteringPage(entering, false); + } + if (leaving) departed.delete(leaving); + }, + }; +}; const disabledButtonGroup = 'ion-buttons:is(.ionic-theme-disabled, .ios-theme-disabled, .ios26-disabled)'; const shellDisabledSelector = '.ios-theme-shell-disabled'; export const isDisabledButtonGroupChild = (element: HTMLElement): boolean => element.matches('ion-button.ios') && element.parentElement?.matches(disabledButtonGroup) === true; -export const isExcluded = (element: HTMLElement): boolean => { - const owner = element.closest(excluded); +const foldableRailTags = new Set(['ion-button', 'ion-back-button', 'ion-buttons', 'ion-menu-button', 'ion-tab-bar']); + +export const isFoldableRailSource = (element: HTMLElement): boolean => + foldableRailTags.has(element.localName) && + (element.matches('ion-tab-bar') || foldableRailOwned(element)) && + !element.closest('ion-menu, ion-modal, ion-popover') && + !!element.closest(':is(ion-app, body).ios-theme-enable-foldable'); + +const excludedBy = (element: HTMLElement, selector: string): boolean => { + const owner = element.closest(selector); return !!owner && !(element.parentElement === owner && isDisabledButtonGroupChild(element)); }; +export const isPermanentlyExcluded = (element: HTMLElement): boolean => excludedBy(element, permanentlyExcluded); +export const isExcluded = (element: HTMLElement, enteringPage?: HTMLElement): boolean => + excludedBy(element, `${permanentlyExcluded}, .ion-page-hidden`) || (!!element.closest('.ion-page-invisible') && !enteringPage); + // A shared native surface must not cover an opted-out descendant either. export const isShellDisabled = (element: Element): boolean => !!element.closest(shellDisabledSelector) || !!element.querySelector(shellDisabledSelector); +export const isFoldableToolbarActionShape = (element: HTMLElement): boolean => + element.matches('ion-menu-button') || + (element.matches('ion-button') && + !!element.querySelector('ion-icon, svg') && + !Array.from(element.childNodes).some((node) => node.nodeType === Node.TEXT_NODE && !!node.textContent?.trim()) && + !element.matches('.ion-color, [color]') && + ['default', 'clear'].includes((element as HTMLIonButtonElement).fill ?? element.getAttribute('fill') ?? 'default')); + +// Placement belongs to the DOM identity for one routed-page epoch. Changes to +// content/disabled state affect rendering, never its chosen surface. +const foldablePlacement = new WeakMap(); +export const setFoldablePlacement = (element: HTMLElement, rail: boolean): void => { + foldablePlacement.set(element, rail); +}; +export const clearFoldablePlacement = (element: HTMLElement): void => { + foldablePlacement.delete(element); +}; +export const foldableRailOwned = (element: HTMLElement): boolean => foldablePlacement.get(element) === true; + +export const isFoldableToolbarAction = (element: HTMLElement): boolean => + element.matches('.ios') && foldableRailOwned(element) && !isExcluded(element, foldableEnteringPage(element)) && !isShellDisabled(element); + +export const foldableToolbarActions = (element: HTMLElement): HTMLElement[] => + Array.from(element.children).filter((child): child is HTMLElement => child instanceof HTMLElement && isFoldableToolbarAction(child)); + +export const isFoldableToolbarGroup = (element: HTMLElement): boolean => { + return ( + element.matches('ion-buttons.ios') && + foldableRailOwned(element) && + !element.matches('.ionic-theme-disabled, .ios-theme-disabled, .ios26-disabled') && + !isShellDisabled(element) + ); +}; + +export const activateProjectedElement = (element: HTMLElement): void => { + const target = element.matches('ion-button, ion-back-button, ion-menu-button') + ? element.shadowRoot?.querySelector('[part~="native"]') + : undefined; + (target ?? element).click(); +}; + export const unprojected = (elements: Iterable, read: () => T): T => { const hidden = Array.from(elements).filter((element) => element.hasAttribute(marker)); hidden.forEach((element) => element.removeAttribute(marker)); @@ -31,14 +151,22 @@ export const unprojected = (elements: Iterable, read: () => T): } }; -export const visible = (element: HTMLElement): boolean => { - if (!element.isConnected || isExcluded(element) || isShellDisabled(element)) return false; +const readVisible = (element: HTMLElement, allowOutsideViewport: boolean): boolean => { + const enteringPage = isFoldableRailSource(element) ? foldableEnteringPage(element) : undefined; + if (!element.isConnected || isExcluded(element, enteringPage) || isShellDisabled(element)) return false; for (let current: HTMLElement | null = element; current; current = current.parentElement) { const style = getComputedStyle(current); - if (style.display === 'none' || style.visibility !== 'visible' || (Number(style.opacity) === 0 && !current.hasAttribute(fadeMarker))) + if ( + style.display === 'none' || + style.visibility !== 'visible' || + (Number(style.opacity) === 0 && !current.hasAttribute(fadeMarker) && current !== enteringPage) + ) return false; - // Moving/collapsing/custom transformed surfaces stay in Web coordinates. + // Ordinary controls on moving/collapsing/custom transformed surfaces stay in Web coordinates. + // Foldable rail controls are placed independently of their Web coordinates and must remain + // owned while Ionic transforms the content behind an open menu. if ( + !allowOutsideViewport && style.transform !== 'none' && !new DOMMatrixReadOnly(style.transform).isIdentity && current !== element && @@ -48,10 +176,15 @@ export const visible = (element: HTMLElement): boolean => { } const rect = element.getBoundingClientRect(); return ( - rect.width > 0 && rect.height > 0 && rect.left >= -1 && rect.top >= -1 && rect.right <= innerWidth + 1 && rect.bottom <= innerHeight + 1 + rect.width > 0 && + rect.height > 0 && + (allowOutsideViewport || (rect.left >= -1 && rect.top >= -1 && rect.right <= innerWidth + 1 && rect.bottom <= innerHeight + 1)) ); }; +export const visible = (element: HTMLElement, allowOutsideViewport = false): boolean => + withoutPrehide(element, () => readVisible(element, allowOutsideViewport)); + export const frame = (rect: DOMRect, origin?: DOMRect): Frame => ({ x: rect.x - (origin?.x ?? 0), y: rect.y - (origin?.y ?? 0), @@ -70,10 +203,11 @@ export const text = (element: Element): string => { export const inFixedToolbar = (element: Element): boolean => { const edge = element.closest('ion-toolbar')?.parentElement; + const foldable = !!element.closest(':is(ion-app, body).ios-theme-enable-foldable'); return ( !!edge?.matches('ion-header, ion-footer') && !element.closest('ion-content') && !edge.hasAttribute('collapse') && - !edge.matches('.header-collapse-main, .header-collapse-condense') + (foldable || !edge.matches('.header-collapse-main, .header-collapse-condense')) ); }; diff --git a/src/styles/components/ion-button.scss b/src/styles/components/ion-button.scss index bb57c26b..e787423e 100644 --- a/src/styles/components/ion-button.scss +++ b/src/styles/components/ion-button.scss @@ -437,6 +437,31 @@ ion-back-button.ios:not(.ios-theme-disabled, .ios26-disabled) { @include theme-button($is-back-button: true); } +.ios-theme-native-ui-shell-prehidden { + position: absolute !important; + visibility: hidden !important; +} + +// Ionic can paint a newly inserted routed page before its WillEnter snapshot. +// Until JS assigns ownership, keep only its foldable back source off the Web +// toolbar. Web-owned/opted-out buttons are released as soon as classified. +html.ios-theme-native-ui-shell-prehide + :is(ion-app, body).ios-theme-enable-foldable + :is(ion-header, ion-footer):not([collapse]):not(:where(ion-content *, ion-menu *, ion-modal *, ion-popover *)) + ion-toolbar + ion-back-button:not( + [icon], + [color], + .ios-theme-foldable-back-web-owned, + .ionic-theme-disabled, + .ios-theme-disabled, + .ios26-disabled, + .ios-theme-foldable-back-button-projection + ):not(:where(.ios-theme-shell-disabled *, .ionic-theme-disabled *, .ios-theme-disabled *, .ios26-disabled *)) { + position: absolute !important; + visibility: hidden !important; +} + :is(ion-app, body).ios-theme-enable-foldable.ios-theme-foldable-toolbar-ready { ion-toolbar.ios:not(.ios-theme-disabled, .ios26-disabled):not(:where(ion-menu *, ion-modal *, ion-popover *)) ion-back-button.ios[data-native-ui-shell]:not(.ios-theme-disabled, .ios26-disabled, .ios-theme-foldable-back-button-projection) { diff --git a/src/styles/components/ion-content.scss b/src/styles/components/ion-content.scss index 44751566..fd804a9c 100644 --- a/src/styles/components/ion-content.scss +++ b/src/styles/components/ion-content.scss @@ -23,6 +23,11 @@ ion-content.ios:not(.ios-theme-disabled, .ios26-disabled).content-fullscreen:not --padding-bottom: calc(60px + var(--ios-theme-floating-safe-area-bottom, var(--ios26-floating-safe-area-bottom))); } +// Keep the Web page's transition dimming, but never shade the foldable rail. +:is(ion-app, body).ios-theme-enable-foldable .ios-transition-shade { + clip-path: inset(0 var(--ios-theme-foldable-safe-area-right-resolved) 0 var(--ios-theme-foldable-safe-area-left-resolved)); +} + // Foldable system UI changes foreground geometry, not the page background. // Reset Ionic's matching inset so descendants do not apply it a second time. :is(ion-app, body).ios-theme-enable-foldable diff --git a/src/styles/components/ion-menu.scss b/src/styles/components/ion-menu.scss index 095145a1..80eeebd1 100644 --- a/src/styles/components/ion-menu.scss +++ b/src/styles/components/ion-menu.scss @@ -140,14 +140,18 @@ ion-menu.ios:not(.ios-theme-disabled, .ios26-disabled) { ion-menu.ios.menu-side-end:not(:dir(rtl)):not(.ios-theme-disabled, .ios26-disabled), ion-menu.ios.menu-side-start:dir(rtl):not(.ios-theme-disabled, .ios26-disabled) { --ion-safe-area-right: 0px; - right: var(--ios-theme-foldable-safe-area-right-resolved); - left: auto; + + &::part(container) { + margin-right: var(--ios-theme-foldable-safe-area-right-resolved); + } } ion-menu.ios.menu-side-start:not(:dir(rtl)):not(.ios-theme-disabled, .ios26-disabled), ion-menu.ios.menu-side-end:dir(rtl):not(.ios-theme-disabled, .ios26-disabled) { --ion-safe-area-left: 0px; - left: var(--ios-theme-foldable-safe-area-left-resolved); - right: auto; + + &::part(container) { + margin-left: var(--ios-theme-foldable-safe-area-left-resolved); + } } } diff --git a/src/transition/ios.transition.ts b/src/transition/ios.transition.ts index 44e9f7e9..cb5e655e 100644 --- a/src/transition/ios.transition.ts +++ b/src/transition/ios.transition.ts @@ -14,6 +14,7 @@ const transitionConfig = { offLeftPercent: 30, getIonPageElement, connectNativeUIShellTransition, + shouldAnimateFixedBackButton: (navEl: HTMLElement) => !navEl.closest(':is(ion-app, body).ios-theme-enable-foldable'), radius: 0, };