@chromvoid/uikit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +8 -0
- package/README.md +96 -0
- package/dist/components/cv-accordion-item.d.ts +69 -0
- package/dist/components/cv-accordion-item.js +176 -0
- package/dist/components/cv-accordion.d.ts +79 -0
- package/dist/components/cv-accordion.js +310 -0
- package/dist/components/cv-alert-dialog.d.ts +86 -0
- package/dist/components/cv-alert-dialog.js +393 -0
- package/dist/components/cv-alert.d.ts +48 -0
- package/dist/components/cv-alert.js +156 -0
- package/dist/components/cv-badge.d.ts +56 -0
- package/dist/components/cv-badge.js +280 -0
- package/dist/components/cv-breadcrumb-item.d.ts +35 -0
- package/dist/components/cv-breadcrumb-item.js +64 -0
- package/dist/components/cv-breadcrumb.d.ts +39 -0
- package/dist/components/cv-breadcrumb.js +160 -0
- package/dist/components/cv-button.d.ts +83 -0
- package/dist/components/cv-button.js +541 -0
- package/dist/components/cv-callout.d.ts +32 -0
- package/dist/components/cv-callout.js +221 -0
- package/dist/components/cv-card.d.ts +48 -0
- package/dist/components/cv-card.js +269 -0
- package/dist/components/cv-carousel-slide.d.ts +25 -0
- package/dist/components/cv-carousel-slide.js +51 -0
- package/dist/components/cv-carousel.d.ts +96 -0
- package/dist/components/cv-carousel.js +457 -0
- package/dist/components/cv-checkbox.d.ts +84 -0
- package/dist/components/cv-checkbox.js +274 -0
- package/dist/components/cv-combobox-group.d.ts +15 -0
- package/dist/components/cv-combobox-group.js +34 -0
- package/dist/components/cv-combobox-option.d.ts +30 -0
- package/dist/components/cv-combobox-option.js +66 -0
- package/dist/components/cv-combobox.d.ts +135 -0
- package/dist/components/cv-combobox.js +834 -0
- package/dist/components/cv-command-item.d.ts +30 -0
- package/dist/components/cv-command-item.js +68 -0
- package/dist/components/cv-command-palette.d.ts +105 -0
- package/dist/components/cv-command-palette.js +578 -0
- package/dist/components/cv-context-menu.d.ts +98 -0
- package/dist/components/cv-context-menu.js +515 -0
- package/dist/components/cv-copy-button.d.ts +61 -0
- package/dist/components/cv-copy-button.js +318 -0
- package/dist/components/cv-date-picker.d.ts +161 -0
- package/dist/components/cv-date-picker.js +803 -0
- package/dist/components/cv-dialog.d.ts +89 -0
- package/dist/components/cv-dialog.js +459 -0
- package/dist/components/cv-disclosure.d.ts +57 -0
- package/dist/components/cv-disclosure.js +241 -0
- package/dist/components/cv-drawer.d.ts +102 -0
- package/dist/components/cv-drawer.js +595 -0
- package/dist/components/cv-feed-article.d.ts +26 -0
- package/dist/components/cv-feed-article.js +52 -0
- package/dist/components/cv-feed.d.ts +62 -0
- package/dist/components/cv-feed.js +310 -0
- package/dist/components/cv-grid-cell.d.ts +30 -0
- package/dist/components/cv-grid-cell.js +57 -0
- package/dist/components/cv-grid-column.d.ts +30 -0
- package/dist/components/cv-grid-column.js +43 -0
- package/dist/components/cv-grid-row.d.ts +30 -0
- package/dist/components/cv-grid-row.js +42 -0
- package/dist/components/cv-grid.d.ts +119 -0
- package/dist/components/cv-grid.js +567 -0
- package/dist/components/cv-icon.d.ts +57 -0
- package/dist/components/cv-icon.js +352 -0
- package/dist/components/cv-input.d.ts +127 -0
- package/dist/components/cv-input.js +482 -0
- package/dist/components/cv-landmark.d.ts +32 -0
- package/dist/components/cv-landmark.js +62 -0
- package/dist/components/cv-link.d.ts +22 -0
- package/dist/components/cv-link.js +99 -0
- package/dist/components/cv-listbox-group.d.ts +15 -0
- package/dist/components/cv-listbox-group.js +42 -0
- package/dist/components/cv-listbox.d.ts +81 -0
- package/dist/components/cv-listbox.js +388 -0
- package/dist/components/cv-menu-button.d.ts +118 -0
- package/dist/components/cv-menu-button.js +822 -0
- package/dist/components/cv-menu-group.d.ts +20 -0
- package/dist/components/cv-menu-group.js +48 -0
- package/dist/components/cv-menu-item.d.ts +52 -0
- package/dist/components/cv-menu-item.js +105 -0
- package/dist/components/cv-menu.d.ts +62 -0
- package/dist/components/cv-menu.js +414 -0
- package/dist/components/cv-meter.d.ts +66 -0
- package/dist/components/cv-meter.js +154 -0
- package/dist/components/cv-number.d.ts +139 -0
- package/dist/components/cv-number.js +553 -0
- package/dist/components/cv-option.d.ts +30 -0
- package/dist/components/cv-option.js +84 -0
- package/dist/components/cv-popover.d.ts +87 -0
- package/dist/components/cv-popover.js +373 -0
- package/dist/components/cv-progress-ring.d.ts +45 -0
- package/dist/components/cv-progress-ring.js +169 -0
- package/dist/components/cv-progress.d.ts +45 -0
- package/dist/components/cv-progress.js +148 -0
- package/dist/components/cv-radio-group.d.ts +79 -0
- package/dist/components/cv-radio-group.js +398 -0
- package/dist/components/cv-radio.d.ts +36 -0
- package/dist/components/cv-radio.js +123 -0
- package/dist/components/cv-select-group.d.ts +15 -0
- package/dist/components/cv-select-group.js +44 -0
- package/dist/components/cv-select-option.d.ts +30 -0
- package/dist/components/cv-select-option.js +66 -0
- package/dist/components/cv-select.d.ts +128 -0
- package/dist/components/cv-select.js +666 -0
- package/dist/components/cv-sidebar-item.d.ts +26 -0
- package/dist/components/cv-sidebar-item.js +142 -0
- package/dist/components/cv-sidebar.d.ts +171 -0
- package/dist/components/cv-sidebar.js +767 -0
- package/dist/components/cv-slider-multi-thumb.d.ts +73 -0
- package/dist/components/cv-slider-multi-thumb.js +374 -0
- package/dist/components/cv-slider.d.ts +84 -0
- package/dist/components/cv-slider.js +328 -0
- package/dist/components/cv-spinbutton.d.ts +121 -0
- package/dist/components/cv-spinbutton.js +486 -0
- package/dist/components/cv-spinner.d.ts +18 -0
- package/dist/components/cv-spinner.js +95 -0
- package/dist/components/cv-switch.d.ts +81 -0
- package/dist/components/cv-switch.js +285 -0
- package/dist/components/cv-tab-panel.d.ts +20 -0
- package/dist/components/cv-tab-panel.js +37 -0
- package/dist/components/cv-tab.d.ts +40 -0
- package/dist/components/cv-tab.js +132 -0
- package/dist/components/cv-table-cell.d.ts +31 -0
- package/dist/components/cv-table-cell.js +49 -0
- package/dist/components/cv-table-column.d.ts +37 -0
- package/dist/components/cv-table-column.js +63 -0
- package/dist/components/cv-table-row.d.ts +30 -0
- package/dist/components/cv-table-row.js +45 -0
- package/dist/components/cv-table.d.ts +147 -0
- package/dist/components/cv-table.js +607 -0
- package/dist/components/cv-tabs.d.ts +70 -0
- package/dist/components/cv-tabs.js +524 -0
- package/dist/components/cv-textarea.d.ts +108 -0
- package/dist/components/cv-textarea.js +328 -0
- package/dist/components/cv-toast-region.d.ts +39 -0
- package/dist/components/cv-toast-region.js +162 -0
- package/dist/components/cv-toast.d.ts +67 -0
- package/dist/components/cv-toast.js +315 -0
- package/dist/components/cv-toolbar-item.d.ts +25 -0
- package/dist/components/cv-toolbar-item.js +72 -0
- package/dist/components/cv-toolbar-separator.d.ts +25 -0
- package/dist/components/cv-toolbar-separator.js +45 -0
- package/dist/components/cv-toolbar.d.ts +63 -0
- package/dist/components/cv-toolbar.js +295 -0
- package/dist/components/cv-tooltip.d.ts +83 -0
- package/dist/components/cv-tooltip.js +455 -0
- package/dist/components/cv-treegrid-cell.d.ts +30 -0
- package/dist/components/cv-treegrid-cell.js +57 -0
- package/dist/components/cv-treegrid-column.d.ts +37 -0
- package/dist/components/cv-treegrid-column.js +53 -0
- package/dist/components/cv-treegrid-row.d.ts +55 -0
- package/dist/components/cv-treegrid-row.js +90 -0
- package/dist/components/cv-treegrid.d.ts +96 -0
- package/dist/components/cv-treegrid.js +632 -0
- package/dist/components/cv-treeitem.d.ts +58 -0
- package/dist/components/cv-treeitem.js +144 -0
- package/dist/components/cv-treeview.d.ts +70 -0
- package/dist/components/cv-treeview.js +396 -0
- package/dist/components/cv-window-splitter.d.ts +79 -0
- package/dist/components/cv-window-splitter.js +316 -0
- package/dist/components/index.d.ts +94 -0
- package/dist/components/index.js +79 -0
- package/dist/dialog/create-dialog-controller.d.ts +31 -0
- package/dist/dialog/create-dialog-controller.js +320 -0
- package/dist/dialog/index.d.ts +2 -0
- package/dist/dialog/index.js +1 -0
- package/dist/form-associated/FormAssociatedReatomElement.d.ts +25 -0
- package/dist/form-associated/FormAssociatedReatomElement.js +70 -0
- package/dist/form-associated/withFormAssociated.d.ts +5 -0
- package/dist/form-associated/withFormAssociated.js +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +9 -0
- package/dist/reatom-lit/ReatomLitElement.d.ts +27 -0
- package/dist/reatom-lit/ReatomLitElement.js +118 -0
- package/dist/reatom-lit/html.d.ts +4 -0
- package/dist/reatom-lit/html.js +10 -0
- package/dist/reatom-lit/index.d.ts +4 -0
- package/dist/reatom-lit/index.js +4 -0
- package/dist/reatom-lit/watch.d.ts +15 -0
- package/dist/reatom-lit/watch.js +40 -0
- package/dist/reatom-lit/withReatomElement.d.ts +4 -0
- package/dist/reatom-lit/withReatomElement.js +57 -0
- package/dist/register.d.ts +1 -0
- package/dist/register.js +84 -0
- package/dist/styles/component-styles.d.ts +4 -0
- package/dist/styles/component-styles.js +78 -0
- package/dist/theme/cv-theme-provider.d.ts +32 -0
- package/dist/theme/cv-theme-provider.js +110 -0
- package/dist/theme/index.d.ts +4 -0
- package/dist/theme/index.js +2 -0
- package/dist/theme/theme-engine.d.ts +4 -0
- package/dist/theme/theme-engine.js +67 -0
- package/dist/theme/tokens.css +265 -0
- package/dist/theme/types.d.ts +7 -0
- package/dist/theme/types.js +1 -0
- package/dist/toast/create-toast-controller.d.ts +12 -0
- package/dist/toast/create-toast-controller.js +12 -0
- package/dist/toast/index.d.ts +2 -0
- package/dist/toast/index.js +1 -0
- package/package.json +146 -0
- package/specs/_template.md +110 -0
- package/specs/components/accordion.md +207 -0
- package/specs/components/alert.md +83 -0
- package/specs/components/badge.md +183 -0
- package/specs/components/breadcrumb.md +152 -0
- package/specs/components/button.md +227 -0
- package/specs/components/callout.md +153 -0
- package/specs/components/card.md +192 -0
- package/specs/components/carousel.md +232 -0
- package/specs/components/checkbox.md +141 -0
- package/specs/components/combobox.md +427 -0
- package/specs/components/context-menu.md +375 -0
- package/specs/components/copy-button.md +236 -0
- package/specs/components/date-picker.md +290 -0
- package/specs/components/dialog.md +184 -0
- package/specs/components/disclosure.md +151 -0
- package/specs/components/drawer.md +216 -0
- package/specs/components/feed.md +266 -0
- package/specs/components/grid.md +423 -0
- package/specs/components/input.md +237 -0
- package/specs/components/landmark.md +92 -0
- package/specs/components/link.md +117 -0
- package/specs/components/listbox.md +327 -0
- package/specs/components/menu.md +508 -0
- package/specs/components/meter.md +148 -0
- package/specs/components/number.md +268 -0
- package/specs/components/option.md +167 -0
- package/specs/components/popover.md +207 -0
- package/specs/components/progress-ring.md +134 -0
- package/specs/components/progress.md +110 -0
- package/specs/components/radio.md +208 -0
- package/specs/components/select.md +305 -0
- package/specs/components/sidebar.md +204 -0
- package/specs/components/spinbutton.md +157 -0
- package/specs/components/spinner.md +83 -0
- package/specs/components/switch.md +145 -0
- package/specs/components/table.md +372 -0
- package/specs/components/tabs.md +242 -0
- package/specs/components/textarea.md +166 -0
- package/specs/components/theme.md +364 -0
- package/specs/components/toast.md +198 -0
- package/specs/components/toolbar.md +258 -0
- package/specs/components/tooltip.md +152 -0
- package/specs/components/treegrid.md +363 -0
- package/specs/components/treeview.md +263 -0
- package/specs/components/window-splitter.md +225 -0
|
@@ -0,0 +1,767 @@
|
|
|
1
|
+
import { createSidebar } from '@chromvoid/headless-ui/sidebar';
|
|
2
|
+
import { css, html, nothing } from 'lit';
|
|
3
|
+
import { ReatomLitElement } from '../reatom-lit/ReatomLitElement.js';
|
|
4
|
+
let cvSidebarNonce = 0;
|
|
5
|
+
const TOP_ANCHOR_THRESHOLDS = [0, 0.25, 0.5, 0.75, 1];
|
|
6
|
+
const VIEWPORT_DOMINANT_THRESHOLDS = Array.from({ length: 21 }, (_, index) => index / 20);
|
|
7
|
+
const VIEWPORT_DOMINANT_VISIBLE_WEIGHT = 0.7;
|
|
8
|
+
const VIEWPORT_DOMINANT_CENTER_WEIGHT = 0.3;
|
|
9
|
+
const VIEWPORT_DOMINANT_HYSTERESIS = 0.08;
|
|
10
|
+
const VIEWPORT_DOMINANT_MIN_VISIBLE_PX = 64;
|
|
11
|
+
function isSidebarItem(source) {
|
|
12
|
+
return source.tagName === 'CV-SIDEBAR-ITEM';
|
|
13
|
+
}
|
|
14
|
+
function isHashHref(href) {
|
|
15
|
+
return href.startsWith('#') && href.length > 1;
|
|
16
|
+
}
|
|
17
|
+
function clamp(value, min, max) {
|
|
18
|
+
return Math.min(Math.max(value, min), max);
|
|
19
|
+
}
|
|
20
|
+
function escapeSelectorId(id) {
|
|
21
|
+
if (typeof CSS !== 'undefined' && typeof CSS.escape === 'function') {
|
|
22
|
+
return CSS.escape(id);
|
|
23
|
+
}
|
|
24
|
+
return id.replace(/["\\#.:?[\]]/g, '\\$&');
|
|
25
|
+
}
|
|
26
|
+
export class CVSidebar extends ReatomLitElement {
|
|
27
|
+
static elementName = 'cv-sidebar';
|
|
28
|
+
static get properties() {
|
|
29
|
+
return {
|
|
30
|
+
expanded: { type: Boolean, reflect: true },
|
|
31
|
+
collapsed: { type: Boolean, reflect: true },
|
|
32
|
+
mobile: { type: Boolean, reflect: true },
|
|
33
|
+
overlayOpen: { type: Boolean, attribute: 'overlay-open', reflect: true },
|
|
34
|
+
size: { type: String, reflect: true },
|
|
35
|
+
breakpoint: { type: String, reflect: true },
|
|
36
|
+
closeOnEscape: { type: Boolean, attribute: 'close-on-escape', reflect: true },
|
|
37
|
+
closeOnOutsidePointer: { type: Boolean, attribute: 'close-on-outside-pointer', reflect: true },
|
|
38
|
+
initialFocusId: { type: String, attribute: 'initial-focus-id' },
|
|
39
|
+
ariaLabel: { type: String, attribute: 'aria-label' },
|
|
40
|
+
scrollspy: { type: Boolean, reflect: true },
|
|
41
|
+
scrollspyOffsetTop: { type: Number, attribute: 'scrollspy-offset-top' },
|
|
42
|
+
scrollspyStrategy: { type: String, attribute: 'scrollspy-strategy', reflect: true },
|
|
43
|
+
scrollspySmoothScroll: { type: Boolean, attribute: 'scrollspy-smooth-scroll', reflect: true },
|
|
44
|
+
scrollspyRoot: { attribute: false },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
idBase = `cv-sidebar-${++cvSidebarNonce}`;
|
|
48
|
+
model;
|
|
49
|
+
lockScrollApplied = false;
|
|
50
|
+
previousBodyOverflow = '';
|
|
51
|
+
lifecycleToken = 0;
|
|
52
|
+
suppressLifecycleFromUpdate = false;
|
|
53
|
+
mediaQuery = null;
|
|
54
|
+
mediaQueryHandler = null;
|
|
55
|
+
scrollspyObserver = null;
|
|
56
|
+
scrollspyBindings = [];
|
|
57
|
+
scrollspyActiveId = null;
|
|
58
|
+
scrollspyRefreshToken = 0;
|
|
59
|
+
scrollspyRecomputeFrame = 0;
|
|
60
|
+
activeRevealToken = 0;
|
|
61
|
+
constructor() {
|
|
62
|
+
super();
|
|
63
|
+
this.expanded = true;
|
|
64
|
+
this.collapsed = false;
|
|
65
|
+
this.mobile = false;
|
|
66
|
+
this.overlayOpen = false;
|
|
67
|
+
this.size = 'medium';
|
|
68
|
+
this.breakpoint = '768px';
|
|
69
|
+
this.closeOnEscape = true;
|
|
70
|
+
this.closeOnOutsidePointer = true;
|
|
71
|
+
this.initialFocusId = '';
|
|
72
|
+
this.ariaLabel = 'Sidebar navigation';
|
|
73
|
+
this.scrollspy = false;
|
|
74
|
+
this.scrollspyOffsetTop = 0;
|
|
75
|
+
this.scrollspyStrategy = 'top-anchor';
|
|
76
|
+
this.scrollspySmoothScroll = true;
|
|
77
|
+
this.scrollspyRoot = null;
|
|
78
|
+
this.model = this.createModel();
|
|
79
|
+
}
|
|
80
|
+
get activeId() {
|
|
81
|
+
return this.scrollspyActiveId;
|
|
82
|
+
}
|
|
83
|
+
static styles = [
|
|
84
|
+
css `
|
|
85
|
+
:host {
|
|
86
|
+
display: block;
|
|
87
|
+
position: relative;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
[part='overlay'] {
|
|
91
|
+
position: fixed;
|
|
92
|
+
inset: 0;
|
|
93
|
+
z-index: calc(var(--cv-sidebar-z-index, 30) + 10);
|
|
94
|
+
background: var(--cv-sidebar-overlay-color, color-mix(in oklab, black 56%, transparent));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
[part='overlay'][hidden] {
|
|
98
|
+
display: none;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
[part='panel'] {
|
|
102
|
+
display: grid;
|
|
103
|
+
grid-template-rows: auto 1fr auto;
|
|
104
|
+
position: relative;
|
|
105
|
+
inline-size: var(--cv-sidebar-inline-size, 280px);
|
|
106
|
+
block-size: 100%;
|
|
107
|
+
background: var(--cv-sidebar-background, var(--cv-color-surface, #141923));
|
|
108
|
+
border-inline-end: 1px solid var(--cv-sidebar-border-color, var(--cv-color-border, #2a3245));
|
|
109
|
+
transition: inline-size var(--cv-sidebar-transition-duration, var(--cv-duration-normal, 200ms))
|
|
110
|
+
var(--cv-sidebar-transition-easing, var(--cv-easing-standard, ease));
|
|
111
|
+
overflow: hidden;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
:host([collapsed]) [part='panel'] {
|
|
115
|
+
inline-size: var(--cv-sidebar-rail-inline-size, 56px);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
:host([size='small']) {
|
|
119
|
+
--cv-sidebar-inline-size: 220px;
|
|
120
|
+
--cv-sidebar-rail-inline-size: 48px;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
:host([size='large']) {
|
|
124
|
+
--cv-sidebar-inline-size: 340px;
|
|
125
|
+
--cv-sidebar-rail-inline-size: 64px;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
:host([mobile]) [part='panel'] {
|
|
129
|
+
position: fixed;
|
|
130
|
+
inset-block: 0;
|
|
131
|
+
inset-inline-start: 0;
|
|
132
|
+
z-index: calc(var(--cv-sidebar-z-index, 30) + 10);
|
|
133
|
+
inline-size: var(--cv-sidebar-inline-size, 280px);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
:host([mobile]:not([overlay-open])) [part='panel'] {
|
|
137
|
+
display: none;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
[part='header'] {
|
|
141
|
+
display: flex;
|
|
142
|
+
align-items: center;
|
|
143
|
+
gap: var(--cv-space-2, 8px);
|
|
144
|
+
padding-block: var(--cv-sidebar-padding-block, var(--cv-space-3, 12px));
|
|
145
|
+
padding-inline: var(--cv-sidebar-padding-inline, var(--cv-space-3, 12px));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
[part='toggle'] {
|
|
149
|
+
display: inline-flex;
|
|
150
|
+
align-items: center;
|
|
151
|
+
justify-content: center;
|
|
152
|
+
min-block-size: 28px;
|
|
153
|
+
min-inline-size: 28px;
|
|
154
|
+
padding: 0;
|
|
155
|
+
border-radius: var(--cv-radius-sm, 6px);
|
|
156
|
+
border: none;
|
|
157
|
+
background: transparent;
|
|
158
|
+
color: var(--cv-color-text-muted, #9aa6bf);
|
|
159
|
+
cursor: pointer;
|
|
160
|
+
margin-inline-start: auto;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
[part='toggle']:focus-visible {
|
|
164
|
+
outline: 2px solid var(--cv-color-primary, #65d7ff);
|
|
165
|
+
outline-offset: 1px;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
[part='body'] {
|
|
169
|
+
padding-block: var(--cv-sidebar-padding-block, var(--cv-space-3, 12px));
|
|
170
|
+
padding-inline: var(--cv-sidebar-padding-inline, var(--cv-space-3, 12px));
|
|
171
|
+
overflow: auto;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
[part='footer'] {
|
|
175
|
+
padding-block: var(--cv-sidebar-padding-block, var(--cv-space-3, 12px));
|
|
176
|
+
padding-inline: var(--cv-sidebar-padding-inline, var(--cv-space-3, 12px));
|
|
177
|
+
}
|
|
178
|
+
`,
|
|
179
|
+
];
|
|
180
|
+
static define() {
|
|
181
|
+
if (!customElements.get(this.elementName)) {
|
|
182
|
+
customElements.define(this.elementName, this);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
connectedCallback() {
|
|
186
|
+
super.connectedCallback();
|
|
187
|
+
this.setupMediaQuery();
|
|
188
|
+
this.syncScrollLock();
|
|
189
|
+
this.scheduleScrollspyRefresh();
|
|
190
|
+
}
|
|
191
|
+
disconnectedCallback() {
|
|
192
|
+
super.disconnectedCallback();
|
|
193
|
+
this.teardownMediaQuery();
|
|
194
|
+
this.releaseScrollLock();
|
|
195
|
+
this.destroyScrollspy();
|
|
196
|
+
}
|
|
197
|
+
willUpdate(changedProperties) {
|
|
198
|
+
super.willUpdate(changedProperties);
|
|
199
|
+
if (changedProperties.has('collapsed')) {
|
|
200
|
+
if (this.collapsed && this.expanded) {
|
|
201
|
+
this.expanded = false;
|
|
202
|
+
}
|
|
203
|
+
else if (!this.collapsed && !this.expanded) {
|
|
204
|
+
this.expanded = true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (changedProperties.has('expanded')) {
|
|
208
|
+
if (this.expanded && this.collapsed) {
|
|
209
|
+
this.collapsed = false;
|
|
210
|
+
}
|
|
211
|
+
else if (!this.expanded && !this.collapsed) {
|
|
212
|
+
this.collapsed = true;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const needsModelRecreate = changedProperties.has('closeOnEscape') ||
|
|
216
|
+
changedProperties.has('closeOnOutsidePointer') ||
|
|
217
|
+
changedProperties.has('initialFocusId') ||
|
|
218
|
+
changedProperties.has('ariaLabel');
|
|
219
|
+
if (needsModelRecreate) {
|
|
220
|
+
this.model = this.createModel();
|
|
221
|
+
}
|
|
222
|
+
if (changedProperties.has('expanded') || changedProperties.has('collapsed') || needsModelRecreate) {
|
|
223
|
+
if (this.expanded && !this.model.state.expanded()) {
|
|
224
|
+
this.model.actions.expand();
|
|
225
|
+
}
|
|
226
|
+
else if (!this.expanded && this.model.state.expanded()) {
|
|
227
|
+
this.model.actions.collapse();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (changedProperties.has('mobile')) {
|
|
231
|
+
if (this.model.state.mobile() !== this.mobile) {
|
|
232
|
+
this.model.actions.setMobile(this.mobile);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (changedProperties.has('overlayOpen')) {
|
|
236
|
+
if (this.overlayOpen && !this.model.state.overlayOpen()) {
|
|
237
|
+
this.model.actions.openOverlay();
|
|
238
|
+
}
|
|
239
|
+
else if (!this.overlayOpen && this.model.state.overlayOpen()) {
|
|
240
|
+
this.model.actions.closeOverlay();
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (changedProperties.has('breakpoint')) {
|
|
244
|
+
this.teardownMediaQuery();
|
|
245
|
+
this.setupMediaQuery();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
updated(changedProperties) {
|
|
249
|
+
super.updated(changedProperties);
|
|
250
|
+
this.syncScrollLock();
|
|
251
|
+
this.syncChildItemContext();
|
|
252
|
+
if (changedProperties.has('collapsed') ||
|
|
253
|
+
changedProperties.has('mobile') ||
|
|
254
|
+
changedProperties.has('scrollspy') ||
|
|
255
|
+
changedProperties.has('scrollspyOffsetTop') ||
|
|
256
|
+
changedProperties.has('scrollspyStrategy') ||
|
|
257
|
+
changedProperties.has('scrollspyRoot')) {
|
|
258
|
+
this.scheduleScrollspyRefresh();
|
|
259
|
+
}
|
|
260
|
+
if (changedProperties.has('expanded') && changedProperties.get('expanded') !== undefined) {
|
|
261
|
+
if (this.suppressLifecycleFromUpdate) {
|
|
262
|
+
this.suppressLifecycleFromUpdate = false;
|
|
263
|
+
}
|
|
264
|
+
else if (!this.mobile) {
|
|
265
|
+
this.dispatchDesktopLifecycle(this.expanded);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (changedProperties.has('overlayOpen') && changedProperties.get('overlayOpen') !== undefined) {
|
|
269
|
+
this.dispatchOverlayLifecycle(this.overlayOpen);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
createModel() {
|
|
273
|
+
return createSidebar({
|
|
274
|
+
id: this.idBase,
|
|
275
|
+
defaultExpanded: this.expanded,
|
|
276
|
+
closeOnEscape: this.closeOnEscape,
|
|
277
|
+
closeOnOutsidePointer: this.closeOnOutsidePointer,
|
|
278
|
+
initialFocusId: this.initialFocusId || undefined,
|
|
279
|
+
ariaLabel: this.ariaLabel || 'Sidebar navigation',
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
captureState() {
|
|
283
|
+
return {
|
|
284
|
+
expanded: this.model.state.expanded(),
|
|
285
|
+
overlayOpen: this.model.state.overlayOpen(),
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
setupMediaQuery() {
|
|
289
|
+
if (typeof window === 'undefined' || !window.matchMedia)
|
|
290
|
+
return;
|
|
291
|
+
const mq = window.matchMedia(`(max-width: ${this.breakpoint})`);
|
|
292
|
+
this.mediaQuery = mq;
|
|
293
|
+
this.mediaQueryHandler = (e) => {
|
|
294
|
+
this.mobile = e.matches;
|
|
295
|
+
this.model.actions.setMobile(e.matches);
|
|
296
|
+
};
|
|
297
|
+
mq.addEventListener('change', this.mediaQueryHandler);
|
|
298
|
+
this.mobile = mq.matches;
|
|
299
|
+
this.model.actions.setMobile(mq.matches);
|
|
300
|
+
}
|
|
301
|
+
teardownMediaQuery() {
|
|
302
|
+
if (this.mediaQuery && this.mediaQueryHandler) {
|
|
303
|
+
this.mediaQuery.removeEventListener('change', this.mediaQueryHandler);
|
|
304
|
+
}
|
|
305
|
+
this.mediaQuery = null;
|
|
306
|
+
this.mediaQueryHandler = null;
|
|
307
|
+
}
|
|
308
|
+
dispatchInput(detail) {
|
|
309
|
+
this.dispatchEvent(new CustomEvent('cv-input', { detail, bubbles: true, composed: true }));
|
|
310
|
+
}
|
|
311
|
+
dispatchChange(detail) {
|
|
312
|
+
this.dispatchEvent(new CustomEvent('cv-change', { detail, bubbles: true, composed: true }));
|
|
313
|
+
}
|
|
314
|
+
dispatchLifecycleEvent(name) {
|
|
315
|
+
this.dispatchEvent(new CustomEvent(name, { bubbles: true, composed: true }));
|
|
316
|
+
}
|
|
317
|
+
dispatchDesktopLifecycle(expanded) {
|
|
318
|
+
const token = ++this.lifecycleToken;
|
|
319
|
+
this.dispatchLifecycleEvent(expanded ? 'cv-expand' : 'cv-collapse');
|
|
320
|
+
this.updateComplete.then(() => {
|
|
321
|
+
if (this.lifecycleToken !== token)
|
|
322
|
+
return;
|
|
323
|
+
this.dispatchLifecycleEvent(expanded ? 'cv-after-expand' : 'cv-after-collapse');
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
dispatchOverlayLifecycle(open) {
|
|
327
|
+
const token = ++this.lifecycleToken;
|
|
328
|
+
this.dispatchLifecycleEvent(open ? 'cv-overlay-open' : 'cv-overlay-close');
|
|
329
|
+
this.updateComplete.then(() => {
|
|
330
|
+
if (this.lifecycleToken !== token)
|
|
331
|
+
return;
|
|
332
|
+
this.dispatchLifecycleEvent(open ? 'cv-after-overlay-open' : 'cv-after-overlay-close');
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
dispatchScrollspyChange(activeId) {
|
|
336
|
+
this.dispatchEvent(new CustomEvent('cv-scrollspy-change', {
|
|
337
|
+
detail: { activeId },
|
|
338
|
+
bubbles: true,
|
|
339
|
+
composed: true,
|
|
340
|
+
}));
|
|
341
|
+
}
|
|
342
|
+
applyInteractionResult(previous) {
|
|
343
|
+
const nextExpanded = this.model.state.expanded();
|
|
344
|
+
const nextOverlayOpen = this.model.state.overlayOpen();
|
|
345
|
+
if (!this.mobile && previous.expanded !== nextExpanded) {
|
|
346
|
+
this.suppressLifecycleFromUpdate = true;
|
|
347
|
+
this.expanded = nextExpanded;
|
|
348
|
+
this.collapsed = !nextExpanded;
|
|
349
|
+
this.dispatchDesktopLifecycle(nextExpanded);
|
|
350
|
+
const detail = { expanded: nextExpanded };
|
|
351
|
+
this.dispatchInput(detail);
|
|
352
|
+
this.dispatchChange(detail);
|
|
353
|
+
}
|
|
354
|
+
if (this.mobile && previous.overlayOpen !== nextOverlayOpen) {
|
|
355
|
+
this.overlayOpen = nextOverlayOpen;
|
|
356
|
+
const detail = { overlayOpen: nextOverlayOpen };
|
|
357
|
+
this.dispatchInput(detail);
|
|
358
|
+
this.dispatchChange(detail);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
syncScrollLock() {
|
|
362
|
+
const shouldLock = this.model.state.shouldLockScroll();
|
|
363
|
+
if (shouldLock && !this.lockScrollApplied) {
|
|
364
|
+
this.previousBodyOverflow = document.body.style.overflow;
|
|
365
|
+
document.body.style.overflow = 'hidden';
|
|
366
|
+
this.lockScrollApplied = true;
|
|
367
|
+
}
|
|
368
|
+
else if (!shouldLock && this.lockScrollApplied) {
|
|
369
|
+
this.releaseScrollLock();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
releaseScrollLock() {
|
|
373
|
+
if (!this.lockScrollApplied)
|
|
374
|
+
return;
|
|
375
|
+
document.body.style.overflow = this.previousBodyOverflow;
|
|
376
|
+
this.lockScrollApplied = false;
|
|
377
|
+
}
|
|
378
|
+
getDefaultSlot() {
|
|
379
|
+
return this.shadowRoot?.querySelector('slot:not([name])') ?? null;
|
|
380
|
+
}
|
|
381
|
+
getAssignedElements() {
|
|
382
|
+
const slot = this.getDefaultSlot();
|
|
383
|
+
return slot?.assignedElements({ flatten: true }) ?? [];
|
|
384
|
+
}
|
|
385
|
+
getSidebarItems() {
|
|
386
|
+
return this.getAssignedElements().filter(isSidebarItem);
|
|
387
|
+
}
|
|
388
|
+
syncChildItemContext() {
|
|
389
|
+
for (const item of this.getSidebarItems()) {
|
|
390
|
+
item.toggleAttribute('data-sidebar-collapsed', this.collapsed);
|
|
391
|
+
item.toggleAttribute('data-sidebar-mobile', this.mobile);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
handleDefaultSlotChange() {
|
|
395
|
+
this.syncChildItemContext();
|
|
396
|
+
this.scheduleScrollspyRefresh();
|
|
397
|
+
}
|
|
398
|
+
handleToggleClick() {
|
|
399
|
+
const previous = this.captureState();
|
|
400
|
+
this.model.actions.toggle();
|
|
401
|
+
this.applyInteractionResult(previous);
|
|
402
|
+
}
|
|
403
|
+
handleOverlayPointerDown(event) {
|
|
404
|
+
if (event.target !== event.currentTarget)
|
|
405
|
+
return;
|
|
406
|
+
const previous = this.captureState();
|
|
407
|
+
this.model.actions.handleOutsidePointer();
|
|
408
|
+
this.applyInteractionResult(previous);
|
|
409
|
+
}
|
|
410
|
+
handlePanelKeyDown(event) {
|
|
411
|
+
if (event.key === 'Escape') {
|
|
412
|
+
event.preventDefault();
|
|
413
|
+
}
|
|
414
|
+
const previous = this.captureState();
|
|
415
|
+
this.model.actions.handleKeyDown({ key: event.key });
|
|
416
|
+
this.applyInteractionResult(previous);
|
|
417
|
+
}
|
|
418
|
+
handleBodyClick(event) {
|
|
419
|
+
if (!this.scrollspy)
|
|
420
|
+
return;
|
|
421
|
+
if (event.defaultPrevented || event.button !== 0)
|
|
422
|
+
return;
|
|
423
|
+
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey)
|
|
424
|
+
return;
|
|
425
|
+
const binding = this.resolveBindingFromEvent(event);
|
|
426
|
+
if (!binding)
|
|
427
|
+
return;
|
|
428
|
+
if (isSidebarItem(binding.source) && binding.source.disabled)
|
|
429
|
+
return;
|
|
430
|
+
const strategy = this.resolveScrollspyStrategy();
|
|
431
|
+
event.preventDefault();
|
|
432
|
+
if (strategy === 'top-anchor') {
|
|
433
|
+
this.updateActiveId(binding.id);
|
|
434
|
+
}
|
|
435
|
+
this.scrollBindingTarget(binding, strategy);
|
|
436
|
+
}
|
|
437
|
+
resolveBindingFromEvent(event) {
|
|
438
|
+
const path = event.composedPath();
|
|
439
|
+
for (const binding of this.scrollspyBindings) {
|
|
440
|
+
if (path.includes(binding.source)) {
|
|
441
|
+
return binding;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
scheduleScrollspyRefresh() {
|
|
447
|
+
const token = ++this.scrollspyRefreshToken;
|
|
448
|
+
queueMicrotask(() => {
|
|
449
|
+
if (token !== this.scrollspyRefreshToken || !this.isConnected)
|
|
450
|
+
return;
|
|
451
|
+
this.refreshScrollspy();
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
refreshScrollspy() {
|
|
455
|
+
this.destroyScrollspy();
|
|
456
|
+
this.syncChildItemContext();
|
|
457
|
+
if (!this.scrollspy) {
|
|
458
|
+
this.updateActiveId(null);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
this.scrollspyBindings = this.collectScrollspyBindings();
|
|
462
|
+
this.syncScrollspyActiveState();
|
|
463
|
+
if (!this.scrollspyBindings.length) {
|
|
464
|
+
this.updateActiveId(null);
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
468
|
+
this.updateActiveId(this.computeActiveId());
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
this.scrollspyObserver = new IntersectionObserver(this.handleScrollspyIntersection.bind(this), this.getScrollspyObserverOptions());
|
|
472
|
+
for (const binding of this.scrollspyBindings) {
|
|
473
|
+
this.scrollspyObserver.observe(binding.target);
|
|
474
|
+
}
|
|
475
|
+
this.scheduleScrollspyRecompute();
|
|
476
|
+
}
|
|
477
|
+
destroyScrollspy() {
|
|
478
|
+
this.cancelScrollspyRecompute();
|
|
479
|
+
this.scrollspyObserver?.disconnect();
|
|
480
|
+
this.scrollspyObserver = null;
|
|
481
|
+
this.scrollspyBindings = [];
|
|
482
|
+
}
|
|
483
|
+
collectScrollspyBindings() {
|
|
484
|
+
const bindings = [];
|
|
485
|
+
for (const source of this.getAssignedElements()) {
|
|
486
|
+
const href = this.getHashHref(source);
|
|
487
|
+
if (!href)
|
|
488
|
+
continue;
|
|
489
|
+
const id = href.slice(1);
|
|
490
|
+
const target = this.resolveScrollspyTarget(id);
|
|
491
|
+
if (!target)
|
|
492
|
+
continue;
|
|
493
|
+
bindings.push({
|
|
494
|
+
href,
|
|
495
|
+
id,
|
|
496
|
+
source: source,
|
|
497
|
+
target,
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
return bindings;
|
|
501
|
+
}
|
|
502
|
+
getHashHref(source) {
|
|
503
|
+
if (isSidebarItem(source)) {
|
|
504
|
+
return isHashHref(source.href) ? source.href : null;
|
|
505
|
+
}
|
|
506
|
+
if (source instanceof HTMLAnchorElement) {
|
|
507
|
+
const href = source.getAttribute('href') ?? '';
|
|
508
|
+
return isHashHref(href) ? href : null;
|
|
509
|
+
}
|
|
510
|
+
return null;
|
|
511
|
+
}
|
|
512
|
+
resolveScrollspyTarget(id) {
|
|
513
|
+
if (!id)
|
|
514
|
+
return null;
|
|
515
|
+
const root = this.resolveScrollspyContainer();
|
|
516
|
+
const target = root.querySelector(`#${escapeSelectorId(id)}`);
|
|
517
|
+
return target instanceof HTMLElement ? target : null;
|
|
518
|
+
}
|
|
519
|
+
resolveScrollspyContainer() {
|
|
520
|
+
if (this.scrollspyRoot) {
|
|
521
|
+
return this.scrollspyRoot;
|
|
522
|
+
}
|
|
523
|
+
const root = this.getRootNode();
|
|
524
|
+
if (root instanceof ShadowRoot || root instanceof Document) {
|
|
525
|
+
return root;
|
|
526
|
+
}
|
|
527
|
+
return this.ownerDocument;
|
|
528
|
+
}
|
|
529
|
+
handleScrollspyIntersection() {
|
|
530
|
+
this.scheduleScrollspyRecompute();
|
|
531
|
+
}
|
|
532
|
+
computeActiveId() {
|
|
533
|
+
return this.resolveScrollspyStrategy() === 'viewport-dominant'
|
|
534
|
+
? this.computeViewportDominantActiveId()
|
|
535
|
+
: this.computeTopAnchorActiveId();
|
|
536
|
+
}
|
|
537
|
+
computeTopAnchorActiveId() {
|
|
538
|
+
if (!this.scrollspyBindings.length)
|
|
539
|
+
return null;
|
|
540
|
+
const offsetTop = Math.max(0, this.scrollspyOffsetTop);
|
|
541
|
+
const viewportHeight = typeof window === 'undefined' ? 0 : window.innerHeight;
|
|
542
|
+
let bestVisible = null;
|
|
543
|
+
let latestCrossed = null;
|
|
544
|
+
let firstUpcoming = null;
|
|
545
|
+
for (const binding of this.scrollspyBindings) {
|
|
546
|
+
const rect = binding.target.getBoundingClientRect();
|
|
547
|
+
const hasLayout = rect.height > 0 || rect.width > 0;
|
|
548
|
+
if (!hasLayout)
|
|
549
|
+
continue;
|
|
550
|
+
const isVisible = rect.top < viewportHeight && rect.bottom > offsetTop;
|
|
551
|
+
const crossedAnchor = rect.top <= offsetTop;
|
|
552
|
+
const distance = Math.abs(rect.top - offsetTop);
|
|
553
|
+
if (isVisible &&
|
|
554
|
+
(!bestVisible ||
|
|
555
|
+
distance < bestVisible.distance ||
|
|
556
|
+
(distance === bestVisible.distance && crossedAnchor && !bestVisible.crossedAnchor))) {
|
|
557
|
+
bestVisible = { id: binding.id, distance, crossedAnchor };
|
|
558
|
+
}
|
|
559
|
+
if (crossedAnchor && (!latestCrossed || rect.top > latestCrossed.top)) {
|
|
560
|
+
latestCrossed = { id: binding.id, top: rect.top };
|
|
561
|
+
}
|
|
562
|
+
if (!crossedAnchor && (!firstUpcoming || rect.top < firstUpcoming.top)) {
|
|
563
|
+
firstUpcoming = { id: binding.id, top: rect.top };
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return bestVisible?.id ?? latestCrossed?.id ?? firstUpcoming?.id ?? null;
|
|
567
|
+
}
|
|
568
|
+
computeViewportDominantActiveId() {
|
|
569
|
+
if (!this.scrollspyBindings.length)
|
|
570
|
+
return null;
|
|
571
|
+
if (typeof window === 'undefined')
|
|
572
|
+
return null;
|
|
573
|
+
const viewportTop = Math.max(0, this.scrollspyOffsetTop);
|
|
574
|
+
const viewportBottom = window.innerHeight;
|
|
575
|
+
const effectiveHeight = Math.max(1, viewportBottom - viewportTop);
|
|
576
|
+
const effectiveCenter = viewportTop + effectiveHeight / 2;
|
|
577
|
+
let bestCandidate = null;
|
|
578
|
+
let currentCandidate = null;
|
|
579
|
+
for (const binding of this.scrollspyBindings) {
|
|
580
|
+
const rect = binding.target.getBoundingClientRect();
|
|
581
|
+
const hasLayout = rect.height > 0 || rect.width > 0;
|
|
582
|
+
if (!hasLayout)
|
|
583
|
+
continue;
|
|
584
|
+
const visibleTop = Math.max(rect.top, viewportTop);
|
|
585
|
+
const visibleBottom = Math.min(rect.bottom, viewportBottom);
|
|
586
|
+
const visiblePx = clamp(visibleBottom - visibleTop, 0, effectiveHeight);
|
|
587
|
+
if (visiblePx <= 0)
|
|
588
|
+
continue;
|
|
589
|
+
const visibleRatio = visiblePx / Math.max(1, Math.min(rect.height, effectiveHeight));
|
|
590
|
+
const sectionCenter = rect.top + rect.height / 2;
|
|
591
|
+
const centerScore = clamp(1 - Math.abs(sectionCenter - effectiveCenter) / Math.max(1, effectiveHeight / 2), 0, 1);
|
|
592
|
+
const score = visibleRatio * VIEWPORT_DOMINANT_VISIBLE_WEIGHT + centerScore * VIEWPORT_DOMINANT_CENTER_WEIGHT;
|
|
593
|
+
if (!bestCandidate || score > bestCandidate.score) {
|
|
594
|
+
bestCandidate = { id: binding.id, score, visiblePx };
|
|
595
|
+
}
|
|
596
|
+
if (binding.id === this.scrollspyActiveId) {
|
|
597
|
+
currentCandidate = { score, visiblePx };
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
if (!bestCandidate)
|
|
601
|
+
return null;
|
|
602
|
+
if (bestCandidate.id === this.scrollspyActiveId)
|
|
603
|
+
return bestCandidate.id;
|
|
604
|
+
if (!currentCandidate)
|
|
605
|
+
return bestCandidate.id;
|
|
606
|
+
if (currentCandidate.visiblePx < VIEWPORT_DOMINANT_MIN_VISIBLE_PX) {
|
|
607
|
+
return bestCandidate.id;
|
|
608
|
+
}
|
|
609
|
+
if (bestCandidate.score >= currentCandidate.score + VIEWPORT_DOMINANT_HYSTERESIS) {
|
|
610
|
+
return bestCandidate.id;
|
|
611
|
+
}
|
|
612
|
+
return this.scrollspyActiveId;
|
|
613
|
+
}
|
|
614
|
+
getScrollspyObserverOptions() {
|
|
615
|
+
if (this.resolveScrollspyStrategy() === 'viewport-dominant') {
|
|
616
|
+
return {
|
|
617
|
+
rootMargin: '0px',
|
|
618
|
+
threshold: VIEWPORT_DOMINANT_THRESHOLDS,
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
return {
|
|
622
|
+
rootMargin: `-${Math.max(0, this.scrollspyOffsetTop)}px 0px -60% 0px`,
|
|
623
|
+
threshold: TOP_ANCHOR_THRESHOLDS,
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
resolveScrollspyStrategy() {
|
|
627
|
+
return this.scrollspyStrategy === 'viewport-dominant' ? 'viewport-dominant' : 'top-anchor';
|
|
628
|
+
}
|
|
629
|
+
scrollBindingTarget(binding, strategy) {
|
|
630
|
+
const behavior = this.scrollspySmoothScroll ? 'smooth' : 'auto';
|
|
631
|
+
if (strategy !== 'viewport-dominant' || typeof window === 'undefined') {
|
|
632
|
+
binding.target.scrollIntoView({ behavior, block: 'start' });
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const viewportTop = Math.max(0, this.scrollspyOffsetTop);
|
|
636
|
+
const viewportBottom = window.innerHeight;
|
|
637
|
+
const effectiveHeight = Math.max(1, viewportBottom - viewportTop);
|
|
638
|
+
const effectiveCenter = viewportTop + effectiveHeight / 2;
|
|
639
|
+
const currentScrollTop = window.scrollY || window.pageYOffset || 0;
|
|
640
|
+
const rect = binding.target.getBoundingClientRect();
|
|
641
|
+
const targetCenter = currentScrollTop + rect.top + rect.height / 2;
|
|
642
|
+
const scrollingElement = document.scrollingElement ?? document.documentElement;
|
|
643
|
+
const maxTop = Math.max(0, scrollingElement.scrollHeight - window.innerHeight);
|
|
644
|
+
const top = clamp(targetCenter - effectiveCenter, 0, maxTop);
|
|
645
|
+
window.scrollTo({ top, behavior });
|
|
646
|
+
}
|
|
647
|
+
scheduleScrollspyRecompute() {
|
|
648
|
+
if (this.scrollspyRecomputeFrame)
|
|
649
|
+
return;
|
|
650
|
+
this.scrollspyRecomputeFrame = requestAnimationFrame(() => {
|
|
651
|
+
this.scrollspyRecomputeFrame = 0;
|
|
652
|
+
if (!this.isConnected || !this.scrollspy)
|
|
653
|
+
return;
|
|
654
|
+
this.updateActiveId(this.computeActiveId());
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
cancelScrollspyRecompute() {
|
|
658
|
+
if (!this.scrollspyRecomputeFrame)
|
|
659
|
+
return;
|
|
660
|
+
cancelAnimationFrame(this.scrollspyRecomputeFrame);
|
|
661
|
+
this.scrollspyRecomputeFrame = 0;
|
|
662
|
+
}
|
|
663
|
+
updateActiveId(nextActiveId) {
|
|
664
|
+
if (this.scrollspyActiveId === nextActiveId) {
|
|
665
|
+
this.syncScrollspyActiveState();
|
|
666
|
+
this.revealActiveBinding();
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
this.scrollspyActiveId = nextActiveId;
|
|
670
|
+
this.syncScrollspyActiveState();
|
|
671
|
+
this.revealActiveBinding();
|
|
672
|
+
this.dispatchScrollspyChange(nextActiveId);
|
|
673
|
+
}
|
|
674
|
+
syncScrollspyActiveState() {
|
|
675
|
+
for (const binding of this.scrollspyBindings) {
|
|
676
|
+
const isActive = binding.id === this.scrollspyActiveId;
|
|
677
|
+
if (isSidebarItem(binding.source)) {
|
|
678
|
+
binding.source.active = isActive;
|
|
679
|
+
}
|
|
680
|
+
else {
|
|
681
|
+
binding.source.toggleAttribute('data-active', isActive);
|
|
682
|
+
if (isActive) {
|
|
683
|
+
binding.source.ariaCurrent = 'location';
|
|
684
|
+
binding.source.setAttribute('aria-current', 'location');
|
|
685
|
+
}
|
|
686
|
+
else {
|
|
687
|
+
binding.source.ariaCurrent = '';
|
|
688
|
+
binding.source.removeAttribute('aria-current');
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
revealActiveBinding() {
|
|
694
|
+
const binding = this.scrollspyBindings.find((candidate) => candidate.id === this.scrollspyActiveId);
|
|
695
|
+
if (!binding)
|
|
696
|
+
return;
|
|
697
|
+
const source = binding.source;
|
|
698
|
+
const body = this.shadowRoot?.querySelector('[part="body"]');
|
|
699
|
+
if (!body)
|
|
700
|
+
return;
|
|
701
|
+
const token = ++this.activeRevealToken;
|
|
702
|
+
requestAnimationFrame(() => {
|
|
703
|
+
if (token !== this.activeRevealToken)
|
|
704
|
+
return;
|
|
705
|
+
const sourceRect = source.getBoundingClientRect();
|
|
706
|
+
const bodyRect = body.getBoundingClientRect();
|
|
707
|
+
const gap = 12;
|
|
708
|
+
if (sourceRect.top < bodyRect.top + gap) {
|
|
709
|
+
body.scrollTop -= bodyRect.top + gap - sourceRect.top;
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
if (sourceRect.bottom > bodyRect.bottom - gap) {
|
|
713
|
+
body.scrollTop += sourceRect.bottom - (bodyRect.bottom - gap);
|
|
714
|
+
}
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
render() {
|
|
718
|
+
const sidebarProps = this.model.contracts.getSidebarProps();
|
|
719
|
+
const toggleProps = this.model.contracts.getToggleProps();
|
|
720
|
+
const overlayProps = this.model.contracts.getOverlayProps();
|
|
721
|
+
return html `
|
|
722
|
+
<div
|
|
723
|
+
id=${overlayProps.id}
|
|
724
|
+
?hidden=${overlayProps.hidden}
|
|
725
|
+
data-open=${overlayProps['data-open']}
|
|
726
|
+
part="overlay"
|
|
727
|
+
@mousedown=${this.handleOverlayPointerDown}
|
|
728
|
+
></div>
|
|
729
|
+
|
|
730
|
+
<aside
|
|
731
|
+
id=${sidebarProps.id}
|
|
732
|
+
role=${sidebarProps.role}
|
|
733
|
+
aria-label=${sidebarProps['aria-label']}
|
|
734
|
+
aria-modal=${sidebarProps['aria-modal'] ?? nothing}
|
|
735
|
+
?data-collapsed=${sidebarProps['data-collapsed'] === 'true'}
|
|
736
|
+
?data-mobile=${sidebarProps['data-mobile'] === 'true'}
|
|
737
|
+
part="panel"
|
|
738
|
+
@keydown=${this.handlePanelKeyDown}
|
|
739
|
+
>
|
|
740
|
+
<header part="header">
|
|
741
|
+
<slot name="header"></slot>
|
|
742
|
+
<button
|
|
743
|
+
id=${toggleProps.id}
|
|
744
|
+
role=${toggleProps.role}
|
|
745
|
+
tabindex=${toggleProps.tabindex}
|
|
746
|
+
aria-expanded=${toggleProps['aria-expanded']}
|
|
747
|
+
aria-controls=${toggleProps['aria-controls']}
|
|
748
|
+
aria-label=${toggleProps['aria-label']}
|
|
749
|
+
type="button"
|
|
750
|
+
part="toggle"
|
|
751
|
+
@click=${this.handleToggleClick}
|
|
752
|
+
>
|
|
753
|
+
<slot name="toggle">☰</slot>
|
|
754
|
+
</button>
|
|
755
|
+
</header>
|
|
756
|
+
|
|
757
|
+
<nav part="body" @click=${this.handleBodyClick}>
|
|
758
|
+
<slot @slotchange=${this.handleDefaultSlotChange}></slot>
|
|
759
|
+
</nav>
|
|
760
|
+
|
|
761
|
+
<footer part="footer">
|
|
762
|
+
<slot name="footer"></slot>
|
|
763
|
+
</footer>
|
|
764
|
+
</aside>
|
|
765
|
+
`;
|
|
766
|
+
}
|
|
767
|
+
}
|