Skip to content

Commit

Permalink
fix(sbb-tab-group): fix nested tab groups logic (#2816)
Browse files Browse the repository at this point in the history
Closes #2814

---------

Co-authored-by: Lukas Spirig <[email protected]>
Co-authored-by: Jeremias Peier <[email protected]>
  • Loading branch information
3 people authored Jun 25, 2024
1 parent 1ec3cb5 commit 4674a61
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ snapshots["sbb-tab-group renders DOM"] =
role="tabpanel"
tabindex="0"
>
<p>
Test tab content 1
</p>
Test tab content 1
</sbb-tab>
<sbb-tab-label
aria-controls="sbb-tab-panel-2"
Expand All @@ -41,9 +39,7 @@ snapshots["sbb-tab-group renders DOM"] =
role="tabpanel"
tabindex="0"
>
<p>
Test tab content 2
</p>
Test tab content 2
</sbb-tab>
<sbb-tab-label
aria-controls="sbb-tab-panel-3"
Expand All @@ -62,9 +58,7 @@ snapshots["sbb-tab-group renders DOM"] =
role="tabpanel"
tabindex="0"
>
<p>
Test tab content 3
</p>
Test tab content 3
</sbb-tab>
<sbb-tab-label
aria-controls="sbb-tab-panel-4"
Expand All @@ -82,9 +76,7 @@ snapshots["sbb-tab-group renders DOM"] =
role="tabpanel"
tabindex="0"
>
<p>
Test tab content 4
</p>
Test tab content 4
</sbb-tab>
</sbb-tab-group>
`;
Expand Down Expand Up @@ -173,7 +165,7 @@ snapshots["sbb-tab-group renders A11y tree Firefox"] =
"children": [
{
"role": "text leaf",
"name": "Test tab content 1"
"name": "Test tab content 1 "
}
]
}
Expand Down
21 changes: 7 additions & 14 deletions src/elements/tabs/tab-group/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@ tab panels can present different sections of content and include text, images, f
```html
<sbb-tab-group>
<sbb-tab-label>I am the first</sbb-tab-label>
<sbb-tab>
<p>Tab content 1</p>
</sbb-tab>
<sbb-tab> Tab content 1 </sbb-tab>

<sbb-tab-label>I am the second</sbb-tab-label>
<sbb-tab>
<p>Tab content 2</p>
</sbb-tab>
<sbb-tab> Tab content 2 </sbb-tab>

<sbb-tab-label>I am the third</sbb-tab-label>
<sbb-tab>
<p>Tab content 3</p>
</sbb-tab>
<sbb-tab> Tab content 3 </sbb-tab>
</sbb-tab-group>
```

To display a tab label within the tab bar, provide a `sbb-tab-label` right before its related tab content;
please refer to the [sbb-tab-label](/docs/elements-sbb-tab-sbb-tab-label--docs) documentation for more details.

The content element must be wrapped in a `sbb-tab` and placed right after its relative `sbb-tab-label`.
Tab groups can also be nested, which means that a tab's content block can be represented by another `sbb-tab-group`,
Tab groups can also be nested, which means that a `sbb-tab` can contain another `sbb-tab-group`,
as shown in the "Nested Tab Groups" example.

## States
Expand Down Expand Up @@ -84,7 +78,6 @@ type SbbTabChangedEventDetails = {

## Slots

| Name | Description |
| --------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| | Use the unnamed slot to add html-content to the `sbb-tab-group`; wrap the content in a `sbb-tab` or provide a nested `sbb-tab-group`. |
| `tab-bar` | When you provide the `sbb-tab-label` tag through the unnamed slot, it will be automatically moved to this slot. You do not need to use it directly. |
| Name | Description |
| ---- | ------------------------------------------------------------------------------------------------------- |
| | Use the unnamed slot to add content to the `sbb-tab-group` via `sbb-tab-label` and `sbb-tab` instances. |
8 changes: 0 additions & 8 deletions src/elements/tabs/tab-group/tab-group.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,3 @@
@include sbb.focus-outline;
}
}

// Make inactive nested tab groups non-focusable, to ensure accessibility
:host([data-nested]:not([active])) *,
:host([data-nested]:not([active])) ::slotted(*) {
visibility: hidden;
opacity: 0;
height: 0;
}
16 changes: 4 additions & 12 deletions src/elements/tabs/tab-group/tab-group.snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,13 @@ describe(`sbb-tab-group`, () => {
element = await fixture(
html`<sbb-tab-group initial-selected-index="0">
<sbb-tab-label>Test tab label 1</sbb-tab-label>
<sbb-tab>
<p>Test tab content 1</p>
</sbb-tab>
<sbb-tab> Test tab content 1 </sbb-tab>
<sbb-tab-label>Test tab label 2</sbb-tab-label>
<sbb-tab>
<p>Test tab content 2</p>
</sbb-tab>
<sbb-tab> Test tab content 2 </sbb-tab>
<sbb-tab-label disabled>Test tab label 3</sbb-tab-label>
<sbb-tab>
<p>Test tab content 3</p>
</sbb-tab>
<sbb-tab> Test tab content 3 </sbb-tab>
<sbb-tab-label>Test tab label 4</sbb-tab-label>
<sbb-tab>
<p>Test tab content 4</p>
</sbb-tab>
<sbb-tab> Test tab content 4 </sbb-tab>
</sbb-tab-group>`,
);
});
Expand Down
24 changes: 8 additions & 16 deletions src/elements/tabs/tab-group/tab-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ describe(`sbb-tab-group`, () => {
element = await fixture(
html`<sbb-tab-group initial-selected-index="1">
<sbb-tab-label id="sbb-tab-1">Test tab label 1</sbb-tab-label>
<sbb-tab><p>Test tab content 1</p></sbb-tab>
<sbb-tab>Test tab content 1</sbb-tab>
<sbb-tab-label id="sbb-tab-2">Test tab label 2</sbb-tab-label>
<sbb-tab><p>Test tab content 2</p></sbb-tab>
<sbb-tab>Test tab content 2</sbb-tab>
<sbb-tab-label id="sbb-tab-3" disabled>Test tab label 3</sbb-tab-label>
<sbb-tab><p>Test tab content 3</p></sbb-tab>
<sbb-tab>Test tab content 3</sbb-tab>
<sbb-tab-label id="sbb-tab-4">Test tab label 4</sbb-tab-label>
<sbb-tab><p>Test tab content 4</p></sbb-tab>
<sbb-tab>Test tab content 4</sbb-tab>
</sbb-tab-group>`,
);
});
Expand Down Expand Up @@ -153,13 +153,9 @@ describe(`sbb-tab-group`, () => {
element = await fixture(
html` <sbb-tab-group initial-selected-index="2">
<sbb-tab-label id="sbb-tab-1">Test tab label 1</sbb-tab-label>
<sbb-tab>
<p>Test tab content 1</p>
</sbb-tab>
<sbb-tab> Test tab content 1 </sbb-tab>
<sbb-tab-label id="sbb-tab-2">Test tab label 2</sbb-tab-label>
<sbb-tab>
<p>Test tab content 2</p>
</sbb-tab>
<sbb-tab> Test tab content 2 </sbb-tab>
</sbb-tab-group>`,
);
const tab = element.querySelector('sbb-tab-label#sbb-tab-1');
Expand All @@ -170,13 +166,9 @@ describe(`sbb-tab-group`, () => {
element = await fixture(
html` <sbb-tab-group initial-selected-index="0">
<sbb-tab-label disabled>Test tab label 1</sbb-tab-label>
<sbb-tab>
<p>Test tab content 1</p>
</sbb-tab>
<sbb-tab> Test tab content 1 </sbb-tab>
<sbb-tab-label id="sbb-tab-2">Test tab label 2</sbb-tab-label>
<sbb-tab>
<p>Test tab content 2</p>
</sbb-tab>
<sbb-tab> Test tab content 2 </sbb-tab>
</sbb-tab-group>`,
);
const tab = element.querySelector('sbb-tab-label#sbb-tab-2');
Expand Down
16 changes: 4 additions & 12 deletions src/elements/tabs/tab-group/tab-group.ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,13 @@ describe(`sbb-tab-group ssr`, () => {
root = await ssrHydratedFixture(
html` <sbb-tab-group initial-selected-index="1">
<sbb-tab-label id="sbb-tab-1">Test tab label 1</sbb-tab-label>
<sbb-tab>
<p>Test tab content 1</p>
</sbb-tab>
<sbb-tab> Test tab content 1 </sbb-tab>
<sbb-tab-label id="sbb-tab-2">Test tab label 2</sbb-tab-label>
<sbb-tab>
<p>Test tab content 2</p>
</sbb-tab>
<sbb-tab> Test tab content 2 </sbb-tab>
<sbb-tab-label id="sbb-tab-3" disabled>Test tab label 3</sbb-tab-label>
<sbb-tab>
<p>Test tab content 3</p>
</sbb-tab>
<sbb-tab> Test tab content 3 </sbb-tab>
<sbb-tab-label id="sbb-tab-4">Test tab label 4</sbb-tab-label>
<sbb-tab>
<p>Test tab content 4</p>
</sbb-tab>
<sbb-tab> Test tab content 4 </sbb-tab>
</sbb-tab-group>`,
{ modules: ['./tab-group.js', '../tab-label.js', '../tab.js'] },
);
Expand Down
34 changes: 15 additions & 19 deletions src/elements/tabs/tab-group/tab-group.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,10 @@ const tabPanelTwo = (): TemplateResult => html`

const tabPanelFour = (): TemplateResult => html`
<sbb-tab>
<p>
Diam maecenas ultricies mi eget mauris pharetra et ultrices neque ornare aenean euismod
elementum nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus
urna neque viverra justo nec ultrices dui sapien eget mi proin sed libero enim sed faucibus
turpis in eu mi bibendum neque egestas congue.
</p>
Diam maecenas ultricies mi eget mauris pharetra et ultrices neque ornare aenean euismod
elementum nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus mattis rhoncus
urna neque viverra justo nec ultrices dui sapien eget mi proin sed libero enim sed faucibus
turpis in eu mi bibendum neque egestas congue.
</sbb-tab>
`;

Expand Down Expand Up @@ -116,34 +114,32 @@ const IconsAndNumbersTemplate = ({ size, label, ...args }: Args): TemplateResult
const NestedTemplate = ({ size, label, ...args }: Args): TemplateResult => html`
<sbb-tab-group size=${size} initial-selected-index="0">
${firstTabTitle(label, args)}
<sbb-tab-group size=${size} initial-selected-index="1">
<sbb-tab-label level="2">Nested tab</sbb-tab-label>
<sbb-tab>
<p>
<sbb-tab>
<sbb-tab-group size=${size} initial-selected-index="1">
<sbb-tab-label level="2">Nested tab</sbb-tab-label>
<sbb-tab>
Diam maecenas ultricies mi eget mauris pharetra et ultrices neque ornare aenean euismod
elementum nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus mattis
rhoncus urna neque viverra justo nec ultrices dui sapien eget mi proin sed libero enim sed
faucibus turpis in eu mi bibendum neque egestas congue.
</p>
</sbb-tab>
</sbb-tab>
<sbb-tab-label level="2">Nested tab</sbb-tab-label>
<sbb-tab>
<p>
<sbb-tab-label level="2">Nested tab</sbb-tab-label>
<sbb-tab>
Diam maecenas ultricies mi eget mauris pharetra et ultrices neque ornare aenean euismod
elementum nisi quis eleifend quam adipiscing vitae proin sagittis nisl rhoncus mattis
rhoncus urna.
</p>
</sbb-tab>
</sbb-tab-group>
</sbb-tab>
</sbb-tab-group>
</sbb-tab>
<sbb-tab-label amount=${args.amount} icon-name="swisspass-small"> Tab title two </sbb-tab-label>
${tabPanelTwo()}
<sbb-tab-label ?disabled=${true} amount=${args.amount} icon-name="train-small">
Tab title three
</sbb-tab-label>
<sbb-tab><p>I was disabled.</p></sbb-tab>
<sbb-tab>I was disabled.</sbb-tab>
<sbb-tab-label amount=${args.amount} icon-name="pie-small"> Tab title four </sbb-tab-label>
${tabPanelFour()}
Expand Down
Loading

0 comments on commit 4674a61

Please sign in to comment.