Skip to content

Commit f57713f

Browse files
authored
feat(ObjectPage): improve (single-section) accessibility (#9093)
- ObjectPage section and subsections now implicitly set their accessible name via `titleText` string. - OPs with single section (no tabbar) do not show a focus outline or enable tabbing to the section. Subsection focus/tabbing is still supported. Closes #9080
1 parent 9d413b6 commit f57713f

7 files changed

Lines changed: 163 additions & 71 deletions

File tree

‎packages/main/src/components/ObjectPage/ObjectPage.stories.tsx‎

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export const Default: Story = {
167167
return (
168168
// `onBeforeNavigate` & `onToggleHeaderArea` have to be added like this, otherwise Storybook invokes them incorrectly sometimes
169169
<ObjectPage {...args} onBeforeNavigate={args.onBeforeNavigate} onToggleHeaderArea={args.onToggleHeaderArea}>
170-
<ObjectPageSection titleText="Goals" id="goals" aria-label="Goals">
170+
<ObjectPageSection titleText="Goals" id="goals">
171171
<Form layout="S1 M2 L3 XL3" labelSpan="S12 M12 L12 XL12">
172172
<FormItem labelContent={<Label showColon>Evangelize the UI framework across the company</Label>}>
173173
<Text>4 days overdue - Cascaded</Text>
@@ -180,11 +180,10 @@ export const Default: Story = {
180180
</FormItem>
181181
</Form>
182182
</ObjectPageSection>
183-
<ObjectPageSection titleText="Personal" id="personal" aria-label="Personal">
183+
<ObjectPageSection titleText="Personal" id="personal">
184184
<ObjectPageSubSection
185185
titleText="Connect"
186186
id="personal-connect"
187-
aria-label="Connect"
188187
actions={
189188
<>
190189
<Button design={ButtonDesign.Emphasized} style={{ minWidth: '120px' }}>
@@ -225,11 +224,7 @@ export const Default: Story = {
225224
</FormGroup>
226225
</Form>
227226
</ObjectPageSubSection>
228-
<ObjectPageSubSection
229-
titleText="Payment Information"
230-
id="personal-payment-information"
231-
aria-label="Payment Information"
232-
>
227+
<ObjectPageSubSection titleText="Payment Information" id="personal-payment-information">
233228
<Form>
234229
<FormGroup headerText="Salary">
235230
<FormItem labelContent={<Label showColon>Bank Transfer</Label>}>
@@ -244,12 +239,8 @@ export const Default: Story = {
244239
</Form>
245240
</ObjectPageSubSection>
246241
</ObjectPageSection>
247-
<ObjectPageSection titleText="Employment" id="employment" aria-label="Employment">
248-
<ObjectPageSubSection
249-
titleText="Job Information"
250-
id="employment-job-information"
251-
aria-label="Job Information"
252-
>
242+
<ObjectPageSection titleText="Employment" id="employment">
243+
<ObjectPageSubSection titleText="Job Information" id="employment-job-information">
253244
<Form>
254245
<FormItem labelContent={<Label showColon>Job Classification</Label>}>
255246
<FlexBox direction={FlexBoxDirection.Column}>
@@ -277,11 +268,7 @@ export const Default: Story = {
277268
</FormItem>
278269
</Form>
279270
</ObjectPageSubSection>
280-
<ObjectPageSubSection
281-
titleText="Employee Details"
282-
id="employment-employee-details"
283-
aria-label="Employee Details"
284-
>
271+
<ObjectPageSubSection titleText="Employee Details" id="employment-employee-details">
285272
<Form>
286273
<FormItem labelContent={<Label showColon>Start Date</Label>}>
287274
<Text>Jan 01, 2018</Text>
@@ -303,11 +290,7 @@ export const Default: Story = {
303290
</FormItem>
304291
</Form>
305292
</ObjectPageSubSection>
306-
<ObjectPageSubSection
307-
titleText="Job Relationship"
308-
id="employment-job-relationship"
309-
aria-label="Job Relationship"
310-
>
293+
<ObjectPageSubSection titleText="Job Relationship" id="employment-job-relationship">
311294
<Form>
312295
<FormItem labelContent={<Label showColon>Manager</Label>}>
313296
<Text>John Doe</Text>
@@ -346,26 +329,18 @@ export const SectionWithCustomHeader: Story = {
346329
render(args) {
347330
return (
348331
<ObjectPage {...args} onBeforeNavigate={args.onBeforeNavigate}>
349-
<ObjectPageSection
350-
titleText="Goals"
351-
hideTitleText
352-
id="goals"
353-
aria-label="Goals"
354-
header={<Title>Custom Header Section One</Title>}
355-
>
332+
<ObjectPageSection titleText="Goals" hideTitleText id="goals" header={<Title>Custom Header Section One</Title>}>
356333
<div style={{ width: '100%', height: '200px', background: 'cadetblue' }} />
357334
</ObjectPageSection>
358335
<ObjectPageSection
359336
titleText="Personal"
360337
hideTitleText
361338
id="personal"
362-
aria-label="Personal"
363339
header={<MessageStrip hideCloseButton>Custom Header Section Two</MessageStrip>}
364340
>
365341
<ObjectPageSubSection
366342
titleText="Connect"
367343
id="personal-connect"
368-
aria-label="Connect"
369344
actions={
370345
<>
371346
<Button design={ButtonDesign.Emphasized} style={{ minWidth: '120px' }}>
@@ -406,11 +381,7 @@ export const SectionWithCustomHeader: Story = {
406381
</FormGroup>
407382
</Form>
408383
</ObjectPageSubSection>
409-
<ObjectPageSubSection
410-
titleText="Payment Information"
411-
id="personal-payment-information"
412-
aria-label="Payment Information"
413-
>
384+
<ObjectPageSubSection titleText="Payment Information" id="personal-payment-information">
414385
<Form>
415386
<FormGroup headerText="Salary">
416387
<FormItem labelContent={<Label showColon>Bank Transfer</Label>}>
@@ -429,7 +400,6 @@ export const SectionWithCustomHeader: Story = {
429400
titleText="Employment"
430401
hideTitleText
431402
id="employment"
432-
aria-label="Employment"
433403
header={
434404
<div style={{ display: 'flex', alignItems: 'center' }}>
435405
<Icon name={sunIcon} style={{ marginInlineEnd: '2rem' }} />
@@ -538,12 +508,8 @@ export const SingleSection: Story = {
538508
render(args) {
539509
return (
540510
<ObjectPage {...args}>
541-
<ObjectPageSection titleText="Employment" id="employment" aria-label="Employment">
542-
<ObjectPageSubSection
543-
titleText="Job Information"
544-
id="employment-job-information"
545-
aria-label="Job Information"
546-
>
511+
<ObjectPageSection titleText="Employment" id="employment">
512+
<ObjectPageSubSection titleText="Job Information" id="employment-job-information">
547513
<Form>
548514
<FormItem labelContent={<Label showColon>Job Classification</Label>}>
549515
<FlexBox direction={FlexBoxDirection.Column}>
@@ -571,11 +537,7 @@ export const SingleSection: Story = {
571537
</FormItem>
572538
</Form>
573539
</ObjectPageSubSection>
574-
<ObjectPageSubSection
575-
titleText="Employee Details"
576-
id="employment-employee-details"
577-
aria-label="Employee Details"
578-
>
540+
<ObjectPageSubSection titleText="Employee Details" id="employment-employee-details">
579541
<Form>
580542
<FormItem labelContent={<Label showColon>Start Date</Label>}>
581543
<Text>Jan 01, 2018</Text>
@@ -597,11 +559,7 @@ export const SingleSection: Story = {
597559
</FormItem>
598560
</Form>
599561
</ObjectPageSubSection>
600-
<ObjectPageSubSection
601-
titleText="Job Relationship"
602-
id="employment-job-relationship"
603-
aria-label="Job Relationship"
604-
>
562+
<ObjectPageSubSection titleText="Job Relationship" id="employment-job-relationship">
605563
<Form>
606564
<FormItem labelContent={<Label showColon>Manager</Label>}>
607565
<Text>John Doe</Text>

‎packages/main/src/components/ObjectPage/context.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { createContext, useContext } from 'react';
22
import { ObjectPageMode } from '../../enums/ObjectPageMode.js';
33
import type { ObjectPagePropTypes } from './types/index.js';
44

5-
export const ObjectPageContext = createContext<ObjectPagePropTypes['mode']>(ObjectPageMode.Default);
5+
interface ObjectPageContextValue {
6+
mode: ObjectPagePropTypes['mode'];
7+
hasOnlySingleSection: boolean;
8+
}
9+
10+
export const ObjectPageContext = createContext<ObjectPageContextValue>({
11+
mode: ObjectPageMode.Default,
12+
hasOnlySingleSection: false,
13+
});
614

715
export const useObjectPageContext = () => useContext(ObjectPageContext);

‎packages/main/src/components/ObjectPage/index.tsx‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const ObjectPage = forwardRef<ObjectPageDomRef, ObjectPagePropTypes>((props, ref
9898
const [tabSelectId, setTabSelectId] = useState<null | string>(null);
9999
const titleAreaInteractive = headerArea && !preserveHeaderStateOnClick;
100100
const hasOnlySingleSection = Children.count(children) === 1;
101+
const objectPageContextValue = useMemo(() => ({ mode, hasOnlySingleSection }), [mode, hasOnlySingleSection]);
101102
const tabContainerHeaderHeight = hasOnlySingleSection ? 1 : TAB_CONTAINER_HEADER_HEIGHT;
102103

103104
const isProgrammaticallyScrolled = useRef(false);
@@ -716,15 +717,24 @@ const ObjectPage = forwardRef<ObjectPageDomRef, ObjectPagePropTypes>((props, ref
716717
}
717718

718719
useEffect(() => {
719-
if (isMounted && children && mode === ObjectPageMode.Default) {
720+
if (isMounted && children && (mode === ObjectPageMode.Default || hasOnlySingleSection)) {
720721
const firstSection: HTMLElement = objectPageContentRef.current.querySelector(
721722
'[data-component-name="ObjectPageSection"]',
722723
);
723724
if (firstSection) {
724-
firstSection.tabIndex = 0;
725+
if (hasOnlySingleSection) {
726+
const firstSubSection = firstSection.querySelector<HTMLElement>(
727+
'[data-component-name="ObjectPageSubSection"]',
728+
);
729+
if (firstSubSection) {
730+
firstSubSection.tabIndex = 0;
731+
}
732+
} else if (mode === ObjectPageMode.Default) {
733+
firstSection.tabIndex = 0;
734+
}
725735
}
726736
}
727-
}, [isMounted, children, mode]);
737+
}, [isMounted, children, mode, hasOnlySingleSection]);
728738

729739
const handleContentBlur: FocusEventHandler<HTMLDivElement> = (e) => {
730740
const opNode = objectPageRef.current;
@@ -746,7 +756,7 @@ const ObjectPage = forwardRef<ObjectPageDomRef, ObjectPagePropTypes>((props, ref
746756
};
747757

748758
return (
749-
<ObjectPageContext.Provider value={mode}>
759+
<ObjectPageContext.Provider value={objectPageContextValue}>
750760
<div
751761
ref={componentRef}
752762
data-component-name="ObjectPage"

‎packages/main/src/components/ObjectPage/test/ObjectPage.gallery.tsx‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Title } from '../../../webComponents/Title/index.js';
55
import { FlexBox } from '../../FlexBox/index.js';
66
import { ObjectPageHeader } from '../../ObjectPageHeader/index.js';
77
import { ObjectPageSection } from '../../ObjectPageSection/index.js';
8+
import { ObjectPageSubSection } from '../../ObjectPageSubSection/index.js';
89
import { ObjectPageTitle } from '../../ObjectPageTitle/index.js';
910
import { ObjectPage } from '../index.js';
1011

@@ -114,3 +115,54 @@ export const ObjectPageShortSectionTabSwitchTestComp = () => {
114115
</ObjectPage>
115116
);
116117
};
118+
119+
export const ObjectPageAccessibleNameTestComp = () => {
120+
return (
121+
<>
122+
<span id="external-section-label">External section label</span>
123+
<ObjectPageSection id="automatic" titleText="Automatic section">
124+
<ObjectPageSubSection id="automatic-subsection" titleText="Automatic subsection">
125+
Content
126+
</ObjectPageSubSection>
127+
</ObjectPageSection>
128+
<ObjectPageSection id="aria-label" titleText="Section with aria-label" aria-label="Custom section label">
129+
Content
130+
</ObjectPageSection>
131+
<ObjectPageSection
132+
id="aria-labelledby"
133+
titleText="Section with aria-labelledby"
134+
aria-labelledby="external-section-label"
135+
>
136+
Content
137+
</ObjectPageSection>
138+
</>
139+
);
140+
};
141+
142+
export const ObjectPageSingleSectionTestComp = () => {
143+
return (
144+
<>
145+
<ObjectPage>
146+
<ObjectPageSection id="single" titleText="Single section">
147+
<ObjectPageSubSection id="first" titleText="First subsection">
148+
First subsection content
149+
</ObjectPageSubSection>
150+
<ObjectPageSubSection id="second" titleText="Second subsection">
151+
Second subsection content
152+
</ObjectPageSubSection>
153+
</ObjectPageSection>
154+
</ObjectPage>
155+
<button type="button">After ObjectPage</button>
156+
</>
157+
);
158+
};
159+
160+
export const ObjectPageSingleSectionIconTabBarTestComp = () => {
161+
return (
162+
<ObjectPage mode={ObjectPageMode.IconTabBar}>
163+
<ObjectPageSection id="single" titleText="Single section">
164+
Single section content
165+
</ObjectPageSection>
166+
</ObjectPage>
167+
);
168+
};

‎packages/main/src/components/ObjectPage/test/ObjectPage.spec.tsx‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,53 @@ test.describe('ObjectPage', () => {
126126
await page.getByRole('tab', { name: 'Short' }).click();
127127
await expect.poll(topSpacerHeight).toBe(0);
128128
});
129+
130+
test('uses titleText as the accessible name and respects explicit names', async ({ mount, page }) => {
131+
await mount('ObjectPage/ObjectPageAccessibleNameTestComp');
132+
133+
const automaticSection = page.getByRole('region', { name: 'Automatic section' });
134+
const automaticSubsection = page.getByRole('region', { name: 'Automatic subsection' });
135+
136+
await expect(automaticSection).toHaveAttribute('aria-label', 'Automatic section');
137+
await expect(automaticSubsection).toHaveAttribute('aria-label', 'Automatic subsection');
138+
139+
await expect(page.getByRole('region', { name: 'Custom section label' })).toHaveAttribute(
140+
'aria-label',
141+
'Custom section label',
142+
);
143+
await expect(page.getByRole('region', { name: 'External section label' })).toHaveAttribute(
144+
'aria-labelledby',
145+
'external-section-label',
146+
);
147+
});
148+
149+
test('keeps only a single section out of the tab order', async ({ mount, page }) => {
150+
await mount('ObjectPage/ObjectPageSingleSectionTestComp');
151+
152+
const section = page.getByRole('region', { name: 'Single section' });
153+
const firstSubSection = page.getByRole('region', { name: 'First subsection' });
154+
const secondSubSection = page.getByRole('region', { name: 'Second subsection' });
155+
156+
expect(await section.getAttribute('tabindex')).toBeNull();
157+
await expect(section).toHaveAttribute('aria-label', 'Single section');
158+
await expect(firstSubSection).toHaveAttribute('tabindex', '0');
159+
await expect(page.locator('[data-component-name="ObjectPageTabContainer"]')).toHaveCount(0);
160+
161+
await firstSubSection.focus();
162+
await page.keyboard.press('ArrowDown');
163+
await expect(secondSubSection).toBeFocused();
164+
await page.keyboard.press('Tab');
165+
await expect(page.getByRole('button', { name: 'After ObjectPage' })).toBeFocused();
166+
await page.keyboard.press('Shift+Tab');
167+
await expect(secondSubSection).toBeFocused();
168+
expect(await section.getAttribute('tabindex')).toBeNull();
169+
170+
await mount('ObjectPage/ObjectPageSingleSectionIconTabBarTestComp');
171+
expect(await page.getByRole('region', { name: 'Single section' }).getAttribute('tabindex')).toBeNull();
172+
await expect(page.locator('[data-component-name="ObjectPageTabContainer"]')).toHaveCount(0);
173+
174+
await mount('ObjectPage/ObjectPageFitContentTabSwitchTestComp');
175+
await expect(page.getByRole('region', { name: 'Fit 1' })).toHaveAttribute('tabindex', '0');
176+
await expect(page.locator('[data-component-name="ObjectPageTabContainer"]').first()).toBeVisible();
177+
});
129178
});

0 commit comments

Comments
 (0)