@altnovatrail/canvas 0.3.11 → 0.3.12
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/lib/element/index.js +172 -28
- package/lib/element/tools.d.ts +8 -0
- package/lib/element/tools.js +12 -0
- package/package.json +1 -1
package/lib/element/index.js
CHANGED
|
@@ -100,8 +100,13 @@ const UP_DOWN = 4 / 5;
|
|
|
100
100
|
// Above the column, as wide, stands the row of the element's own buttons, outside the container's
|
|
101
101
|
// scrolling: it sticks to the top of whatever scrolls the element, as far down as the consumer's
|
|
102
102
|
// --novatrail-canvas-top says, while the element is in sight, over the sheet as it scrolls by
|
|
103
|
-
// (R28).
|
|
104
|
-
//
|
|
103
|
+
// (R28). In a low window, where the row would cover more than a quarter of what it sticks in, it
|
|
104
|
+
// stands in its place instead, `loose`, and goes with the scrolling (1.4.10, 2.4.11). A button in
|
|
105
|
+
// focus shows a ring in the ink's colour, two pixels out, that the row's side margins keep on its
|
|
106
|
+
// paper; an unavailable one is paler by its colour, not its opacity, which would pale the ring too
|
|
107
|
+
// (2.4.7, 1.4.11). Nothing in the element moves but the scrolling, which jumps (2.3.3). The
|
|
108
|
+
// element is as tall as the row, the statement and the sheet unless it is given a height: then
|
|
109
|
+
// the statement and the sheet scroll in it, under the row. A worked example's open
|
|
105
110
|
// parts stand between the statement and the sheet, and the region that tells of them is out of
|
|
106
111
|
// sight, but not out of the tree a screen reader reads, where a hidden region would tell nothing
|
|
107
112
|
// (R42). A picture's frame there stands in its item's line, from its top, so that the item's
|
|
@@ -130,9 +135,10 @@ const STYLE = `
|
|
|
130
135
|
.tools {
|
|
131
136
|
position: sticky; top: var(--novatrail-canvas-top, 0px); z-index: 1; flex: none;
|
|
132
137
|
box-sizing: border-box; width: min(100%, ${K_MAX * SHEET.width}px); margin: 0 auto;
|
|
133
|
-
padding: 8px
|
|
138
|
+
padding: 8px 4px; display: flex; flex-wrap: wrap; gap: 8px;
|
|
134
139
|
background: var(--novatrail-canvas-paper, ${PAPER}); color: var(--novatrail-canvas-ink, ${INK});
|
|
135
140
|
}
|
|
141
|
+
.tools.loose { position: static; }
|
|
136
142
|
.tools .demo, .tools .choice, .tools .updown {
|
|
137
143
|
display: flex; gap: 8px; margin-inline-end: 16px;
|
|
138
144
|
}
|
|
@@ -145,7 +151,18 @@ const STYLE = `
|
|
|
145
151
|
background: var(--novatrail-canvas-ink, ${INK}); color: var(--novatrail-canvas-paper, ${PAPER});
|
|
146
152
|
border-color: var(--novatrail-canvas-ink, ${INK});
|
|
147
153
|
}
|
|
148
|
-
.tools
|
|
154
|
+
.tools button:focus-visible {
|
|
155
|
+
outline: 2px solid var(--novatrail-canvas-ink, ${INK}); outline-offset: 2px;
|
|
156
|
+
}
|
|
157
|
+
.tools [aria-disabled="true"] {
|
|
158
|
+
color: color-mix(
|
|
159
|
+
in srgb, var(--novatrail-canvas-ink, ${INK}) 40%, var(--novatrail-canvas-paper, ${PAPER})
|
|
160
|
+
);
|
|
161
|
+
cursor: default;
|
|
162
|
+
}
|
|
163
|
+
@supports not (color: color-mix(in srgb, red 40%, blue)) {
|
|
164
|
+
.tools [aria-disabled="true"] { opacity: 0.4; }
|
|
165
|
+
}
|
|
149
166
|
.tools [hidden] { display: none; }
|
|
150
167
|
.tools button svg {
|
|
151
168
|
display: inline-block; width: 1.125em; height: 1.125em; margin-inline-end: 4px;
|
|
@@ -224,7 +241,8 @@ canvas {
|
|
|
224
241
|
.tools [aria-checked="true"] {
|
|
225
242
|
background: Highlight; color: HighlightText; border-color: Highlight;
|
|
226
243
|
}
|
|
227
|
-
.tools [aria-disabled="true"] { color: GrayText; border-color: GrayText;
|
|
244
|
+
.tools [aria-disabled="true"] { color: GrayText; border-color: GrayText; }
|
|
245
|
+
.tools button:focus-visible { outline-color: Highlight; }
|
|
228
246
|
}
|
|
229
247
|
`;
|
|
230
248
|
/** The sheet of STYLE that the elements of a document adopt, made by the first of them there. */
|
|
@@ -362,12 +380,27 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
362
380
|
*/
|
|
363
381
|
#heard = (event) => {
|
|
364
382
|
const target = event.target;
|
|
365
|
-
if (target === document || (target instanceof Element && holds(target, this)))
|
|
383
|
+
if (target === document || (target instanceof Element && holds(target, this))) {
|
|
384
|
+
// A new --novatrail-canvas-top changes no size: the next scroll is the first to hear of it,
|
|
385
|
+
// by one read of the row's style, and only a new offset asks anew whether the row sticks.
|
|
386
|
+
if (getComputedStyle(this.#tools.element).top !== this.#topSeen)
|
|
387
|
+
this.#stick();
|
|
366
388
|
this.#edges();
|
|
389
|
+
}
|
|
367
390
|
};
|
|
368
|
-
/**
|
|
369
|
-
|
|
370
|
-
|
|
391
|
+
/**
|
|
392
|
+
* The page grown or shrunk, or the window, the scroller the row sticks in or the row itself: what
|
|
393
|
+
* scrolls the element may have room now, or none, and the row may cover too much of it to stick.
|
|
394
|
+
*/
|
|
395
|
+
#resized = () => {
|
|
396
|
+
this.#stick();
|
|
397
|
+
this.#edges();
|
|
398
|
+
};
|
|
399
|
+
#page = new ResizeObserver(this.#resized);
|
|
400
|
+
/** The scroll container the row sticks in, which `#page` watches, or null for the page's viewport. */
|
|
401
|
+
#port = null;
|
|
402
|
+
/** The row's `top` as the last check of whether it sticks read it: the consumer's offset. */
|
|
403
|
+
#topSeen = "";
|
|
371
404
|
#unwatchPen;
|
|
372
405
|
#tool = "pen";
|
|
373
406
|
#task;
|
|
@@ -829,6 +862,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
829
862
|
// page that grows or shrinks scrolls nothing, and neither does a window of another size.
|
|
830
863
|
document.addEventListener("scroll", this.#heard, { capture: true, passive: true });
|
|
831
864
|
this.#page.observe(document.documentElement);
|
|
865
|
+
// The row wraps anew with the element's width, its language, the canvas kind and the finger's
|
|
866
|
+
// button, and grows or shrinks.
|
|
867
|
+
this.#page.observe(this.#tools.element);
|
|
832
868
|
window.addEventListener("resize", this.#resized);
|
|
833
869
|
this.#refresh();
|
|
834
870
|
}
|
|
@@ -920,6 +956,7 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
920
956
|
this.#unwatchPen = undefined;
|
|
921
957
|
document.removeEventListener("scroll", this.#heard, { capture: true });
|
|
922
958
|
this.#page.disconnect();
|
|
959
|
+
this.#port = null;
|
|
923
960
|
window.removeEventListener("resize", this.#resized);
|
|
924
961
|
// A stroke that ended here may be the one the other rows of the page waited for.
|
|
925
962
|
PEN_MODE.settle();
|
|
@@ -1275,7 +1312,8 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1275
1312
|
* on the screen, after whatever transform the page puts on the element. The container's
|
|
1276
1313
|
* rectangle gives the part of the canvas it shows: with the statement in sight, the canvas
|
|
1277
1314
|
* goes on below the container's bottom. The row of buttons above the container hides what it
|
|
1278
|
-
* covers, once it sticks over the sheet as the page scrolls the element by (R28)
|
|
1315
|
+
* covers, once it sticks over the sheet as the page scrolls the element by (R28); in its place,
|
|
1316
|
+
* in a low window, it stands above the container and hides nothing of it.
|
|
1279
1317
|
*/
|
|
1280
1318
|
#view() {
|
|
1281
1319
|
const canvas = this.#canvas.getBoundingClientRect();
|
|
@@ -1328,8 +1366,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1328
1366
|
}
|
|
1329
1367
|
/**
|
|
1330
1368
|
* How much of what a scroller shows is in sight, down the screen: within the ancestors that clip
|
|
1331
|
-
* it and the visual viewport, and below the row of buttons while the row sticks over its top
|
|
1332
|
-
*
|
|
1369
|
+
* it and the visual viewport, and below the row of buttons while the row sticks over its top, or
|
|
1370
|
+
* below the consumer's band, --novatrail-canvas-top, while the row stands in its place in a low
|
|
1371
|
+
* window. A scroller out of sight counts whole.
|
|
1333
1372
|
*/
|
|
1334
1373
|
#seen(target) {
|
|
1335
1374
|
const sight = window.visualViewport;
|
|
@@ -1350,14 +1389,57 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1350
1389
|
bottom = Math.min(bottom, clip.bottom);
|
|
1351
1390
|
}
|
|
1352
1391
|
}
|
|
1353
|
-
// The row hides the top while it sticks there, as far down as the consumer's offset puts it.
|
|
1392
|
+
// The row hides the top while it sticks there, as far down as the consumer's offset puts it. A
|
|
1393
|
+
// row that stands in its place, in a low window, leaves the consumer's band over it alone.
|
|
1354
1394
|
const row = this.#tools.element;
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
top
|
|
1395
|
+
if (this.#tools.sticks) {
|
|
1396
|
+
const box = row.getBoundingClientRect();
|
|
1397
|
+
if (box.top <= top + offsetOf(row, 0) * scaleOf(row, box) + 0.5) {
|
|
1398
|
+
top = Math.max(top, box.bottom);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
else {
|
|
1402
|
+
top = Math.max(top, this.#band(target));
|
|
1358
1403
|
}
|
|
1359
1404
|
return bottom > top ? bottom - top : whole;
|
|
1360
1405
|
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Whether the row sticks (R28): while it is no taller than a quarter of what it sticks in, below
|
|
1408
|
+
* the consumer's offset. In a lower window, or once it wraps over more of it, it stands in its
|
|
1409
|
+
* place, above the statement, and goes with the scrolling, as WCAG's technique C34 un-fixes a
|
|
1410
|
+
* sticky header (1.4.10, 2.4.11). Its place and its size are the same either way, so the choice
|
|
1411
|
+
* changes nothing it is made from. The scroller it sticks in is watched for its size. Only an
|
|
1412
|
+
* element on the page asks: what calls this goes when the element leaves it.
|
|
1413
|
+
*/
|
|
1414
|
+
#stick() {
|
|
1415
|
+
const room = roomOf(this.#tools.element);
|
|
1416
|
+
if (room.port !== this.#port) {
|
|
1417
|
+
if (this.#port !== null)
|
|
1418
|
+
this.#page.unobserve(this.#port);
|
|
1419
|
+
if (room.port !== null)
|
|
1420
|
+
this.#page.observe(room.port);
|
|
1421
|
+
this.#port = room.port;
|
|
1422
|
+
}
|
|
1423
|
+
const row = this.#tools.element;
|
|
1424
|
+
const tall = row.getBoundingClientRect().height;
|
|
1425
|
+
if (tall > 0 && room.height > 0)
|
|
1426
|
+
this.#tools.stick(4 * tall <= room.height - room.offset);
|
|
1427
|
+
this.#topSeen = getComputedStyle(row).top;
|
|
1428
|
+
}
|
|
1429
|
+
/**
|
|
1430
|
+
* Where the consumer's band over the top of what the row sticks in ends, down the screen: its
|
|
1431
|
+
* offset, --novatrail-canvas-top, below the top of the scroller or of the window — for a target
|
|
1432
|
+
* that is or lies within what the row sticks in, and above everything for any other.
|
|
1433
|
+
*/
|
|
1434
|
+
#band(target) {
|
|
1435
|
+
const room = roomOf(this.#tools.element);
|
|
1436
|
+
// The band stands over the top of what the row sticks in, and so over a scroller within it,
|
|
1437
|
+
// but not over the page's when the row sticks in a scroller of its own.
|
|
1438
|
+
const within = room.port === null
|
|
1439
|
+
? target !== undefined
|
|
1440
|
+
: target instanceof Element && (target === room.port || holds(room.port, target));
|
|
1441
|
+
return within ? room.top + room.offset : Number.NEGATIVE_INFINITY;
|
|
1442
|
+
}
|
|
1361
1443
|
/**
|
|
1362
1444
|
* Up and Down as the scrolling now is (R33): each stands unavailable while what two fingers would
|
|
1363
1445
|
* scroll its way can go no further. The element hears its container, the page and the ancestors
|
|
@@ -1400,13 +1482,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1400
1482
|
if (scrollsOn(this.#viewport, onward)) {
|
|
1401
1483
|
return { target: this.#viewport, scale: scaleOf(this.#viewport), rest: 0 };
|
|
1402
1484
|
}
|
|
1403
|
-
|
|
1404
|
-
// Overflow 3): then the body scrolls nothing of its own, and the page scrolls only as it allows.
|
|
1405
|
-
const root = getComputedStyle(document.documentElement).overflowY;
|
|
1406
|
-
const body = document.body === null ? "visible" : getComputedStyle(document.body).overflowY;
|
|
1407
|
-
const fromBody = root === "visible" && body !== "visible";
|
|
1485
|
+
const { overflow, fromBody } = pageOverflow();
|
|
1408
1486
|
for (let node = parentOf(this); node !== null; node = parentOf(node)) {
|
|
1409
|
-
if (node
|
|
1487
|
+
if (isPage(node, fromBody))
|
|
1410
1488
|
break;
|
|
1411
1489
|
const style = getComputedStyle(node);
|
|
1412
1490
|
if (!SCROLLING.test(style.overflowY))
|
|
@@ -1419,7 +1497,6 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1419
1497
|
}
|
|
1420
1498
|
}
|
|
1421
1499
|
// A page that hides its overflow, as one under a modal does, a finger would not scroll: nor do two.
|
|
1422
|
-
const overflow = fromBody ? body : root;
|
|
1423
1500
|
if (overflow === "hidden" || overflow === "clip")
|
|
1424
1501
|
return undefined;
|
|
1425
1502
|
const page = document.scrollingElement;
|
|
@@ -1631,7 +1708,9 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1631
1708
|
/**
|
|
1632
1709
|
* An item of the list, or an area of the sheet, comes into sight at once, as near as it is (R42),
|
|
1633
1710
|
* and then out from under the row of buttons, should the row stick over where it came to: what
|
|
1634
|
-
* scrolls back up goes back as far as the row covers it. A
|
|
1711
|
+
* scrolls back up goes back as far as the row covers it. A row in its place, in a low window,
|
|
1712
|
+
* covers nothing, and the consumer's band, --novatrail-canvas-top, is what it comes out from
|
|
1713
|
+
* under then. A margin of scrolling would hold for
|
|
1635
1714
|
* every scroller of the chain alike — the sheet's container, which the row stands above, and the
|
|
1636
1715
|
* page, which it sticks over. An area of the sheet taller than what is in sight comes by its top
|
|
1637
1716
|
* from below, but by its bottom from above, and then the row's cover brings its top back into
|
|
@@ -1643,10 +1722,14 @@ export class NovatrailCanvas extends HTMLElement {
|
|
|
1643
1722
|
// the chain goes once.
|
|
1644
1723
|
const gone = new Set();
|
|
1645
1724
|
for (;;) {
|
|
1646
|
-
const
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1725
|
+
const latch = this.#scrollerFor(false);
|
|
1726
|
+
// A row in its place, in a low window, covers nothing of it: the consumer's band may, over
|
|
1727
|
+
// what the row sticks in.
|
|
1728
|
+
const over = this.#tools.sticks
|
|
1729
|
+
? this.#tools.element.getBoundingClientRect().bottom
|
|
1730
|
+
: this.#band(latch?.target);
|
|
1731
|
+
const cover = over - item.getBoundingClientRect().top;
|
|
1732
|
+
if (cover <= 0.5 || latch === undefined || gone.has(latch.target))
|
|
1650
1733
|
return;
|
|
1651
1734
|
gone.add(latch.target);
|
|
1652
1735
|
latch.target.scrollBy({ top: -cover / latch.scale, behavior: "instant" });
|
|
@@ -1926,6 +2009,67 @@ function contactOf(event) {
|
|
|
1926
2009
|
}
|
|
1927
2010
|
/** The values of `overflow-y` that make an element scroll its content. */
|
|
1928
2011
|
const SCROLLING = /^(auto|scroll|overlay)$/;
|
|
2012
|
+
/**
|
|
2013
|
+
* The values of `overflow-y` that make an element a scroll container, which a sticky box sticks in:
|
|
2014
|
+
* a hidden overflow too, which a script may scroll though no hand does (CSS Overflow 3).
|
|
2015
|
+
*/
|
|
2016
|
+
const CONTAINING = /^(auto|scroll|overlay|hidden)$/;
|
|
2017
|
+
/**
|
|
2018
|
+
* The overflow of the page's viewport: the root's, or the body's when the root's is visible (CSS
|
|
2019
|
+
* Overflow 3), and then the body scrolls nothing of its own.
|
|
2020
|
+
*/
|
|
2021
|
+
function pageOverflow() {
|
|
2022
|
+
const root = getComputedStyle(document.documentElement).overflowY;
|
|
2023
|
+
const body = document.body === null ? "visible" : getComputedStyle(document.body).overflowY;
|
|
2024
|
+
const fromBody = root === "visible" && body !== "visible";
|
|
2025
|
+
return { overflow: fromBody ? body : root, fromBody };
|
|
2026
|
+
}
|
|
2027
|
+
/** Whether an ancestor on the way up is where the page's viewport takes over. */
|
|
2028
|
+
function isPage(node, fromBody) {
|
|
2029
|
+
return node === document.documentElement || (fromBody && node === document.body);
|
|
2030
|
+
}
|
|
2031
|
+
/**
|
|
2032
|
+
* The scroll container a sticky box sticks in: its nearest ancestor in the flat tree that is one,
|
|
2033
|
+
* or null for the page's viewport.
|
|
2034
|
+
*/
|
|
2035
|
+
function stickyPortOf(element) {
|
|
2036
|
+
const { fromBody } = pageOverflow();
|
|
2037
|
+
let node = parentOf(element);
|
|
2038
|
+
while (node !== null && !isPage(node, fromBody)) {
|
|
2039
|
+
if (CONTAINING.test(getComputedStyle(node).overflowY))
|
|
2040
|
+
return node;
|
|
2041
|
+
node = parentOf(node);
|
|
2042
|
+
}
|
|
2043
|
+
return null;
|
|
2044
|
+
}
|
|
2045
|
+
/**
|
|
2046
|
+
* What a sticky box sticks in, on the screen: the scroll container, or null for the page's
|
|
2047
|
+
* viewport, its top and its height — the viewport's without its scroll bar, which does not change
|
|
2048
|
+
* as a phone's toolbar of the browser comes and goes — and the box's offset below that top.
|
|
2049
|
+
*/
|
|
2050
|
+
function roomOf(element) {
|
|
2051
|
+
const port = stickyPortOf(element);
|
|
2052
|
+
const laid = port === null ? (document.scrollingElement?.clientHeight ?? 0) : port.clientHeight;
|
|
2053
|
+
const own = port === null ? undefined : clientBoxOf(port);
|
|
2054
|
+
return {
|
|
2055
|
+
port,
|
|
2056
|
+
top: own?.top ?? 0,
|
|
2057
|
+
height: own === undefined ? laid : own.bottom - own.top,
|
|
2058
|
+
offset: offsetOf(element, laid) * scaleOf(element),
|
|
2059
|
+
};
|
|
2060
|
+
}
|
|
2061
|
+
/**
|
|
2062
|
+
* How far below the top of what it sticks in a sticky box stands, by its `top`, in CSS pixels as
|
|
2063
|
+
* laid out: a percentage of the scroller's `height`, and nothing for `auto`. A sticky box's `top`
|
|
2064
|
+
* reads as the pixels it is used at, a static one's as it was given.
|
|
2065
|
+
*/
|
|
2066
|
+
function offsetOf(element, height) {
|
|
2067
|
+
const top = getComputedStyle(element).top;
|
|
2068
|
+
const value = Number.parseFloat(top);
|
|
2069
|
+
if (!Number.isFinite(value))
|
|
2070
|
+
return 0;
|
|
2071
|
+
return top.endsWith("%") ? (value / 100) * height : value;
|
|
2072
|
+
}
|
|
1929
2073
|
/** Whether an element can scroll on down its content, for `onward`, or back up it. */
|
|
1930
2074
|
function scrollsOn(element, onward) {
|
|
1931
2075
|
const end = element.scrollHeight - element.clientHeight;
|
package/lib/element/tools.d.ts
CHANGED
|
@@ -57,6 +57,14 @@ export declare class Tools {
|
|
|
57
57
|
* nowhere to go stands unavailable. Only what has changed is written.
|
|
58
58
|
*/
|
|
59
59
|
edges(above: boolean, below: boolean): void;
|
|
60
|
+
/**
|
|
61
|
+
* Whether the row sticks over what scrolls the element (R28): in a low window it stands in its
|
|
62
|
+
* place instead, above the statement, and scrolls away with it. The same state writes nothing:
|
|
63
|
+
* a token forced to what it is already changes no attribute.
|
|
64
|
+
*/
|
|
65
|
+
stick(sticks: boolean): void;
|
|
66
|
+
/** Whether the row sticks, or stands in its place in a low window. */
|
|
67
|
+
get sticks(): boolean;
|
|
60
68
|
/**
|
|
61
69
|
* The labels in the language of a tag — the element's `lang` — and the row's own `lang` theirs,
|
|
62
70
|
* so that a screen reader reads them in the language they are in.
|
package/lib/element/tools.js
CHANGED
|
@@ -151,6 +151,18 @@ export class Tools {
|
|
|
151
151
|
write(this.#up, "aria-disabled", above ? null : "true");
|
|
152
152
|
write(this.#down, "aria-disabled", below ? null : "true");
|
|
153
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Whether the row sticks over what scrolls the element (R28): in a low window it stands in its
|
|
156
|
+
* place instead, above the statement, and scrolls away with it. The same state writes nothing:
|
|
157
|
+
* a token forced to what it is already changes no attribute.
|
|
158
|
+
*/
|
|
159
|
+
stick(sticks) {
|
|
160
|
+
this.element.classList.toggle("loose", !sticks);
|
|
161
|
+
}
|
|
162
|
+
/** Whether the row sticks, or stands in its place in a low window. */
|
|
163
|
+
get sticks() {
|
|
164
|
+
return !this.element.classList.contains("loose");
|
|
165
|
+
}
|
|
154
166
|
/**
|
|
155
167
|
* The labels in the language of a tag — the element's `lang` — and the row's own `lang` theirs,
|
|
156
168
|
* so that a screen reader reads them in the language they are in.
|