@diegopluna/narvi 0.5.4 → 0.5.5

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.
@@ -1,8 +1,7 @@
1
1
  <script lang="ts" module>
2
2
  import * as stylex from '@stylexjs/stylex';
3
3
  import Glyph from '../../internal/Glyph.svelte';
4
- import { shared } from '../../internal/shared.js';
5
- import { whileVisible } from '../../internal/visible.js';
4
+ import { spin } from '../../internal/loops.js';
6
5
  import type { Sx } from '../../internal/sx.js';
7
6
  import { colors, motion, radius } from '../../tokens.stylex.js';
8
7
  import { media } from '../../media.stylex.js';
@@ -133,8 +132,8 @@
133
132
  viewBox="0 0 26 26"
134
133
  fill="none"
135
134
  aria-hidden="true"
136
- {...stylex.attrs(styles.ring, shared.spin)}
137
- {@attach whileVisible}
135
+ {...stylex.attrs(styles.ring)}
136
+ {@attach spin}
138
137
  >
139
138
  <path
140
139
  d="M13 2.5A10.5 10.5 0 0 1 23.5 13"
@@ -24,9 +24,8 @@
24
24
  import type { MarkdownPlugin } from '../../markdown/plugin.js';
25
25
  import * as stylex from '@stylexjs/stylex';
26
26
  import { attrs, classes, type Sx } from '../../internal/sx.js';
27
- import { media } from '../../media.stylex.js';
28
- import { colors, play } from '../../tokens.stylex.js';
29
- import { whileVisible } from '../../internal/visible.js';
27
+ import { colors } from '../../tokens.stylex.js';
28
+ import { blink } from '../../internal/loops.js';
30
29
  import { resolvePlugins, setMarkdownContext, type MarkdownContext } from './context.js';
31
30
  import MarkdownBlocks from './MarkdownBlocks.svelte';
32
31
  import MarkdownFootnotes from './MarkdownFootnotes.svelte';
@@ -114,16 +113,10 @@
114
113
  sx?: Sx | undefined;
115
114
  };
116
115
 
117
- const blink = stylex.keyframes({
118
- '0%': { opacity: 1 },
119
- '50%': { opacity: 1 },
120
- '50.01%': { opacity: 0 },
121
- '100%': { opacity: 0 }
122
- });
123
-
124
116
  // The caret takes no room in its line: it is an empty box whose shape is its shadow, so a
125
117
  // line is as wide with it as without, and a caret at a line's end starts no new line and
126
- // no scrollbar. It is `accent`, the colour of something live.
118
+ // no scrollbar. It is `accent`, the colour of something live. It blinks on Narvi's clock
119
+ // (`blink`, in `internal/loops.ts`), only while it can be seen.
127
120
  const caretStyles = stylex.create({
128
121
  base: {
129
122
  display: 'inline-block',
@@ -132,12 +125,7 @@
132
125
  color: colors.accent,
133
126
  pointerEvents: 'none',
134
127
  userSelect: 'none',
135
- WebkitUserSelect: 'none',
136
- animationName: { default: null, [media.motion]: blink },
137
- animationDuration: '1.1s',
138
- animationIterationCount: 'infinite',
139
- // It blinks only while it can be seen
140
- animationPlayState: play.whileVisible
128
+ WebkitUserSelect: 'none'
141
129
  },
142
130
  block: { boxShadow: '0.36em 0 0 0.28em currentColor', borderRadius: '1px' },
143
131
  circle: {
@@ -582,7 +570,7 @@
582
570
  }
583
571
  caretElement.className = caret === 'circle' ? caretClasses.circle : caretClasses.block;
584
572
  placeCaret(root, caretElement);
585
- stopWatchingCaret ??= whileVisible(caretElement);
573
+ stopWatchingCaret ??= blink(caretElement);
586
574
  });
587
575
  $effect(() => removeCaret);
588
576
 
@@ -3,8 +3,8 @@
3
3
  import * as stylex from '@stylexjs/stylex';
4
4
  import type { Loose, PartProps } from '../../internal/loose.js';
5
5
  import { attrs, type Sx } from '../../internal/sx.js';
6
- import { whileVisible } from '../../internal/visible.js';
7
- import { colors, motion, play, radius } from '../../tokens.stylex.js';
6
+ import { sweep } from '../../internal/loops.js';
7
+ import { colors, motion, radius } from '../../tokens.stylex.js';
8
8
  import { media } from '../../media.stylex.js';
9
9
 
10
10
  export interface ProgressProps extends Loose<
@@ -15,19 +15,6 @@
15
15
  }
16
16
  > {}
17
17
 
18
- // The bar of an unknown amount crosses the track, stepped, and only while it can be
19
- // seen. With less motion asked for it fills the track and breathes in place instead.
20
- const sweep = stylex.keyframes({ from: { translate: '-100% 0' }, to: { translate: '250% 0' } });
21
- const sweepRtl = stylex.keyframes({
22
- from: { translate: '100% 0' },
23
- to: { translate: '-250% 0' }
24
- });
25
- const breathe = stylex.keyframes({
26
- '0%': { opacity: 1 },
27
- '50%': { opacity: 0.35 },
28
- '100%': { opacity: 1 }
29
- });
30
-
31
18
  const styles = stylex.create({
32
19
  track: {
33
20
  position: 'relative',
@@ -73,16 +60,12 @@
73
60
  transitionDuration: motion.slow,
74
61
  transitionTimingFunction: motion.easeOut
75
62
  },
63
+ // The bar of an unknown amount crosses the track, stepped, and only while it can be
64
+ // seen, drawn on Narvi's clock (`sweep`, in `internal/loops.ts`). With less motion asked
65
+ // for it fills the track and breathes in place instead.
76
66
  unknown: {
77
67
  width: { default: '100%', [media.motion]: '40%' },
78
- animationName: {
79
- default: breathe,
80
- [media.motion]: { default: sweep, ':dir(rtl)': sweepRtl }
81
- },
82
- animationDuration: { default: '2s', [media.motion]: '1.4s' },
83
- animationTimingFunction: { default: 'steps(12)', [media.motion]: 'steps(28)' },
84
- animationIterationCount: 'infinite',
85
- animationPlayState: play.whileVisible
68
+ translate: { default: null, [media.motion]: '-100% 0' }
86
69
  }
87
70
  });
88
71
  </script>
@@ -97,7 +80,7 @@
97
80
  <span {...stylex.attrs(styles.clip)}>
98
81
  <span
99
82
  {...stylex.attrs(styles.fill, known ? styles.known : styles.unknown)}
100
- {@attach !known && whileVisible}
83
+ {@attach !known && sweep}
101
84
  ></span>
102
85
  </span>
103
86
  </UiProgress>
@@ -3,8 +3,8 @@
3
3
  import type { Snippet } from 'svelte';
4
4
  import type { HTMLAttributes } from 'svelte/elements';
