Skip to content
Merged
15 changes: 15 additions & 0 deletions .changeset/29131-phone-nav-drawer-back.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@object-ui/app-shell': patch
---

fix(app-shell): on a phone, Back with the navigation drawer open closes the drawer and stays on the page

Clause-②: no
Metadata-protocol: none

Below 768 px the Console's sidebar is a drawer over the page. Back with the drawer open (the browser's Back, or Android's back gesture in a WebView host) used to leave the page the drawer was opened on. The open drawer now owns one history entry:

- **Back closes the drawer.** The URL stays the page it was on; the next Back leaves the page as before.
- **No stray entries.** Closing the drawer any other way (an outside tap, Escape, the sidebar trigger) removes its entry, so Back never stops on a page that does not change. Choosing an entry in the drawer replaces it, so Back from the new page returns to the page the drawer was opened on.
- **A reload does not reopen it.** A reload with the drawer open comes back with the drawer closed and no drawer entry left; Forward to a drawer entry reopens the drawer.
- Wider windows are unchanged: opening and closing the sidebar there writes no history.
137 changes: 137 additions & 0 deletions ui/e2e/live/phone-nav-drawer-back-29131.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { test, expect, type Page } from '@playwright/test';

/**
* Live e2e: at phone width, Back with the nav drawer open closes the drawer
* and stays on the page (objectstack-ai/objectstack#29131). Before, Back left
* the list for the page behind it.
*
* The drawer owns one history entry while it is open
* (`usePhoneNavDrawerHistory` in `@object-ui/app-shell`); the unit pins of
* every path are beside the hook, in a memory router. This file drives the
* real Console in Chromium with touch, against the real history: Back is the
* browser's own, the close is an outside tap, and the reload is a real one,
* which keeps `history.state`.
*
* In `test:e2e:live:nightly`, not the required `:ci` allowlist.
*/

const APP = 'com.example.showcase';
const LIST = `/apps/${APP}/showcase_task`;
const DRAWER = '[data-sidebar="sidebar"][data-mobile="true"]';
const TRIGGER = 'button[data-sidebar="trigger"]';
const FLAG = '__navDrawerOpen';

async function openList(page: Page) {
// A page behind the list, so Back has somewhere to go.
await page.goto('/home');
await expect(page.locator('#root')).not.toBeEmpty();
await page.goto(LIST);
await expect(page.locator(TRIGGER).first()).toBeVisible();
}

async function openDrawer(page: Page) {
await page.locator(TRIGGER).first().tap();
await expect(page.locator(DRAWER)).toBeVisible();
await expect.poll(() => drawerFlag(page)).toBe(true);
}

function drawerFlag(page: Page): Promise<boolean> {
return page.evaluate((flag) => {
const usr = (window.history.state as { usr?: Record<string, unknown> } | null)?.usr;
return !!usr && usr[flag] === true;
}, FLAG);
}

function historyLength(page: Page): Promise<number> {
return page.evaluate(() => window.history.length);
}

