Skip to content

Commit 9bd0874

Browse files
committed
fix(elements): table th role from context instead of dom
1 parent 1fffb2c commit 9bd0874

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

elements/pf-table/context.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createContextWithRoot } from '@patternfly/pfe-core/functions/context.js';
2+
3+
export const thRoleContext: {
4+
__context__: unknown;
5+
} = createContextWithRoot<'rowheader' | 'colheader'>('pf-th-role');

elements/pf-table/pf-table.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { customElement } from 'lit/decorators/custom-element.js';
33
import { styleMap } from 'lit/directives/style-map.js';
44
import { state } from 'lit/decorators/state.js';
55

6+
import { provide } from '@lit/context';
7+
import { thRoleContext } from './context.js';
8+
69
import { PfTh, RequestSortEvent } from './pf-th.js';
10+
import { PfTd } from './pf-td.js';
711
import { PfTr, RequestExpandEvent } from './pf-tr.js';
812

913
export * from './pf-caption.js';
@@ -14,7 +18,6 @@ export * from './pf-th.js';
1418
export * from './pf-td.js';
1519

1620
import styles from './pf-table.css';
17-
import { PfTd } from './pf-td.js';
1821

1922
const rowQuery = [
2023
':scope > pf-tbody:not([expandable]) > pf-tr',
@@ -671,6 +674,8 @@ export class PfTable extends LitElement {
671674

672675
@state() private columns = 0;
673676

677+
@provide({ context: thRoleContext }) private thRowContext = 'rowheader';
678+
674679
override connectedCallback(): void {
675680
super.connectedCallback();
676681
this.setAttribute('role', 'table');

elements/pf-table/pf-th.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { customElement } from 'lit/decorators/custom-element.js';
33
import { property } from 'lit/decorators/property.js';
44
import { classMap } from 'lit/directives/class-map.js';
55

6+
import { consume } from '@lit/context';
7+
8+
import { thRoleContext } from './context.js';
9+
610
import '@patternfly/elements/pf-button/pf-button.js';
711

812
import styles from './pf-th.css';
@@ -46,13 +50,12 @@ export class PfTh extends LitElement {
4650

4751
@property() key!: string;
4852

53+
@consume({ context: thRoleContext })
54+
private contextualRole: 'colheader' | 'rowheader' = 'rowheader';
55+
4956
override connectedCallback(): void {
5057
super.connectedCallback();
51-
const closestThead = this.closest('pf-thead');
52-
const closestTable = this.closest('pf-table');
53-
const isChildOfThead = !!closestThead && !!closestTable?.contains(closestThead);
54-
const role = isChildOfThead ? 'colheader' : 'rowheader';
55-
this.setAttribute('role', role);
58+
this.setAttribute('role', this.contextualRole);
5659
}
5760

5861
render(): TemplateResult<1> {

elements/pf-table/pf-thead.ts

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

4+
import { thRoleContext } from './context.js';
5+
46
import styles from './pf-thead.css';
7+
import { provide } from '@lit/context';
58

69
/**
710
* Table head
@@ -11,6 +14,8 @@ import styles from './pf-thead.css';
1114
export class PfThead extends LitElement {
1215
static readonly styles: CSSStyleSheet[] = [styles];
1316

17+
@provide({ context: thRoleContext }) private thRowContext = 'colheader';
18+
1419
connectedCallback(): void {
1520
super.connectedCallback();
1621
this.setAttribute('role', 'rowgroup');

0 commit comments

Comments
 (0)