-
Notifications
You must be signed in to change notification settings - Fork 705
Expand file tree
/
Copy pathbreadcrumbs.ts
More file actions
174 lines (145 loc) · 4.98 KB
/
Copy pathbreadcrumbs.ts
File metadata and controls
174 lines (145 loc) · 4.98 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {html, LitElement} from 'lit';
import {property, query, queryAssignedElements, state} from 'lit/decorators.js';
import '../icon/icon.js';
import type CraftBreadcrumbItem from '../breadcrumb-item/breadcrumb-item.js';
import styles from './breadcrumbs.styles.js';
import {t} from '../../utilities/translate';
type BreadcrumbItem = {
label?: string;
href?: string;
value: string;
offsetWidth: number;
isVisible: boolean; // false if displayed in menu overlay
};
export default class CraftBreadcrumbs extends LitElement {
static override styles = [styles];
@query('slot') defaultSlot: HTMLSlotElement;
@query('slot[name="separator"]') separatorSlot: HTMLSlotElement;
@queryAssignedElements({selector: 'craft-breadcrumb-item'})
private breadcrumbsElements!: CraftBreadcrumbItem[];
/**
* The label to use for the breadcrumb control. This will not be shown on the screen, but it will be announced by
* screen readers and other assistive devices to provide more context for users.
*/
@property() label = t('Breadcrumbs');
@state()
private items: BreadcrumbItem[] = [];
@state()
private visibleItems = 0;
private resizeObserver: ResizeObserver | undefined;
private firstRender = true;
private getSeparator() {
const separator = this.separatorSlot.assignedElements({
flatten: true,
})[0] as HTMLElement;
// Clone it, remove ids, and slot it
const clone = separator.cloneNode(true) as HTMLElement;
[clone, ...clone.querySelectorAll('[id]')].forEach((el) =>
el.removeAttribute('id')
);
clone.setAttribute('data-default', '');
clone.slot = 'separator';
return clone;
}
/**
* We need to understand how much space (px) each breadcrumb item occupies,
* in order to know if it fits the available horizontal space.
*/
private calculateBreadcrumbItemsWidth(): void {
this.items = this.breadcrumbsElements.map((el, index) => {
let width = el.offsetWidth;
/**
* For breadcrumbs which are hidden,
* we need to temporarily remove the hidden attribute to calculate the width.
*/
if (el.hasAttribute('hidden')) {
el.removeAttribute('hidden');
width = el.offsetWidth;
el.setAttribute('hidden', '');
}
return {
label: el.innerText,
href: el.href,
value: /*el.value || */ index.toString(),
offsetWidth: width,
isVisible: true,
};
});
}
private async handleSlotChange() {
const items = [
...this.defaultSlot.assignedElements({flatten: true}),
].filter(
(item) => item.tagName.toLowerCase() === 'craft-breadcrumb-item'
) as CraftBreadcrumbItem[];
items.forEach((item, index) => {
// Append separators to each item if they don't already have one
const separator = item.querySelector('[slot="separator"]');
if (separator === null) {
// No separator exists, add one
item.append(this.getSeparator());
} else if (separator.hasAttribute('data-default')) {
// A default separator exists, replace it
separator.replaceWith(this.getSeparator());
} else {
// The user provided a custom separator, leave it alone
}
// The last breadcrumb item is the "current page"
if (index === items.length - 1) {
item.setAttribute('aria-current', 'page');
} else {
item.removeAttribute('aria-current');
}
});
if (this.breadcrumbsElements.length === 0) {
this.items = [];
this.visibleItems = 0;
return;
}
// Wait for all breadcrumb items to complete their updates
await Promise.all(this.breadcrumbsElements.map((el) => el.updateComplete));
// Force a recalculation of widths and overflow
this.calculateBreadcrumbItemsWidth();
// Reset visibleItems to 0 to force a full recalculation
this.visibleItems = 0;
this.adjustOverflow();
}
override connectedCallback() {
super.connectedCallback();
this.resizeObserver = new ResizeObserver(() => {
if (this.firstRender) {
// Don't adjust overflow on first render, it is adjusted in slotChangeHandler
this.firstRender = false;
return;
}
this.adjustOverflow();
});
this.resizeObserver.observe(this);
}
private adjustOverflow() {
const availableSpace = this.getBoundingClientRect().width;
console.log({availableSpace});
}
override disconnectedCallback() {
this.resizeObserver?.unobserve(this);
super.disconnectedCallback();
}
override render() {
return html`
<nav class="breadcrumbs" aria-label="${this.label}">
<slot @slotchange="${this.handleSlotChange}"></slot>
</nav>
<span hidden aria-hidden="true">
<slot name="separator"><span class="separator">/</span></slot>
</span>
`;
}
}
if (!customElements.get('craft-breadcrumbs')) {
customElements.define('craft-breadcrumbs', CraftBreadcrumbs);
}
declare global {
interface HTMLElementTagNameMap {
'craft-breadcrumbs': CraftBreadcrumbs;
}
}