@fusion.dev/helix-core 0.244.2 → 0.245.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/index.d.ts +4 -0
- package/dist/index.js +4 -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/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
package/dist/index.d.ts
CHANGED
|
@@ -94,6 +94,7 @@ export { columnStep } from './utils/column-step/column-step.js';
|
|
|
94
94
|
export { contextBudget, type Holder, LIVE, pageBudget, } from './utils/context-budget/context-budget.js';
|
|
95
95
|
export { type ControlEntry, type ControlKind, chordOfAria, controlName, controlReachable, type ListedControl, listControls, listShortcuts, registerControl, registerShortcut, runControl, type ShortcutEntry, unregisterControl, } from './utils/controls/controls.js';
|
|
96
96
|
export { countLabel } from './utils/count-label/count-label.js';
|
|
97
|
+
export { type CountToOptions, countTo } from './utils/count-to/count-to.js';
|
|
97
98
|
export { createSheet } from './utils/create-sheet/create-sheet.js';
|
|
98
99
|
export { type Color, type ColorFormat, formatColor, parseColor, toCss, toHsl, toOklch, toRgb, } from './utils/css-color/css-color.js';
|
|
99
100
|
export { customElement, definedTags, duplicateRegistrations, } from './utils/custom-element/custom-element.js';
|
|
@@ -114,6 +115,7 @@ export { FIND_CURRENT, FIND_MATCH, type FindDirection, type Finder, type FindQue
|
|
|
114
115
|
export { focusTarget } from './utils/focus-target/focus-target.js';
|
|
115
116
|
export { autofocusTarget, deepActiveElement, deepContains, focusAs, focusableElements, openingFocus, PRESSABLE, pressedOwnControl, refocusWithin, } from './utils/focusable-elements/focusable-elements.js';
|
|
116
117
|
export { afterLinkFollowed, followLink, type LinkModifiers, type LinkTarget, } from './utils/follow-link/follow-link.js';
|
|
118
|
+
export { type FollowPointerOptions, followPointer } from './utils/follow-pointer/follow-pointer.js';
|
|
117
119
|
export { formatNumbers } from './utils/format-numbers/format-numbers.js';
|
|
118
120
|
export { glyphPath } from './utils/glyph-path/glyph-path.js';
|
|
119
121
|
export { type GridBounds, type GridKeys, type GridPlace, gridMove, pageRows, } from './utils/grid-move/grid-move.js';
|
|
@@ -133,6 +135,7 @@ export { type EndInSight, endInSight } from './utils/load-more/load-more.js';
|
|
|
133
135
|
export { localeOf, runtimeLocale } from './utils/locale-of/locale-of.js';
|
|
134
136
|
export { markRegion, type RegionOptions } from './utils/mark-region/mark-region.js';
|
|
135
137
|
export { copiesOf, type MediaCopy, sameCopies, } from './utils/media-copy/media-copy.js';
|
|
138
|
+
export { type MoveItemsOptions, moveItems } from './utils/move-items/move-items.js';
|
|
136
139
|
export { nameText } from './utils/name-text/name-text.js';
|
|
137
140
|
export { nearest } from './utils/nearest/nearest.js';
|
|
138
141
|
export { numberConverter, timeConverter } from './utils/number-converter/number-converter.js';
|
|
@@ -179,6 +182,7 @@ export { tellDisclosure } from './utils/tell-disclosure/tell-disclosure.js';
|
|
|
179
182
|
export { subscribe } from './utils/ticker/ticker.js';
|
|
180
183
|
export { type HelixStatusTone, TONE_MARKS, type ToneMark, toneMark, } from './utils/tone-marks/tone-marks.js';
|
|
181
184
|
export { toolbarAround, toolbarAxis } from './utils/toolbar-around/toolbar-around.js';
|
|
185
|
+
export { type TrackedIndicator, type TrackIndicatorOptions, trackIndicator, } from './utils/track-indicator/track-indicator.js';
|
|
182
186
|
export { type TreeArrow, type TreeArrowRow, treeArrow } from './utils/tree-arrow/tree-arrow.js';
|
|
183
187
|
export { Typeahead, type TypeaheadBy, typeaheadBy, typeaheadByWords, typesCharacter, } from './utils/typeahead/typeahead.js';
|
|
184
188
|
export { dateAsValue, isClockValue, isDateValue, isTimeValue, minutesAsValue, numberAsValue, secondsAsValue, timeAsValue, valueAsDate, valueAsMinutes, valueAsNumber, valueAsSeconds, valueAsTime, } from './utils/value-as/value-as.js';
|
package/dist/index.js
CHANGED
|
@@ -94,6 +94,7 @@ export { columnStep } from './utils/column-step/column-step.js';
|
|
|
94
94
|
export { contextBudget, LIVE, pageBudget, } from './utils/context-budget/context-budget.js';
|
|
95
95
|
export { chordOfAria, controlName, controlReachable, listControls, listShortcuts, registerControl, registerShortcut, runControl, unregisterControl, } from './utils/controls/controls.js';
|
|
96
96
|
export { countLabel } from './utils/count-label/count-label.js';
|
|
97
|
+
export { countTo } from './utils/count-to/count-to.js';
|
|
97
98
|
export { createSheet } from './utils/create-sheet/create-sheet.js';
|
|
98
99
|
export { formatColor, parseColor, toCss, toHsl, toOklch, toRgb, } from './utils/css-color/css-color.js';
|
|
99
100
|
export { customElement, definedTags, duplicateRegistrations, } from './utils/custom-element/custom-element.js';
|
|
@@ -114,6 +115,7 @@ export { FIND_CURRENT, FIND_MATCH, findPattern, findRanges, findTextRanges, text
|
|
|
114
115
|
export { focusTarget } from './utils/focus-target/focus-target.js';
|
|
115
116
|
export { autofocusTarget, deepActiveElement, deepContains, focusAs, focusableElements, openingFocus, PRESSABLE, pressedOwnControl, refocusWithin, } from './utils/focusable-elements/focusable-elements.js';
|
|
116
117
|
export { afterLinkFollowed, followLink, } from './utils/follow-link/follow-link.js';
|
|
118
|
+
export { followPointer } from './utils/follow-pointer/follow-pointer.js';
|
|
117
119
|
export { formatNumbers } from './utils/format-numbers/format-numbers.js';
|
|
118
120
|
export { glyphPath } from './utils/glyph-path/glyph-path.js';
|
|
119
121
|
export { gridMove, pageRows, } from './utils/grid-move/grid-move.js';
|
|
@@ -133,6 +135,7 @@ export { endInSight } from './utils/load-more/load-more.js';
|
|
|
133
135
|
export { localeOf, runtimeLocale } from './utils/locale-of/locale-of.js';
|
|
134
136
|
export { markRegion } from './utils/mark-region/mark-region.js';
|
|
135
137
|
export { copiesOf, sameCopies, } from './utils/media-copy/media-copy.js';
|
|
138
|
+
export { moveItems } from './utils/move-items/move-items.js';
|
|
136
139
|
export { nameText } from './utils/name-text/name-text.js';
|
|
137
140
|
export { nearest } from './utils/nearest/nearest.js';
|
|
138
141
|
export { numberConverter, timeConverter } from './utils/number-converter/number-converter.js';
|
|
@@ -179,6 +182,7 @@ export { tellDisclosure } from './utils/tell-disclosure/tell-disclosure.js';
|
|
|
179
182
|
export { subscribe } from './utils/ticker/ticker.js';
|
|
180
183
|
export { TONE_MARKS, toneMark, } from './utils/tone-marks/tone-marks.js';
|
|
181
184
|
export { toolbarAround, toolbarAxis } from './utils/toolbar-around/toolbar-around.js';
|
|
185
|
+
export { trackIndicator, } from './utils/track-indicator/track-indicator.js';
|
|
182
186
|
export { treeArrow } from './utils/tree-arrow/tree-arrow.js';
|
|
183
187
|
export { Typeahead, typeaheadBy, typeaheadByWords, typesCharacter, } from './utils/typeahead/typeahead.js';
|
|
184
188
|
export { dateAsValue, isClockValue, isDateValue, isTimeValue, minutesAsValue, numberAsValue, secondsAsValue, timeAsValue, valueAsDate, valueAsMinutes, valueAsNumber, valueAsSeconds, valueAsTime, } from './utils/value-as/value-as.js';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** How `countTo` counts. */
|
|
2
|
+
export interface CountToOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The number to count from. Defaults to the number the element was last
|
|
5
|
+
* counted to, or else the number its text reads as, which a figure written
|
|
6
|
+
* with grouping in another language may not: pass it the first time there.
|
|
7
|
+
*/
|
|
8
|
+
from?: number | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* How long the count takes: milliseconds, a time such as `800ms`, or a
|
|
11
|
+
* custom property. Defaults to `--helix-duration-slow`. `0` writes the new
|
|
12
|
+
* figure at once.
|
|
13
|
+
*/
|
|
14
|
+
duration?: number | string | undefined;
|
|
15
|
+
/** The curve the count follows, or a custom property. Defaults to `--helix-easing-decelerate`, which slows as it arrives. */
|
|
16
|
+
easing?: string | undefined;
|
|
17
|
+
/** Writes a figure as text. Defaults to the number written in the element's language. */
|
|
18
|
+
format?: ((value: number) => string) | undefined;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Counts the figure an element shows to a new number, once a frame, as a
|
|
22
|
+
* Helix `Stat` counts, and resolves once the new figure is written.
|
|
23
|
+
*
|
|
24
|
+
* ```js
|
|
25
|
+
* import { countTo } from '@fusion.dev/helix-core'
|
|
26
|
+
*
|
|
27
|
+
* await countTo(document.querySelector('#revenue'), 1284)
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* **The element's text is the figure**, so it holds nothing else. Each frame
|
|
31
|
+
* is rounded to as many decimals as the two numbers have, so a count to 1,284
|
|
32
|
+
* shows no fractions. A count started while another runs carries on from the
|
|
33
|
+
* figure drawn, and the first one resolves then.
|
|
34
|
+
*
|
|
35
|
+
* **Not in a live region.** The text changes every frame, which a live region
|
|
36
|
+
* would read out each time; announce the new number some other way.
|
|
37
|
+
*
|
|
38
|
+
* Under reduced motion, or with no time to take, the new figure is written at
|
|
39
|
+
* once.
|
|
40
|
+
*/
|
|
41
|
+
export declare const countTo: (element: HTMLElement, to: number, options?: CountToOptions) => Promise<void>;
|
|
@@ -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,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.245.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.245.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.245.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.245.0",
|
|
95
|
+
"@fusion.dev/helix-testing": "0.245.0",
|
|
96
96
|
"@lit-labs/ssr": "^4.1.0",
|
|
97
97
|
"lit": "^3.2.1"
|
|
98
98
|
},
|