@georgepxto/glass-nav 0.0.0-stage → 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 +21 -0
- package/README.md +133 -2
- package/dist/glass-nav.d.ts +20 -0
- package/dist/glass-nav.js +283 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/sections.d.ts +4 -0
- package/dist/sections.js +37 -0
- package/dist/styles.d.ts +1 -0
- package/dist/styles.js +175 -0
- package/package.json +44 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 georgepxto
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,134 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `<glass-nav>`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A dependency-free web component for a floating, frosted-glass navigation bar. It highlights the link of the section in view and scrolls to a section without hiding its heading under the bar. Works in any stack: plain HTML, React, Vue, Svelte, Astro, Rails templates.
|
|
4
|
+
|
|
5
|
+
<picture>
|
|
6
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/georgepxto/web-components/main/glass-nav/screenshots/dark.png">
|
|
7
|
+
<img alt="A rounded translucent bar floating over striped content, with a logo, four links with Services highlighted, and a green call-to-action button" src="https://raw.githubusercontent.com/georgepxto/web-components/main/glass-nav/screenshots/light.png" width="880">
|
|
8
|
+
</picture>
|
|
9
|
+
|
|
10
|
+
- No runtime dependencies, Shadow DOM, about 4 kB gzipped
|
|
11
|
+
- Your own links and markup in three slots: logo, links and call to action
|
|
12
|
+
- Highlights the current section with `IntersectionObserver` and `aria-current`, with a pill that slides and stretches from link to link
|
|
13
|
+
- Smooth scrolling that stops below the bar, instant with `prefers-reduced-motion`
|
|
14
|
+
- Takes no space in the page flow
|
|
15
|
+
- In narrow screens the bar wraps into two rows instead of hiding the links behind a menu button
|
|
16
|
+
|
|
17
|
+
<img alt="On a phone the bar becomes a rounded rectangle: logo and button on the first row, the links on the second" src="https://raw.githubusercontent.com/georgepxto/web-components/main/glass-nav/screenshots/mobile-light.png" width="375">
|
|
18
|
+
|
|
19
|
+
<!-- TODO: add a GIF of the active link following the scroll and a click scrolling to a section (screenshots/scroll.gif) -->
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install @georgepxto/glass-nav
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import '@georgepxto/glass-nav';
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Or load it from a CDN with no build step:
|
|
32
|
+
|
|
33
|
+
```html
|
|
34
|
+
<script type="module" src="https://cdn.jsdelivr.net/gh/georgepxto/web-components@main/glass-nav/dist/index.js"></script>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
Put the bar at the top of `<body>`. Links without a `slot` go in the middle and point to sections on the page by `id`:
|
|
40
|
+
|
|
41
|
+
```html
|
|
42
|
+
<glass-nav label="Main">
|
|
43
|
+
<a slot="logo" href="#top">acme</a>
|
|
44
|
+
<a href="#work">Work</a>
|
|
45
|
+
<a href="#process">Process</a>
|
|
46
|
+
<a href="#about">About</a>
|
|
47
|
+
<a slot="cta" class="button" href="#contact">Get in touch</a>
|
|
48
|
+
</glass-nav>
|
|
49
|
+
|
|
50
|
+
<main id="top">
|
|
51
|
+
<section id="work">…</section>
|
|
52
|
+
<section id="process">…</section>
|
|
53
|
+
<section id="about">…</section>
|
|
54
|
+
<section id="contact">…</section>
|
|
55
|
+
</main>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The element is `position: sticky` with a height of zero, so the bar floats over the top of the page and stays there while it scrolls. Give the first block of your page enough top padding to clear it.
|
|
59
|
+
|
|
60
|
+
Links in the `logo` and `cta` slots scroll smoothly too, but only links in the default slot are highlighted.
|
|
61
|
+
|
|
62
|
+
## API
|
|
63
|
+
|
|
64
|
+
### Attributes
|
|
65
|
+
|
|
66
|
+
| Attribute | Default | Description |
|
|
67
|
+
| --- | --- | --- |
|
|
68
|
+
| `offset` | `88` | Distance in pixels kept between the top of the window and a section after scrolling to it. It is never smaller than the bar's own height plus 12px, so a wrapped bar doesn't cover the heading |
|
|
69
|
+
| `label` | | Accessible name of the `<nav>` landmark, for example `Main` |
|
|
70
|
+
| `theme` | `auto` | `light`, `dark` or `auto` (follows `prefers-color-scheme`) |
|
|
71
|
+
| `wrapped` | | Set by the component while the bar is split into two rows. Useful for styling |
|
|
72
|
+
|
|
73
|
+
### Properties
|
|
74
|
+
|
|
75
|
+
| Property | Description |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| `active` | `id` of the section currently highlighted, or `null` |
|
|
78
|
+
| `offset` | The `offset` attribute as a number |
|
|
79
|
+
|
|
80
|
+
### Events
|
|
81
|
+
|
|
82
|
+
| Event | Detail | Description |
|
|
83
|
+
| --- | --- | --- |
|
|
84
|
+
| `activechange` | `{ id }` | Fired when the highlighted section changes. `id` is `null` when no section is in view |
|
|
85
|
+
|
|
86
|
+
## Theming
|
|
87
|
+
|
|
88
|
+
| Property | Default (light / dark) | Description |
|
|
89
|
+
| --- | --- | --- |
|
|
90
|
+
| `--glass-bg` | `#f6f8f6` / `#0b0e0c` | Base color of the glass, used at 58% opacity |
|
|
91
|
+
| `--glass-fg` | `#0d1310` / `#e9eeea` | Text color. The border is mixed from it at 12% |
|
|
92
|
+
| `--glass-active` | `#1d8049` / `#43c97e` | Color of the active link and the focus ring |
|
|
93
|
+
| `--glass-indicator` | `--glass-active` at 12% | Background of the pill behind the active link |
|
|
94
|
+
| `--glass-highlight` | `rgba(255, 255, 255, 0.55)` / `rgba(255, 255, 255, 0.06)` | Inner highlight along the top edge |
|
|
95
|
+
| `--glass-max-width` | `1148px` | Widest the bar gets. It is never wider than the window minus 24px |
|
|
96
|
+
| `--glass-top` | `14px` | Distance from the top of the window |
|
|
97
|
+
| `--glass-wrapped-radius` | `20px` | Corner radius when the bar wraps into two rows |
|
|
98
|
+
| `--glass-z` | `50` | `z-index` of the bar |
|
|
99
|
+
|
|
100
|
+
```css
|
|
101
|
+
glass-nav {
|
|
102
|
+
--glass-bg: #1e1b4b;
|
|
103
|
+
--glass-fg: #eef2ff;
|
|
104
|
+
--glass-active: #a5b4fc;
|
|
105
|
+
--glass-max-width: 960px;
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Style the internals with `::part()`: `nav`, `logo`, `links`, `cta`, `indicator`. Links in the default slot get a pill style, and the `indicator` part is the pill that slides behind the active one; the logo and call to action are left to you. Styles from your page that target `a` directly win over the component's link styles, so scope them if needed, for example `glass-nav a { … }`.
|
|
110
|
+
|
|
111
|
+
## Accessibility
|
|
112
|
+
|
|
113
|
+
- The bar is a `<nav>` landmark; name it with `label`
|
|
114
|
+
- The active link has `aria-current="true"`
|
|
115
|
+
- After a click, focus moves to the section, so keyboard and screen reader users continue from there. Sections that can't take focus get `tabindex="-1"`
|
|
116
|
+
- The address bar is updated with the section's hash, so the back button works
|
|
117
|
+
- With `prefers-reduced-motion: reduce` the page jumps to the section instead of scrolling, and the pill moves to the new link without sliding
|
|
118
|
+
|
|
119
|
+
## Browser support
|
|
120
|
+
|
|
121
|
+
All current browsers. The glass needs `backdrop-filter`; where it is missing, the bar falls back to a nearly opaque background.
|
|
122
|
+
|
|
123
|
+
## Development
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
npm install
|
|
127
|
+
npm test
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`npm test` builds `dist/` and runs the unit tests. To try the demo, serve the package folder over HTTP and open `/demo/`.
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ActiveChangeDetail {
|
|
2
|
+
id: string | null;
|
|
3
|
+
}
|
|
4
|
+
declare const Base: typeof HTMLElement;
|
|
5
|
+
export declare class GlassNav extends Base {
|
|
6
|
+
#private;
|
|
7
|
+
static observedAttributes: string[];
|
|
8
|
+
constructor();
|
|
9
|
+
get active(): string | null;
|
|
10
|
+
get offset(): number;
|
|
11
|
+
connectedCallback(): void;
|
|
12
|
+
disconnectedCallback(): void;
|
|
13
|
+
attributeChangedCallback(name: string, previous: string | null, value: string | null): void;
|
|
14
|
+
}
|
|
15
|
+
declare global {
|
|
16
|
+
interface HTMLElementTagNameMap {
|
|
17
|
+
'glass-nav': GlassNav;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { pickActive, readOffset, scrollTop, sectionId } from './sections.js';
|
|
2
|
+
import { styles } from './styles.js';
|
|
3
|
+
const TAG = 'glass-nav';
|
|
4
|
+
const BAND = '-55%';
|
|
5
|
+
const SCROLL_LOCK = 1200;
|
|
6
|
+
const CLEARANCE = 12;
|
|
7
|
+
const Base = (typeof HTMLElement === 'undefined' ? class {
|
|
8
|
+
} : HTMLElement);
|
|
9
|
+
let sharedSheet;
|
|
10
|
+
const adoptStyles = (shadow) => {
|
|
11
|
+
try {
|
|
12
|
+
if (!sharedSheet) {
|
|
13
|
+
sharedSheet = new CSSStyleSheet();
|
|
14
|
+
sharedSheet.replaceSync(styles);
|
|
15
|
+
}
|
|
16
|
+
shadow.adoptedStyleSheets = [sharedSheet];
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
const style = document.createElement('style');
|
|
20
|
+
style.textContent = styles;
|
|
21
|
+
shadow.append(style);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const region = (className, name) => {
|
|
25
|
+
const element = document.createElement('div');
|
|
26
|
+
element.className = className;
|
|
27
|
+
element.setAttribute('part', className);
|
|
28
|
+
const slot = document.createElement('slot');
|
|
29
|
+
if (name)
|
|
30
|
+
slot.name = name;
|
|
31
|
+
element.append(slot);
|
|
32
|
+
return element;
|
|
33
|
+
};
|
|
34
|
+
const focusable = (element) => element.matches('a[href], button, input, select, textarea, [tabindex]');
|
|
35
|
+
export class GlassNav extends Base {
|
|
36
|
+
static observedAttributes = ['offset', 'label'];
|
|
37
|
+
#nav;
|
|
38
|
+
#clearance = 0;
|
|
39
|
+
#links;
|
|
40
|
+
#indicator;
|
|
41
|
+
#controller;
|
|
42
|
+
#intersection;
|
|
43
|
+
#mutation;
|
|
44
|
+
#resize;
|
|
45
|
+
#reducedMotion;
|
|
46
|
+
#sections = [];
|
|
47
|
+
#intersecting = new Set();
|
|
48
|
+
#active = null;
|
|
49
|
+
#locked = null;
|
|
50
|
+
#lockTimer = 0;
|
|
51
|
+
#frame = 0;
|
|
52
|
+
constructor() {
|
|
53
|
+
super();
|
|
54
|
+
const shadow = this.attachShadow({ mode: 'open' });
|
|
55
|
+
adoptStyles(shadow);
|
|
56
|
+
this.#nav = document.createElement('nav');
|
|
57
|
+
this.#nav.className = 'pill';
|
|
58
|
+
this.#nav.setAttribute('part', 'nav');
|
|
59
|
+
this.#links = region('links');
|
|
60
|
+
this.#indicator = document.createElement('span');
|
|
61
|
+
this.#indicator.className = 'indicator';
|
|
62
|
+
this.#indicator.setAttribute('part', 'indicator');
|
|
63
|
+
this.#indicator.setAttribute('aria-hidden', 'true');
|
|
64
|
+
this.#links.prepend(this.#indicator);
|
|
65
|
+
this.#nav.append(region('logo', 'logo'), this.#links, region('cta', 'cta'));
|
|
66
|
+
shadow.append(this.#nav);
|
|
67
|
+
}
|
|
68
|
+
get active() {
|
|
69
|
+
return this.#active;
|
|
70
|
+
}
|
|
71
|
+
get offset() {
|
|
72
|
+
return readOffset(this.getAttribute('offset'));
|
|
73
|
+
}
|
|
74
|
+
connectedCallback() {
|
|
75
|
+
this.#controller?.abort();
|
|
76
|
+
this.#controller = new AbortController();
|
|
77
|
+
const { signal } = this.#controller;
|
|
78
|
+
this.#reducedMotion = matchMedia('(prefers-reduced-motion: reduce)');
|
|
79
|
+
this.#renderLabel();
|
|
80
|
+
this.addEventListener('click', (event) => this.#onClick(event), { signal });
|
|
81
|
+
this.#links.querySelector('slot')?.addEventListener('slotchange', () => this.#refresh(), { signal });
|
|
82
|
+
this.shadowRoot?.querySelectorAll('slot').forEach((slot) => slot.addEventListener('slotchange', () => this.#measure(), { signal }));
|
|
83
|
+
window.addEventListener('scroll', () => this.#onScroll(), { passive: true, signal });
|
|
84
|
+
window.addEventListener('scrollend', () => this.#unlock(), { passive: true, signal });
|
|
85
|
+
window.addEventListener('hashchange', () => this.#recompute(), { signal });
|
|
86
|
+
this.#mutation = new MutationObserver(() => this.#refresh());
|
|
87
|
+
this.#mutation.observe(this, { childList: true, subtree: true, attributes: true, attributeFilter: ['href'] });
|
|
88
|
+
this.#resize = new ResizeObserver(() => this.#measure());
|
|
89
|
+
this.#resize.observe(this);
|
|
90
|
+
void document.fonts?.ready.then(() => this.#measure());
|
|
91
|
+
if (document.readyState === 'loading') {
|
|
92
|
+
document.addEventListener('DOMContentLoaded', () => this.#refresh(), { once: true, signal });
|
|
93
|
+
}
|
|
94
|
+
this.#refresh();
|
|
95
|
+
this.#measure();
|
|
96
|
+
}
|
|
97
|
+
disconnectedCallback() {
|
|
98
|
+
this.#controller?.abort();
|
|
99
|
+
this.#controller = undefined;
|
|
100
|
+
this.#intersection?.disconnect();
|
|
101
|
+
this.#intersection = undefined;
|
|
102
|
+
this.#mutation?.disconnect();
|
|
103
|
+
this.#mutation = undefined;
|
|
104
|
+
this.#resize?.disconnect();
|
|
105
|
+
this.#resize = undefined;
|
|
106
|
+
cancelAnimationFrame(this.#frame);
|
|
107
|
+
this.#frame = 0;
|
|
108
|
+
window.clearTimeout(this.#lockTimer);
|
|
109
|
+
this.#locked = null;
|
|
110
|
+
}
|
|
111
|
+
#scrollOffset() {
|
|
112
|
+
return Math.max(this.offset, this.#clearance);
|
|
113
|
+
}
|
|
114
|
+
attributeChangedCallback(name, previous, value) {
|
|
115
|
+
if (previous === value || !this.isConnected)
|
|
116
|
+
return;
|
|
117
|
+
if (name === 'label')
|
|
118
|
+
this.#renderLabel();
|
|
119
|
+
else
|
|
120
|
+
this.#observe();
|
|
121
|
+
}
|
|
122
|
+
#renderLabel() {
|
|
123
|
+
const label = this.getAttribute('label');
|
|
124
|
+
if (label)
|
|
125
|
+
this.#nav.setAttribute('aria-label', label);
|
|
126
|
+
else
|
|
127
|
+
this.#nav.removeAttribute('aria-label');
|
|
128
|
+
}
|
|
129
|
+
#navLinks() {
|
|
130
|
+
return Array.from(this.querySelectorAll(':scope > a[href]:not([slot])'));
|
|
131
|
+
}
|
|
132
|
+
#refresh() {
|
|
133
|
+
const byId = new Map();
|
|
134
|
+
for (const link of this.#navLinks()) {
|
|
135
|
+
const id = sectionId(link.getAttribute('href') ?? '', location.href);
|
|
136
|
+
const element = id ? document.getElementById(id) : null;
|
|
137
|
+
if (!id || !element) {
|
|
138
|
+
link.removeAttribute('aria-current');
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const section = byId.get(id) ?? { id, element, links: [] };
|
|
142
|
+
section.links.push(link);
|
|
143
|
+
byId.set(id, section);
|
|
144
|
+
}
|
|
145
|
+
this.#sections = Array.from(byId.values()).sort((a, b) => a.element.compareDocumentPosition(b.element) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1);
|
|
146
|
+
this.#observe();
|
|
147
|
+
this.#placeIndicator(true);
|
|
148
|
+
}
|
|
149
|
+
#observe() {
|
|
150
|
+
this.#intersection?.disconnect();
|
|
151
|
+
this.#intersecting.clear();
|
|
152
|
+
if (!this.#sections.length || !('IntersectionObserver' in window)) {
|
|
153
|
+
this.#recompute();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
this.#intersection = new IntersectionObserver((entries) => {
|
|
157
|
+
for (const entry of entries) {
|
|
158
|
+
const id = entry.target.id;
|
|
159
|
+
if (entry.isIntersecting)
|
|
160
|
+
this.#intersecting.add(id);
|
|
161
|
+
else
|
|
162
|
+
this.#intersecting.delete(id);
|
|
163
|
+
}
|
|
164
|
+
this.#recompute();
|
|
165
|
+
}, { rootMargin: `-${this.#scrollOffset()}px 0px ${BAND} 0px` });
|
|
166
|
+
this.#sections.forEach((section) => this.#intersection?.observe(section.element));
|
|
167
|
+
}
|
|
168
|
+
#onScroll() {
|
|
169
|
+
if (this.#frame)
|
|
170
|
+
return;
|
|
171
|
+
this.#frame = requestAnimationFrame(() => {
|
|
172
|
+
this.#frame = 0;
|
|
173
|
+
this.#recompute();
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
#recompute() {
|
|
177
|
+
const ordered = this.#sections.map((section) => section.id);
|
|
178
|
+
const root = document.documentElement;
|
|
179
|
+
const scrollable = root.scrollHeight > innerHeight + 2;
|
|
180
|
+
const atEnd = scrollable && innerHeight + scrollY >= root.scrollHeight - 2;
|
|
181
|
+
const next = this.#locked ?? pickActive(ordered, this.#intersecting, atEnd);
|
|
182
|
+
this.#setActive(next);
|
|
183
|
+
}
|
|
184
|
+
#setActive(id) {
|
|
185
|
+
for (const section of this.#sections) {
|
|
186
|
+
for (const link of section.links) {
|
|
187
|
+
if (section.id === id)
|
|
188
|
+
link.setAttribute('aria-current', 'true');
|
|
189
|
+
else
|
|
190
|
+
link.removeAttribute('aria-current');
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (id === this.#active)
|
|
194
|
+
return;
|
|
195
|
+
this.#active = id;
|
|
196
|
+
this.#placeIndicator();
|
|
197
|
+
this.dispatchEvent(new CustomEvent('activechange', { bubbles: true, detail: { id } }));
|
|
198
|
+
}
|
|
199
|
+
#unlock() {
|
|
200
|
+
if (!this.#locked)
|
|
201
|
+
return;
|
|
202
|
+
window.clearTimeout(this.#lockTimer);
|
|
203
|
+
this.#locked = null;
|
|
204
|
+
this.#recompute();
|
|
205
|
+
}
|
|
206
|
+
#onClick(event) {
|
|
207
|
+
if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const link = event.target?.closest?.('a[href]');
|
|
211
|
+
if (!link || !this.contains(link) || link.hasAttribute('download'))
|
|
212
|
+
return;
|
|
213
|
+
if (link.target && link.target !== '_self')
|
|
214
|
+
return;
|
|
215
|
+
const id = sectionId(link.getAttribute('href') ?? '', location.href);
|
|
216
|
+
const element = id ? document.getElementById(id) : null;
|
|
217
|
+
if (!id || !element)
|
|
218
|
+
return;
|
|
219
|
+
event.preventDefault();
|
|
220
|
+
const reduced = this.#reducedMotion?.matches ?? false;
|
|
221
|
+
window.scrollTo({
|
|
222
|
+
top: scrollTop(element.getBoundingClientRect().top, scrollY, this.#scrollOffset()),
|
|
223
|
+
behavior: reduced ? 'auto' : 'smooth',
|
|
224
|
+
});
|
|
225
|
+
if (location.hash !== `#${encodeURIComponent(id)}` && location.hash !== `#${id}`) {
|
|
226
|
+
history.pushState(history.state, '', `#${encodeURIComponent(id)}`);
|
|
227
|
+
}
|
|
228
|
+
if (!focusable(element))
|
|
229
|
+
element.setAttribute('tabindex', '-1');
|
|
230
|
+
element.focus({ preventScroll: true });
|
|
231
|
+
if (this.#sections.some((section) => section.id === id)) {
|
|
232
|
+
this.#locked = id;
|
|
233
|
+
window.clearTimeout(this.#lockTimer);
|
|
234
|
+
this.#lockTimer = window.setTimeout(() => this.#unlock(), reduced ? 0 : SCROLL_LOCK);
|
|
235
|
+
this.#recompute();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
#measure() {
|
|
239
|
+
if (!this.isConnected)
|
|
240
|
+
return;
|
|
241
|
+
const wrapped = this.hasAttribute('wrapped');
|
|
242
|
+
if (wrapped)
|
|
243
|
+
this.removeAttribute('wrapped');
|
|
244
|
+
const overflowing = this.#links.scrollWidth > this.#links.clientWidth + 1;
|
|
245
|
+
if (overflowing)
|
|
246
|
+
this.setAttribute('wrapped', '');
|
|
247
|
+
this.#placeIndicator(true);
|
|
248
|
+
const clearance = Math.ceil((parseFloat(getComputedStyle(this.#nav).top) || 0) + this.#nav.offsetHeight + CLEARANCE);
|
|
249
|
+
if (clearance === this.#clearance)
|
|
250
|
+
return;
|
|
251
|
+
this.#clearance = clearance;
|
|
252
|
+
if (this.#sections.length)
|
|
253
|
+
this.#observe();
|
|
254
|
+
}
|
|
255
|
+
#placeIndicator(snap = false) {
|
|
256
|
+
const indicator = this.#indicator;
|
|
257
|
+
const link = this.#sections
|
|
258
|
+
.find((section) => section.id === this.#active)
|
|
259
|
+
?.links.find((candidate) => candidate.getClientRects().length > 0);
|
|
260
|
+
if (!link || !this.#links.clientWidth) {
|
|
261
|
+
indicator.classList.remove('visible');
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const box = this.#links.getBoundingClientRect();
|
|
265
|
+
const rect = link.getBoundingClientRect();
|
|
266
|
+
const x = rect.left - box.left - this.#links.clientLeft + this.#links.scrollLeft;
|
|
267
|
+
const y = rect.top - box.top - this.#links.clientTop + this.#links.scrollTop;
|
|
268
|
+
const instant = snap || !indicator.classList.contains('visible');
|
|
269
|
+
if (instant)
|
|
270
|
+
indicator.style.transitionProperty = 'opacity';
|
|
271
|
+
indicator.style.transform = `translate(${x}px, ${y}px)`;
|
|
272
|
+
indicator.style.width = `${rect.width}px`;
|
|
273
|
+
indicator.style.height = `${rect.height}px`;
|
|
274
|
+
if (instant)
|
|
275
|
+
void indicator.offsetWidth;
|
|
276
|
+
indicator.classList.add('visible');
|
|
277
|
+
if (instant)
|
|
278
|
+
indicator.style.transitionProperty = '';
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (typeof customElements !== 'undefined' && !customElements.get(TAG)) {
|
|
282
|
+
customElements.define(TAG, GlassNav);
|
|
283
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const sectionId: (href: string, base: string) => string | null;
|
|
2
|
+
export declare const pickActive: (ordered: string[], intersecting: ReadonlySet<string>, atEnd: boolean) => string | null;
|
|
3
|
+
export declare const scrollTop: (top: number, scrollY: number, offset: number) => number;
|
|
4
|
+
export declare const readOffset: (value: string | null | undefined, fallback?: number) => number;
|
package/dist/sections.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const sectionId = (href, base) => {
|
|
2
|
+
let url;
|
|
3
|
+
let page;
|
|
4
|
+
try {
|
|
5
|
+
url = new URL(href, base);
|
|
6
|
+
page = new URL(base);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
if (url.origin !== page.origin || url.pathname !== page.pathname || url.search !== page.search)
|
|
12
|
+
return null;
|
|
13
|
+
if (url.hash.length < 2)
|
|
14
|
+
return null;
|
|
15
|
+
try {
|
|
16
|
+
return decodeURIComponent(url.hash.slice(1));
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
export const pickActive = (ordered, intersecting, atEnd) => {
|
|
23
|
+
if (atEnd && ordered.length)
|
|
24
|
+
return ordered[ordered.length - 1];
|
|
25
|
+
for (let index = ordered.length - 1; index >= 0; index--) {
|
|
26
|
+
if (intersecting.has(ordered[index]))
|
|
27
|
+
return ordered[index];
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
};
|
|
31
|
+
export const scrollTop = (top, scrollY, offset) => Math.max(0, Math.round(top + scrollY - offset));
|
|
32
|
+
export const readOffset = (value, fallback = 88) => {
|
|
33
|
+
if (value === null || value === undefined || value.trim() === '')
|
|
34
|
+
return fallback;
|
|
35
|
+
const number = Number(value);
|
|
36
|
+
return Number.isFinite(number) ? Math.max(0, number) : fallback;
|
|
37
|
+
};
|
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const styles = "\n :host {\n \n --_bg: var(--glass-bg, #f6f8f6);\n --_fg: var(--glass-fg, #0d1310);\n --_active: var(--glass-active, #1d8049);\n --_highlight: var(--glass-highlight, rgba(255, 255, 255, 0.55));\n\n position: sticky;\n top: 0;\n z-index: var(--glass-z, 50);\n display: block;\n height: 0;\n }\n\n :host([theme='dark']) {\n \n --_bg: var(--glass-bg, #0b0e0c);\n --_fg: var(--glass-fg, #e9eeea);\n --_active: var(--glass-active, #43c97e);\n --_highlight: var(--glass-highlight, rgba(255, 255, 255, 0.06));\n\n }\n\n @media (prefers-color-scheme: dark) {\n :host(:not([theme='light'])) {\n \n --_bg: var(--glass-bg, #0b0e0c);\n --_fg: var(--glass-fg, #e9eeea);\n --_active: var(--glass-active, #43c97e);\n --_highlight: var(--glass-highlight, rgba(255, 255, 255, 0.06));\n\n }\n }\n\n :host([hidden]) {\n display: none;\n }\n\n *,\n *::before,\n *::after {\n box-sizing: border-box;\n }\n\n .pill {\n position: relative;\n top: var(--glass-top, 14px);\n display: flex;\n align-items: center;\n gap: 8px;\n width: min(var(--glass-max-width, 1148px), calc(100% - 24px));\n min-height: 58px;\n margin-inline: auto;\n padding: 8px 8px 8px 20px;\n border: 1px solid color-mix(in oklab, var(--_fg) 12%, transparent);\n border-radius: 29px;\n background: color-mix(in oklab, var(--_bg) 58%, transparent);\n -webkit-backdrop-filter: blur(18px) saturate(160%);\n backdrop-filter: blur(18px) saturate(160%);\n box-shadow:\n 0 12px 32px -8px rgba(0, 0, 0, 0.28),\n inset 0 1px 0 var(--_highlight);\n color: var(--_fg);\n }\n\n .logo,\n .cta {\n display: flex;\n flex: none;\n align-items: center;\n gap: 8px;\n }\n\n .logo {\n min-width: 0;\n }\n\n .links {\n position: relative;\n isolation: isolate;\n display: flex;\n flex: 0 1 auto;\n align-items: center;\n gap: 2px;\n min-width: 0;\n margin-left: auto;\n overflow: hidden;\n }\n\n .indicator {\n position: absolute;\n top: 0;\n left: 0;\n z-index: -1;\n width: 0;\n height: 0;\n border-radius: 999px;\n background: var(--glass-indicator, color-mix(in oklab, var(--_active) 12%, transparent));\n opacity: 0;\n pointer-events: none;\n transition:\n transform 0.55s cubic-bezier(0.2, 0.8, 0.2, 1),\n width 0.55s cubic-bezier(0.2, 0.8, 0.2, 1),\n height 0.55s cubic-bezier(0.2, 0.8, 0.2, 1),\n opacity 0.3s ease;\n }\n\n .indicator.visible {\n opacity: 1;\n }\n\n ::slotted(a) {\n display: inline-flex;\n flex: none;\n align-items: center;\n min-height: 36px;\n padding: 0 12px;\n border-radius: 999px;\n color: color-mix(in oklab, var(--_fg) 72%, transparent);\n font-size: 14px;\n line-height: 1;\n text-decoration: none;\n white-space: nowrap;\n transition: color 0.15s ease, background-color 0.15s ease;\n }\n\n ::slotted(a:hover) {\n color: var(--_fg);\n }\n\n ::slotted(a[aria-current='true']) {\n color: var(--_active);\n transition: color 0.3s ease;\n }\n\n ::slotted(a:focus-visible) {\n outline: 2px solid var(--_active);\n outline-offset: 2px;\n }\n\n :host([wrapped]) .pill {\n flex-wrap: wrap;\n row-gap: 4px;\n padding: 8px 8px 6px 16px;\n border-radius: var(--glass-wrapped-radius, 20px);\n }\n\n :host([wrapped]) .cta {\n margin-left: auto;\n }\n\n :host([wrapped]) .links {\n order: 3;\n flex: 1 1 100%;\n margin: 0 -2px 0 -10px;\n padding-top: 4px;\n border-top: 1px solid color-mix(in oklab, var(--_fg) 8%, transparent);\n overflow-x: auto;\n scrollbar-width: none;\n }\n\n :host([wrapped]) .links::-webkit-scrollbar {\n display: none;\n }\n\n @media (prefers-reduced-motion: reduce) {\n .indicator {\n transition: opacity 0.3s ease;\n }\n }\n\n @supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {\n .pill {\n background: color-mix(in oklab, var(--_bg) 94%, transparent);\n }\n }\n";
|
package/dist/styles.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
const light = `
|
|
2
|
+
--_bg: var(--glass-bg, #f6f8f6);
|
|
3
|
+
--_fg: var(--glass-fg, #0d1310);
|
|
4
|
+
--_active: var(--glass-active, #1d8049);
|
|
5
|
+
--_highlight: var(--glass-highlight, rgba(255, 255, 255, 0.55));
|
|
6
|
+
`;
|
|
7
|
+
const dark = `
|
|
8
|
+
--_bg: var(--glass-bg, #0b0e0c);
|
|
9
|
+
--_fg: var(--glass-fg, #e9eeea);
|
|
10
|
+
--_active: var(--glass-active, #43c97e);
|
|
11
|
+
--_highlight: var(--glass-highlight, rgba(255, 255, 255, 0.06));
|
|
12
|
+
`;
|
|
13
|
+
export const styles = `
|
|
14
|
+
:host {
|
|
15
|
+
${light}
|
|
16
|
+
position: sticky;
|
|
17
|
+
top: 0;
|
|
18
|
+
z-index: var(--glass-z, 50);
|
|
19
|
+
display: block;
|
|
20
|
+
height: 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
:host([theme='dark']) {
|
|
24
|
+
${dark}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@media (prefers-color-scheme: dark) {
|
|
28
|
+
:host(:not([theme='light'])) {
|
|
29
|
+
${dark}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
:host([hidden]) {
|
|
34
|
+
display: none;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
*,
|
|
38
|
+
*::before,
|
|
39
|
+
*::after {
|
|
40
|
+
box-sizing: border-box;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
.pill {
|
|
44
|
+
position: relative;
|
|
45
|
+
top: var(--glass-top, 14px);
|
|
46
|
+
display: flex;
|
|
47
|
+
align-items: center;
|
|
48
|
+
gap: 8px;
|
|
49
|
+
width: min(var(--glass-max-width, 1148px), calc(100% - 24px));
|
|
50
|
+
min-height: 58px;
|
|
51
|
+
margin-inline: auto;
|
|
52
|
+
padding: 8px 8px 8px 20px;
|
|
53
|
+
border: 1px solid color-mix(in oklab, var(--_fg) 12%, transparent);
|
|
54
|
+
border-radius: 29px;
|
|
55
|
+
background: color-mix(in oklab, var(--_bg) 58%, transparent);
|
|
56
|
+
-webkit-backdrop-filter: blur(18px) saturate(160%);
|
|
57
|
+
backdrop-filter: blur(18px) saturate(160%);
|
|
58
|
+
box-shadow:
|
|
59
|
+
0 12px 32px -8px rgba(0, 0, 0, 0.28),
|
|
60
|
+
inset 0 1px 0 var(--_highlight);
|
|
61
|
+
color: var(--_fg);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.logo,
|
|
65
|
+
.cta {
|
|
66
|
+
display: flex;
|
|
67
|
+
flex: none;
|
|
68
|
+
align-items: center;
|
|
69
|
+
gap: 8px;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.logo {
|
|
73
|
+
min-width: 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.links {
|
|
77
|
+
position: relative;
|
|
78
|
+
isolation: isolate;
|
|
79
|
+
display: flex;
|
|
80
|
+
flex: 0 1 auto;
|
|
81
|
+
align-items: center;
|
|
82
|
+
gap: 2px;
|
|
83
|
+
min-width: 0;
|
|
84
|
+
margin-left: auto;
|
|
85
|
+
overflow: hidden;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.indicator {
|
|
89
|
+
position: absolute;
|
|
90
|
+
top: 0;
|
|
91
|
+
left: 0;
|
|
92
|
+
z-index: -1;
|
|
93
|
+
width: 0;
|
|
94
|
+
height: 0;
|
|
95
|
+
border-radius: 999px;
|
|
96
|
+
background: var(--glass-indicator, color-mix(in oklab, var(--_active) 12%, transparent));
|
|
97
|
+
opacity: 0;
|
|
98
|
+
pointer-events: none;
|
|
99
|
+
transition:
|
|
100
|
+
transform 0.55s cubic-bezier(0.2, 0.8, 0.2, 1),
|
|
101
|
+
width 0.55s cubic-bezier(0.2, 0.8, 0.2, 1),
|
|
102
|
+
height 0.55s cubic-bezier(0.2, 0.8, 0.2, 1),
|
|
103
|
+
opacity 0.3s ease;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.indicator.visible {
|
|
107
|
+
opacity: 1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
::slotted(a) {
|
|
111
|
+
display: inline-flex;
|
|
112
|
+
flex: none;
|
|
113
|
+
align-items: center;
|
|
114
|
+
min-height: 36px;
|
|
115
|
+
padding: 0 12px;
|
|
116
|
+
border-radius: 999px;
|
|
117
|
+
color: color-mix(in oklab, var(--_fg) 72%, transparent);
|
|
118
|
+
font-size: 14px;
|
|
119
|
+
line-height: 1;
|
|
120
|
+
text-decoration: none;
|
|
121
|
+
white-space: nowrap;
|
|
122
|
+
transition: color 0.15s ease, background-color 0.15s ease;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
::slotted(a:hover) {
|
|
126
|
+
color: var(--_fg);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
::slotted(a[aria-current='true']) {
|
|
130
|
+
color: var(--_active);
|
|
131
|
+
transition: color 0.3s ease;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
::slotted(a:focus-visible) {
|
|
135
|
+
outline: 2px solid var(--_active);
|
|
136
|
+
outline-offset: 2px;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
:host([wrapped]) .pill {
|
|
140
|
+
flex-wrap: wrap;
|
|
141
|
+
row-gap: 4px;
|
|
142
|
+
padding: 8px 8px 6px 16px;
|
|
143
|
+
border-radius: var(--glass-wrapped-radius, 20px);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
:host([wrapped]) .cta {
|
|
147
|
+
margin-left: auto;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
:host([wrapped]) .links {
|
|
151
|
+
order: 3;
|
|
152
|
+
flex: 1 1 100%;
|
|
153
|
+
margin: 0 -2px 0 -10px;
|
|
154
|
+
padding-top: 4px;
|
|
155
|
+
border-top: 1px solid color-mix(in oklab, var(--_fg) 8%, transparent);
|
|
156
|
+
overflow-x: auto;
|
|
157
|
+
scrollbar-width: none;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
:host([wrapped]) .links::-webkit-scrollbar {
|
|
161
|
+
display: none;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@media (prefers-reduced-motion: reduce) {
|
|
165
|
+
.indicator {
|
|
166
|
+
transition: opacity 0.3s ease;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
|
|
171
|
+
.pill {
|
|
172
|
+
background: color-mix(in oklab, var(--_bg) 94%, transparent);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@georgepxto/glass-nav",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@georgepxto/glass-nav",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A dependency-free <glass-nav> web component: a floating frosted-glass navigation bar that highlights the section in view and scrolls to it with an offset.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "georgepxto",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/georgepxto/web-components.git",
|
|
11
|
+
"directory": "glass-nav"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"web-component",
|
|
15
|
+
"custom-element",
|
|
16
|
+
"navigation",
|
|
17
|
+
"navbar",
|
|
18
|
+
"glassmorphism",
|
|
19
|
+
"scrollspy"
|
|
20
|
+
],
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"sideEffects": [
|
|
33
|
+
"./dist/index.js",
|
|
34
|
+
"./dist/glass-nav.js"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc -p tsconfig.json",
|
|
38
|
+
"test": "npm run build && node --test test/*.test.js",
|
|
39
|
+
"prepublishOnly": "npm test"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"typescript": "^7.0.2"
|
|
43
|
+
}
|
|
44
|
+
}
|