Skip to content

Commit

Permalink
feat(table): add hide caption visually attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jialin-he committed Mar 12, 2024
1 parent 9bef87b commit 10aa6bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/pharos/src/components/table/pharos-table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@
height: var(--pharos-spacing-one-and-a-half-x);
}
}

.hide-table-caption {
visibility: hidden;
pointer-events: none;
position: absolute;
}
15 changes: 14 additions & 1 deletion packages/pharos/src/components/table/pharos-table.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PharosElement } from '../base/pharos-element';
import ScopedRegistryMixin from '../../utils/mixins/scoped-registry';
import { html, nothing } from 'lit';
import { classMap } from 'lit/directives/class-map.js';
import { state } from 'lit/decorators.js';
import type { TemplateResult, CSSResultArray } from 'lit';
import { property } from 'lit/decorators.js';
Expand Down Expand Up @@ -80,6 +81,9 @@ export class PharosTable extends ScopedRegistryMixin(PharosElement) {
@property({ type: String, reflect: true, attribute: 'caption' })
public caption: string = '';

@property({ type: String, reflect: true, attribute: 'hide-caption-visually' })
public hideCaptionVisually: boolean = true;

@state()
private _pageSize = 50;

Expand All @@ -93,6 +97,11 @@ export class PharosTable extends ScopedRegistryMixin(PharosElement) {
}

protected override updated(): void {
if (!this.caption) {
throw new Error(
`Table must have an accessible name. Please provide a caption for the table using the caption attribute. You can hide it visually by setting hide-caption-visually to be true.`
);
}
this._pageSize = this.hidePagination ? this.rowData.length : this._pageSize;
this.totalResults =
this.hidePagination || !this.totalResults ? this.rowData.length : this.totalResults;
Expand Down Expand Up @@ -190,7 +199,11 @@ export class PharosTable extends ScopedRegistryMixin(PharosElement) {
protected override render(): TemplateResult {
return html`
<table class="table">
<caption>
<caption
class="${classMap({
[`hide-table-caption`]: this.hideCaptionVisually,
})}"
>
${this.caption}
</caption>
<thead>
Expand Down

0 comments on commit 10aa6bb

Please sign in to comment.