@plusguard/anchor 0.0.0-stage → 3.4.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 +666 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.js +761 -0
- package/early.js +37 -0
- package/index.css +166 -0
- package/index.ts +780 -0
- package/package.json +22 -5
package/README.md
CHANGED
|
@@ -1,3 +1,667 @@
|
|
|
1
|
-
#
|
|
1
|
+
# anchor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Scrolling to a place on the page, straight from markup: to a section, the
|
|
4
|
+
top or the bottom, with a glide of a set length — the same with or without
|
|
5
|
+
Lenis. A back-to-top button that shows after a set scroll and steps out of
|
|
6
|
+
the way of marked areas, menus that mark the section you are in, tabs that
|
|
7
|
+
bring the reader back to the start of the one they open, and rows that
|
|
8
|
+
scroll sideways keeping their current item in view. One script, one
|
|
9
|
+
stylesheet, no dependencies, framework-agnostic.
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<a href="#contact" data-pg-anchor>Contact</a>
|
|
13
|
+
<div data-pg-anchor="#products">…a whole card…</div>
|
|
14
|
+
|
|
15
|
+
<button data-pg-anchor="top" data-pg-anchor-visible="false" aria-label="Back to top">↑</button>
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install @plusguard/anchor
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { initAnchor } from "@plusguard/anchor";
|
|
24
|
+
import "@plusguard/anchor/index.css";
|
|
25
|
+
|
|
26
|
+
initAnchor();
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Or from CSS, which lets you pick the cascade layer:
|
|
30
|
+
|
|
31
|
+
```css
|
|
32
|
+
@import "@plusguard/anchor/index.css" layer(components);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Without a build step
|
|
36
|
+
|
|
37
|
+
Load both halves straight from jsDelivr — no install, and the page works
|
|
38
|
+
when opened by double-clicking:
|
|
39
|
+
|
|
40
|
+
```html
|
|
41
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@plusguard/anchor@3.4.0/index.css" />
|
|
42
|
+
|
|
43
|
+
<script type="module">
|
|
44
|
+
import { initAnchor } from "https://cdn.jsdelivr.net/npm/@plusguard/anchor@3.4.0/dist/index.js";
|
|
45
|
+
|
|
46
|
+
initAnchor();
|
|
47
|
+
</script>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Pin the version (`@3.4.0`) on a live site. A pinned URL never changes;
|
|
51
|
+
`@3` follows the newest 3.x release.
|
|
52
|
+
|
|
53
|
+
## Who does what
|
|
54
|
+
|
|
55
|
+
A plain `<a href="#contact">` already scrolls, and two lines of CSS make it
|
|
56
|
+
smooth and keep it clear of a fixed header. Use those wherever they are
|
|
57
|
+
enough; this package is for the rest.
|
|
58
|
+
|
|
59
|
+
| Need | Browser / Lenis alone | anchor |
|
|
60
|
+
| ------------------------------------------- | -------------------------------------------------- | ------------------------------------------------- |
|
|
61
|
+
| Smooth scroll to `#id` | `scroll-behavior: smooth`, or Lenis `anchors` | ✓ |
|
|
62
|
+
| Clear a fixed header | `scroll-padding-top` / `scroll-margin-top` | ✓ reads the same CSS, plus `--pg-anchor-edge` |
|
|
63
|
+
| How long the scroll takes | the browser decides; Lenis only | ✓ `--pg-anchor-duration`, per screen size, with or without Lenis |
|
|
64
|
+
| A `<div>` or a `<button>` that scrolls | — | ✓ made reachable by keyboard too |
|
|
65
|
+
| Keep the address bar as it is | — the `#id` is always written | ✓ `data-pg-anchor-hash`: as it is, written with a Back step, or written in place |
|
|
66
|
+
| Stop when the user scrolls mid-way | Lenis only | ✓ |
|
|
67
|
+
| Show a button after some scrolling | — | ✓ `data-pg-anchor-visible` |
|
|
68
|
+
| Keep it off the footer and other areas | — | ✓ `data-pg-anchor-avoid`: step aside, or pass under |
|
|
69
|
+
| Mark the menu link of the current section | — | ✓ `data-pg-anchor-spy`, any number of menus |
|
|
70
|
+
| Only back up to a tab's start, never down | — | ✓ `--pg-anchor-when: passed`, per screen size |
|
|
71
|
+
| Keep an accordion's question in place while the answer above closes | `overflow-anchor`, which picks the closing answer itself | ✓ `data-pg-anchor-stay`, any animation |
|
|
72
|
+
| Keep a sideways row's current item in view | `scrollIntoView`, by hand, on every change | ✓ `data-pg-anchor-strip`: centred, its own speed |
|
|
73
|
+
|
|
74
|
+
## Files
|
|
75
|
+
|
|
76
|
+
| File | For |
|
|
77
|
+
| ----------------- | ---------------------------------------------------------------- |
|
|
78
|
+
| `index.ts` | TypeScript source — import it directly in a TS / bundler project |
|
|
79
|
+
| `index.css` | The CSS half: the lengths, hiding, stepping aside, passing under |
|
|
80
|
+
| `dist/index.js` | Compiled ES module — plain HTML, no build step |
|
|
81
|
+
| `dist/index.d.ts` | Types for `dist/index.js` |
|
|
82
|
+
| `early.js` | A small classic script for `<head>`: on a reload, the back-to-top button from the first frame (see Back to top) |
|
|
83
|
+
|
|
84
|
+
Importing `dist/index.js` from your own files? Serve the page over HTTP —
|
|
85
|
+
browsers block module imports on `file://`.
|
|
86
|
+
|
|
87
|
+
`dist/` is committed. After editing `index.ts`, rebuild it:
|
|
88
|
+
|
|
89
|
+
```sh
|
|
90
|
+
npx -p typescript tsc index.ts --outDir dist --declaration --target es2020 --module es2020 --lib dom,es2020 --strict
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Attributes
|
|
94
|
+
|
|
95
|
+
| Attribute | On | Meaning |
|
|
96
|
+
| ---------------------------- | ------------------------ | ----------------------------------------------------------------------- |
|
|
97
|
+
| `data-pg-anchor` | anything clickable | The target: `"#id"`, `"top"` or `"bottom"`. On a link, empty: its `#hash` |
|
|
98
|
+
| `data-pg-anchor-hash` | it, or anything above | `"true"` writes `#id` to the address bar, a Back step each time; `"replace"` writes it in place — no step; `"false"` (default) does not |
|
|
99
|
+
| `data-pg-anchor-visible` | a floating element | Write `"false"`; anchor sets `"true"` once the page has scrolled enough |
|
|
100
|
+
| `data-pg-anchor-progress` | a floating element | A ring round it, filling as the page scrolls — see Back to top |
|
|
101
|
+
| `data-pg-anchor-avoid` | an area | Floating elements step out of its way; `"hide"`: they pass under it |
|
|
102
|
+
| `data-pg-anchor-spy` | a menu | Its links mark the section you are in |
|
|
103
|
+
| `data-pg-anchor-strip` | a row that scrolls sideways | Keeps its current item in view |
|
|
104
|
+
| `data-pg-anchor-stay` | an accordion, or any box | The item clicked keeps its place on screen while the page above changes |
|
|
105
|
+
| `data-pg-anchor-covered` | a floating element | `"true"` while wholly under a `"hide"` area, written by anchor |
|
|
106
|
+
| `data-pg-anchor-current` | a link in a spy menu | `"true"` or `"false"`, written by anchor |
|
|
107
|
+
|
|
108
|
+
"It, or anything above": the nearest one wins, so a value on `<body>` is
|
|
109
|
+
the site's default and a single link can still set its own.
|
|
110
|
+
|
|
111
|
+
```html
|
|
112
|
+
<body data-pg-anchor-hash="true">
|
|
113
|
+
…
|
|
114
|
+
<a href="#prices" data-pg-anchor data-pg-anchor-hash="false">Prices</a>
|
|
115
|
+
</body>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## CSS variables
|
|
119
|
+
|
|
120
|
+
Whatever changes with the screen is CSS, not an attribute — no pixels and
|
|
121
|
+
no media queries in the markup. Set them on the element or anywhere above
|
|
122
|
+
it, in media queries or with Tailwind's `md:[--…]`; `1rem`, `100svh` and
|
|
123
|
+
`calc()` all work.
|
|
124
|
+
|
|
125
|
+
| Variable | Default | Meaning |
|
|
126
|
+
| ----------------------------- | -------- | --------------------------------------------------------------------- |
|
|
127
|
+
| `--pg-anchor-duration` | `1200ms` | On a link: how long the page glides; `0s` jumps |
|
|
128
|
+
| `--pg-anchor-easing` | Lenis's curve | On a link: `linear`, `ease`, `ease-in`, `ease-out`, `ease-in-out` or `cubic-bezier(…)` |
|
|
129
|
+
| `--pg-anchor-edge` | `0px` | Extra room above a section it lands on |
|
|
130
|
+
| `--pg-anchor-show-after` | `400px` | How far the page scrolls before a `data-pg-anchor-visible` element shows |
|
|
131
|
+
| `--pg-anchor-avoid-space` | `0px` | The gap kept from an avoided area |
|
|
132
|
+
| `--pg-anchor-progress-color` | `currentColor` | The ring's filled part |
|
|
133
|
+
| `--pg-anchor-progress-track` | `transparent` | The ring's rest |
|
|
134
|
+
| `--pg-anchor-progress-width` | `2px` | The ring's thickness; `0px` hides it — on one screen size, say |
|
|
135
|
+
| `--pg-anchor-avoid` | `auto` | On a floating element: `none` ignores avoided areas |
|
|
136
|
+
| `--pg-anchor-when` | `always` | On a link: when a click scrolls — `always`, `passed`, `never` |
|
|
137
|
+
| `--pg-anchor-stay` | `auto` | Inside a stay box: `none` lets the item clicked move |
|
|
138
|
+
| `--pg-anchor-strip` | `center` | On a strip: where its current item goes — `center`, `start`, `end`, `none` |
|
|
139
|
+
| `--pg-anchor-strip-duration` | `500ms` | On a strip: how long it slides; `0s` is at once |
|
|
140
|
+
| `--pg-anchor-strip-easing` | Lenis's curve | On a strip: `linear`, `ease`, `ease-in`, `ease-out`, `ease-in-out` or `cubic-bezier(…)` |
|
|
141
|
+
|
|
142
|
+
anchor writes these, to read in your CSS:
|
|
143
|
+
|
|
144
|
+
| Variable | On | Meaning |
|
|
145
|
+
| --------------------------------------- | ------------------ | --------------------------------------------- |
|
|
146
|
+
| `--pg-anchor-current-left` / `-top` | a spy menu | Where the current link sits inside the menu |
|
|
147
|
+
| `--pg-anchor-current-width` / `-height` | a spy menu | Its size — `0px` when no section is current |
|
|
148
|
+
| `--pg-anchor-progress` | a floating element | `0` to `1`: how far the page has scrolled since it showed (used by `index.css`) |
|
|
149
|
+
| `--pg-anchor-lift` | a floating element | How far it stepped aside (used by `index.css`) |
|
|
150
|
+
| `--pg-anchor-clip` | a floating element | The part a `"hide"` area covers (used by `index.css`) |
|
|
151
|
+
|
|
152
|
+
## Where a section lands
|
|
153
|
+
|
|
154
|
+
**Plain CSS first.** A section lands below the page's `scroll-padding-top`
|
|
155
|
+
and its own `scroll-margin-top` — the same rules the browser uses for a
|
|
156
|
+
plain `#link`, so both kinds of link land in the same place. Clear a fixed
|
|
157
|
+
header there, per screen size:
|
|
158
|
+
|
|
159
|
+
```css
|
|
160
|
+
:root {
|
|
161
|
+
scroll-padding-top: 4.5rem; /* the header on phones, and some room */
|
|
162
|
+
}
|
|
163
|
+
@media (width >= 48rem) {
|
|
164
|
+
:root {
|
|
165
|
+
scroll-padding-top: 5.5rem;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Or per section — handy when only part of the page has a sticky header:
|
|
171
|
+
|
|
172
|
+
```html
|
|
173
|
+
<section id="prices" class="scroll-mt-20 md:scroll-mt-28">…</section>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
**Special cases: `--pg-anchor-edge`.** Room added on top of the CSS above,
|
|
177
|
+
on the clicked element or anything above it — one link that should land a
|
|
178
|
+
little lower than the rest:
|
|
179
|
+
|
|
180
|
+
```html
|
|
181
|
+
<button data-pg-anchor="#prices" style="--pg-anchor-edge: 3rem">Prices</button>
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## The glide
|
|
185
|
+
|
|
186
|
+
anchor scrolls frame by frame for `--pg-anchor-duration` — `1200ms` by
|
|
187
|
+
default — on `--pg-anchor-easing`, by default Lenis's curve: off fast,
|
|
188
|
+
then a long soft landing. The user's wheel, a touch or a key stops it. With
|
|
189
|
+
reduced motion asked for, it jumps.
|
|
190
|
+
|
|
191
|
+
Both are CSS, on the link or anywhere above it: the site's default on
|
|
192
|
+
`:root`, a single link its own, and a phone its own in a media query.
|
|
193
|
+
|
|
194
|
+
```css
|
|
195
|
+
:root {
|
|
196
|
+
--pg-anchor-duration: 1.9s;
|
|
197
|
+
}
|
|
198
|
+
@media (width < 48rem) {
|
|
199
|
+
:root {
|
|
200
|
+
--pg-anchor-duration: 1.2s; /* a shorter page, a shorter glide */
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
```html
|
|
206
|
+
<button data-pg-anchor="#prices" style="--pg-anchor-duration: 400ms">Prices</button>
|
|
207
|
+
<a href="#contact" data-pg-anchor class="[--pg-anchor-easing:ease-in-out]">Contact</a>
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
A click that also closes a menu (an off-canvas sidebar locking the page)
|
|
211
|
+
is fine: the scroll starts a frame later, after the menu has let go. What
|
|
212
|
+
the link asks for — `--pg-anchor-when`, `-duration`, `-easing`, `-edge`,
|
|
213
|
+
`data-pg-anchor-hash` — is read as it is clicked, so a link the same click
|
|
214
|
+
takes away still gets it: a copy in fit-more-nav's "More", whose list is
|
|
215
|
+
written again when the tab picked comes into the row.
|
|
216
|
+
|
|
217
|
+
### With Lenis
|
|
218
|
+
|
|
219
|
+
Hand Lenis in, and Lenis glides instead — the same duration and curve,
|
|
220
|
+
handed to it:
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
import Lenis from "lenis";
|
|
224
|
+
import { initAnchor, setAnchorScroller } from "@plusguard/anchor";
|
|
225
|
+
|
|
226
|
+
const lenis = new Lenis({ autoRaf: true });
|
|
227
|
+
|
|
228
|
+
initAnchor();
|
|
229
|
+
setAnchorScroller((top, ms, easing) =>
|
|
230
|
+
lenis.scrollTo(top, ms ? { duration: ms / 1000, easing } : { immediate: true }),
|
|
231
|
+
);
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
`anchorEasing`, Lenis's curve, is exported too.
|
|
235
|
+
|
|
236
|
+
`setAnchorScroller(null)` goes back to the built-in glide. Leave Lenis's
|
|
237
|
+
own `anchors` option off — both would scroll on the same click.
|
|
238
|
+
|
|
239
|
+
## The address bar
|
|
240
|
+
|
|
241
|
+
By default the address is left as it is, as the old theme did. To write
|
|
242
|
+
the `#id` there — so the link can be shared, and a reload lands where you
|
|
243
|
+
are — choose how Back should go:
|
|
244
|
+
|
|
245
|
+
| `data-pg-anchor-hash` | The address | Back |
|
|
246
|
+
| --------------------- | --------------------------------- | ------------------------------------------- |
|
|
247
|
+
| `"false"` (default) | left as it is | the page before |
|
|
248
|
+
| `"true"` | ends in the `#id` pressed | through every `#id` pressed, then the page before |
|
|
249
|
+
| `"replace"` | ends in the `#id` pressed | the page before — no step per press |
|
|
250
|
+
|
|
251
|
+
`top` and `bottom` clear the hash. Set it on one link, or on `<body>` for
|
|
252
|
+
all of them.
|
|
253
|
+
|
|
254
|
+
Left as it is, an address that already has a `#` keeps it: open a page on
|
|
255
|
+
`#t-notices`, press another tab, reload — and Notices opens again. Where
|
|
256
|
+
that matters (tabs, above all), `"true"` or `"replace"` keep the address
|
|
257
|
+
in step with what is open.
|
|
258
|
+
|
|
259
|
+
## Back to top
|
|
260
|
+
|
|
261
|
+
```html
|
|
262
|
+
<button
|
|
263
|
+
class="up"
|
|
264
|
+
data-pg-anchor="top"
|
|
265
|
+
data-pg-anchor-visible="false"
|
|
266
|
+
data-pg-anchor-progress
|
|
267
|
+
aria-label="Back to top"
|
|
268
|
+
>↑</button>
|
|
269
|
+
|
|
270
|
+
<div class="band" data-pg-anchor-avoid="hide">…</div>
|
|
271
|
+
<footer data-pg-anchor-avoid>…</footer>
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
```css
|
|
275
|
+
.up {
|
|
276
|
+
position: fixed;
|
|
277
|
+
right: 1rem;
|
|
278
|
+
bottom: 1rem;
|
|
279
|
+
transition: opacity 0.3s, visibility 0.3s;
|
|
280
|
+
--pg-anchor-show-after: 100svh; /* after one screen */
|
|
281
|
+
--pg-anchor-avoid-space: 1rem;
|
|
282
|
+
}
|
|
283
|
+
@media (width >= 48rem) {
|
|
284
|
+
.up {
|
|
285
|
+
right: 1.75rem;
|
|
286
|
+
bottom: 1.75rem;
|
|
287
|
+
--pg-anchor-show-after: 600px;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
- **Showing.** `data-pg-anchor-visible` turns `"true"` once the page has
|
|
293
|
+
scrolled `--pg-anchor-show-after`. `index.css` hides the `"false"` state
|
|
294
|
+
at zero weight — add your own transition to fade it.
|
|
295
|
+
- **On a reload — `early.js`.** The browser puts a reloaded page back
|
|
296
|
+
where it was at once, but the button would come a moment later, once the
|
|
297
|
+
module has measured the scroll. As the page is left, `initAnchor()`
|
|
298
|
+
notes whether its floating elements are showing (and how far round the
|
|
299
|
+
ring is) in `sessionStorage`; `early.js`, a small classic script in
|
|
300
|
+
`<head>`, reads that before anything is drawn and marks `<html>` with
|
|
301
|
+
`data-pg-anchor-early="shown"` — `index.css` then shows them from the
|
|
302
|
+
first frame, the ring where it was, with no fade. Once the page has
|
|
303
|
+
loaded, `initAnchor()` measures again and takes over. Only on a reload
|
|
304
|
+
or Back/Forward, where the browser restores the scroll — a fresh visit
|
|
305
|
+
starts at the top, and so does the button; and only when every floating
|
|
306
|
+
element was showing, so none ever shows that should not.
|
|
307
|
+
|
|
308
|
+
```html
|
|
309
|
+
<head>
|
|
310
|
+
…
|
|
311
|
+
<script src="https://cdn.jsdelivr.net/npm/@plusguard/anchor@3.4.0/early.js"></script>
|
|
312
|
+
</head>
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
Not deferred, not a module — or its text written inline there.
|
|
316
|
+
- **A ring that fills — `data-pg-anchor-progress`.** Round the button, on
|
|
317
|
+
its edge: empty where it first shows, full at the end of the page,
|
|
318
|
+
filling from the top, clockwise, as the page scrolls — and emptying as it
|
|
319
|
+
goes back up. Leave the attribute out for no ring. Its look is CSS:
|
|
320
|
+
`--pg-anchor-progress-color` (default the text's colour),
|
|
321
|
+
`--pg-anchor-progress-track` (the unfilled part, default none) and
|
|
322
|
+
`--pg-anchor-progress-width` (default `2px`; `0px` in a media query hides
|
|
323
|
+
it on one screen size). Drawn by `::after` on a round, positioned
|
|
324
|
+
element — a fixed button is both.
|
|
325
|
+
- **Stepping aside — `data-pg-anchor-avoid`.** When the area reaches it,
|
|
326
|
+
it moves to that area's edge and keeps `--pg-anchor-avoid-space`: up when
|
|
327
|
+
it sits in the lower half of the screen, down in the upper. Areas back to
|
|
328
|
+
back are cleared together. It moves with the `translate` property, so
|
|
329
|
+
your `bottom` and any `transform` are kept. An area taller than the
|
|
330
|
+
screen cannot be stepped around — the element stays on screen over it.
|
|
331
|
+
- **Passing under — `data-pg-anchor-avoid="hide"`.** It stays where it is
|
|
332
|
+
and goes under the area: the part the area covers is cut off
|
|
333
|
+
(`clip-path`), so it slides in at one edge and comes out at the other.
|
|
334
|
+
Wholly under, it gets `data-pg-anchor-covered="true"` and is out of reach
|
|
335
|
+
of Tab and clicks.
|
|
336
|
+
- **Which one.** Stepping aside suits areas at the end of the page, such as
|
|
337
|
+
the footer: they never pass by. An area in the middle of the page does
|
|
338
|
+
pass by — once its lower edge has gone above the element, the element
|
|
339
|
+
drops back down by the area's height in one go. Mid-page, passing under
|
|
340
|
+
is smoother.
|
|
341
|
+
- **Per screen size: `--pg-anchor-avoid`.** `none` on the element turns
|
|
342
|
+
both off — in a media query, for one screen size:
|
|
343
|
+
`@media (width < 62rem) { .up { --pg-anchor-avoid: none } }` avoids on
|
|
344
|
+
wide screens only.
|
|
345
|
+
|
|
346
|
+
Any fixed element can float this way, not only a back-to-top button; give
|
|
347
|
+
it `data-pg-anchor-visible` and a `--pg-anchor-show-after` of `0px` to
|
|
348
|
+
have it always shown.
|
|
349
|
+
|
|
350
|
+
## Menus that follow along
|
|
351
|
+
|
|
352
|
+
```html
|
|
353
|
+
<nav class="menu" data-pg-anchor-spy>
|
|
354
|
+
<a href="#about" data-pg-anchor>About</a>
|
|
355
|
+
<a href="#work" data-pg-anchor>Work</a>
|
|
356
|
+
<span class="marker" aria-hidden="true"></span>
|
|
357
|
+
</nav>
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
```css
|
|
361
|
+
.menu {
|
|
362
|
+
position: relative; /* or sticky / fixed */
|
|
363
|
+
}
|
|
364
|
+
.menu [data-pg-anchor-current="true"] {
|
|
365
|
+
color: black;
|
|
366
|
+
}
|
|
367
|
+
.menu .marker {
|
|
368
|
+
position: absolute;
|
|
369
|
+
bottom: 0;
|
|
370
|
+
left: var(--pg-anchor-current-left);
|
|
371
|
+
width: var(--pg-anchor-current-width);
|
|
372
|
+
height: 2px;
|
|
373
|
+
background: black;
|
|
374
|
+
transition: left 0.3s, width 0.3s;
|
|
375
|
+
}
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
- A section is current once its top has reached the line it would land on
|
|
379
|
+
— so after a click, the link you clicked is the one marked. At the very
|
|
380
|
+
bottom of the page, the last section on screen is marked even if it
|
|
381
|
+
never reached that line.
|
|
382
|
+
- **Any number of menus.** Each `data-pg-anchor-spy` follows on its own: a
|
|
383
|
+
header menu and a phone's sidebar with the same links are both marked,
|
|
384
|
+
each styled its own way.
|
|
385
|
+
- The current link also gets `aria-current="location"`.
|
|
386
|
+
- A vertical list uses `--pg-anchor-current-top` and `-height` instead.
|
|
387
|
+
|
|
388
|
+
## Tabs
|
|
389
|
+
|
|
390
|
+
A tab bar, with the targetFunctions package opening one tab at a time and
|
|
391
|
+
anchor doing the scrolling. Each tab is a link to its panel:
|
|
392
|
+
|
|
393
|
+
```html
|
|
394
|
+
<div class="tabs">
|
|
395
|
+
<nav class="tab-list" data-pg-anchor-strip>
|
|
396
|
+
<a href="#t-policies" data-pg-anchor data-pg-target-functions="close-tab-except-t1 open-t1">Policies</a>
|
|
397
|
+
<a href="#t-notices" data-pg-anchor data-pg-target-functions="close-tab-except-t3 open-t3">Notices</a>
|
|
398
|
+
</nav>
|
|
399
|
+
<div id="t-policies" data-pg-target="t1 tab" data-pg-target-active="true">…a long tab…</div>
|
|
400
|
+
<div id="t-notices" data-pg-target="t3 tab" data-pg-target-active="false">…a short one…</div>
|
|
401
|
+
</div>
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
```css
|
|
405
|
+
.tabs {
|
|
406
|
+
--pg-anchor-when: passed;
|
|
407
|
+
}
|
|
408
|
+
.tab-list {
|
|
409
|
+
position: sticky; /* the bar follows the reader down — no script */
|
|
410
|
+
top: var(--header);
|
|
411
|
+
display: flex;
|
|
412
|
+
overflow-x: auto;
|
|
413
|
+
}
|
|
414
|
+
.tabs [data-pg-target] {
|
|
415
|
+
/* The bar's height: a panel lands below it, and below the header that
|
|
416
|
+
the page's scroll-padding-top already clears. */
|
|
417
|
+
scroll-margin-top: 3rem;
|
|
418
|
+
}
|
|
419
|
+
.tabs [data-pg-target-active="false"] {
|
|
420
|
+
display: none;
|
|
421
|
+
}
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
**Why `passed`.** Read to the end of a long tab, press a short one on the
|
|
425
|
+
sticky bar: the page is suddenly much shorter, and the reader is left where
|
|
426
|
+
the long tab ended — the footer, or the next section — with the tab they
|
|
427
|
+
asked for somewhere above. `passed` brings them back to its start. Only
|
|
428
|
+
back up: at the start of the tabs, or above them, nothing moves — a tab
|
|
429
|
+
pressed there never drags the page down to itself.
|
|
430
|
+
|
|
431
|
+
| `--pg-anchor-when` | A click… |
|
|
432
|
+
| ------------------ | ------------------------------------------------------------- |
|
|
433
|
+
| `always` (default) | scrolls to the target, up or down |
|
|
434
|
+
| `passed` | scrolls only up, to a target the reader has scrolled past |
|
|
435
|
+
| `never` | does not scroll — the address is still written, if asked |
|
|
436
|
+
|
|
437
|
+
It works the same on a computer as on a phone. For one screen size only, a
|
|
438
|
+
media query: `@media (width >= 62rem) { .tabs { --pg-anchor-when: never } }`.
|
|
439
|
+
|
|
440
|
+
**The address.** By default it is left as it is — but a page opened on a
|
|
441
|
+
tab's `#id` keeps that `#id` when another tab is pressed, and a reload
|
|
442
|
+
brings the first tab back. With `data-pg-anchor-hash` on the tabs, a tab
|
|
443
|
+
writes its panel's `#id`; opening that address opens that tab —
|
|
444
|
+
targetFunctions reads it. `"true"`: Back goes back to the tab before.
|
|
445
|
+
`"replace"`: the address follows the tab, and Back leaves the page.
|
|
446
|
+
|
|
447
|
+
**The bar** is a strip: on a phone, the tab pressed slides to the middle
|
|
448
|
+
(next section).
|
|
449
|
+
|
|
450
|
+
## Accordions — the question stays put
|
|
451
|
+
|
|
452
|
+
One answer at a time: pressing a question closes the open answer. When
|
|
453
|
+
that answer is above the question, everything below it rides up as it
|
|
454
|
+
closes — the question pressed too, off the top of a phone's screen if the
|
|
455
|
+
answer was long. The reader is left looking at whatever came after.
|
|
456
|
+
|
|
457
|
+
```html
|
|
458
|
+
<div class="acc" data-pg-anchor-stay>
|
|
459
|
+
<button data-pg-target-functions="close-faq-except-q1 toggle-q1">…</button>
|
|
460
|
+
<div data-pg-target="q1 faq" data-pg-target-active="false">…</div>
|
|
461
|
+
<button data-pg-target-functions="close-faq-except-q2 toggle-q2">…</button>
|
|
462
|
+
<div data-pg-target="q2 faq" data-pg-target-active="false">…</div>
|
|
463
|
+
</div>
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
With `data-pg-anchor-stay`, the question pressed keeps its place on the
|
|
467
|
+
screen: the page scrolls by exactly what it would have moved, frame by
|
|
468
|
+
frame, for as long as it moves. The answer above slides out of the top of
|
|
469
|
+
the screen; the question, and its answer opening below it, stay where the
|
|
470
|
+
reader is looking.
|
|
471
|
+
|
|
472
|
+
- **Any animation.** It measures every frame instead of knowing the
|
|
473
|
+
transition — any length, any curve, or none.
|
|
474
|
+
- **Only what is above.** An answer closing below the question moves
|
|
475
|
+
nothing on screen, so there is nothing to hold.
|
|
476
|
+
- **Until it settles.** It lets go once the question has been still for a
|
|
477
|
+
moment, or when the user turns the wheel, touches or presses a key.
|
|
478
|
+
- **At the very top** the page cannot scroll any further up: the question
|
|
479
|
+
moves by what is left.
|
|
480
|
+
- **Per screen size:** `--pg-anchor-stay: none` in a media query turns it
|
|
481
|
+
off there — on a phone where every answer may stay open, nothing above
|
|
482
|
+
ever closes anyway.
|
|
483
|
+
- **Not on links that scroll.** An item with `data-pg-anchor` scrolls the
|
|
484
|
+
page itself; it is left to that.
|
|
485
|
+
|
|
486
|
+
The box can be anything that holds the clickable items — an accordion, a
|
|
487
|
+
footer's columns, a list whose rows expand. Tabs are the other case: there
|
|
488
|
+
the bar is sticky and does not move, and the content changes at once —
|
|
489
|
+
`--pg-anchor-when: passed` (above) brings the reader to the new tab's
|
|
490
|
+
start.
|
|
491
|
+
|
|
492
|
+
## Strips — rows that scroll sideways
|
|
493
|
+
|
|
494
|
+
A tab bar, a row of chips, a header menu on a phone: anything that scrolls
|
|
495
|
+
sideways. `data-pg-anchor-strip` keeps its current item in view:
|
|
496
|
+
|
|
497
|
+
- **Which item.** The one marked `aria-current` (a spy's current link),
|
|
498
|
+
`aria-selected="true"` or `aria-expanded="true"` (a targetFunctions tab).
|
|
499
|
+
If nothing in the row is marked, the one clicked.
|
|
500
|
+
- **Where.** `--pg-anchor-strip: center`, the default — and at the ends it
|
|
501
|
+
simply stops: the first items stay at the start, the last at the end,
|
|
502
|
+
only those in between are centred, so there is always something to see
|
|
503
|
+
on both sides. `start` puts the item at the row's start (and its
|
|
504
|
+
`scroll-padding-left`), `end` at its end, `none` leaves the row alone.
|
|
505
|
+
- **How.** `--pg-anchor-strip-duration` (`500ms`; `0s` at once) and
|
|
506
|
+
`--pg-anchor-strip-easing`. With reduced motion asked for, it is at once;
|
|
507
|
+
the user's own wheel, touch or press on the row stops it.
|
|
508
|
+
- **On load,** the current item is put in view at once — a page opened on
|
|
509
|
+
the fourth tab shows the fourth tab in the bar.
|
|
510
|
+
- **When the row changes width** — a phone turned on its side and back, a
|
|
511
|
+
panel beside the row opened — the current item is put back in place at
|
|
512
|
+
once, not slid. The row's own width is watched, not the window's.
|
|
513
|
+
- **Only the row moves**, never the page.
|
|
514
|
+
- **Edge to edge.** A row inside the page's gutter is cut off short of the
|
|
515
|
+
screen's side, and a scrolled item seems to vanish into a gap. Step it
|
|
516
|
+
out of the gutter, and put its first item back in line with padding;
|
|
517
|
+
`scroll-padding` does the same for `start` and `end`:
|
|
518
|
+
|
|
519
|
+
```css
|
|
520
|
+
.tab-list {
|
|
521
|
+
margin-inline: -1rem; /* the page's gutter */
|
|
522
|
+
padding-inline: 1rem;
|
|
523
|
+
scroll-padding-inline: 1rem;
|
|
524
|
+
}
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
```css
|
|
528
|
+
.tab-list {
|
|
529
|
+
--pg-anchor-strip-duration: 300ms;
|
|
530
|
+
--pg-anchor-strip-easing: ease-out;
|
|
531
|
+
}
|
|
532
|
+
@media (width >= 62rem) {
|
|
533
|
+
.tab-list {
|
|
534
|
+
--pg-anchor-strip: none; /* wide enough for every tab */
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
A spy menu can be a strip too — `data-pg-anchor-spy data-pg-anchor-strip`:
|
|
540
|
+
on a phone, the link to the section you are in stays in view as you read.
|
|
541
|
+
|
|
542
|
+
## Keyboard and screen readers
|
|
543
|
+
|
|
544
|
+
A `<p>` or `<div>` with `data-pg-anchor` gets `tabindex="0"` and
|
|
545
|
+
`role="link"` (unless it has its own), and Enter presses it. After a
|
|
546
|
+
keyboard press, focus moves to the section — Tab carries on from there,
|
|
547
|
+
as it would after a real jump. Not with `passed`: that is no jump, only the
|
|
548
|
+
start of what is being read brought back — focus stays on the tab. Prefer `<a href="#id">` where you can: it
|
|
549
|
+
also works before the script has loaded.
|
|
550
|
+
|
|
551
|
+
A Cmd- or Ctrl-click on a link is left to the browser — it opens a tab.
|
|
552
|
+
|
|
553
|
+
Without JavaScript, links with an `href` still jump, and nothing is hidden.
|
|
554
|
+
|
|
555
|
+
## From the old scripts
|
|
556
|
+
|
|
557
|
+
| Before (quaSmoothScroll, quaSmoothScrollUpVisibility, smoothScroll.js) | Now |
|
|
558
|
+
| ---------------------------------------------------------------------- | ---------------------------------------------------------- |
|
|
559
|
+
| `data-qua-smoothScroll="#id"` / `"top"` | `data-pg-anchor="#id"` / `"top"` |
|
|
560
|
+
| Lenis-only: nothing happened without `window.lenis` | Its own glide; Lenis through `setAnchorScroller` |
|
|
561
|
+
| jQuery `animate`, 500 / 750 ms | `--pg-anchor-duration`, in both modes |
|
|
562
|
+
| Header height − 30px, measured | `scroll-padding-top` / `scroll-margin-top`, `--pg-anchor-edge` |
|
|
563
|
+
| `qua-active` after 400px | `data-pg-anchor-visible`, `--pg-anchor-show-after` |
|
|
564
|
+
| `data-qua-smoothScrollSpace`, footer only, desktop only | `data-pg-anchor-avoid` (or `="hide"`) on any area, `--pg-anchor-avoid-space`, `--pg-anchor-avoid` |
|
|
565
|
+
| "Scroll Color Switch" and `.qua-slider` | `data-pg-anchor-spy`, `--pg-anchor-current-*` |
|
|
566
|
+
| `qua-tabClickSmooth.js`: back to the tab's top below 992px, header measured | `--pg-anchor-when: passed` — every screen, or per media query |
|
|
567
|
+
| `qua-tabStickyScroll.js`: a class added on scroll | `position: sticky` — CSS alone |
|
|
568
|
+
|
|
569
|
+
## Upgrading from 2.x
|
|
570
|
+
|
|
571
|
+
The glide's length moved into CSS, beside the strip's: it changes per
|
|
572
|
+
screen size now, and has an easing.
|
|
573
|
+
|
|
574
|
+
```html
|
|
575
|
+
<!-- 2.x -->
|
|
576
|
+
<body data-pg-anchor-duration="1900">
|
|
577
|
+
<button data-pg-anchor="#prices" data-pg-anchor-duration="400">
|
|
578
|
+
```
|
|
579
|
+
|
|
580
|
+
```html
|
|
581
|
+
<!-- 3.x -->
|
|
582
|
+
<body style="--pg-anchor-duration: 1.9s"> <!-- or :root in your CSS -->
|
|
583
|
+
<button data-pg-anchor="#prices" style="--pg-anchor-duration: 400ms">
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
With Lenis, take the easing your scroller is now handed —
|
|
587
|
+
`(top, ms, easing) => lenis.scrollTo(top, { duration: ms / 1000, easing })` —
|
|
588
|
+
so `--pg-anchor-easing` reaches Lenis too. A scroller that ignores it keeps
|
|
589
|
+
working, on its own curve.
|
|
590
|
+
|
|
591
|
+
## Upgrading from 1.x
|
|
592
|
+
|
|
593
|
+
`data-pg-anchor-avoid-media` is gone: a screen size belongs in CSS. Move it
|
|
594
|
+
into a media query, inverted — it said where to avoid, the variable says
|
|
595
|
+
where not to:
|
|
596
|
+
|
|
597
|
+
```html
|
|
598
|
+
<!-- 1.x -->
|
|
599
|
+
<button class="up" data-pg-anchor-avoid-media="(width >= 62rem)" …>
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
```css
|
|
603
|
+
/* 2.x */
|
|
604
|
+
@media (width < 62rem) {
|
|
605
|
+
.up {
|
|
606
|
+
--pg-anchor-avoid: none;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
Everything else is as it was; `--pg-anchor-when` and strips are new.
|
|
612
|
+
|
|
613
|
+
## React
|
|
614
|
+
|
|
615
|
+
Call `initAnchor` once, in an effect — it returns its own cleanup:
|
|
616
|
+
|
|
617
|
+
```tsx
|
|
618
|
+
useEffect(() => initAnchor(), []);
|
|
619
|
+
```
|
|
620
|
+
|
|
621
|
+
The links and sections need no client code — tabs and strips included. A bare `data-pg-anchor` in JSX
|
|
622
|
+
is fine: React writes it as `"true"`, which counts as empty. JSX sets the
|
|
623
|
+
starting `data-pg-anchor-visible="false"` and must not change it
|
|
624
|
+
afterwards; from then on anchor owns it.
|
|
625
|
+
|
|
626
|
+
With `lenis/react`, hand Lenis in from inside `<ReactLenis>`:
|
|
627
|
+
|
|
628
|
+
```tsx
|
|
629
|
+
const lenis = useLenis();
|
|
630
|
+
useEffect(() => {
|
|
631
|
+
if (!lenis) return;
|
|
632
|
+
setAnchorScroller((top, ms, easing) =>
|
|
633
|
+
lenis.scrollTo(top, ms ? { duration: ms / 1000, easing } : { immediate: true }),
|
|
634
|
+
);
|
|
635
|
+
return () => setAnchorScroller(null);
|
|
636
|
+
}, [lenis]);
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
If `initAnchor` is called again before the first cleanup runs (React
|
|
640
|
+
remounting in development), only the newest call answers.
|
|
641
|
+
|
|
642
|
+
`early.js` goes in the root layout's `<head>` — inline, so the page waits
|
|
643
|
+
for no extra file. It marks `<html>` only, never the button, so only
|
|
644
|
+
`<html>` needs `suppressHydrationWarning` (a theme script usually has it
|
|
645
|
+
there already). In Next, a server component can read the file's text:
|
|
646
|
+
|
|
647
|
+
```tsx
|
|
648
|
+
import { readFileSync } from "node:fs";
|
|
649
|
+
import path from "node:path";
|
|
650
|
+
|
|
651
|
+
// From the app's folder, where next runs.
|
|
652
|
+
const early = readFileSync(path.join(process.cwd(), "node_modules/@plusguard/anchor/early.js"), "utf8");
|
|
653
|
+
|
|
654
|
+
// in the root layout:
|
|
655
|
+
<html suppressHydrationWarning>
|
|
656
|
+
<head>
|
|
657
|
+
<script dangerouslySetInnerHTML={{ __html: early }} />
|
|
658
|
+
</head>
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
See [`examples/react/Example.tsx`](examples/react/Example.tsx). A plain HTML
|
|
662
|
+
version, with every case above and a `?lenis` switch, is in
|
|
663
|
+
[`examples/static/index.html`](examples/static/index.html) — an accordion
|
|
664
|
+
whose question stays (or, switched off, rides up), and tabs that have
|
|
665
|
+
switches for `passed`, `always` and `never` (read to the end of the long
|
|
666
|
+
tab, press the short one), for the address, and for the strip's place and
|
|
667
|
+
speed; narrow the window for the strips.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const anchorEasing: (t: number) => number;
|
|
2
|
+
export type AnchorScroller = (top: number, duration: number, easing: (t: number) => number) => void;
|
|
3
|
+
export declare function setAnchorScroller(next: AnchorScroller | null): void;
|
|
4
|
+
export declare function initAnchor(): () => void;
|