diff --git a/.changeset/five-tips-camp.md b/.changeset/five-tips-camp.md new file mode 100644 index 000000000..2f87bd588 --- /dev/null +++ b/.changeset/five-tips-camp.md @@ -0,0 +1,5 @@ +--- +'@lion/tabs': patch +--- + +Only look for tabs and panels as direct children, this allows to have nested tabs. diff --git a/packages/tabs/src/LionTabs.js b/packages/tabs/src/LionTabs.js index 091a2a090..f6e0e5bd6 100644 --- a/packages/tabs/src/LionTabs.js +++ b/packages/tabs/src/LionTabs.js @@ -184,12 +184,12 @@ export class LionTabs extends LitElement { __setupStore() { /** @type {StoreEntry[]} */ this.__store = []; - const buttons = /** @type {HTMLElement[]} */ (Array.from( - this.querySelectorAll('[slot="tab"]'), - )); - const panels = /** @type {HTMLElement[]} */ (Array.from( - this.querySelectorAll('[slot="panel"]'), - )); + const buttons = /** @type {HTMLElement[]} */ (Array.from(this.children)).filter( + child => child.slot === 'tab', + ); + const panels = /** @type {HTMLElement[]} */ (Array.from(this.children)).filter( + child => child.slot === 'panel', + ); if (buttons.length !== panels.length) { // eslint-disable-next-line no-console console.warn( diff --git a/packages/tabs/test/lion-tabs.test.js b/packages/tabs/test/lion-tabs.test.js index 4ac7fe8dd..56d531238 100644 --- a/packages/tabs/test/lion-tabs.test.js +++ b/packages/tabs/test/lion-tabs.test.js @@ -80,6 +80,25 @@ describe('', () => { ); stub.restore(); }); + + it('only takes direct children into account', async () => { + const el = /** @type {LionTabs} */ (await fixture(html` + + +
+ +
nested panel
+
+ +
panel 2
+
+ `)); + el.selectedIndex = 1; + const selectedTab = /** @type {Element} */ (Array.from(el.children).find( + child => child.slot === 'tab' && child.hasAttribute('selected'), + )); + expect(selectedTab.textContent).to.equal('tab 2'); + }); }); describe('Tabs ([slot=tab])', () => {