Skip to content

Commit 2151c88

Browse files
authored
fix(AppShell): a second shell on a page gets its own main id (#10199)
* test(AppShell): two shells on a page have unique main ids; the first keeps the fixed id (red) * fix(AppShell): a second shell on a page gets its own main id; the first keeps the fixed id useAppShellMainId lets the first mounted shell keep astryx-app-shell-main and gives each other shell a generated id, which its skip link targets. When the first shell unmounts, the next takes the fixed id. AppShell FR4 and FR5 state the rule. * fix(AppShell): a shell nested in another shell's content never takes the fixed main id AppShell provides AppShellNestingContext around its content; a nested shell always uses its generated id and does not claim, so the page's own shell keeps astryx-app-shell-main even though child layout effects run first. * Merge main into lane4/appshell-unique-main-id
1 parent 0c168a3 commit 2151c88

5 files changed

Lines changed: 151 additions & 9 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] Two `AppShell`s on one page no longer share the main region's id. The first mounted shell keeps `id="astryx-app-shell-main"`, so single-shell pages, skip links and selectors that name it keep working. Each other shell's main region gets its own generated id, which its skip link targets.
6+
7+
@cixzhang

‎docs/specs/components/AppShell/AppShell.spec.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ the only component record in the AppShell root.
174174
| FR1 | The root element carries `app-shell`, the forwarded `ref`, `data-testid`, and other DOM props, and contains, in order: the skip link, the Layout frame (header region, inline side region, content region), then any mobile drawer. | Current source and tests |
175175
| FR2 | A slot is present when its value is renderable: not `null`, `undefined`, a boolean, or an empty string. A component that renders nothing still counts as present. | Current source and public types |
176176
| FR3 | The header region renders only when `topNav` or `banner` is present; it carries `app-shell-header` and `role="banner"`, places `banner` above `topNav`, and is never inside another landmark. The sidenav-only mobile top bar (FR9) also carries `app-shell-header` and takes `role="banner"` only when no header region renders. AppShell renders at most one banner landmark. The header region pads its items by `env(safe-area-inset-top, 0px)` at the block start and the physical `env(safe-area-inset-left/right, 0px)` on its inline sides (swapped in right-to-left), while its background still reaches under the status bar and to the screen's sides; the sidenav-only mobile top bar takes the same insets when no header region renders, and only the side insets below one. In `auto`, the sidenav-only mobile top bar below a header region sticks at `env(safe-area-inset-top, 0px)`, not at the block start (FR6). | Current source and tests |
177-
| FR4 | `children` render in one region with `role="main"` and `id="astryx-app-shell-main"`, programmatically focusable so the skip link can move focus to it; programmatic focus on it draws no focus ring. Its padding is `contentPadding`, default `0`, and its block-end padding adds `env(safe-area-inset-bottom, 0px)`, in the page or in main's own scroller. The region stays border-box, and the inset it publishes to its descendants stays `contentPadding` alone, so a bleed child subtracts only `contentPadding` and the safe area survives below it. Whether it is a sequential tab stop is keyboard access to a scroll region (`spec:AST-025` FR12). Focus that Tab moves onto it while it is a stop draws the standard focus ring, inset, until focus leaves it; in `elevated` with both navs the region takes the surface's rounded top-start corner while that ring shows. | `architecture:container-padding`; current source and tests |
178-
| FR5 | The skip link is the first focusable element in the shell. It links to `#astryx-app-shell-main`, shows the translated `@astryx.appShell.skipToContent` text, is visually hidden until focused, and on activation moves focus to the main region. | WCAG 2.4.1; current source and tests |
177+
| FR4 | `children` render in one region with `role="main"` and an id: `astryx-app-shell-main` for the first mounted shell on the page, and a generated unique id for each other shell, so a page has no duplicate ids; programmatically focusable so the skip link can move focus to it; programmatic focus on it draws no focus ring. Its padding is `contentPadding`, default `0`, and its block-end padding adds `env(safe-area-inset-bottom, 0px)`, in the page or in main's own scroller. The region stays border-box, and the inset it publishes to its descendants stays `contentPadding` alone, so a bleed child subtracts only `contentPadding` and the safe area survives below it. Whether it is a sequential tab stop is keyboard access to a scroll region (`spec:AST-025` FR12). Focus that Tab moves onto it while it is a stop draws the standard focus ring, inset, until focus leaves it; in `elevated` with both navs the region takes the surface's rounded top-start corner while that ring shows. | `architecture:container-padding`; current source and tests |
178+
| FR5 | The skip link is the first focusable element in the shell. It links to its own main region's id (FR4), shows the translated `@astryx.appShell.skipToContent` text, is visually hidden until focused, and on activation moves focus to the main region. | WCAG 2.4.1; current source and tests |
179179
| FR6 | `fill` sizes the root to `100dvh` and the inline side region and main region scroll on their own. `auto` gives the root a `100dvh` minimum, makes the header sticky at the block start, keeps the inline side region sticky below the measured height of a header present when the shell mounts in `auto`, with its own scrolling, and lets the page scroll. Header height is measured only in `auto`. | Current source and tests |
180180
| FR7 | Variant backgrounds: `wash` paints body background on the frame, nav areas, and content; `surface` paints surface on all three; `section` paints surface on the frame and divides the nav areas from content, and in `auto` the header paints surface itself; `elevated` paints body background on the frame and nav areas and surface on content (FR16 for the corner). | Current source and tests |
181181
| FR8 | The mobile layout follows `mobileNav.breakpoint` under `spec:AST-012` FR8 and `architecture:theme-application` INV10; the breakpoint defaults to `md`, and `defaultIsMobile` is ignored when it is `none`. | `spec:AST-012` FR8; tests |

