Skip to content

Commit 5a6683b

Browse files
committed
fix(tabs): assign random id in BaseTab/Panel
1 parent 28f26cb commit 5a6683b

File tree

5 files changed

+10
-13
lines changed

5 files changed

+10
-13
lines changed

.changeset/base-tab-id.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@patternfly/elements": patch
3+
---
4+
`<pf-tabs>`: improved accessibility for elements extending `BaseTab` and
5+
`BaseTabPanel` by assigning random IDs when no exists.

elements/pf-tabs/BaseTab.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LitElement, html } from 'lit';
44
import { queryAssignedElements } from 'lit/decorators/query-assigned-elements.js';
55
import { query } from 'lit/decorators/query.js';
66

7+
import { getRandomId } from '@patternfly/pfe-core/functions/random.js';
78
import { ComposedEvent } from '@patternfly/pfe-core';
89

910
import style from './BaseTab.css';
@@ -37,6 +38,7 @@ export abstract class BaseTab extends LitElement {
3738

3839
connectedCallback() {
3940
super.connectedCallback();
41+
this.id ||= getRandomId(this.localName);
4042
this.addEventListener('click', this.#clickHandler);
4143
this.#internals.role = 'tab';
4244
}

elements/pf-tabs/BaseTabPanel.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { LitElement, html } from 'lit';
22

33
import style from './BaseTabPanel.css';
44

5+
import { getRandomId } from '@patternfly/pfe-core/functions/random.js';
6+
57
export abstract class BaseTabPanel extends LitElement {
68
static readonly styles = [style];
79

@@ -17,6 +19,7 @@ export abstract class BaseTabPanel extends LitElement {
1719

1820
connectedCallback() {
1921
super.connectedCallback();
22+
this.id ||= getRandomId('pf-tab-panel');
2023
this.#internals.role = 'tabpanel';
2124
/*
2225
To make it easy for screen reader users to navigate from a tab

elements/pf-tabs/pf-tab-panel.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { customElement } from 'lit/decorators/custom-element.js';
22

3-
import { getRandomId } from '@patternfly/pfe-core/functions/random.js';
4-
53
import styles from './pf-tab-panel.css';
64

75
import { BaseTabPanel } from './BaseTabPanel.js';
@@ -16,11 +14,6 @@ import { BaseTabPanel } from './BaseTabPanel.js';
1614
@customElement('pf-tab-panel')
1715
export class PfTabPanel extends BaseTabPanel {
1816
static readonly styles = [...BaseTabPanel.styles, styles];
19-
20-
connectedCallback() {
21-
super.connectedCallback();
22-
this.id ||= getRandomId('pf-tab-panel');
23-
}
2417
}
2518

2619
declare global {

elements/pf-tabs/pf-tab.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { customElement } from 'lit/decorators/custom-element.js';
22
import { property } from 'lit/decorators/property.js';
33

44
import { observed } from '@patternfly/pfe-core/decorators.js';
5-
import { getRandomId } from '@patternfly/pfe-core/functions/random.js';
65

76
import { BaseTab } from './BaseTab.js';
87

@@ -77,11 +76,6 @@ export class PfTab extends BaseTab {
7776

7877
@observed
7978
@property({ reflect: true, type: Boolean }) disabled = false;
80-
81-
connectedCallback() {
82-
super.connectedCallback();
83-
this.id ||= getRandomId('pf-tab');
84-
}
8579
}
8680

8781
declare global {

0 commit comments

Comments
 (0)