@fusion.dev/helix-core 0.244.2 → 0.246.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/custom-elements.json +1 -1
- package/dist/controllers/readings-motion-controller/readings-motion-controller.d.ts +167 -0
- package/dist/controllers/readings-motion-controller/readings-motion-controller.js +306 -0
- package/dist/events/details.d.ts +6 -2
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/utils/count-to/count-to.d.ts +41 -0
- package/dist/utils/count-to/count-to.js +63 -0
- package/dist/utils/follow-pointer/follow-pointer.d.ts +35 -0
- package/dist/utils/follow-pointer/follow-pointer.js +19 -0
- package/dist/utils/move-items/move-items.d.ts +55 -0
- package/dist/utils/move-items/move-items.js +19 -0
- package/dist/utils/plain-host/plain-host.d.ts +44 -0
- package/dist/utils/plain-host/plain-host.js +110 -0
- package/dist/utils/readings-motion/readings-motion.d.ts +45 -0
- package/dist/utils/readings-motion/readings-motion.js +40 -0
- package/dist/utils/track-indicator/track-indicator.d.ts +68 -0
- package/dist/utils/track-indicator/track-indicator.js +27 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { CountController } from '../../controllers/count-controller/count-controller.js';
|
|
2
|
+
import { readTiming, runOn } from '../plain-host/plain-host.js';
|
|
3
|
+
const counts = new WeakMap();
|
|
4
|
+
const reached = new WeakMap();
|
|
5
|
+
const readNumber = (element) => {
|
|
6
|
+
const text = (element.textContent ?? '').replace(/[^\d.-]/g, '');
|
|
7
|
+
const value = Number.parseFloat(text);
|
|
8
|
+
return Number.isFinite(value) ? value : 0;
|
|
9
|
+
};
|
|
10
|
+
const formatterOf = (element) => {
|
|
11
|
+
const language = element.closest('[lang]')?.getAttribute('lang') || undefined;
|
|
12
|
+
try {
|
|
13
|
+
return new Intl.NumberFormat(language).format;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return new Intl.NumberFormat().format;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
export const countTo = (element, to, options = {}) => new Promise((resolve) => {
|
|
20
|
+
const from = options.from ?? reached.get(element) ?? readNumber(element);
|
|
21
|
+
const write = (count, value) => {
|
|
22
|
+
element.textContent = (count.options.format ?? formatterOf(element))(value);
|
|
23
|
+
};
|
|
24
|
+
const finish = (count) => {
|
|
25
|
+
counts.delete(element);
|
|
26
|
+
reached.set(element, count.to);
|
|
27
|
+
write(count, count.to);
|
|
28
|
+
count.running.release();
|
|
29
|
+
count.done();
|
|
30
|
+
};
|
|
31
|
+
let count = counts.get(element);
|
|
32
|
+
if (count) {
|
|
33
|
+
count.done();
|
|
34
|
+
Object.assign(count, { done: resolve, options, to });
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
let made;
|
|
38
|
+
const running = runOn(element, (host) => new CountController(host, {
|
|
39
|
+
timing: () => ({
|
|
40
|
+
duration: readTiming(element, made?.options.duration, '--helix-duration-slow', '320ms'),
|
|
41
|
+
easing: readTiming(element, made?.options.easing, '--helix-easing-decelerate', 'cubic-bezier(0, 0, 0, 1)'),
|
|
42
|
+
}),
|
|
43
|
+
}), () => {
|
|
44
|
+
if (!made) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const figure = made.running.controller.counting;
|
|
48
|
+
if (figure === undefined) {
|
|
49
|
+
finish(made);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
write(made, figure);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
made = { done: resolve, options, running, to };
|
|
56
|
+
count = made;
|
|
57
|
+
}
|
|
58
|
+
counts.set(element, count);
|
|
59
|
+
count.running.controller.start(from, to);
|
|
60
|
+
if (count.running.controller.counting === undefined) {
|
|
61
|
+
finish(count);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** What `followPointer` tells the page, beside the two properties it writes. */
|
|
2
|
+
export interface FollowPointerOptions {
|
|
3
|
+
/** Where the pointer is, once a frame while it moves: `x` and `y` from -1 to 1, the middle 0 and 0. */
|
|
4
|
+
moved?: ((x: number, y: number) => void) | undefined;
|
|
5
|
+
/** The pointer left the element, or stopped being followed. */
|
|
6
|
+
left?: (() => void) | undefined;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Follows a mouse or pen across any element, as `Tilt` and `Magnetic` do, and
|
|
10
|
+
* writes where it is onto the element, so a stylesheet can lean or drift it
|
|
11
|
+
* toward the pointer. Returns the function that stops it.
|
|
12
|
+
*
|
|
13
|
+
* ```js
|
|
14
|
+
* import { followPointer } from '@fusion.dev/helix-core'
|
|
15
|
+
*
|
|
16
|
+
* followPointer(document.querySelector('.card'))
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* ```css
|
|
20
|
+
* .card {
|
|
21
|
+
* rotate: x calc(var(--pointer-y, 0) * -6deg);
|
|
22
|
+
* transition: rotate var(--helix-duration-normal) var(--helix-easing-standard);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* **`--pointer-x` and `--pointer-y` run from -1 to 1**, from the inline start
|
|
27
|
+
* edge to the end and from the top to the bottom, so the middle is 0. They are
|
|
28
|
+
* mirrored on a right-to-left page, and taken off when the pointer leaves,
|
|
29
|
+
* which a stylesheet with `0` as the fallback reads as the middle.
|
|
30
|
+
* `data-following` is on the element while they are written.
|
|
31
|
+
*
|
|
32
|
+
* **A touch is not followed**, since a finger is there only while it presses,
|
|
33
|
+
* and nothing is followed for a reader who asked for less motion.
|
|
34
|
+
*/
|
|
35
|
+
export declare const followPointer: (element: HTMLElement, options?: FollowPointerOptions) => (() => void);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PointerFollowController } from '../../controllers/pointer-follow-controller/pointer-follow-controller.js';
|
|
2
|
+
import { runOn } from '../plain-host/plain-host.js';
|
|
3
|
+
export const followPointer = (element, options = {}) => {
|
|
4
|
+
const running = runOn(element, (host) => new PointerFollowController(host, {
|
|
5
|
+
left: () => {
|
|
6
|
+
element.style.removeProperty('--pointer-x');
|
|
7
|
+
element.style.removeProperty('--pointer-y');
|
|
8
|
+
element.removeAttribute('data-following');
|
|
9
|
+
options.left?.();
|
|
10
|
+
},
|
|
11
|
+
moved: (x, y) => {
|
|
12
|
+
element.style.setProperty('--pointer-x', x.toFixed(3));
|
|
13
|
+
element.style.setProperty('--pointer-y', y.toFixed(3));
|
|
14
|
+
element.toggleAttribute('data-following', true);
|
|
15
|
+
options.moved?.(x, y);
|
|
16
|
+
},
|
|
17
|
+
}));
|
|
18
|
+
return running.release;
|
|
19
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** How `moveItems` moves the items of an element. */
|
|
2
|
+
export interface MoveItemsOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Which elements move: a selector matched inside the element, or a function
|
|
5
|
+
* that returns them in the order they are drawn. Defaults to the element's
|
|
6
|
+
* children.
|
|
7
|
+
*/
|
|
8
|
+
items?: string | (() => readonly HTMLElement[]) | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* How long a move takes: milliseconds, a time such as `320ms`, or a custom
|
|
11
|
+
* property such as `--helix-duration-slow`. Defaults to
|
|
12
|
+
* `--helix-duration-normal`. `0` moves nothing.
|
|
13
|
+
*/
|
|
14
|
+
duration?: number | string | undefined;
|
|
15
|
+
/** The curve a move follows, or a custom property that holds one. Defaults to `--helix-easing-standard`. */
|
|
16
|
+
easing?: string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* A positioned box around the element, such as a wrapper with
|
|
19
|
+
* `position: relative`, where a copy of an item that leaves fades out where
|
|
20
|
+
* it was. Not the element itself, whose children are what moves. Left out,
|
|
21
|
+
* an item that leaves is gone at once and the items after it slide into
|
|
22
|
+
* its room.
|
|
23
|
+
*/
|
|
24
|
+
leaving?: HTMLElement | undefined;
|
|
25
|
+
/** Whether the first items into an empty element fade in, as a reader's first choice does. Off by default. */
|
|
26
|
+
arriveIntoEmpty?: boolean | undefined;
|
|
27
|
+
/** The box the items scroll in, where it is not the page. */
|
|
28
|
+
scroller?: Element | undefined;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Moves the items of any element from where they were to where they are,
|
|
32
|
+
* whenever the page adds, removes, reorders or hides them, as Helix's own
|
|
33
|
+
* lists do. Returns the function that stops it.
|
|
34
|
+
*
|
|
35
|
+
* ```js
|
|
36
|
+
* import { moveItems } from '@fusion.dev/helix-core'
|
|
37
|
+
*
|
|
38
|
+
* const stop = moveItems(document.querySelector('ul'))
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* **The element is watched, not told.** Change its children however the page
|
|
42
|
+
* or its framework does, and each item slides from its old place to its new
|
|
43
|
+
* one. An item that arrives fades in, unless every item is new at once, which
|
|
44
|
+
* is a list drawn again and stands still. A move interrupted by another
|
|
45
|
+
* carries on from where the item is drawn.
|
|
46
|
+
*
|
|
47
|
+
* **Nothing moves for a reader who asked for less motion.** Only `transform`
|
|
48
|
+
* and `opacity` animate, so a move never sets off layout, and each one is
|
|
49
|
+
* added to the item's own transform.
|
|
50
|
+
*
|
|
51
|
+
* A copy of an item that leaves is never laid among the element's children,
|
|
52
|
+
* which a framework reconciles. It goes into `leaving`, and is skipped as an
|
|
53
|
+
* item.
|
|
54
|
+
*/
|
|
55
|
+
export declare const moveItems: (element: HTMLElement, options?: MoveItemsOptions) => (() => void);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { LayoutMotionController } from '../../controllers/layout-motion-controller/layout-motion-controller.js';
|
|
2
|
+
import { readTiming, runOn } from '../plain-host/plain-host.js';
|
|
3
|
+
export const moveItems = (element, options = {}) => {
|
|
4
|
+
const { items } = options;
|
|
5
|
+
const pick = typeof items === 'function'
|
|
6
|
+
? items
|
|
7
|
+
: () => [...element.querySelectorAll(items ?? ':scope > *')].filter((one) => !one.hasAttribute('data-leaving'));
|
|
8
|
+
const running = runOn(element, (host) => new LayoutMotionController(host, {
|
|
9
|
+
arriveIntoEmpty: options.arriveIntoEmpty ?? false,
|
|
10
|
+
items: pick,
|
|
11
|
+
leaving: () => options.leaving,
|
|
12
|
+
scroller: () => options.scroller,
|
|
13
|
+
timing: () => ({
|
|
14
|
+
duration: readTiming(element, options.duration, '--helix-duration-normal', '200ms'),
|
|
15
|
+
easing: readTiming(element, options.easing, '--helix-easing-standard', 'cubic-bezier(0.2, 0, 0, 1)'),
|
|
16
|
+
}),
|
|
17
|
+
}));
|
|
18
|
+
return running.release;
|
|
19
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
|
+
/** A controller running over an element, and how to take it off again. */
|
|
3
|
+
export interface Running<C extends ReactiveController> {
|
|
4
|
+
/** The controller. */
|
|
5
|
+
readonly controller: C;
|
|
6
|
+
/** Tells the controller the element has updated, as a component's update would. */
|
|
7
|
+
updated(): void;
|
|
8
|
+
/** Disconnects the controller, and takes back what was lent once nothing else holds the element. */
|
|
9
|
+
release(): void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Runs a controller over an element the page wrote, such as a `<ul>`, with no
|
|
13
|
+
* component around it.
|
|
14
|
+
*
|
|
15
|
+
* **A controller asks four members of its host, and the element for the
|
|
16
|
+
* rest.** It observes, measures and animates its host as the element it is,
|
|
17
|
+
* so the host has to be the element itself: a wrapper object fails every
|
|
18
|
+
* `MutationObserver`, `ResizeObserver` and `getComputedStyle` call. The four,
|
|
19
|
+
* `addController`, `removeController`, `requestUpdate` and `updateComplete`,
|
|
20
|
+
* are set on the element, unlisted, while a controller runs over it, and taken
|
|
21
|
+
* off when the last one is released.
|
|
22
|
+
*
|
|
23
|
+
* **An element that is a host already is used as it is**, so a Lit element
|
|
24
|
+
* keeps its own members and its own lifecycle runs the controller. Lit
|
|
25
|
+
* connects a controller added to a connected host at once, as this does.
|
|
26
|
+
*
|
|
27
|
+
* `draw` runs where the controller asks for an update, as a count does each
|
|
28
|
+
* frame, in place of the render a component would do.
|
|
29
|
+
*/
|
|
30
|
+
export declare const runOn: <C extends ReactiveController>(element: HTMLElement, make: (host: ReactiveControllerHost & HTMLElement) => C, draw?: () => void) => Running<C>;
|
|
31
|
+
/**
|
|
32
|
+
* A time or a curve for a function's options, read when a move starts.
|
|
33
|
+
*
|
|
34
|
+
* A number is milliseconds, a name that starts with `--` is a custom property
|
|
35
|
+
* read off the element, such as a token, and anything else is written as it
|
|
36
|
+
* is. Left out, it is `token`.
|
|
37
|
+
*
|
|
38
|
+
* **A custom property the page does not define reads as `standing`**, the
|
|
39
|
+
* value the token holds in Helix's own stylesheet, so a page that calls a
|
|
40
|
+
* function without that stylesheet still moves. Under reduced motion the
|
|
41
|
+
* stylesheet shortens the time tokens to almost nothing, and each controller
|
|
42
|
+
* checks the reader's setting itself as well.
|
|
43
|
+
*/
|
|
44
|
+
export declare const readTiming: (element: Element, value: number | string | undefined, token: string, standing: string) => string;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const MEMBERS = ['addController', 'removeController', 'requestUpdate', 'updateComplete'];
|
|
2
|
+
const lent = new WeakMap();
|
|
3
|
+
let making;
|
|
4
|
+
const lend = (element) => {
|
|
5
|
+
const known = lent.get(element);
|
|
6
|
+
if (known) {
|
|
7
|
+
return known;
|
|
8
|
+
}
|
|
9
|
+
const made = { controllers: new Set(), draws: new Map() };
|
|
10
|
+
lent.set(element, made);
|
|
11
|
+
const members = {
|
|
12
|
+
addController: (one) => {
|
|
13
|
+
made.controllers.add(one);
|
|
14
|
+
making?.(one);
|
|
15
|
+
if (element.isConnected) {
|
|
16
|
+
one.hostConnected?.();
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
removeController: (one) => {
|
|
20
|
+
made.controllers.delete(one);
|
|
21
|
+
made.draws.delete(one);
|
|
22
|
+
},
|
|
23
|
+
requestUpdate: () => {
|
|
24
|
+
for (const draw of made.draws.values()) {
|
|
25
|
+
draw();
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
updateComplete: Promise.resolve(true),
|
|
29
|
+
};
|
|
30
|
+
for (const name of MEMBERS) {
|
|
31
|
+
Object.defineProperty(element, name, {
|
|
32
|
+
configurable: true,
|
|
33
|
+
enumerable: false,
|
|
34
|
+
value: members[name],
|
|
35
|
+
writable: true,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return made;
|
|
39
|
+
};
|
|
40
|
+
const unlend = (element, record) => {
|
|
41
|
+
if (record.controllers.size > 0) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
lent.delete(element);
|
|
45
|
+
for (const name of MEMBERS) {
|
|
46
|
+
delete element[name];
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
const isHost = (element) => !lent.has(element) &&
|
|
50
|
+
typeof element.addController === 'function';
|
|
51
|
+
export const runOn = (element, make, draw) => {
|
|
52
|
+
if (isHost(element)) {
|
|
53
|
+
const host = element;
|
|
54
|
+
const controller = make(host);
|
|
55
|
+
const own = draw ? { hostUpdated: draw } : undefined;
|
|
56
|
+
if (own) {
|
|
57
|
+
host.addController(own);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
controller,
|
|
61
|
+
release: () => {
|
|
62
|
+
controller.hostDisconnected?.();
|
|
63
|
+
host.removeController(controller);
|
|
64
|
+
if (own) {
|
|
65
|
+
host.removeController(own);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
updated: () => controller.hostUpdated?.(),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const record = lend(element);
|
|
72
|
+
let mine;
|
|
73
|
+
making = (one) => {
|
|
74
|
+
mine ??= one;
|
|
75
|
+
};
|
|
76
|
+
let controller;
|
|
77
|
+
try {
|
|
78
|
+
controller = make(element);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
making = undefined;
|
|
82
|
+
}
|
|
83
|
+
const owned = mine ?? controller;
|
|
84
|
+
if (draw) {
|
|
85
|
+
record.draws.set(owned, draw);
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
controller,
|
|
89
|
+
release: () => {
|
|
90
|
+
if (!record.controllers.has(owned)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
controller.hostDisconnected?.();
|
|
94
|
+
record.controllers.delete(owned);
|
|
95
|
+
record.draws.delete(owned);
|
|
96
|
+
unlend(element, record);
|
|
97
|
+
},
|
|
98
|
+
updated: () => controller.hostUpdated?.(),
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
export const readTiming = (element, value, token, standing) => {
|
|
102
|
+
if (typeof value === 'number') {
|
|
103
|
+
return `${value}ms`;
|
|
104
|
+
}
|
|
105
|
+
const written = value ?? token;
|
|
106
|
+
if (!written.startsWith('--')) {
|
|
107
|
+
return written;
|
|
108
|
+
}
|
|
109
|
+
return getComputedStyle(element).getPropertyValue(written).trim() || standing;
|
|
110
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic a chart's readings move by: a number between two, an axis
|
|
3
|
+
* between two ranges, the keys a chart knows its items by, and the curve a
|
|
4
|
+
* chart grows in along.
|
|
5
|
+
*
|
|
6
|
+
* Kept apart from `ReadingsMotionController` because a frame between two
|
|
7
|
+
* tables is arithmetic a picture hides: a tick drawn past the end of its axis,
|
|
8
|
+
* or a name written at another category's place, shows for a frame and is
|
|
9
|
+
* gone before anyone can point at it.
|
|
10
|
+
*/
|
|
11
|
+
/** A number `share` of the way from `from` to `to`, where `share` runs from 0 to 1. */
|
|
12
|
+
export declare const mix: (from: number, to: number, share: number) => number;
|
|
13
|
+
/**
|
|
14
|
+
* The key each item is known by from one table to the next, from its name:
|
|
15
|
+
* the name itself, and where the same name comes again, the name and how
|
|
16
|
+
* many times it came before. A chart matches its items across two tables by
|
|
17
|
+
* these keys.
|
|
18
|
+
*/
|
|
19
|
+
export declare const keysOf: (names: readonly string[]) => string[];
|
|
20
|
+
/** An axis as a chart publishes it: its range, the ticks on it, and the words and grid lines that go with them. */
|
|
21
|
+
export interface MovingAxis {
|
|
22
|
+
labels?: readonly string[] | undefined;
|
|
23
|
+
range: readonly [number, number];
|
|
24
|
+
rules?: readonly number[] | undefined;
|
|
25
|
+
ticks: readonly number[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The axis `share` of the way from `from` to `to`.
|
|
29
|
+
*
|
|
30
|
+
* **The range moves, and the ticks are the new axis's from the start**, each
|
|
31
|
+
* at its own number, so they slide with the range as it eases to its new
|
|
32
|
+
* ends. A tick, and its word, outside the range drawn this frame is left out
|
|
33
|
+
* until the range reaches it, so nothing is written past the end of the axis.
|
|
34
|
+
* The grid's lines are kept inside the range the same way.
|
|
35
|
+
*/
|
|
36
|
+
export declare const mixAxis: <A extends MovingAxis>(from: A, to: A, share: number) => A;
|
|
37
|
+
/**
|
|
38
|
+
* How much of its reading an item at `step` of `steps` has reached `elapsed`
|
|
39
|
+
* milliseconds after a chart began to grow in, from 0 to 1. **In reading
|
|
40
|
+
* order**, each step a little after the one before it, fast at first and slow
|
|
41
|
+
* to rest, and never past its reading.
|
|
42
|
+
*/
|
|
43
|
+
export declare const grownAt: (step: number, steps: number, elapsed: number) => number;
|
|
44
|
+
/** How long the items of `steps` steps take to grow in, the last one included, in milliseconds. */
|
|
45
|
+
export declare const growTime: (steps: number) => number;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export const mix = (from, to, share) => share >= 1 ? to : share <= 0 ? from : from + (to - from) * share;
|
|
2
|
+
export const keysOf = (names) => {
|
|
3
|
+
const seen = new Map();
|
|
4
|
+
return names.map((name) => {
|
|
5
|
+
const count = seen.get(name) ?? 0;
|
|
6
|
+
seen.set(name, count + 1);
|
|
7
|
+
return count === 0 ? name : `${name}\u0000${count}`;
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
export const mixAxis = (from, to, share) => {
|
|
11
|
+
if (share >= 1) {
|
|
12
|
+
return to;
|
|
13
|
+
}
|
|
14
|
+
const range = [
|
|
15
|
+
mix(from.range[0], to.range[0], share),
|
|
16
|
+
mix(from.range[1], to.range[1], share),
|
|
17
|
+
];
|
|
18
|
+
const [low, high] = [Math.min(...range), Math.max(...range)];
|
|
19
|
+
const inside = (value) => value >= low - 1e-9 * (high - low || 1) && value <= high + 1e-9 * (high - low || 1);
|
|
20
|
+
const kept = to.ticks.flatMap((tick, at) => (inside(tick) ? [at] : []));
|
|
21
|
+
return {
|
|
22
|
+
...to,
|
|
23
|
+
range,
|
|
24
|
+
ticks: kept.map((at) => to.ticks[at]),
|
|
25
|
+
...(to.labels ? { labels: kept.map((at) => to.labels?.[at] ?? '') } : {}),
|
|
26
|
+
...(to.rules ? { rules: to.rules.filter(inside) } : {}),
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const GROW = 1000;
|
|
30
|
+
const STAGGER = 110;
|
|
31
|
+
const SPREAD = 660;
|
|
32
|
+
const stepOf = (steps) => (steps > 1 ? Math.min(STAGGER, SPREAD / (steps - 1)) : 0);
|
|
33
|
+
export const grownAt = (step, steps, elapsed) => {
|
|
34
|
+
const t = (elapsed - step * stepOf(steps)) / GROW;
|
|
35
|
+
if (t <= 0) {
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
return t >= 1 ? 1 : 1 - (1 - t) ** 3;
|
|
39
|
+
};
|
|
40
|
+
export const growTime = (steps) => GROW + stepOf(steps) * Math.max(0, steps - 1);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** What `trackIndicator` marks, and with what. */
|
|
2
|
+
export interface TrackIndicatorOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The item marked now: a selector matched inside the element, such as
|
|
5
|
+
* `[aria-current="page"]`, or a function that returns it. Nothing matching
|
|
6
|
+
* takes the mark away.
|
|
7
|
+
*/
|
|
8
|
+
active: string | (() => HTMLElement | null | undefined);
|
|
9
|
+
/** The mark, an absolutely positioned element inside the element, that travels to the item. */
|
|
10
|
+
indicator: HTMLElement;
|
|
11
|
+
/**
|
|
12
|
+
* The box the mark is positioned against, which has to be its containing
|
|
13
|
+
* block. Defaults to the mark's `offsetParent`, which is that box, or else
|
|
14
|
+
* the element.
|
|
15
|
+
*/
|
|
16
|
+
surface?: HTMLElement | undefined;
|
|
17
|
+
}
|
|
18
|
+
/** A mark tracking an item, and the two things a page does with it. */
|
|
19
|
+
export interface TrackedIndicator {
|
|
20
|
+
/** Measures again, for a change the element cannot see, such as one to a style sheet. */
|
|
21
|
+
place(): void;
|
|
22
|
+
/** Stops tracking. The mark keeps the place it was last given. */
|
|
23
|
+
stop(): void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Sends one mark from item to item as the marked item changes, as a Helix
|
|
27
|
+
* `Tabs` moves its underline, inside any element.
|
|
28
|
+
*
|
|
29
|
+
* ```js
|
|
30
|
+
* import { trackIndicator } from '@fusion.dev/helix-core'
|
|
31
|
+
*
|
|
32
|
+
* trackIndicator(document.querySelector('nav'), {
|
|
33
|
+
* active: '[aria-current="page"]',
|
|
34
|
+
* indicator: document.querySelector('nav .mark'),
|
|
35
|
+
* })
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* ```css
|
|
39
|
+
* nav { position: relative; }
|
|
40
|
+
* nav .mark {
|
|
41
|
+
* position: absolute;
|
|
42
|
+
* inset-block-start: 0;
|
|
43
|
+
* inset-inline-start: 0;
|
|
44
|
+
* inline-size: var(--indicator-inline-size, 0);
|
|
45
|
+
* block-size: var(--indicator-block-size, 0);
|
|
46
|
+
* translate: var(--indicator-x, 0) var(--indicator-y, 0);
|
|
47
|
+
* opacity: 0;
|
|
48
|
+
* transition:
|
|
49
|
+
* translate var(--helix-duration-normal) var(--helix-easing-emphasized),
|
|
50
|
+
* inline-size var(--helix-duration-normal) var(--helix-easing-emphasized),
|
|
51
|
+
* opacity var(--helix-duration-fast);
|
|
52
|
+
* }
|
|
53
|
+
* nav .mark[data-placed] { opacity: 1; }
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* **The mark is written four custom properties**, `--indicator-x`,
|
|
57
|
+
* `--indicator-y`, `--indicator-inline-size` and `--indicator-block-size`, in
|
|
58
|
+
* the surface's own pixels, signed along the inline axis on a right-to-left
|
|
59
|
+
* page. The stylesheet moves it, so its transition is the move, and reduced
|
|
60
|
+
* motion is whatever the stylesheet says for it. `data-placed` is on the mark
|
|
61
|
+
* once it has a place, and `data-moving` while it travels to a new one.
|
|
62
|
+
*
|
|
63
|
+
* **The element is watched**: a change to an attribute inside it, such as
|
|
64
|
+
* `aria-current` moving to another link, an item added or taken away, or a
|
|
65
|
+
* change in its size places the mark again. An item still sliding into place
|
|
66
|
+
* is measured once it comes to rest.
|
|
67
|
+
*/
|
|
68
|
+
export declare const trackIndicator: (element: HTMLElement, options: TrackIndicatorOptions) => TrackedIndicator;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { IndicatorController } from '../../controllers/indicator-controller/indicator-controller.js';
|
|
2
|
+
import { runOn } from '../plain-host/plain-host.js';
|
|
3
|
+
export const trackIndicator = (element, options) => {
|
|
4
|
+
const { active, indicator } = options;
|
|
5
|
+
const pick = typeof active === 'function' ? active : () => element.querySelector(active);
|
|
6
|
+
const running = runOn(element, (host) => new IndicatorController(host, {
|
|
7
|
+
active: pick,
|
|
8
|
+
indicator: () => indicator,
|
|
9
|
+
observe: () => [...element.children].filter((one) => one !== indicator),
|
|
10
|
+
surface: () => options.surface ??
|
|
11
|
+
(indicator.offsetParent instanceof HTMLElement ? indicator.offsetParent : element),
|
|
12
|
+
}));
|
|
13
|
+
const mutations = new MutationObserver((records) => {
|
|
14
|
+
if (records.some((one) => one.target !== indicator && !indicator.contains(one.target))) {
|
|
15
|
+
running.controller.place();
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
mutations.observe(element, { attributes: true, childList: true, subtree: true });
|
|
19
|
+
running.controller.place();
|
|
20
|
+
return {
|
|
21
|
+
place: () => running.controller.place(),
|
|
22
|
+
stop: () => {
|
|
23
|
+
mutations.disconnect();
|
|
24
|
+
running.release();
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
};
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.246.0';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fusion.dev/helix-core",
|
|
3
3
|
"description": "Helix element primitives: base classes, mixins, decorators, utilities.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.246.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Fusion Development Group",
|
|
7
7
|
"repository": {
|
|
@@ -87,12 +87,12 @@
|
|
|
87
87
|
"bugs": "https://github.com/project-fusion/fusion/issues",
|
|
88
88
|
"dependencies": {
|
|
89
89
|
"@floating-ui/dom": "^1.8.0",
|
|
90
|
-
"@fusion.dev/helix-icons": "0.
|
|
90
|
+
"@fusion.dev/helix-icons": "0.246.0",
|
|
91
91
|
"tslib": "^2.9.1"
|
|
92
92
|
},
|
|
93
93
|
"devDependencies": {
|
|
94
|
-
"@fusion.dev/helix-cem": "0.
|
|
95
|
-
"@fusion.dev/helix-testing": "0.
|
|
94
|
+
"@fusion.dev/helix-cem": "0.246.0",
|
|
95
|
+
"@fusion.dev/helix-testing": "0.246.0",
|
|
96
96
|
"@lit-labs/ssr": "^4.1.0",
|
|
97
97
|
"lit": "^3.2.1"
|
|
98
98
|
},
|