5
5
  import { attrs, type Sx } from '../../internal/sx.js';
6
- import { whileVisible } from '../../internal/visible.js';
7
- import { colors, play } from '../../tokens.stylex.js';
6
+ import { glint } from '../../internal/loops.js';
7
+ import { colors } from '../../tokens.stylex.js';
8
8
  import { media } from '../../media.stylex.js';
9
9
 
10
10
  /**
@@ -22,22 +22,6 @@
22
22
  children?: Snippet | undefined;
23
23
  };
24
24
 
25
- // A band of light crosses the line, start to end
26
- const glint = stylex.keyframes({
27
- from: { backgroundPosition: '100% 0' },
28
- to: { backgroundPosition: '0% 0' }
29
- });
30
- const glintRtl = stylex.keyframes({
31
- from: { backgroundPosition: '0% 0' },
32
- to: { backgroundPosition: '100% 0' }
33
- });
34
- // With less motion asked for, the text breathes in place instead
35
- const breathe = stylex.keyframes({
36
- '0%': { opacity: 1 },
37
- '50%': { opacity: 0.55 },
38
- '100%': { opacity: 1 }
39
- });
40
-
41
25
  const styles = stylex.create({
42
26
  // Inline, so it wraps and truncates like the text around it
43
27
  base: {
@@ -53,18 +37,10 @@
53
37
  backgroundSize: '300% 100%',
54
38
  backgroundClip: 'text',
55
39
  WebkitBackgroundClip: 'text',
56
- // Stepped: thirty frames in 2.2 seconds is about fourteen a second, which reads as
57
- // smooth for a slow band of light and leaves the display's other frames alone
58
- animationName: {
59
- default: glint,
60
- ':dir(rtl)': glintRtl,
61
- [media.reducedMotion]: breathe,
62
- [media.forcedColors]: 'none'
63
- },
64
- animationDuration: { default: '2.2s', [media.reducedMotion]: '2s' },
65
- animationTimingFunction: { default: 'steps(30)', [media.reducedMotion]: 'steps(12)' },
66
- animationIterationCount: 'infinite',
67
- animationPlayState: play.whileVisible
40
+ // A band of light crosses the line, start to end, in thirty steps every 2.2 seconds,
41
+ // drawn on Narvi's clock (`glint`, in `internal/loops.ts`). With less motion asked for,
42
+ // the text breathes in place instead; in forced colours it is still.
43
+ backgroundPosition: '100% 0'
68
44
  },
69
45
  // The band: the text's own colour over its quieter one, or the accent
70
46
  neutral: {
@@ -101,7 +77,7 @@
101
77
  data-tone={tone}
102
78
  {...rest}
103
79
  {...attrs([styles.base, active && styles.active, active && styles[tone], sx], className)}
104
- {@attach active && whileVisible}
80
+ {@attach active && glint}
105
81
  >
106
82
  {@render children?.()}
107
83
  </span>
@@ -14,6 +14,7 @@
14
14
  <script lang="ts" module>
15
15
  import * as stylex from '@stylexjs/stylex';
16
16
  import { untrack, type Snippet } from 'svelte';
17
+ import type { Attachment } from 'svelte/attachments';
17
18
  import type { HTMLAnchorAttributes, HTMLAttributes } from 'svelte/elements';
18
19
  import { sidebarItemMarker } from '../../internal/markers.stylex.js';
19
20
  import { attrs, type Sx } from '../../internal/sx.js';
@@ -130,9 +131,6 @@
130
131
  outlineWidth: { default: 0, ':focus-visible': '2px', [media.forcedColors]: '1px' },
131
132
  outlineColor: { default: 'transparent', ':focus-visible': colors.focusRing },
132
133
  outlineOffset: '-2px',
133
- transitionProperty: 'color',
134
- transitionDuration: motion.fast,
135
- transitionTimingFunction: 'ease',
136
134
  '::before': {
137
135
  content: '""',
138
136
  position: 'absolute',
@@ -144,9 +142,18 @@
144
142
  default: 'transparent',
145
143
  ':hover': { default: null, [media.hover]: colors.fillHover },
146
144
  [stylex.when.ancestor(LIT, sidebarItemMarker)]: colors.fillHover
147
- },
145
+ }
146
+ }
147
+ },
148
+ // The fill fades in as the pointer comes over the row, and at no other time: a change
149
+ // of `current`, `picked` or `disabled` shows at once, under the pointer too, and so
150
+ // does the pointer leaving. A fade there kept the window changing for 120ms after its
151
+ // content had. A transition takes the duration of the state it goes to, so this is
152
+ // read only while the row is under the pointer, and only until its state changes.
153
+ fades: {
154
+ '::before': {
148
155
  transitionProperty: 'background-color',
149
- transitionDuration: motion.fast,
156
+ transitionDuration: { default: '0s', ':hover': motion.fast },
150
157
  transitionTimingFunction: 'ease'
151
158
  }
152
159
  },
@@ -326,6 +333,18 @@
326
333
  event.stopImmediatePropagation();
327
334
  }
328
335
 
336
+ // Whether the fill fades: from the pointer coming over the row until its state changes
337
+ let fading = $state(false);
338
+ $effect.pre(() => {
339
+ void [current, picked, disabled];
340
+ fading = false;
341
+ });
342
+ const fade: Attachment<HTMLElement> = (row) => {
343
+ const enter = () => (fading = true);
344
+ row.addEventListener('pointerenter', enter);
345
+ return () => row.removeEventListener('pointerenter', enter);
346
+ };
347
+
329
348
  const rowAttrs = $derived(
330
349
  attrs(
331
350
  [
@@ -337,6 +356,7 @@
337
356
  current && styles.current,
338
357
  picked && styles.picked,
339
358
  disabled && styles.disabled,
359
+ fading && styles.fades,
340
360
  sx
341
361
  ],
342
362
  className
@@ -385,6 +405,7 @@
385
405
  {...rest}
386
406
  {...rowAttrs}
387
407
  onclickcapture={disabled ? block : (rest.onclickcapture as typeof block | undefined)}
408
+ {@attach fade}
388
409
  >
389
410
  {@render inside()}
390
411
  </a>
@@ -399,6 +420,7 @@
399
420
  {...rest as Record<string, unknown>}
400
421
  {...rowAttrs}
401
422
  onclickcapture={disabled ? block : (rest.onclickcapture as typeof block | undefined)}
423
+ {@attach fade}
402
424
  >
403
425
  {@render inside()}
404
426
  </button>
@@ -6,9 +6,8 @@
6
6
  <script lang="ts" module>
7
7
  import * as stylex from '@stylexjs/stylex';
8
8
  import { attrs, type Sx } from '../../internal/sx.js';
9
- import { whileVisible } from '../../internal/visible.js';
10
- import { colors, play, radius } from '../../tokens.stylex.js';
11
- import { media } from '../../media.stylex.js';
9
+ import { skeletonBreath } from '../../internal/loops.js';
10
+ import { colors, radius } from '../../tokens.stylex.js';
12
11
 
13
12
  export type SkeletonProps = {
14
13
  /** Lines of text to stand in for; the last is shorter. Leave out for one block. */
