@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/early.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* anchor — early.js: on a reload, the back-to-top button where it was.
|
|
3
|
+
*
|
|
4
|
+
* A data-pg-anchor-visible element shows once initAnchor() has measured
|
|
5
|
+
* the scroll — a moment after the page is drawn. On a reload halfway down,
|
|
6
|
+
* the browser puts the page back where it was at once, but the button
|
|
7
|
+
* comes a moment later. initAnchor() notes, as the page is left, whether
|
|
8
|
+
* its floating elements were showing; this little script reads that before
|
|
9
|
+
* anything is drawn and marks <html> with data-pg-anchor-early="shown" —
|
|
10
|
+
* index.css shows them from the first frame, and initAnchor() takes over
|
|
11
|
+
* once the page has loaded.
|
|
12
|
+
*
|
|
13
|
+
* Only on a reload or Back/Forward to the same address, where the browser
|
|
14
|
+
* restores the scroll; a fresh visit starts at the top, as the button does.
|
|
15
|
+
*
|
|
16
|
+
* A classic script in <head> — not deferred, not a module: this file from
|
|
17
|
+
* the CDN as its src, or its text written inline there (README). No
|
|
18
|
+
* dependencies; nothing to call. Nothing here may spell the tag that ends
|
|
19
|
+
* a script, so that the text can be inlined as it is.
|
|
20
|
+
*/
|
|
21
|
+
(function () {
|
|
22
|
+
var nav = performance.getEntriesByType && performance.getEntriesByType("navigation")[0];
|
|
23
|
+
var type = nav ? nav.type : "";
|
|
24
|
+
if (type !== "reload" && type !== "back_forward") return;
|
|
25
|
+
if (history.scrollRestoration === "manual") return;
|
|
26
|
+
var saved;
|
|
27
|
+
try {
|
|
28
|
+
saved = JSON.parse(sessionStorage.getItem("pg-anchor-early") || "{}")[location.pathname + location.search];
|
|
29
|
+
} catch (e) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!saved || saved.shown !== true) return;
|
|
33
|
+
var root = document.documentElement;
|
|
34
|
+
root.setAttribute("data-pg-anchor-early", "shown");
|
|
35
|
+
// The ring, as far round as it was.
|
|
36
|
+
if (typeof saved.progress === "number") root.style.setProperty("--pg-anchor-early-progress", String(saved.progress));
|
|
37
|
+
})();
|
package/index.css
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* anchor — the CSS half.
|
|
3
|
+
*
|
|
4
|
+
* Lengths, set anywhere above the element — with media queries for each
|
|
5
|
+
* screen size:
|
|
6
|
+
* --pg-anchor-edge extra room above a section it lands on, on
|
|
7
|
+
* top of scroll-padding-top / scroll-margin-top.
|
|
8
|
+
* Default 0px
|
|
9
|
+
* --pg-anchor-show-after how far the page scrolls before a
|
|
10
|
+
* data-pg-anchor-visible element shows.
|
|
11
|
+
* Default 400px; 100svh is "one screen"
|
|
12
|
+
* --pg-anchor-avoid-space the gap kept from a data-pg-anchor-avoid area.
|
|
13
|
+
* Default 0px
|
|
14
|
+
*
|
|
15
|
+
* Keywords, the same way — they change with the screen too:
|
|
16
|
+
* --pg-anchor-when on a link, or above: when a click scrolls.
|
|
17
|
+
* always (default) · passed (only back up to a
|
|
18
|
+
* section scrolled past) · never
|
|
19
|
+
* --pg-anchor-avoid on a floating element, or above: auto
|
|
20
|
+
* (default) keeps clear of avoided areas, none
|
|
21
|
+
* ignores them
|
|
22
|
+
* --pg-anchor-stay inside a data-pg-anchor-stay box: auto
|
|
23
|
+
* (default) keeps the item clicked in place,
|
|
24
|
+
* none does not
|
|
25
|
+
* --pg-anchor-strip on a data-pg-anchor-strip row: where its
|
|
26
|
+
* current item is put. center (default) ·
|
|
27
|
+
* start · end · none
|
|
28
|
+
*
|
|
29
|
+
* The page's glide, on the link or above:
|
|
30
|
+
* --pg-anchor-duration a time. Default 1200ms; 0s is a jump
|
|
31
|
+
* --pg-anchor-easing linear, ease, ease-in, ease-out,
|
|
32
|
+
* ease-in-out or cubic-bezier(…). Default:
|
|
33
|
+
* Lenis's own curve
|
|
34
|
+
*
|
|
35
|
+
* The strip's slide:
|
|
36
|
+
* --pg-anchor-strip-duration a time. Default 500ms; 0s is at once
|
|
37
|
+
* --pg-anchor-strip-easing linear, ease, ease-in, ease-out,
|
|
38
|
+
* ease-in-out or cubic-bezier(…). Default:
|
|
39
|
+
* the page glide's own curve
|
|
40
|
+
*
|
|
41
|
+
* The ring of a data-pg-anchor-progress element — round, positioned (it
|
|
42
|
+
* is drawn by ::after, over the element's edge):
|
|
43
|
+
* --pg-anchor-progress-color the part filled. Default currentColor
|
|
44
|
+
* --pg-anchor-progress-track the rest. Default transparent
|
|
45
|
+
* --pg-anchor-progress-width its thickness. Default 2px; 0px hides it
|
|
46
|
+
*
|
|
47
|
+
* index.ts writes --pg-anchor-progress (0 to 1, on a
|
|
48
|
+
* data-pg-anchor-progress element), --pg-anchor-lift (how far the element stepped aside),
|
|
49
|
+
* --pg-anchor-clip (the part a "hide" area covers) and, on a
|
|
50
|
+
* data-pg-anchor-spy menu, --pg-anchor-current-left / -top / -width /
|
|
51
|
+
* -height.
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/* Registered as lengths so the browser resolves 1rem, 100svh, calc() to
|
|
55
|
+
pixels — index.ts reads them back. */
|
|
56
|
+
@property --pg-anchor-edge {
|
|
57
|
+
syntax: "<length>";
|
|
58
|
+
inherits: true;
|
|
59
|
+
initial-value: 0px;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@property --pg-anchor-show-after {
|
|
63
|
+
syntax: "<length>";
|
|
64
|
+
inherits: true;
|
|
65
|
+
initial-value: 400px;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@property --pg-anchor-avoid-space {
|
|
69
|
+
syntax: "<length>";
|
|
70
|
+
inherits: true;
|
|
71
|
+
initial-value: 0px;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@property --pg-anchor-when {
|
|
75
|
+
syntax: "always | passed | never";
|
|
76
|
+
inherits: true;
|
|
77
|
+
initial-value: always;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@property --pg-anchor-avoid {
|
|
81
|
+
syntax: "auto | none";
|
|
82
|
+
inherits: true;
|
|
83
|
+
initial-value: auto;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@property --pg-anchor-stay {
|
|
87
|
+
syntax: "auto | none";
|
|
88
|
+
inherits: true;
|
|
89
|
+
initial-value: auto;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@property --pg-anchor-strip {
|
|
93
|
+
syntax: "center | start | end | none";
|
|
94
|
+
inherits: true;
|
|
95
|
+
initial-value: center;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* Times, so 0.3s and 300ms both read back the same. */
|
|
99
|
+
@property --pg-anchor-duration {
|
|
100
|
+
syntax: "<time>";
|
|
101
|
+
inherits: true;
|
|
102
|
+
initial-value: 1200ms;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
@property --pg-anchor-strip-duration {
|
|
106
|
+
syntax: "<time>";
|
|
107
|
+
inherits: true;
|
|
108
|
+
initial-value: 500ms;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* Not a link or a button, but still clickable. Zero weight: yours wins. */
|
|
112
|
+
:where([data-pg-anchor]:not(a, button)) {
|
|
113
|
+
cursor: pointer;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* Hidden until the page has scrolled far enough — only when JS can say it
|
|
117
|
+
has. Zero weight, so a transition of your own can fade it. */
|
|
118
|
+
@media (scripting: enabled) {
|
|
119
|
+
:where([data-pg-anchor-visible="false"]) {
|
|
120
|
+
visibility: hidden;
|
|
121
|
+
opacity: 0;
|
|
122
|
+
}
|
|
123
|
+
/* A reload halfway down: early.js marked <html> before the first frame,
|
|
124
|
+
so it shows from that frame, its ring as far round as it was —
|
|
125
|
+
until initAnchor() has the real scroll (index.ts). */
|
|
126
|
+
:where(html[data-pg-anchor-early="shown"]) :where([data-pg-anchor-visible="false"]) {
|
|
127
|
+
visibility: visible;
|
|
128
|
+
opacity: 1;
|
|
129
|
+
}
|
|
130
|
+
:where(html[data-pg-anchor-early="shown"]) :where([data-pg-anchor-progress]) {
|
|
131
|
+
--pg-anchor-progress: var(--pg-anchor-early-progress, 0);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/* Out of the way of an avoided area, by as much as index.ts measured. The
|
|
136
|
+
translate property, not transform: a transform of yours is kept. Under a
|
|
137
|
+
"hide" area, the covered part is cut off. */
|
|
138
|
+
[data-pg-anchor-visible] {
|
|
139
|
+
translate: 0 calc(var(--pg-anchor-lift, 0px) * -1);
|
|
140
|
+
clip-path: var(--pg-anchor-clip, none);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/* Wholly under a "hide" area: out of reach of Tab and clicks too. */
|
|
144
|
+
[data-pg-anchor-covered="true"] {
|
|
145
|
+
visibility: hidden;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/* The ring round a floating element: filled from the top, clockwise, as
|
|
149
|
+
far as the page has scrolled since the element showed. Drawn on the
|
|
150
|
+
element's own edge; a mask leaves only the ring. */
|
|
151
|
+
:where([data-pg-anchor-progress])::after {
|
|
152
|
+
content: "";
|
|
153
|
+
position: absolute;
|
|
154
|
+
inset: 0;
|
|
155
|
+
border-radius: inherit;
|
|
156
|
+
pointer-events: none;
|
|
157
|
+
background: conic-gradient(
|
|
158
|
+
var(--pg-anchor-progress-color, currentColor) calc(var(--pg-anchor-progress, 0) * 1turn),
|
|
159
|
+
var(--pg-anchor-progress-track, transparent) 0
|
|
160
|
+
);
|
|
161
|
+
mask: radial-gradient(
|
|
162
|
+
farthest-side,
|
|
163
|
+
transparent calc(100% - var(--pg-anchor-progress-width, 2px)),
|
|
164
|
+
#000 calc(100% - var(--pg-anchor-progress-width, 2px) + 0.5px)
|
|
165
|
+
);
|
|
166
|
+
}
|