-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathpf-jump-links.ts
More file actions
138 lines (112 loc) · 4.32 KB
/
pf-jump-links.ts
File metadata and controls
138 lines (112 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { html, LitElement, type TemplateResult } from 'lit';
import { customElement } from 'lit/decorators/custom-element.js';
import { property } from 'lit/decorators/property.js';
import { ScrollSpyController } from '@patternfly/pfe-core/controllers/scroll-spy-controller.js';
import { RovingTabindexController } from '@patternfly/pfe-core/controllers/roving-tabindex-controller.js';
import { PfJumpLinksItem } from './pf-jump-links-item.js';
import '@patternfly/elements/pf-icon/pf-icon.js';
import style from './pf-jump-links.css';
/**
* **Jump links** allow users to navigate to sections within a page.
* @alias Jump Links
* @fires toggle - when the `expanded` disclosure widget is toggled
*/
@customElement('pf-jump-links')
export class PfJumpLinks extends LitElement {
static readonly styles: CSSStyleSheet[] = [style];
/** Whether the element features a disclosure widget around the nav items */
@property({ reflect: true, type: Boolean }) expandable = false;
/** Whether the expandable element's disclosure widget is expanded */
@property({ reflect: true, type: Boolean }) expanded = false;
/** Whether the layout of children is vertical or horizontal. */
@property({ reflect: true, type: Boolean }) vertical = false;
/** Whether to center children. */
@property({ reflect: true, type: Boolean }) centered = false;
/** Offset to add to the scroll position, potentially for a masthead which content scrolls under. */
@property({ type: Number }) offset = 0;
/** Label to add to nav element. Required for accessibility. */
@property({ reflect: true }) label = 'Jump to section';
#kids = this.querySelectorAll?.<LitElement>(':is(pf-jump-links-item, pf-jump-links-list)');
get #items() {
return Array.from(this.#kids ?? [])
.flatMap(i => [
...i.shadowRoot?.querySelectorAll?.('a') ?? [],
...i.querySelectorAll?.('a') ?? [],
]);
}
#tabindex = RovingTabindexController.of<HTMLAnchorElement>(this, {
getItems: () => this.#items,
});
#spy = new ScrollSpyController(this, {
rootMargin: `${this.offset}px 0px 0px 0px`,
tagNames: ['pf-jump-links-item'],
});
protected override async getUpdateComplete(): Promise<boolean> {
const here = await super.getUpdateComplete();
const ps = await Promise.all(Array.from(this.#kids, x => x.updateComplete));
return here && ps.every(x => !!x);
}
override connectedCallback(): void {
super.connectedCallback();
this.addEventListener('slotchange', this.#onSlotChange);
this.addEventListener('select', this.#onSelect);
}
override firstUpdated(): void {
const active = this.querySelector?.<PfJumpLinksItem>('pf-jump-links-item[active]');
if (active) {
this.#setActiveItem(active);
}
}
override updated(changed: Map<string, unknown>): void {
if (changed.has('offset')) {
this.#spy.rootMargin = `${this.offset ?? 0}px 0px 0px 0px`;
}
}
render(): TemplateResult<1> {
return html`
<nav id="container" aria-labelledby="label">${this.expandable ? html`
<details ?open="${this.expanded}" @toggle="${this.#onToggle}">
<summary>
<pf-icon icon="chevron-right"></pf-icon>
<span part="label" id="label">${this.label}</span>
</summary>
<div role="listbox" aria-labelledby="label">
<!-- Place pf-jump-links-items here -->
<slot></slot>
</div>
</details>` : html`
<span id="label">${this.label}</span>
<div role="listbox" aria-labelledby="label">
<!-- Place pf-jump-links-items here -->
<slot></slot>
</div>`}
</nav>
`;
}
#onSlotChange() {
this.#tabindex.items = this.#items;
}
#onSelect(event: Event) {
if (event.target instanceof PfJumpLinksItem) {
this.#setActiveItem(event.target);
}
}
#setActiveItem(item: PfJumpLinksItem) {
const itemLink = item.shadowRoot?.querySelector?.('a') ?? null;
if (itemLink) {
this.#tabindex.atFocusedItemIndex = this.#tabindex.items.indexOf(itemLink);
this.#spy.setActive(item);
}
}
#onToggle(event: Event) {
if (event.target instanceof HTMLDetailsElement) {
this.expanded = event.target.open;
}
this.dispatchEvent(new Event('toggle'));
}
}
declare global {
interface HTMLElementTagNameMap {
'pf-jump-links': PfJumpLinks;
}
}