for (const width of [375, 390]) {
test.describe(`phone nav drawer and Back at ${width} px`, () => {
test.use({ viewport: { width, height: 844 }, isMobile: true, hasTouch: true });

test('Back with the drawer open closes it and stays on the list', async ({ page }) => {
await openList(page);
const url = page.url();
await openDrawer(page);

await page.goBack();

await expect(page.locator(DRAWER)).toBeHidden();
expect(page.url()).toBe(url);
expect(await drawerFlag(page)).toBe(false);
});

test('an outside tap closes the drawer and pops its entry: Back goes to the previous page', async ({ page }) => {
await openList(page);
const before = await historyLength(page);
await openDrawer(page);

// Right of the 288 px drawer, on the overlay.
await page.touchscreen.tap(width - 20, 420);
await expect(page.locator(DRAWER)).toBeHidden();
await expect.poll(() => drawerFlag(page)).toBe(false);
expect(page.url()).toContain(LIST);

await page.goBack();
await expect(page).toHaveURL(/\/home(?:[?#]|$)/);
// The popped entry is forward history now, not an extra stop behind the list.
expect(await historyLength(page)).toBe(before + 1);
});

test('choosing a nav entry replaces the drawer entry: Back returns to the list', async ({ page }) => {
await openList(page);
const listUrl = page.url();
await openDrawer(page);

const target = await page.evaluate(({ drawer, list }) => {
const anchors = Array.from(document.querySelectorAll<HTMLAnchorElement>(`${drawer} a[href]`));
const other = anchors.find((a) => {
const path = new URL(a.href).pathname;
return path.includes('/apps/') && !path.endsWith(list);
});
return other?.getAttribute('href') ?? null;
}, { drawer: DRAWER, list: LIST });
expect(target, 'the drawer offers another entry').toBeTruthy();

await page.locator(`${DRAWER} a[href="${target}"]`).first().tap();
await expect(page.locator(DRAWER)).toBeHidden();
await expect.poll(() => new URL(page.url()).pathname).toBe(target);

await page.goBack();
await expect.poll(() => page.url()).toBe(listUrl);
await expect(page.locator(DRAWER)).toBeHidden();
expect(await drawerFlag(page)).toBe(false);
});

test('a reload with the drawer open leaves it closed, with no flagged entry left', async ({ page }) => {
await openList(page);
await openDrawer(page);
const length = await historyLength(page);

await page.reload();
await expect(page.locator(TRIGGER).first()).toBeVisible();

await expect.poll(() => drawerFlag(page)).toBe(false);
await expect(page.locator(DRAWER)).toBeHidden();
expect(page.url()).toContain(LIST);
expect(await historyLength(page)).toBe(length);
});
});
}

test.describe('desktop sidebar', () => {
test.use({ viewport: { width: 1280, height: 800 } });

test('CONTROL — opening and closing the sidebar writes no history entry', async ({ page }) => {
await openList(page);
const url = page.url();
const length = await historyLength(page);

await page.locator(TRIGGER).first().click();
await page.locator(TRIGGER).first().click();

expect(await historyLength(page)).toBe(length);
expect(page.url()).toBe(url);
});
});
2 changes: 1 addition & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"test:e2e:ui": "playwright test --ui",
"test:e2e:live": "playwright test --config=playwright.live.config.ts",
"test:e2e:live:ci": "playwright test --config=playwright.live.config.ts screen-flow.spec.ts action-modal.spec.ts master-detail.spec.ts saved-view-filter.spec.ts studio-object-designer.spec.ts studio-editor.spec.ts record-form-direct-url.spec.ts list-row-action-cel.spec.ts",
"test:e2e:live:nightly": "playwright test --config=playwright.live.config.ts cascading-options.spec.ts declared-sharing-rules-reach-13091.spec.ts field-conditional-rules.spec.ts form-view-subforms.spec.ts grid-conditional-rules.spec.ts grid-file-upload.spec.ts grid-parent-rules.spec.ts inline-edit-polish-2572.spec.ts read-only-member-record.spec.ts record-history-display.spec.ts required-when-submit.spec.ts summary-rollup.spec.ts user-filters.spec.ts",
"test:e2e:live:nightly": "playwright test --config=playwright.live.config.ts cascading-options.spec.ts declared-sharing-rules-reach-13091.spec.ts field-conditional-rules.spec.ts form-view-subforms.spec.ts grid-conditional-rules.spec.ts grid-file-upload.spec.ts grid-parent-rules.spec.ts inline-edit-polish-2572.spec.ts read-only-member-record.spec.ts record-history-display.spec.ts required-when-submit.spec.ts summary-rollup.spec.ts user-filters.spec.ts phone-nav-drawer-back-29131.spec.ts",
"test:e2e:import-harness": "playwright test --config=playwright.import-harness.config.ts",
"test:e2e:import-console": "playwright test --config=playwright.import-console.config.ts"
},
Expand Down
2 changes: 2 additions & 0 deletions ui/packages/app-shell/src/layout/ConsoleLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ConsoleNotificationBanners } from './ConsoleNotificationBanners.js';
import { UnifiedSidebar } from './UnifiedSidebar.js';
import { AppHeader } from './AppHeader.js';
import { useResponsiveSidebar } from '../hooks/useResponsiveSidebar.js';
import { usePhoneNavDrawerHistory } from './usePhoneNavDrawerHistory.js';
import { usePhoneViewport } from '../hooks/usePhoneViewport.js';
import { primeConsoleSidebarOpen } from './consoleSidebarRail.js';
import { useAiSurfaceEnabled } from '../hooks/useAiSurface.js';
Expand Down Expand Up @@ -131,6 +132,7 @@ export function isRecordPagePath(pathname: string): boolean {
/** Inner component that can access SidebarProvider context */
function ConsoleLayoutInner({ children }: { children: React.ReactNode }) {
useResponsiveSidebar();
usePhoneNavDrawerHistory();
useAdvertiseShortcut(TOGGLE_SIDEBAR_SHORTCUT);
return <>{children}</>;
}
Expand Down
Loading
Loading