@@ -20,21 +19,9 @@
20
19
  class?: string | undefined;
21
20
  };
22
21
 
23
- // One breath for however many bars sit under it: long holds with short stepped ramps
24
- // between, so the compositor draws a handful of frames a cycle
25
- const breath = stylex.keyframes({
26
- '0%, 42%': { opacity: 1, animationTimingFunction: 'steps(4)' },
27
- '50%, 92%': { opacity: 0.55, animationTimingFunction: 'steps(4)' },
28
- '100%': { opacity: 1 }
29
- });
30
-
22
+ // One breath for however many bars sit under it, drawn on Narvi's clock: long holds with
23
+ // short stepped ramps between (`skeletonBreath`, in `internal/loops.ts`)
31
24
  const styles = stylex.create({
32
- breathes: {
33
- animationName: { default: null, [media.motion]: breath },
34
- animationDuration: '2.4s',
35
- animationIterationCount: 'infinite',
36
- animationPlayState: play.whileVisible
37
- },
38
25
  block: { display: 'block', borderRadius: radius.item, backgroundColor: colors.fillMuted },
39
26
  lines: { display: 'flex', flexDirection: 'column', gap: '0.5em' },
40
27
  line: { height: '0.8em', borderRadius: radius.chip, backgroundColor: colors.fillMuted }
@@ -49,9 +36,9 @@
49
36
  <span
50
37
  aria-hidden="true"
51
38
  data-slot="skeleton"
52
- {...attrs([styles.lines, styles.breathes, sx], className)}
39
+ {...attrs([styles.lines, sx], className)}
53
40
  style:width
54
- {@attach whileVisible}
41
+ {@attach skeletonBreath}
55
42
  >
56
43
  {#each { length: lines }, index (index)}
57
44
  <span
@@ -64,9 +51,9 @@
64
51
  <span
65
52
  aria-hidden="true"
66
53
  data-slot="skeleton"
67
- {...attrs([styles.block, styles.breathes, sx], className)}
54
+ {...attrs([styles.block, sx], className)}
68
55
  style:width
69
56
  style:height
70
- {@attach whileVisible}
57
+ {@attach skeletonBreath}
71
58
  ></span>
72
59
  {/if}
@@ -1,9 +1,8 @@
1
1
  <script lang="ts" module>
2
2
  import * as stylex from '@stylexjs/stylex';
3
3
  import type { SVGAttributes } from 'svelte/elements';
4
- import { shared } from '../../internal/shared.js';
4
+ import { spin } from '../../internal/loops.js';
5
5
  import { attrs, type Sx } from '../../internal/sx.js';
6
- import { whileVisible } from '../../internal/visible.js';
7
6
 
8
7
  export type SpinnerProps = Omit<SVGAttributes<SVGSVGElement>, 'children'> & {
9
8
  /** Width and height in pixels, or any CSS length. Defaults to `1em`. */
@@ -38,8 +37,8 @@
38
37
  aria-label={label ?? undefined}
39
38
  aria-hidden={label ? undefined : true}
40
39
  {...rest}
41
- {...attrs([styles.base, shared.spin, sx], className)}
42
- {@attach whileVisible}
40
+ {...attrs([styles.base, sx], className)}
41
+ {@attach spin}
43
42
  >
44
43
  <circle cx="8" cy="8" r="6.25" stroke="currentColor" stroke-width="1.5" opacity="0.25" />
45
44
  <path
package/dist/index.d.ts CHANGED
@@ -169,6 +169,8 @@ export { default as Theme } from './components/theme/Theme.svelte';
169
169
  export type { ThemeOverrides, ThemeProps, ThemeScheme } from './components/theme/Theme.svelte';
170
170
  export { default as WorkFold } from './components/work-fold/WorkFold.svelte';
171
171
  export type { WorkFoldProps } from './components/work-fold/WorkFold.svelte';
172
+ export { onFrame } from './internal/clock.js';
173
+ export type { FrameDraw, FrameOptions } from './internal/clock.js';
172
174
  export { setPageHidden, whileVisible } from './internal/visible.js';
173
175
  export { providePopupContainer } from './internal/popup.js';
174
176
  export type { PopupContainer } from './internal/popup.js';
package/dist/index.js CHANGED
@@ -128,10 +128,14 @@ export { toast } from './components/toast/toasts.svelte.js';
128
128
  export * as Tooltip from './components/tooltip/index.js';
129
129
  export { default as Theme } from './components/theme/Theme.svelte';
130
130
  export { default as WorkFold } from './components/work-fold/WorkFold.svelte';
131
- // For an animation of your own that never ends: `whileVisible` runs it only while it can be
132
- // seen, with `animationPlayState: play.whileVisible` from the tokens. Spinner, Shimmer,
133
- // Skeleton, Progress and Markdown's caret use it. `setPageHidden` is the host's word that the
134
- // page is out of sight where the document cannot tell, such as a minimized Electron window.
131
+ // Narvi's clock: Spinner, Shimmer, Skeleton, Progress and Markdown's caret are drawn on it, at
132
+ // 30 frames a second (10 while the document lacks focus), only while they can be seen. `onFrame`
133
+ // draws a loop of your own, or streamed content, in the same frames. `setPageHidden` is the
134
+ // host's word that the page is out of sight where the document cannot tell, such as a minimized
135
+ // Electron window; the clock stops while it is. `whileVisible`, with
136
+ // `animationPlayState: play.whileVisible` from the tokens, pauses a CSS animation of your own
137
+ // out of sight, for one that has to stay in CSS.
138
+ export { onFrame } from './internal/clock.js';
135
139
  export { setPageHidden, whileVisible } from './internal/visible.js';
136
140
  // Where popups render: in place by default. `providePopupContainer(() => document.body)` in the
137
141
  // root layout's script renders every menu, popover, select, card, tooltip, dialog and sheet
@@ -0,0 +1,44 @@
1
+ /** Draws one frame, at `time`: milliseconds on the page's clock, as `performance.now()`. */
2
+ export type FrameDraw = (time: number) => void;
3
+ export type FrameOptions = {
4
+ /**
5
+ * Draws only while this element can be seen: in the viewport, on a page that is showing.
6
+ * Without it, `draw` runs on every frame while the page is showing.
7
+ */
8
+ element?: Element | undefined;
9
+ };
10
+ /**
11
+ * Draws on Narvi's clock: about 30 frames a second, 10 while the document lacks focus, in the
12
+ * same frames as Narvi's own loops, and not at all while the page is hidden. For a loop of
13
+ * your own, or streamed content shown as it arrives, so the window draws once for all of
14
+ * them rather than at the display's rate for each. Returns a function that stops it.
15
+ *
16
+ * Write what changes as inline styles, and keep the values on the clock's `time`, not on a
17
+ * count of frames: frames are dropped while the page is hidden and slower behind another
18
+ * window, and loops alike move together because they read the same clock.
19
+ *
20
+ * ```svelte
21
+ * <script lang="ts">
22
+ * import { onFrame } from '@diegopluna/narvi';
23
+ *
24
+ * // A pulse, drawn only while the dot can be seen; the attachment stops it when removed
25
+ * const pulse = (dot: HTMLElement) =>
26
+ * onFrame((time) => (dot.style.opacity = String(0.6 + 0.4 * Math.cos(time / 320))), {
27
+ * element: dot
28
+ * });
29
+ * </script>
30
+ *
31
+ * <span {@attach pulse}></span>
32
+ * ```
33
+ *
34
+ * Something that moves for people who asked for less motion should check
35
+ * `prefers-reduced-motion` itself, as Narvi's loops do.
36
+ */
37
+ export declare function onFrame(draw: FrameDraw, options?: FrameOptions): () => void;
38
+ /**
39
+ * An attachment that draws an element on the clock while it can be seen, and takes back the
40
+ * inline styles it wrote once it is removed, so the element's own styles hold again. `start`
41
+ * is called once for the element and returns what to draw: the styles for a frame, of which
42
+ * only those that changed are written, and `''` takes one away.
43
+ */
44
+ export declare function loop<T extends HTMLElement | SVGElement>(start: (element: T) => (time: number) => Record<string, string>): (element: T) => () => void;
@@ -0,0 +1,206 @@
1
+ /**
2
+ * One clock for everything that moves without end: Narvi's spinners, shimmers, skeletons,
3
+ * bars of an unknown amount and the caret, and whatever an app draws with `onFrame`.
4
+ *
5
+ * A CSS animation that never ends costs a whole window frame at the display's rate in
6
+ * Chromium, 120 a second on a ProMotion Mac, however little it changes: Chromium visits it
7
+ * every frame, so `steps()` timing saves nothing, and an animation of `rotate` alone, which
8
+ * the compositor runs, costs the same, as every frame is still one the GPU process composites.
9
+ * Measured in Eregion, one 12px working StatusIndicator cost about 12% of a core. The cost is
10
+ * the rate, so this draws at 30 frames a second, 10 while the document lacks focus, and every
11
+ * loop in the same frames: loops alike move together, and the window draws once for all of
12
+ * them. It stops when there is nothing to draw: no loop in view, or the page hidden, by the
13
+ * document or by the host (`setPageHidden`).
14
+ *
15
+ * A timer wakes it, and the frame is drawn in `requestAnimationFrame`, so its writes land on
16
+ * the display's beat and it wakes no more often than it draws.
17
+ */
18
+ import { BROWSER } from 'esm-env';
19
+ import { onPageShown, pageShown } from './visible.js';
20
+ const FOCUSED_RATE = 30;
21
+ const UNFOCUSED_RATE = 10;
22
+ // A frame is drawn on the first beat of the display at most this early: half a beat at 120Hz,
23
+ // so a 30-a-second clock lands on every fourth beat there and every second at 60Hz
24
+ const EARLY = 4;
25
+ /** What draws on the next frame: everything subscribed, less what is out of view. */
26
+ const drawing = new Set();
27
+ /** What draws while an element is in view, and whether it is. */
28
+ const watched = new Map();
29
+ let observer = null;
30
+ let timer;
31
+ let request;
32
+ let last = -Infinity;
33
+ let listening = false;
34
+ const interval = () => 1000 / (document.hasFocus() ? FOCUSED_RATE : UNFOCUSED_RATE);
35
+ function tick(time) {
36
+ request = undefined;
37
+ // Woken a beat early, the frame waits for a later one
38
+ if (time - last >= interval() - EARLY) {
39
+ last = time;
40
+ for (const draw of drawing) {
41
+ try {
42
+ draw(time);
43
+ }
44
+ catch (error) {
45
+ // One loop's error stops neither the clock nor the others
46
+ reportError(error);
47
+ }
48
+ }
49
+ }
50
+ schedule();
51
+ }
52
+ const running = () => drawing.size > 0 && pageShown();
53
+ function schedule() {
54
+ if (!running()) {
55
+ clearTimeout(timer);
56
+ if (request !== undefined)
57
+ cancelAnimationFrame(request);
58
+ timer = request = undefined;
59
+ return;
60
+ }
61
+ if (timer !== undefined || request !== undefined)
62
+ return;
63
+ // A little before the frame is due, so the beat it is drawn on is not the one after
64
+ const delay = last + interval() - EARLY * 2 - performance.now();
65
+ if (delay <= 0) {
66
+ request = requestAnimationFrame(tick);
67
+ return;
68
+ }
69
+ timer = setTimeout(() => {
70
+ timer = undefined;
71
+ if (running())
72
+ request = requestAnimationFrame(tick);
73
+ }, delay);
74
+ }
75
+ // Back to the front: the faster rate from the next frame, not after a slow one
76
+ function refocus() {
77
+ clearTimeout(timer);
78
+ timer = undefined;
79
+ schedule();
80
+ }
81
+ function listen() {
82
+ if (listening)
83
+ return;
84
+ listening = true;
85
+ onPageShown(schedule);
86
+ window.addEventListener('focus', refocus);
87
+ }
88
+ function observe(element, entry) {
89
+ observer ??= new IntersectionObserver((entries) => {
90
+ for (const { target, isIntersecting } of entries) {
91
+ for (const each of watched.get(target) ?? []) {
92
+ each.inView = isIntersecting;
93
+ if (isIntersecting)
94
+ drawing.add(each.draw);
95
+ else
96
+ drawing.delete(each.draw);
97
+ }
98
+ }
99
+ schedule();
100
+ });
101
+ let entries = watched.get(element);
102
+ if (!entries) {
103
+ entries = new Set();
104
+ watched.set(element, entries);
105
+ observer.observe(element);
106
+ }
107
+ else {
108
+ // Observed already: it is in view if the others are
109
+ entry.inView = [...entries].some((each) => each.inView);
110
+ if (entry.inView)
111
+ drawing.add(entry.draw);
112
+ }
113
+ entries.add(entry);
114
+ return () => {
115
+ entries.delete(entry);
116
+ if (entries.size > 0)
117
+ return;
118
+ watched.delete(element);
119
+ observer?.unobserve(element);
120
+ if (watched.size === 0) {
121
+ observer?.disconnect();
122
+ observer = null;
123
+ }
124
+ };
125
+ }
126
+ /**
127
+ * Draws on Narvi's clock: about 30 frames a second, 10 while the document lacks focus, in the
128
+ * same frames as Narvi's own loops, and not at all while the page is hidden. For a loop of
129
+ * your own, or streamed content shown as it arrives, so the window draws once for all of
130
+ * them rather than at the display's rate for each. Returns a function that stops it.
131
+ *
132
+ * Write what changes as inline styles, and keep the values on the clock's `time`, not on a
133
+ * count of frames: frames are dropped while the page is hidden and slower behind another
134
+ * window, and loops alike move together because they read the same clock.
135
+ *
136
+ * ```svelte
137
+ * <script lang="ts">
138
+ * import { onFrame } from '@diegopluna/narvi';
139
+ *
140
+ * // A pulse, drawn only while the dot can be seen; the attachment stops it when removed
141
+ * const pulse = (dot: HTMLElement) =>
142
+ * onFrame((time) => (dot.style.opacity = String(0.6 + 0.4 * Math.cos(time / 320))), {
143
+ * element: dot
144
+ * });
145
+ * </script>
146
+ *
147
+ * <span {@attach pulse}></span>
148
+ * ```
149
+ *
150
+ * Something that moves for people who asked for less motion should check
151
+ * `prefers-reduced-motion` itself, as Narvi's loops do.
152
+ */
153
+ export function onFrame(draw, options = {}) {
154
+ if (!BROWSER)
155
+ return () => { };
156
+ listen();
157
+ // Its own function, so the same draw subscribed twice is stopped once each
158
+ const own = (time) => draw(time);
159
+ const { element } = options;
160
+ let unobserve;
161
+ if (element && typeof IntersectionObserver !== 'undefined') {
162
+ // Not drawn until the observer has looked, which it does before the next frame
163
+ unobserve = observe(element, { draw: own, inView: false });
164
+ }
165
+ else {
166
+ drawing.add(own);
167
+ }
168
+ schedule();
169
+ let stopped = false;
170
+ return () => {
171
+ if (stopped)
172
+ return;
173
+ stopped = true;
174
+ drawing.delete(own);
175
+ unobserve?.();
176
+ schedule();
177
+ };
178
+ }
179
+ /**
180
+ * An attachment that draws an element on the clock while it can be seen, and takes back the
181
+ * inline styles it wrote once it is removed, so the element's own styles hold again. `start`
182
+ * is called once for the element and returns what to draw: the styles for a frame, of which
183
+ * only those that changed are written, and `''` takes one away.
184
+ */
185
+ export function loop(start) {
186
+ return (element) => {
187
+ const draw = start(element);
188
+ const written = new Map();
189
+ const stop = onFrame((time) => {
190
+ for (const [property, value] of Object.entries(draw(time))) {
191
+ if ((written.get(property) ?? '') === value)
192
+ continue;
193
+ written.set(property, value);
194
+ if (value === '')
195
+ element.style.removeProperty(property);
196
+ else
197
+ element.style.setProperty(property, value);
198
+ }
199
+ }, { element });
200
+ return () => {
201
+ stop();
202
+ for (const property of written.keys())
203
+ element.style.removeProperty(property);
204
+ };
205
+ };
206
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A spinner's turn, once every 2.4s: 5° a frame at 30 frames a second, which reads as smooth
3
+ * at 12px. The same with less motion asked for: it still has to read as progress.
4
+ */
5
+ export declare const spin: (element: SVGElement | HTMLElement) => () => void;
6
+ /** Shimmer's band of light across the line, start to end; a breath in place with less motion. */
7
+ export declare const glint: (element: HTMLElement) => () => void;
8
+ /**
9
+ * A skeleton's breath: long holds with short stepped ramps between, every 2.4s. Still with
10
+ * less motion asked for.
11
+ */
12
+ export declare const skeletonBreath: (element: SVGElement | HTMLElement) => () => void;
13
+ /**
14
+ * The bar of an unknown amount crossing the track, every 1.4s in 28 steps. With less motion
15
+ * asked for it fills the track (its CSS) and breathes in place instead.
16
+ */
17
+ export declare const sweep: (element: HTMLElement) => () => void;
18
+ /** The caret's blink: on for half of every 1.1s, off for the other. Steady with less motion. */
19
+ export declare const blink: (element: SVGElement | HTMLElement) => () => void;
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Narvi's loops that never end, drawn on its clock (`clock.ts`) as inline styles while they
3
+ * can be seen. Each keeps the timing its CSS animation had, `steps()` included, so it writes
4
+ * only as often as it changes; with less motion asked for, each does what its CSS did then.
5
+ */
6
+ import { loop } from './clock.js';
7
+ const reduceQuery = typeof matchMedia === 'function' ? matchMedia('(prefers-reduced-motion: reduce)') : undefined;
8
+ const forcedQuery = typeof matchMedia === 'function' ? matchMedia('(forced-colors: active)') : undefined;
9
+ /** Whether the reader has asked for less motion; read each frame, so a change shows at once. */
10
+ const reduced = () => reduceQuery?.matches ?? false;
11
+ const forced = () => forcedQuery?.matches ?? false;
12
+ /** How far through a cycle of `period` ms the clock is, from 0 to 1, in `steps` if given. */
13
+ function phase(time, period, steps) {
14
+ const progress = (time % period) / period;
15
+ return steps ? Math.floor(progress * steps) / steps : progress;
16
+ }
17
+ /** Down to `low` and back over `period`, each half in `steps`: a CSS breath of three keyframes. */
18
+ function breath(time, period, low, steps) {
19
+ const progress = phase(time, period);
20
+ const half = progress < 0.5 ? progress * 2 : progress * 2 - 1;
21
+ const step = Math.floor(half * steps) / steps;
22
+ const opacity = progress < 0.5 ? 1 - (1 - low) * step : low + (1 - low) * step;
23
+ return String(Math.round(opacity * 1000) / 1000);
24
+ }
25
+ const rtl = (element) => getComputedStyle(element).direction === 'rtl';
26
+ /**
27
+ * A spinner's turn, once every 2.4s: 5° a frame at 30 frames a second, which reads as smooth
28
+ * at 12px. The same with less motion asked for: it still has to read as progress.
29
+ */
30
+ export const spin = loop(() => (time) => ({
31
+ // In whole steps, so the angle is a whole number of degrees and never 55.00000000000001
32
+ rotate: `${Math.floor(phase(time, 2400) * 72) * 5}deg`
33
+ }));
34
+ /** Shimmer's band of light across the line, start to end; a breath in place with less motion. */
35
+ export const glint = loop((element) => {
36
+ const backwards = rtl(element);
37
+ return (time) => {
38
+ if (forced())
39
+ return { 'background-position': '', opacity: '' };
40
+ if (reduced())
41
+ return { 'background-position': '', opacity: breath(time, 2000, 0.55, 12) };
42
+ const along = phase(time, 2200, 30);
43
+ const position = (backwards ? along : 1 - along) * 100;
44
+ return { 'background-position': `${Math.round(position * 100) / 100}% 0`, opacity: '' };
45
+ };
46
+ });
47
+ /**
48
+ * A skeleton's breath: long holds with short stepped ramps between, every 2.4s. Still with
49
+ * less motion asked for.
50
+ */
51
+ export const skeletonBreath = loop(() => (time) => {
52
+ if (reduced())
53
+ return { opacity: '' };
54
+ const progress = phase(time, 2400);
55
+ const ramp = (from, to, start) => {
56
+ const step = Math.floor(((progress - start) / 0.08) * 4) / 4;
57
+ return from + (to - from) * step;
58
+ };
59
+ const opacity = progress < 0.42
60
+ ? 1
61
+ : progress < 0.5
62
+ ? ramp(1, 0.55, 0.42)
63
+ : progress < 0.92
64
+ ? 0.55
65
+ : ramp(0.55, 1, 0.92);
66
+ return { opacity: String(Math.round(opacity * 1000) / 1000) };
67
+ });
68
+ /**
69
+ * The bar of an unknown amount crossing the track, every 1.4s in 28 steps. With less motion
70
+ * asked for it fills the track (its CSS) and breathes in place instead.
71
+ */
72
+ export const sweep = loop((element) => {
73
+ const backwards = rtl(element);
74
+ return (time) => {
75
+ if (reduced())
76
+ return { translate: '', opacity: breath(time, 2000, 0.35, 12) };
77
+ // From one bar's width before the track to past its end: -100% to 250% of the bar
78
+ const at = -100 + 350 * phase(time, 1400, 28);
79
+ return { translate: `${Math.round((backwards ? -at : at) * 100) / 100}% 0`, opacity: '' };
80
+ };
81
+ });
82
+ /** The caret's blink: on for half of every 1.1s, off for the other. Steady with less motion. */
83
+ export const blink = loop(() => (time) => ({
84
+ opacity: reduced() || phase(time, 1100) < 0.5 ? '' : '0'
85
+ }));
@@ -11,13 +11,6 @@ export declare const shared: Readonly<{
11
11
  readonly outlineOffset: stylex.StyleXClassNameFor<"outlineOffset", "2px" | "-1px">;
12
12
  readonly WebkitAppRegion: stylex.StyleXClassNameFor<"WebkitAppRegion", "no-drag">;
13
13
  }>;
14
- readonly spin: Readonly<{
15
- readonly animationName: stylex.StyleXClassNameFor<"animationName", string>;
16
- readonly animationDuration: stylex.StyleXClassNameFor<"animationDuration", "600ms" | "2.4s">;
17
- readonly animationTimingFunction: stylex.StyleXClassNameFor<"animationTimingFunction", "linear">;
18
- readonly animationIterationCount: stylex.StyleXClassNameFor<"animationIterationCount", "infinite">;
19
- readonly animationPlayState: stylex.StyleXClassNameFor<"animationPlayState", string>;
20
- }>;
21
14
  readonly dragRegion: Readonly<{
22
15
  readonly WebkitAppRegion: stylex.StyleXClassNameFor<"WebkitAppRegion", "drag">;
23
16
  readonly WebkitUserSelect: stylex.StyleXClassNameFor<"WebkitUserSelect", "none">;
@@ -1,7 +1,5 @@
1
1
  import * as stylex from '@stylexjs/stylex';
2
- import { colors, font, play } from '../tokens.stylex.js';
3
- import { media } from '../media.stylex.js';
4
- const turn = stylex.keyframes({ to: { rotate: '1turn' } });
2
+ import { colors, font } from '../tokens.stylex.js';
5
3
  // Focus as the keyboard gives it, or as the catalog shows it without a keyboard
6
4
  const FOCUS = ':is(:focus-visible, [data-preview~="focus"])';
7
5
  /**
@@ -19,18 +17,6 @@ export const shared = stylex.create({
19
17
  // never receive the click. Ignored everywhere else.
20
18
  WebkitAppRegion: 'no-drag'
21
19
  },
22
- // A spinner's turn. It animates `rotate` and nothing else, so the compositor turns it,
23
- // smoothly and at the display's rate, with no style or paint on the main thread. Narvi's
24
- // other loops are stepped; a stepped spinner reads as slow. Slower, not still, for people
25
- // who have asked for less motion: it still has to read as progress. Attach `whileVisible`
26
- // to what takes this, so it turns only while it can be seen.
27
- spin: {
28
- animationName: turn,
29
- animationDuration: { default: '2.4s', [media.motion]: '600ms' },
30
- animationTimingFunction: 'linear',
31
- animationIterationCount: 'infinite',
32
- animationPlayState: play.whileVisible
33
- },
34
20
  // The parts of a window's title bar that drag the window, in Electron
35
21
  dragRegion: {
36
22
  WebkitAppRegion: 'drag',
@@ -1,27 +1,22 @@
1
1
  /**
2
- * Lets an animation that never ends run only while it can be seen.
3
- *
4
- * Such an animation costs something for as long as it runs: a shimmer three
5
- * screens up the conversation, or in a window behind another, is paid for and
6
- * never looked at. So every one of Narvi's reads its play state from a custom
7
- * property, and this sets that property: running while the element is in the
8
- * viewport and the page is showing, paused otherwise.
2
+ * Whether the page is showing, and `whileVisible`, for a CSS animation of your own.
9
3
  *
10
4
  * The page is showing unless the document says it is hidden or the host does. A
11
5
  * desktop app that keeps its window running in the background, such as an Electron
12
6
  * one with background throttling off, is never hidden to the document: minimized or
13
7
  * closed to the tray, it still counts as showing. The host knows better, and says so
14
- * with `setPageHidden`.
8
+ * with `setPageHidden`. Narvi's clock (`clock.ts`) stops while the page is hidden.
15
9
  *
16
- * The animations themselves are stepped (`steps(30)` over two seconds, say), so
17
- * the compositor draws a handful of frames a second, not one for every refresh
18
- * of the display. A spinner is the exception: it has to turn smoothly, so it is
19
- * a plain rotation, and only the pausing applies to it.
10
+ * Narvi's own loops are drawn on that clock, not in CSS: in Chromium a CSS animation that
11
+ * never ends costs a window frame at the display's rate, stepped or not, and one of `rotate`
12
+ * alone, which the compositor runs, costs the same. `whileVisible` is for an animation that
13
+ * has to stay in CSS: it reads its play state from a custom property, and this sets that
14
+ * property, running while the element is in the viewport and the page is showing, paused
15
+ * otherwise. Prefer `onFrame` for a new one.
20
16
  *
21
17
  * Each time one starts to run, it is set to where the page's clock puts it, as if every
22
18
  * animation had started as the page loaded. So animations alike move together however late each
23
- * came into view: the spinners of a list of rows turn as one, rather than each from the moment
24
- * its row scrolled in.
19
+ * came into view.
25
20
  *
26
21
  * Without this the animation runs: the property's fallback is `running`.
27
22
  *
@@ -33,19 +28,23 @@
33
28
  * pulse: {
34
29
  * animationName: pulse,
35
30
  * animationDuration: '2s',
36
- * animationTimingFunction: 'steps(12)',
37
31
  * animationIterationCount: 'infinite',
38
32
  * animationPlayState: play.whileVisible // from tokens.stylex.js
39
33
  * }
40
34
  * ```
41
35
  */
42
36
  import type { Attachment } from 'svelte/attachments';
37
+ /** Whether the page is showing: not hidden by the document, nor by the host. */
38
+ export declare const pageShown: () => boolean;
39
+ /** Calls `listener` each time the page may have been shown or hidden. */
40
+ export declare function onPageShown(listener: () => void): void;
43
41
  /**
44
42
  * Tells Narvi whether the host has hidden the page, where the document cannot tell: an
45
43
  * Electron window minimized or closed to the tray with background throttling off, a view
46
- * kept alive out of sight. While it is hidden, every animation that never ends pauses: the
47
- * spinners, shimmers, skeletons, progress bars and carets, and your own that use
48
- * `whileVisible`. They run again once it is shown, where they can be seen.
44
+ * kept alive out of sight. While it is hidden, every animation that never ends pauses: Narvi's
45
+ * clock stops, and with it the spinners, shimmers, skeletons, progress bars and carets and
46
+ * whatever you draw with `onFrame`, and your own CSS animations that use `whileVisible`. They
47
+ * run again once it is shown, where they can be seen.
49
48
  *
50
49
  * ```ts
51
50
  * import { setPageHidden } from '@diegopluna/narvi';
@@ -15,7 +15,7 @@ function align(element) {
15
15
  }
16
16
  }
17
17
  function apply(item) {
18
- const running = item.inView && !document.hidden && !hiddenByHost;
18
+ const running = item.inView && pageShown();
19
19
  item.element.style.setProperty('--narvi-play', running ? 'running' : 'paused');
20
20
  if (running && !item.running)
21
21
  align(item.element);
@@ -25,12 +25,29 @@ function applyAll() {
25
25
  for (const item of watched.values())
26
26
  apply(item);
27
27
  }
28
+ // What else follows whether the page is showing: the clock
29
+ const followers = new Set();
30
+ let followingDocument = false;
31
+ /** Whether the page is showing: not hidden by the document, nor by the host. */
32
+ export const pageShown = () => !document.hidden && !hiddenByHost;
33
+ /** Calls `listener` each time the page may have been shown or hidden. */
34
+ export function onPageShown(listener) {
35
+ followers.add(listener);
36
+ if (followingDocument)
37
+ return;
38
+ followingDocument = true;
39
+ document.addEventListener('visibilitychange', () => {
40
+ for (const follower of followers)
41
+ follower();
42
+ });
43
+ }
28
44
  /**
29
45
  * Tells Narvi whether the host has hidden the page, where the document cannot tell: an
30
46
  * Electron window minimized or closed to the tray with background throttling off, a view
31
- * kept alive out of sight. While it is hidden, every animation that never ends pauses: the
32
- * spinners, shimmers, skeletons, progress bars and carets, and your own that use
33
- * `whileVisible`. They run again once it is shown, where they can be seen.
47
+ * kept alive out of sight. While it is hidden, every animation that never ends pauses: Narvi's
48
+ * clock stops, and with it the spinners, shimmers, skeletons, progress bars and carets and
49
+ * whatever you draw with `onFrame`, and your own CSS animations that use `whileVisible`. They
50
+ * run again once it is shown, where they can be seen.
34
51
  *
35
52
  * ```ts
36
53
  * import { setPageHidden } from '@diegopluna/narvi';
@@ -46,6 +63,8 @@ export function setPageHidden(hidden) {
46
63
  return;
47
64
  hiddenByHost = hidden;
48
65
  applyAll();
66
+ for (const follower of followers)
67
+ follower();
49
68
  }
50
69
  export const whileVisible = (element) => {
51
70
  // Nothing to watch with: leave the animation running, as it would without this
@@ -129,10 +129,12 @@ export declare const motion: stylex.VarGroup<Readonly<{
129
129
  easeInOut: string;
130
130
  }>, symbol>;
131
131
  /**
132
- * For an animation that never ends. Set `animationPlayState: play.whileVisible` on it and
132
+ * For a CSS animation that never ends. Set `animationPlayState: play.whileVisible` on it and
133
133
  * attach `whileVisible` to its element: it then runs only while it can be seen. Without the
134
134
  * attachment it runs as any animation does. A constant, not a variable, so it is read on the
135
- * element it is used on.
135
+ * element it is used on. In Chromium such an animation still costs a window frame at the
136
+ * display's rate while it runs, stepped or not: draw a new loop with `onFrame` instead, on
137
+ * Narvi's clock, as Narvi's own loops are.
136
138
  */
137
139
  export declare const play: {
138
140
  whileVisible: string;
@@ -189,10 +189,12 @@ export const motion = stylex.defineVars({
189
189
  easeInOut: 'cubic-bezier(0.77, 0, 0.175, 1)'
190
190
  });
191
191
  /**
192
- * For an animation that never ends. Set `animationPlayState: play.whileVisible` on it and
192
+ * For a CSS animation that never ends. Set `animationPlayState: play.whileVisible` on it and
193
193
  * attach `whileVisible` to its element: it then runs only while it can be seen. Without the
194
194
  * attachment it runs as any animation does. A constant, not a variable, so it is read on the
195
- * element it is used on.
195
+ * element it is used on. In Chromium such an animation still costs a window frame at the
196
+ * display's rate while it runs, stepped or not: draw a new loop with `onFrame` instead, on
197
+ * Narvi's clock, as Narvi's own loops are.
196
198
  */
197
199
  export const play = stylex.defineConsts({
198
200
  whileVisible: 'var(--narvi-play, running)'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diegopluna/narvi",
3
- "version": "0.5.4",
3
+ "version": "0.5.5",
4
4
  "packageManager": "pnpm@12.18.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,7 +12,7 @@ metadata:
12
12
  Grid, and Narvi's conventions for colour, labels and copy.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.4'
15
+ library_version: '0.5.5'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -48,6 +48,7 @@ sources:
48
48
  - src/lib/components/command/*.svelte
49
49
  - src/lib/components/menu/MenuContent.svelte
50
50
  - src/lib/internal/popup.ts
51
+ - src/lib/internal/clock.ts
51
52
  ---
52
53
 
53
54
  # Build interfaces with Narvi
@@ -323,8 +324,7 @@ and `Kbd` inline, `Link` inside text, `Button href` for a link that looks like a
323
324
 
324
325
  ### Lists, badges and folds
325
326
 
326
- - Rows on one card with a line between each: `List.Root` (a `ul`, or `as="div"`) holding
327
- `List.Item`s, or rows of your own. Don't redraw the card and its lines.
327
+ - Rows on one card with a line between each: `List.Root` (a `ul`, or `as="div"`) holding `List.Item`s, or rows of your own. Don't redraw the card and its lines.
328
328
  - `Badge` shows its tone as a dot; `plain` hides the dot and colours the words in the tone.
329
329
  `color` paints the dot alone in a colour of its own (a label's `#d73a4a`): don't draw a dot.
330
330
  `size="sm"` is 18px, a sidebar row's line, so a row keeps its height; an icon in it is 12px.
@@ -509,10 +509,19 @@ the request nearest the composer, and what waits behind it in a region that scro
509
509
 
510
510
  - `sx()`, `classes()` and the `Sx` type: see the `styling` skill.
511
511
  - `resolveColor()` and `resolveRgba()`: see the `theming` skill.
512
- - `whileVisible`: an attachment that runs an animation that never ends only while it can be seen, with `animationPlayState: play.whileVisible` from the tokens. Step the animation itself. Each time it starts to run it is set to the page's clock, so animations alike, such as the spinners down a list, move together; don't stagger them by hand.
513
- Shimmer, Skeleton, Progress and Markdown's caret run on it, stepped. Spinner pauses with it
514
- too, but is not stepped: it is a CSS animation of `rotate`, one turn in 600ms, so it stays smooth.
512
+ - `onFrame(draw, { element })`: Narvi's clock, for a loop that never ends or streamed content
513
+ shown as it arrives. It calls `draw(time)` (milliseconds, as `performance.now()`) about 30
514
+ times a second, 10 while the document lacks focus, in the same frames as Narvi's own loops,
515
+ and returns a function that stops it. With `element` it draws only while that element is in
516
+ the viewport; it never draws while the page is hidden. Write inline styles from `time`, not a
517
+ count of frames, and check `prefers-reduced-motion` yourself. Spinner (and StatusIndicator's
518
+ working arc), Shimmer, Skeleton, Progress and Markdown's caret are drawn on it: Spinner turns
519
+ once every 2.4s, 5° a frame. Don't write a CSS animation that never ends: in Chromium it costs
520
+ a whole window frame at the display's rate, stepped or not, even one of `rotate` alone.
521
+ - `whileVisible`: an attachment that pauses a CSS animation that never ends while it can't be
522
+ seen, with `animationPlayState: play.whileVisible` from the tokens. Only for one that has to
523
+ stay in CSS; prefer `onFrame`.
515
524
  - `setPageHidden(hidden)`: tells Narvi the host has hidden the page where the document cannot
516
- tell, such as a minimized Electron window that keeps running. Every `whileVisible` animation
517
- pauses until it is called with `false`.
525
+ tell, such as a minimized Electron window that keeps running. The clock stops, and every
526
+ `whileVisible` animation pauses, until it is called with `false`.
518
527
  - `DevStyles`: see the `setup` skill.
@@ -12,7 +12,7 @@ metadata:
12
12
  patching the DOM, and decide which links and images text from a model may load.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.4'
15
+ library_version: '0.5.5'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -11,7 +11,7 @@ metadata:
11
11
  4 when the app uses it.
12
12
  type: lifecycle
13
13
  library: '@diegopluna/narvi'
14
- library_version: '0.5.4'
14
+ library_version: '0.5.5'
15
15
  sources:
16
16
  - src/lib/vite.ts
17
17
  - src/lib/base.css
@@ -12,7 +12,7 @@ metadata:
12
12
  hover, motion and forced-colour preferences.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.4'
15
+ library_version: '0.5.5'
16
16
  requires:
17
17
  - setup
18
18
  sources:
@@ -24,6 +24,7 @@ sources:
24
24
  - src/lib/markers.stylex.ts
25
25
  - src/lib/visually-hidden.ts
26
26
  - src/lib/internal/visible.ts
27
+ - src/lib/internal/clock.ts
27
28
  - src/lib/typography.ts
28
29
  ---
29
30
 
@@ -121,15 +122,22 @@ package root) breaks them.
121
122
  forced colours. `focusRing.frame` goes on a box around a field of your own, such as an editor:
122
123
  its hairline takes the ring and halo while the field inside is typed into, as a text field's
123
124
  does. Don't redraw either with `:focus-visible` styles of your own.
124
- - `play.whileVisible`: the `animationPlayState` for an animation that never ends. Attach
125
- `whileVisible` (from the package root) to its element and it runs only while it can be seen,
126
- in step with the page's other animations of the same length.
127
- Step such an animation (`steps(30)` over two seconds) so it costs a few frames a second.
125
+ - A loop that never ends goes on Narvi's clock, not in a CSS animation: in Chromium a CSS
126
+ animation that never ends costs a whole window frame at the display's rate, stepped or not,
127
+ even one of `rotate` alone. `onFrame(draw, { element })` (from the package root) calls
128
+ `draw(time)` about 30 times a second, 10 while the document lacks focus, only while `element`
129
+ is in the viewport and the page is showing, and returns a function that stops it. Write the
130
+ changing value as an inline style worked out from `time`, take it away when you stop, and
131
+ check `prefers-reduced-motion` yourself. Narvi's spinners, shimmers, skeletons, progress bars
132
+ and Markdown's caret are drawn on it.
133
+ - `play.whileVisible`: the `animationPlayState` for a CSS animation that never ends, for one
134
+ that has to stay in CSS. Attach `whileVisible` (from the package root) to its element and it
135
+ runs only while it can be seen, in step with the page's other animations of the same length.
128
136
  - `setPageHidden(hidden)` (from the package root) is the host's word that the page is out of
129
137
  sight where `document.hidden` stays false, as in an Electron window with background
130
138
  throttling off, minimized or closed to the tray. Call it with `true` then and `false` when the
131
- window is back: every `whileVisible` animation pauses meanwhile, Narvi's spinners, shimmers,
132
- skeletons, progress bars and Markdown's caret among them. Don't pause them by hand.
139
+ window is back: the clock stops and every `whileVisible` animation pauses meanwhile. Don't
140
+ pause them by hand.
133
141
  - `motion`: `fast` 120ms, `base` 150ms, `slow` 200ms; `easeOut` for feedback, `easeInOut` for
134
142
  things that travel. Nothing in an interface takes longer than `motion.slow`.
135
143
  - `pageMotion`: a slower scale for pages read as they scroll (`Reveal` uses it). Never use it
@@ -12,7 +12,7 @@ metadata:
12
12
  menus and dialogs, and resolve token colours for drawing code that cannot read CSS variables.
13
13
  type: core
14
14
  library: '@diegopluna/narvi'
15
- library_version: '0.5.4'
15
+ library_version: '0.5.5'
16
16
  requires:
17
17
  - setup
18
18
  sources: