@plusguard/anchor 0.0.0-stage → 3.3.2

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