oneslash-design-system 2.25.0 → 2.26.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.
|
@@ -10,6 +10,8 @@ A person reads a long page and wants to see its sections, jump to one and know w
|
|
|
10
10
|
- Keep the labels the same as the headings, in the same case.
|
|
11
11
|
- Name the nav 'On this page', which is the default, or pass an `aria-label` that fits the page.
|
|
12
12
|
- Let a click scroll the heading to just under the header and keep its item current until the scroll ends. With reduced motion it jumps.
|
|
13
|
+
- Pass `history="replace"` when Back should leave the page, such as a page that closes by going back. Keep the default when a person should be able to go back to where they were reading.
|
|
14
|
+
- Pass `keepInView` when images or other content above a section can load late and push it down.
|
|
13
15
|
- Keep the aside to the list. A phone does not show it, so the page must read in order without it. The sections are not places: on a phone the person scrolls to them.
|
|
14
16
|
|
|
15
17
|
## Don't
|
package/dist/cases.json
CHANGED
|
@@ -5146,6 +5146,8 @@
|
|
|
5146
5146
|
"Keep the labels the same as the headings, in the same case.",
|
|
5147
5147
|
"Name the nav 'On this page', which is the default, or pass an `aria-label` that fits the page.",
|
|
5148
5148
|
"Let a click scroll the heading to just under the header and keep its item current until the scroll ends. With reduced motion it jumps.",
|
|
5149
|
+
"Pass `history=\"replace\"` when Back should leave the page, such as a page that closes by going back. Keep the default when a person should be able to go back to where they were reading.",
|
|
5150
|
+
"Pass `keepInView` when images or other content above a section can load late and push it down.",
|
|
5149
5151
|
"Keep the aside to the list. A phone does not show it, so the page must read in order without it. The sections are not places: on a phone the person scrolls to them."
|
|
5150
5152
|
],
|
|
5151
5153
|
"dont": [
|
|
@@ -16,14 +16,25 @@ interface OnThisPageProps {
|
|
|
16
16
|
/**
|
|
17
17
|
* How far below the top of the scroll area a heading counts as reached, in px. Default 40, plus the height of a
|
|
18
18
|
* `SiteHeader` above the page (`--os-header-offset`) when the window scrolls. A click scrolls the heading to just
|
|
19
|
-
* under that header.
|
|
19
|
+
* under that header, or to its `scroll-margin-top` when that is more.
|
|
20
20
|
*/
|
|
21
21
|
offset?: number;
|
|
22
|
+
/**
|
|
23
|
+
* What a click does to the browser history. 'push', the default, adds an entry for the `#hash`, so Back returns
|
|
24
|
+
* to where the reader was. 'replace' changes the address only, for a page that Back should leave.
|
|
25
|
+
*/
|
|
26
|
+
history?: 'push' | 'replace';
|
|
27
|
+
/**
|
|
28
|
+
* After a click, keep the heading in place while the page changes height, as images above it load. It lets go
|
|
29
|
+
* after 3 seconds, or when the reader scrolls, presses a key or presses the pointer.
|
|
30
|
+
*/
|
|
31
|
+
keepInView?: boolean;
|
|
22
32
|
}
|
|
23
33
|
/**
|
|
24
34
|
* Links to a long page's headings, with the one the reader is at marked. The current heading is the last one
|
|
25
35
|
* above `offset`; the last item at the end of the scroll; the one in the `#hash` on load. A click keeps its item
|
|
26
|
-
* current until the scroll ends. Reduced motion jumps instead of scrolling.
|
|
36
|
+
* current until the scroll ends. Reduced motion jumps instead of scrolling. `history="replace"` leaves no entry
|
|
37
|
+
* for Back, and `keepInView` holds the heading while the page above it loads.
|
|
27
38
|
*/
|
|
28
|
-
export default function OnThisPage({ items, 'aria-label': ariaLabel, root, offset }: OnThisPageProps): React.JSX.Element | null;
|
|
39
|
+
export default function OnThisPage({ items, 'aria-label': ariaLabel, root, offset, history: historyMode, keepInView, }: OnThisPageProps): React.JSX.Element | null;
|
|
29
40
|
export { OnThisPage };
|
|
@@ -5,24 +5,35 @@ import { focusRing } from './sharedStyles';
|
|
|
5
5
|
import { hashId, isAtBottom, pickCurrent } from '../internal/scrollSpy';
|
|
6
6
|
// A click or a #hash pins its item as current while the page scrolls there, so the items in between don't flash past
|
|
7
7
|
var PIN_LIMIT = 1000;
|
|
8
|
+
// With keepInView, how long after a click the heading is held in place
|
|
9
|
+
var KEEP_LIMIT = 3000;
|
|
8
10
|
var headerOffset = function () { return parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--os-header-offset')) || 0; };
|
|
11
|
+
// Where a jump puts a heading, from the top of the scroll area: under the header (`gap`), or lower when the
|
|
12
|
+
// heading's own scroll-margin-top asks for it, as a #hash link would
|
|
13
|
+
var landing = function (heading, gap) {
|
|
14
|
+
return Math.max(gap, parseFloat(getComputedStyle(heading).scrollMarginTop) || 0);
|
|
15
|
+
};
|
|
9
16
|
var INPUT_EVENTS = ['wheel', 'touchstart', 'keydown', 'pointerdown'];
|
|
10
17
|
/**
|
|
11
18
|
* Links to a long page's headings, with the one the reader is at marked. The current heading is the last one
|
|
12
19
|
* above `offset`; the last item at the end of the scroll; the one in the `#hash` on load. A click keeps its item
|
|
13
|
-
* current until the scroll ends. Reduced motion jumps instead of scrolling.
|
|
20
|
+
* current until the scroll ends. Reduced motion jumps instead of scrolling. `history="replace"` leaves no entry
|
|
21
|
+
* for Back, and `keepInView` holds the heading while the page above it loads.
|
|
14
22
|
*/
|
|
15
23
|
export default function OnThisPage(_a) {
|
|
16
24
|
var _b, _c;
|
|
17
|
-
var items = _a.items, _d = _a["aria-label"], ariaLabel = _d === void 0 ? 'On this page' : _d, root = _a.root, offset = _a.offset;
|
|
25
|
+
var items = _a.items, _d = _a["aria-label"], ariaLabel = _d === void 0 ? 'On this page' : _d, root = _a.root, offset = _a.offset, _e = _a.history, historyMode = _e === void 0 ? 'push' : _e, _f = _a.keepInView, keepInView = _f === void 0 ? false : _f;
|
|
18
26
|
var ids = items.map(function (item) { return item.id; });
|
|
19
27
|
var key = ids.join('\n');
|
|
20
|
-
var
|
|
21
|
-
var
|
|
28
|
+
var _g = useState((_c = (_b = items[0]) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : null), current = _g[0], setCurrent = _g[1];
|
|
29
|
+
var _h = useSlidingIndicator("".concat(key, "|").concat(current)), containerRef = _h.containerRef, indicator = _h.indicator;
|
|
22
30
|
var pin = useRef(null);
|
|
23
31
|
var pinTimer = useRef(undefined);
|
|
24
32
|
// The effect below puts its recompute here, for the click handler
|
|
25
33
|
var refresh = useRef(function () { });
|
|
34
|
+
// Ends the hold a click with keepInView started
|
|
35
|
+
var letGo = useRef(function () { });
|
|
36
|
+
useEffect(function () { return function () { return letGo.current(); }; }, []);
|
|
26
37
|
var setPin = function (id, until) {
|
|
27
38
|
clearTimeout(pinTimer.current);
|
|
28
39
|
pin.current = { id: id, until: until };
|
|
@@ -46,9 +57,14 @@ export default function OnThisPage(_a) {
|
|
|
46
57
|
if (pin.current)
|
|
47
58
|
return;
|
|
48
59
|
var base = rootEl ? rootEl.getBoundingClientRect().top : 0;
|
|
60
|
+
var gap = rootEl ? 0 : headerOffset();
|
|
49
61
|
var tops = ids.map(function (id) {
|
|
50
62
|
var el = document.getElementById(id);
|
|
51
|
-
|
|
63
|
+
if (!el)
|
|
64
|
+
return null;
|
|
65
|
+
// By default a heading with a deeper scroll margin counts as reached that much lower, so it is current
|
|
66
|
+
// where a jump leaves it. A caller's `offset` is taken as it is.
|
|
67
|
+
return el.getBoundingClientRect().top - base - (offset === undefined ? landing(el, gap) - gap : 0);
|
|
52
68
|
});
|
|
53
69
|
var metrics = rootEl
|
|
54
70
|
? { scrollTop: rootEl.scrollTop, clientHeight: rootEl.clientHeight, scrollHeight: rootEl.scrollHeight }
|
|
@@ -57,7 +73,7 @@ export default function OnThisPage(_a) {
|
|
|
57
73
|
clientHeight: window.innerHeight,
|
|
58
74
|
scrollHeight: document.documentElement.scrollHeight,
|
|
59
75
|
};
|
|
60
|
-
var i = pickCurrent(tops, offset !== null && offset !== void 0 ? offset : 40 +
|
|
76
|
+
var i = pickCurrent(tops, offset !== null && offset !== void 0 ? offset : 40 + gap, isAtBottom(metrics));
|
|
61
77
|
if (i >= 0)
|
|
62
78
|
setCurrent(ids[i]);
|
|
63
79
|
};
|
|
@@ -103,6 +119,47 @@ export default function OnThisPage(_a) {
|
|
|
103
119
|
INPUT_EVENTS.forEach(function (type) { return target.removeEventListener(type, onInput); });
|
|
104
120
|
};
|
|
105
121
|
}, [key, root, offset]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
122
|
+
// Scrolls to the heading again each time the page changes height. Until the click's own scroll ends it keeps
|
|
123
|
+
// that scroll's behavior, so the glide carries on to where the heading is now; after that it jumps.
|
|
124
|
+
var keep = function (id, heading, rootEl, behavior) {
|
|
125
|
+
if (typeof ResizeObserver === 'undefined')
|
|
126
|
+
return;
|
|
127
|
+
var target = rootEl !== null && rootEl !== void 0 ? rootEl : window;
|
|
128
|
+
var arrived = false;
|
|
129
|
+
var onScrollEnd = function () {
|
|
130
|
+
arrived = true;
|
|
131
|
+
};
|
|
132
|
+
var arrival = setTimeout(onScrollEnd, PIN_LIMIT);
|
|
133
|
+
var align = function () {
|
|
134
|
+
if (!heading.isConnected)
|
|
135
|
+
return stop();
|
|
136
|
+
var base = rootEl ? rootEl.getBoundingClientRect().top : 0;
|
|
137
|
+
var delta = heading.getBoundingClientRect().top - base - landing(heading, rootEl ? 0 : headerOffset());
|
|
138
|
+
var top = rootEl ? rootEl.scrollTop : window.scrollY;
|
|
139
|
+
var room = rootEl
|
|
140
|
+
? rootEl.scrollHeight - rootEl.clientHeight - top
|
|
141
|
+
: document.documentElement.scrollHeight - window.innerHeight - top;
|
|
142
|
+
if (Math.abs(delta > 0 ? Math.min(delta, room) : Math.max(delta, -top)) < 1)
|
|
143
|
+
return;
|
|
144
|
+
setPin(id, 'scrollend');
|
|
145
|
+
(rootEl !== null && rootEl !== void 0 ? rootEl : window).scrollBy({ top: delta, behavior: arrived ? 'instant' : behavior });
|
|
146
|
+
};
|
|
147
|
+
var observer = new ResizeObserver(align);
|
|
148
|
+
// A scrolling element keeps its own size as its content grows, so its children are watched
|
|
149
|
+
(rootEl ? Array.from(rootEl.children) : [document.body]).forEach(function (el) { return observer.observe(el); });
|
|
150
|
+
var limit = setTimeout(function () { return stop(); }, KEEP_LIMIT);
|
|
151
|
+
var onInput = function () { return stop(); };
|
|
152
|
+
function stop() {
|
|
153
|
+
clearTimeout(arrival);
|
|
154
|
+
clearTimeout(limit);
|
|
155
|
+
observer.disconnect();
|
|
156
|
+
target.removeEventListener('scrollend', onScrollEnd);
|
|
157
|
+
INPUT_EVENTS.forEach(function (type) { return window.removeEventListener(type, onInput); });
|
|
158
|
+
}
|
|
159
|
+
target.addEventListener('scrollend', onScrollEnd);
|
|
160
|
+
INPUT_EVENTS.forEach(function (type) { return window.addEventListener(type, onInput, { passive: true }); });
|
|
161
|
+
letGo.current = stop;
|
|
162
|
+
};
|
|
106
163
|
var onLinkClick = function (e, id) {
|
|
107
164
|
if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)
|
|
108
165
|
return;
|
|
@@ -115,8 +172,15 @@ export default function OnThisPage(_a) {
|
|
|
115
172
|
// The heading ends just under a header above the page; 'instant' ignores a page's own `scroll-behavior: smooth`
|
|
116
173
|
var behavior = reduced ? 'instant' : 'smooth';
|
|
117
174
|
var gap = rootEl ? 0 : headerOffset();
|
|
118
|
-
var delta = heading.getBoundingClientRect().top - (rootEl ? rootEl.getBoundingClientRect().top : 0) - gap;
|
|
119
|
-
|
|
175
|
+
var delta = heading.getBoundingClientRect().top - (rootEl ? rootEl.getBoundingClientRect().top : 0) - landing(heading, gap);
|
|
176
|
+
var address = "#".concat(encodeURIComponent(id));
|
|
177
|
+
if (historyMode === 'replace')
|
|
178
|
+
window.history.replaceState(window.history.state, '', address);
|
|
179
|
+
else
|
|
180
|
+
window.history.pushState(window.history.state, '', address);
|
|
181
|
+
letGo.current();
|
|
182
|
+
if (keepInView)
|
|
183
|
+
keep(id, heading, rootEl, behavior);
|
|
120
184
|
var top = rootEl ? rootEl.scrollTop : window.scrollY;
|
|
121
185
|
var room = rootEl
|
|
122
186
|
? rootEl.scrollHeight - rootEl.clientHeight - top
|
|
@@ -103,3 +103,14 @@ export var HashScrolledProbe = {
|
|
|
103
103
|
<JumpToHash scrolledOn/>
|
|
104
104
|
</Bleed>); },
|
|
105
105
|
};
|
|
106
|
+
// For tests/interactions.spec.ts only: Probe with the address replaced and the heading kept in view. Scroll anchoring
|
|
107
|
+
// is off, as in a browser without it, so only OnThisPage can hold the heading when something loads above it
|
|
108
|
+
export var KeepProbe = {
|
|
109
|
+
tags: ["!dev", "!autodocs", "test-only"],
|
|
110
|
+
render: function () { return (<Bleed>
|
|
111
|
+
<style>{"html, body { overflow-anchor: none; }"}</style>
|
|
112
|
+
<PageShell id="main" aside={<OnThisPage items={items} history="replace" keepInView/>}>
|
|
113
|
+
<Sections />
|
|
114
|
+
</PageShell>
|
|
115
|
+
</Bleed>); },
|
|
116
|
+
};
|