‎packages/core/src/AppShell/AppShell.test.tsx‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,64 @@ describe('AppShell', () => {
238238
expect(document.activeElement).toBe(main);
239239
});
240240

241+
it('gives a second shell its own main id; the first keeps the fixed id', () => {
242+
const view = render(
243+
<div>
244+
<AppShell key="first">
245+
<div>First</div>
246+
</AppShell>
247+
<AppShell key="second">
248+
<div>Second</div>
249+
</AppShell>
250+
</div>,
251+
);
252+
const [firstMain, secondMain] = screen.getAllByRole('main');
253+
const [firstSkip, secondSkip] = screen.getAllByTestId('skip-to-content');
254+
expect(firstMain).toHaveAttribute('id', 'astryx-app-shell-main');
255+
expect(secondMain.id).not.toBe('astryx-app-shell-main');
256+
expect(secondMain.id).not.toBe('');
257+
expect(document.querySelectorAll(`[id="${secondMain.id}"]`)).toHaveLength(
258+
1,
259+
);
260+
expect(firstSkip).toHaveAttribute('href', '#astryx-app-shell-main');
261+
expect(secondSkip).toHaveAttribute('href', `#${secondMain.id}`);
262+
263+
fireEvent.click(secondSkip);
264+
expect(document.activeElement).toBe(secondMain);
265+
266+
// When the first shell leaves, the remaining shell takes the fixed id.
267+
view.rerender(
268+
<div>
269+
<AppShell key="second">
270+
<div>Second</div>
271+
</AppShell>
272+
</div>,
273+
);
274+
expect(screen.getByRole('main')).toHaveAttribute(
275+
'id',
276+
'astryx-app-shell-main',
277+
);
278+
});
279+
280+
it('keeps the fixed id on the page shell when another shell is nested in its content', () => {
281+
render(
282+
<AppShell data-testid="outer">
283+
<AppShell data-testid="inner">
284+
<div>Nested</div>
285+
</AppShell>
286+
</AppShell>,
287+
);
288+
const outerMain = screen
289+
.getByTestId('outer')
290+
.querySelector<HTMLElement>(':scope [role="main"]');
291+
const innerMain = screen
292+
.getByTestId('inner')
293+
.querySelector<HTMLElement>('[role="main"]');
294+
expect(outerMain).toHaveAttribute('id', 'astryx-app-shell-main');
295+
expect(innerMain?.id).not.toBe('astryx-app-shell-main');
296+
expect(innerMain?.id).not.toBe('');
297+
});
298+
241299
it('skip link text comes from the i18n catalog', () => {
242300
render(
243301
<InternationalizationProvider

0 commit comments

Comments
 (0)