@design-edito/tools 0.5.17 → 0.5.19
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/agnostic/colors/index.d.ts +2 -2
- package/agnostic/colors/index.js +2 -2
- package/agnostic/html/hyper-json/smart-tags/coalesced/index.d.ts +11 -11
- package/agnostic/html/hyper-json/smart-tags/coalesced/index.js +11 -11
- package/agnostic/html/hyper-json/smart-tags/isolated/index.d.ts +6 -6
- package/agnostic/html/hyper-json/smart-tags/isolated/index.js +6 -6
- package/agnostic/html/index.d.ts +2 -2
- package/agnostic/html/index.js +2 -2
- package/agnostic/misc/index.d.ts +3 -3
- package/agnostic/misc/index.js +3 -3
- package/agnostic/misc/logs/index.d.ts +1 -1
- package/agnostic/misc/logs/index.js +1 -1
- package/agnostic/numbers/index.d.ts +3 -3
- package/agnostic/numbers/index.js +3 -3
- package/agnostic/objects/index.d.ts +1 -1
- package/agnostic/objects/index.js +1 -1
- package/agnostic/optim/index.d.ts +1 -1
- package/agnostic/optim/index.js +1 -1
- package/agnostic/strings/index.d.ts +3 -3
- package/agnostic/strings/index.js +3 -3
- package/agnostic/time/index.d.ts +2 -2
- package/agnostic/time/index.js +2 -2
- package/components/Image/index.d.ts +6 -0
- package/components/Image/index.js +3 -18
- package/components/Video/index.controlled.d.ts +5 -22
- package/components/Video/index.controlled.js +28 -42
- package/components/Video/index.d.ts +36 -37
- package/components/Video/index.js +83 -88
- package/components/Video/types.d.ts +42 -0
- package/components/Video/types.js +30 -0
- package/components/Video/utils.d.ts +38 -1
- package/components/Video/utils.js +23 -4
- package/components/index.d.ts +1 -1
- package/components/index.js +1 -1
- package/components/utils/index.d.ts +21 -0
- package/components/utils/index.js +40 -0
- package/components/utils/viewport-behaviours/index.d.ts +54 -0
- package/components/utils/viewport-behaviours/index.js +163 -0
- package/components/utils/viewport-behaviours/types.d.ts +109 -0
- package/components/utils/viewport-behaviours/types.js +14 -0
- package/components/utils/viewport-behaviours/utils.d.ts +47 -0
- package/components/utils/viewport-behaviours/utils.js +86 -0
- package/node/@aws-s3/storage/file/index.d.ts +1 -1
- package/node/@aws-s3/storage/file/index.js +1 -1
- package/node/cloud-storage/operations/index.d.ts +1 -1
- package/node/cloud-storage/operations/index.js +1 -1
- package/package.json +16 -1
|
@@ -1,35 +1,34 @@
|
|
|
1
1
|
import { type FunctionComponent } from 'react';
|
|
2
|
-
import type
|
|
2
|
+
import { type ViewportBehaviours } from '../utils/viewport-behaviours/index.js';
|
|
3
|
+
import type { VideoAction } from './types.js';
|
|
3
4
|
import { type Props as ControlledProps } from './index.controlled.js';
|
|
4
5
|
/**
|
|
5
6
|
* Props for the {@link Video} component.
|
|
6
7
|
*
|
|
7
8
|
* Extends all ControlledVideo props except play, mute, fullscreen, volume, playbackRate, and their associated event handlers
|
|
8
|
-
* @property
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* @
|
|
17
|
-
* @property
|
|
18
|
-
* the
|
|
19
|
-
* @property
|
|
20
|
-
*
|
|
21
|
-
* counts as visible, forwarded to the internal {@link IntersectionObserver}. `0.3`
|
|
22
|
-
* to start on a third of it; omitted, a single pixel is enough.
|
|
23
|
-
* @property root - The observer's root. Defaults to the viewport.
|
|
24
|
-
* @property rootMargin - Grows or shrinks that root before measuring.
|
|
25
|
-
* @property onVisibilityChanged - Called on every crossing with the new value,
|
|
26
|
-
* whether or not an `auto…` behaviour is bound to it. Never on mount.
|
|
9
|
+
* @property behavioursSuspended - Holds back the instructions that **start** something,
|
|
10
|
+
* and lets through those that stop it. For a consumer withholding the video behind
|
|
11
|
+
* something — a warning to accept, typically: a video kept back that plays anyway is not
|
|
12
|
+
* withheld, it is merely hard to see. What was held back is replayed the moment this
|
|
13
|
+
* goes false, so lifting the veil on an already-visible video does what was asked, which
|
|
14
|
+
* a crossing-based trigger could never do. `':force'` does not override it: it speaks of
|
|
15
|
+
* consent not yet given, where a surrender speaks of an intention already expressed.
|
|
16
|
+
* @property visibilityThreshold - How much of the video has to be on screen to count as
|
|
17
|
+
* seen. See {@link VisibilityOptions} for this and the four that follow it.
|
|
18
|
+
* @property whenVisible - What to do each time it becomes visible.
|
|
19
|
+
* @property whenHidden - The same, on the way out.
|
|
20
|
+
* @property onVisibilityChanged - Called with the new value once it has settled — the
|
|
21
|
+
* delays included, so this and the instructions are told the same story. Never on mount.
|
|
27
22
|
* @property currentTimeMs - When provided, hands ownership of the current time
|
|
28
23
|
* (in milliseconds) to the parent, which is then responsible for updating it —
|
|
29
24
|
* typically to scrub the video from scroll position. A controlled time implies a
|
|
30
25
|
* stopped video, since a playing element would advance a value it does not own:
|
|
31
|
-
* `autoPlay`, `
|
|
26
|
+
* `autoPlay`, a `'play'` instruction and the play button have no effect for as
|
|
32
27
|
* long as this prop is provided.
|
|
28
|
+
* @property togglePlayOnClick - When `true`, a click on the picture plays or pauses it.
|
|
29
|
+
* A **second** way to reach what the play and pause buttons already do, never the only
|
|
30
|
+
* one: the element takes no focus stop, so the keyboard keeps using the buttons. The
|
|
31
|
+
* controls painted over the picture are not part of the surface.
|
|
33
32
|
* @property defaultSubtitlesOn - Whether the subtitles start shown. `true` by default —
|
|
34
33
|
* an article that supplies cues means them to be read. The reader's button takes it from
|
|
35
34
|
* there, so this is a starting point and not a setting: `default…` and never `initial…`,
|
|
@@ -38,16 +37,9 @@ import { type Props as ControlledProps } from './index.controlled.js';
|
|
|
38
37
|
* @property children - React children rendered inside the `<video>` element itself
|
|
39
38
|
* (e.g. fallback content).
|
|
40
39
|
*/
|
|
41
|
-
export type Props =
|
|
40
|
+
export type Props = Omit<ControlledProps, 'play' | 'fullscreen' | 'volume' | 'mute' | 'playbackRate' | 'subtitlesOn' | 'rootRef' | 'videoRef'> & ViewportBehaviours<VideoAction> & {
|
|
42
41
|
defaultSubtitlesOn?: boolean;
|
|
43
|
-
|
|
44
|
-
autoPlayOnceVisible?: boolean;
|
|
45
|
-
autoPauseWhenHidden?: boolean;
|
|
46
|
-
autoPauseOnceHidden?: boolean;
|
|
47
|
-
autoLoudWhenVisible?: boolean;
|
|
48
|
-
autoLoudOnceVisible?: boolean;
|
|
49
|
-
autoMuteWhenHidden?: boolean;
|
|
50
|
-
autoMuteOnceHidden?: boolean;
|
|
42
|
+
behavioursSuspended?: boolean;
|
|
51
43
|
};
|
|
52
44
|
/**
|
|
53
45
|
* Full-featured video player component. Wraps a native `<video>` element with
|
|
@@ -70,11 +62,18 @@ export type Props = WithViewportObservation<Omit<ControlledProps, 'play' | 'full
|
|
|
70
62
|
* label: a screen reader announces a video player. Worth a `tag` prop the day that
|
|
71
63
|
* matters more than autoplay — not before.
|
|
72
64
|
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
65
|
+
* **Viewport-driven behaviour is declared, not named by a prop.** `whenVisible` and
|
|
66
|
+
* `whenHidden` take a verb or a list of them, out of {@link VideoAction} — `'play'`,
|
|
67
|
+
* `'mute'`, `'jump-to:500'` — each optionally suffixed by `':once'` and `':force'`. See
|
|
68
|
+
* `components/utils/viewport-behaviours` for the grammar and its rules, and the
|
|
69
|
+
* `visibility…` props for what « visible » means and how long
|
|
70
|
+
* it has to have been true.
|
|
71
|
+
*
|
|
72
|
+
* **An instruction yields to the reader by default.** Touching a control surrenders its
|
|
73
|
+
* domain for the rest of the mount: playback covers the play and pause buttons, the
|
|
74
|
+
* picture when it toggles, and the timeline, since seeking is saying where one wants to
|
|
75
|
+
* be. Sound covers the two sound buttons and the volume slider. Fullscreen and playback
|
|
76
|
+
* rate surrender nothing, being neither. `':force'` opts out.
|
|
78
77
|
*
|
|
79
78
|
* **`loop` reaches the element untouched**, like every other native media attribute
|
|
80
79
|
* this component does not drive itself. It was once destructured out of the props and
|
|
@@ -83,8 +82,8 @@ export type Props = WithViewportObservation<Omit<ControlledProps, 'play' | 'full
|
|
|
83
82
|
* consequence worth knowing: a looping element never fires `ended`, so the `--ended`
|
|
84
83
|
* modifier and the `isEnded` it feeds to `Subtitles` simply never come.
|
|
85
84
|
*
|
|
86
|
-
* Browsers refuse an unmuted `play()` outside a user gesture, so pairing
|
|
87
|
-
*
|
|
85
|
+
* Browsers refuse an unmuted `play()` outside a user gesture, so pairing a `'loud'`
|
|
86
|
+
* instruction with a `'play'` one will usually have the playback rejected. The
|
|
88
87
|
* refusal is caught rather than ignored — the element is read back once the attempt
|
|
89
88
|
* settles, and the play state follows what it says — so the controls stay truthful.
|
|
90
89
|
* The media still won't play, though: autoplay muted, and leave unmuting to the
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useCallback, useEffect,
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { useViewportBehaviours } from '../utils/viewport-behaviours/index.js';
|
|
4
|
+
import { forceJumpTo, muteAttributeWorkaround } from './utils.js';
|
|
5
5
|
import { ControlledVideo } from './index.controlled.js';
|
|
6
6
|
/**
|
|
7
7
|
* Full-featured video player component. Wraps a native `<video>` element with
|
|
@@ -24,11 +24,18 @@ import { ControlledVideo } from './index.controlled.js';
|
|
|
24
24
|
* label: a screen reader announces a video player. Worth a `tag` prop the day that
|
|
25
25
|
* matters more than autoplay — not before.
|
|
26
26
|
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
27
|
+
* **Viewport-driven behaviour is declared, not named by a prop.** `whenVisible` and
|
|
28
|
+
* `whenHidden` take a verb or a list of them, out of {@link VideoAction} — `'play'`,
|
|
29
|
+
* `'mute'`, `'jump-to:500'` — each optionally suffixed by `':once'` and `':force'`. See
|
|
30
|
+
* `components/utils/viewport-behaviours` for the grammar and its rules, and the
|
|
31
|
+
* `visibility…` props for what « visible » means and how long
|
|
32
|
+
* it has to have been true.
|
|
33
|
+
*
|
|
34
|
+
* **An instruction yields to the reader by default.** Touching a control surrenders its
|
|
35
|
+
* domain for the rest of the mount: playback covers the play and pause buttons, the
|
|
36
|
+
* picture when it toggles, and the timeline, since seeking is saying where one wants to
|
|
37
|
+
* be. Sound covers the two sound buttons and the volume slider. Fullscreen and playback
|
|
38
|
+
* rate surrender nothing, being neither. `':force'` opts out.
|
|
32
39
|
*
|
|
33
40
|
* **`loop` reaches the element untouched**, like every other native media attribute
|
|
34
41
|
* this component does not drive itself. It was once destructured out of the props and
|
|
@@ -37,14 +44,14 @@ import { ControlledVideo } from './index.controlled.js';
|
|
|
37
44
|
* consequence worth knowing: a looping element never fires `ended`, so the `--ended`
|
|
38
45
|
* modifier and the `isEnded` it feeds to `Subtitles` simply never come.
|
|
39
46
|
*
|
|
40
|
-
* Browsers refuse an unmuted `play()` outside a user gesture, so pairing
|
|
41
|
-
*
|
|
47
|
+
* Browsers refuse an unmuted `play()` outside a user gesture, so pairing a `'loud'`
|
|
48
|
+
* instruction with a `'play'` one will usually have the playback rejected. The
|
|
42
49
|
* refusal is caught rather than ignored — the element is read back once the attempt
|
|
43
50
|
* settles, and the play state follows what it says — so the controls stay truthful.
|
|
44
51
|
* The media still won't play, though: autoplay muted, and leave unmuting to the
|
|
45
52
|
* reader.
|
|
46
53
|
*/
|
|
47
|
-
export const Video = ({ defaultSubtitlesOn = true,
|
|
54
|
+
export const Video = ({ defaultSubtitlesOn = true, behavioursSuspended, visibilityThreshold, visibilityRoot, visibilityRootMargin, visibilityOnAfterMs, visibilityOffAfterMs, whenVisible, whenHidden, onVisibilityChanged, onVideoClicked, onPlayButtonClicked, onPauseButtonClicked, onLoudButtonClicked, onMuteButtonClicked, onVolumeRangeChanged, onRateRangeChanged, onFullscreenButtonClicked, onSubtitlesButtonClicked, ...controlledProps }) => {
|
|
48
55
|
// State & refs
|
|
49
56
|
const [play, setPlay] = useState(false);
|
|
50
57
|
const [volume, setVolume] = useState(1);
|
|
@@ -52,39 +59,55 @@ export const Video = ({ defaultSubtitlesOn = true, autoPlayWhenVisible, autoPlay
|
|
|
52
59
|
const [playbackRate, setPlaybackRate] = useState(1);
|
|
53
60
|
const [fullscreen, setFullscreen] = useState(false);
|
|
54
61
|
const [subtitlesOn, setSubtitlesOn] = useState(defaultSubtitlesOn);
|
|
55
|
-
//
|
|
56
|
-
//
|
|
57
|
-
//
|
|
58
|
-
// behaviours that had nothing to do with playback.
|
|
59
|
-
const hasAutoPlayedOnce = useRef(false);
|
|
60
|
-
const hasAutoPausedOnce = useRef(false);
|
|
61
|
-
const hasAutoLoudedOnce = useRef(false);
|
|
62
|
-
const hasAutoMutedOnce = useRef(false);
|
|
63
|
-
// Several paths below ask for playback — the play button, autoPlayWhenVisible,
|
|
64
|
-
// autoPlay itself. None of them may win over a parent-owned time, so the
|
|
65
|
-
// invariant is applied where the state is forwarded rather than guarded at each
|
|
66
|
-
// of those call sites.
|
|
62
|
+
// Several paths below ask for playback — the play button, a `'play'` instruction,
|
|
63
|
+
// `autoPlay` itself. None of them may win over a parent-owned time, so the invariant
|
|
64
|
+
// is applied where the state is forwarded rather than guarded at each call site.
|
|
67
65
|
const isTimeControlled = controlledProps.currentTimeMs !== undefined;
|
|
68
66
|
const rootRef = useRef(null);
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
67
|
+
const videoRef = useRef(null);
|
|
68
|
+
// Viewport behaviours
|
|
69
|
+
// What each verb does, and what it competes with. Rebuilt on every render — it closes
|
|
70
|
+
// over the setters — which is why the generic layer reads it through a ref rather than
|
|
71
|
+
// a dependency list.
|
|
72
|
+
//
|
|
73
|
+
// **Seeking belongs to `playback`.** A reader who has dragged the timeline has said
|
|
74
|
+
// where they want to be, and an automatic jump would take it back from them.
|
|
75
|
+
const actions = {
|
|
76
|
+
play: { kind: 'start', domain: 'playback', run: () => setPlay(true) },
|
|
77
|
+
pause: { kind: 'stop', domain: 'playback', run: () => setPlay(false) },
|
|
78
|
+
loud: { kind: 'start', domain: 'sound', run: () => setMute(false) },
|
|
79
|
+
mute: { kind: 'stop', domain: 'sound', run: () => setMute(true) },
|
|
80
|
+
// A seek starts nothing on its own — it moves a playhead, playing or not — so a gate
|
|
81
|
+
// has no reason to hold it back.
|
|
82
|
+
'jump-to': {
|
|
83
|
+
kind: 'stop',
|
|
84
|
+
domain: 'playback',
|
|
85
|
+
run: arg => {
|
|
86
|
+
const targetMs = Number(arg);
|
|
87
|
+
if (!Number.isFinite(targetMs))
|
|
88
|
+
return;
|
|
89
|
+
forceJumpTo(videoRef.current, targetMs);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
'jump-start': { kind: 'stop', domain: 'playback', run: () => forceJumpTo(videoRef.current, 0) },
|
|
93
|
+
'jump-end': { kind: 'stop', domain: 'playback', run: () => forceJumpTo(videoRef.current, -1) }
|
|
94
|
+
};
|
|
95
|
+
const { surrender: surrenderAnyDomain } = useViewportBehaviours(rootRef, {
|
|
96
|
+
visibilityThreshold,
|
|
97
|
+
visibilityRoot,
|
|
98
|
+
visibilityRootMargin,
|
|
99
|
+
visibilityOnAfterMs,
|
|
100
|
+
visibilityOffAfterMs,
|
|
101
|
+
whenVisible,
|
|
102
|
+
whenHidden,
|
|
86
103
|
onVisibilityChanged
|
|
87
|
-
|
|
104
|
+
}, actions, behavioursSuspended === true);
|
|
105
|
+
// Narrowed back to this component's own domains. The generic layer takes a `string`,
|
|
106
|
+
// having no vocabulary of its own; handing the handlers a typed door means a
|
|
107
|
+
// misspelled domain fails to compile instead of quietly never surrendering anything.
|
|
108
|
+
const surrender = useCallback((domain) => {
|
|
109
|
+
surrenderAnyDomain(domain);
|
|
110
|
+
}, [surrenderAnyDomain]);
|
|
88
111
|
// Intrisic event handlers
|
|
89
112
|
const handleOnPlayEvent = useCallback((e) => {
|
|
90
113
|
controlledProps.onPlay?.(e);
|
|
@@ -122,28 +145,41 @@ export const Video = ({ defaultSubtitlesOn = true, autoPlayWhenVisible, autoPlay
|
|
|
122
145
|
// User actions
|
|
123
146
|
const handlePlayButtonClick = useCallback((e, isPlaying, video) => {
|
|
124
147
|
onPlayButtonClicked?.(e, isPlaying, video);
|
|
148
|
+
surrender('playback');
|
|
125
149
|
setPlay(true);
|
|
126
|
-
}, [onPlayButtonClicked]);
|
|
150
|
+
}, [onPlayButtonClicked, surrender]);
|
|
127
151
|
const handlePauseButtonClick = useCallback((e, isPlaying, video) => {
|
|
128
152
|
onPauseButtonClicked?.(e, isPlaying, video);
|
|
153
|
+
surrender('playback');
|
|
129
154
|
setPlay(false);
|
|
130
|
-
}, [onPauseButtonClicked]);
|
|
155
|
+
}, [onPauseButtonClicked, surrender]);
|
|
156
|
+
// The picture toggles, where the two buttons each say one thing. It is the same gesture
|
|
157
|
+
// as pressing one of them — a consumer tracking whether the reader has taken over
|
|
158
|
+
// playback has to count this one too, since it is the same decision made elsewhere.
|
|
159
|
+
const handleVideoClick = useCallback((e, isPlaying, video) => {
|
|
160
|
+
onVideoClicked?.(e, isPlaying, video);
|
|
161
|
+
surrender('playback');
|
|
162
|
+
setPlay(!isPlaying);
|
|
163
|
+
}, [onVideoClicked, surrender]);
|
|
131
164
|
const handleLoudButtonClick = useCallback((e, isLoud, video) => {
|
|
132
165
|
onLoudButtonClicked?.(e, isLoud, video);
|
|
166
|
+
surrender('sound');
|
|
133
167
|
setMute(false);
|
|
134
|
-
}, [onLoudButtonClicked]);
|
|
168
|
+
}, [onLoudButtonClicked, surrender]);
|
|
135
169
|
const handleMuteButtonClick = useCallback((e, isLoud, video) => {
|
|
136
170
|
onMuteButtonClicked?.(e, isLoud, video);
|
|
171
|
+
surrender('sound');
|
|
137
172
|
setMute(true);
|
|
138
|
-
}, [onMuteButtonClicked]);
|
|
173
|
+
}, [onMuteButtonClicked, surrender]);
|
|
139
174
|
const handleRateRangeChange = useCallback((e, targetRate, currentRate, video) => {
|
|
140
175
|
onRateRangeChanged?.(e, targetRate, currentRate, video);
|
|
141
176
|
setPlaybackRate(targetRate);
|
|
142
177
|
}, [onRateRangeChanged]);
|
|
143
178
|
const handleVolumeRangeChange = useCallback((e, targetVolume, currentVolume, video) => {
|
|
144
179
|
onVolumeRangeChanged?.(e, targetVolume, currentVolume, video);
|
|
180
|
+
surrender('sound');
|
|
145
181
|
setVolume(targetVolume);
|
|
146
|
-
}, [onVolumeRangeChanged]);
|
|
182
|
+
}, [onVolumeRangeChanged, surrender]);
|
|
147
183
|
const handleFullscreenButtonClick = useCallback((e, isFullscreen, video) => {
|
|
148
184
|
onFullscreenButtonClicked?.(e, isFullscreen, video);
|
|
149
185
|
setFullscreen(!isFullscreen);
|
|
@@ -156,53 +192,12 @@ export const Video = ({ defaultSubtitlesOn = true, autoPlayWhenVisible, autoPlay
|
|
|
156
192
|
onSubtitlesButtonClicked?.(e, isSubtitlesOn, video);
|
|
157
193
|
setSubtitlesOn(!isSubtitlesOn);
|
|
158
194
|
}, [onSubtitlesButtonClicked]);
|
|
159
|
-
// Intersection Observer
|
|
160
|
-
const onIntersected = useCallback(({ ioEntry }) => {
|
|
161
|
-
if (ioEntry === undefined)
|
|
162
|
-
return;
|
|
163
|
-
const { isIntersecting } = ioEntry;
|
|
164
|
-
onVisibilityChanged?.(isIntersecting);
|
|
165
|
-
if (isIntersecting) {
|
|
166
|
-
if (shouldRunAutoBehaviour(autoPlayWhenVisible, autoPlayOnceVisible, hasAutoPlayedOnce.current)) {
|
|
167
|
-
hasAutoPlayedOnce.current = true;
|
|
168
|
-
setPlay(true);
|
|
169
|
-
}
|
|
170
|
-
if (shouldRunAutoBehaviour(autoLoudWhenVisible, autoLoudOnceVisible, hasAutoLoudedOnce.current)) {
|
|
171
|
-
hasAutoLoudedOnce.current = true;
|
|
172
|
-
setMute(false);
|
|
173
|
-
}
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
if (shouldRunAutoBehaviour(autoPauseWhenHidden, autoPauseOnceHidden, hasAutoPausedOnce.current)) {
|
|
177
|
-
hasAutoPausedOnce.current = true;
|
|
178
|
-
setPlay(false);
|
|
179
|
-
}
|
|
180
|
-
if (shouldRunAutoBehaviour(autoMuteWhenHidden, autoMuteOnceHidden, hasAutoMutedOnce.current)) {
|
|
181
|
-
hasAutoMutedOnce.current = true;
|
|
182
|
-
setMute(true);
|
|
183
|
-
}
|
|
184
|
-
}, [
|
|
185
|
-
autoPlayWhenVisible,
|
|
186
|
-
autoPlayOnceVisible,
|
|
187
|
-
autoPauseWhenHidden,
|
|
188
|
-
autoPauseOnceHidden,
|
|
189
|
-
autoMuteWhenHidden,
|
|
190
|
-
autoMuteOnceHidden,
|
|
191
|
-
autoLoudWhenVisible,
|
|
192
|
-
autoLoudOnceVisible,
|
|
193
|
-
onVisibilityChanged
|
|
194
|
-
]);
|
|
195
195
|
// `autoPlay` is forwarded to the element, but the play state is owned here, so
|
|
196
196
|
// it has to be seeded once on mount for the controls to agree with the element.
|
|
197
197
|
useEffect(() => {
|
|
198
198
|
if (controlledProps.autoPlay === true)
|
|
199
199
|
setPlay(true);
|
|
200
200
|
}, []);
|
|
201
|
-
// The observer watches the `<figure>` itself. `needsObserve` says whether anything
|
|
202
|
-
// asked for it — a hook cannot be skipped, so the condition is passed in rather than
|
|
203
|
-
// wrapped around the call, and no `IntersectionObserver` is created without a reason
|
|
204
|
-
// to create one.
|
|
205
|
-
useIntersectionObserver(rootRef, { threshold, root, rootMargin }, onIntersected, needsObserve);
|
|
206
201
|
// Render
|
|
207
202
|
//
|
|
208
203
|
// **The `<figure>` is the root, and there is nothing above it.** The observer runs on
|
|
@@ -210,5 +205,5 @@ export const Video = ({ defaultSubtitlesOn = true, autoPlayWhenVisible, autoPlay
|
|
|
210
205
|
// position — the one a consumer lays out — and leave `className` naming an inner
|
|
211
206
|
// element, which is the wrong way round. So no wrapper, and no second class-name prop
|
|
212
207
|
// to reach past it.
|
|
213
|
-
return _jsx(ControlledVideo, { ...controlledProps, rootRef: rootRef, play: play && !isTimeControlled, volume: volume, mute: mute, playbackRate: playbackRate, fullscreen: fullscreen, subtitlesOn: subtitlesOn, onPlay: handleOnPlayEvent, onPause: handleOnPauseEvent, onIsPlayingChanged: handleIsPlayingChanged, onVolumeChange: handleOnVolumeChangeEvent, onRateChange: handleOnRateChangeEvent, onLoadedMetadata: handleOnLoadedMetadataEvent, onFullscreenChange: handleFullscreenChange, onPlayButtonClicked: handlePlayButtonClick, onPauseButtonClicked: handlePauseButtonClick, onLoudButtonClicked: handleLoudButtonClick, onMuteButtonClicked: handleMuteButtonClick, onVolumeRangeChanged: handleVolumeRangeChange, onRateRangeChanged: handleRateRangeChange, onFullscreenButtonClicked: handleFullscreenButtonClick, onSubtitlesButtonClicked: handleSubtitlesButtonClick });
|
|
208
|
+
return _jsx(ControlledVideo, { ...controlledProps, rootRef: rootRef, videoRef: videoRef, play: play && !isTimeControlled, volume: volume, mute: mute, playbackRate: playbackRate, fullscreen: fullscreen, subtitlesOn: subtitlesOn, onPlay: handleOnPlayEvent, onPause: handleOnPauseEvent, onIsPlayingChanged: handleIsPlayingChanged, onVolumeChange: handleOnVolumeChangeEvent, onRateChange: handleOnRateChangeEvent, onLoadedMetadata: handleOnLoadedMetadataEvent, onFullscreenChange: handleFullscreenChange, onVideoClicked: handleVideoClick, onPlayButtonClicked: handlePlayButtonClick, onPauseButtonClicked: handlePauseButtonClick, onLoudButtonClicked: handleLoudButtonClick, onMuteButtonClicked: handleMuteButtonClick, onVolumeRangeChanged: handleVolumeRangeChange, onRateRangeChanged: handleRateRangeChange, onFullscreenButtonClicked: handleFullscreenButtonClick, onSubtitlesButtonClicked: handleSubtitlesButtonClick });
|
|
214
209
|
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The verbs a video answers to, as the table keys them.
|
|
3
|
+
*
|
|
4
|
+
* `jump-to` is the only one taking an argument, and the table is keyed by the verb
|
|
5
|
+
* alone — the argument is parsed out of the instruction before the lookup.
|
|
6
|
+
*/
|
|
7
|
+
export declare const VIDEO_VERBS: readonly ["play", "pause", "loud", "mute", "jump-to", "jump-start", "jump-end"];
|
|
8
|
+
export type VideoVerb = typeof VIDEO_VERBS[number];
|
|
9
|
+
/**
|
|
10
|
+
* How to read the argument of each verb that takes one.
|
|
11
|
+
*
|
|
12
|
+
* **Exported because a consumer has to validate what it is handed**, and a type gives a
|
|
13
|
+
* runtime validator nothing to compare against. lm-link reads an article's props out of
|
|
14
|
+
* static XML and has to reject `'jump-to:banana'` before it ever reaches here — with the
|
|
15
|
+
* list restated on its side, the day a verb is added is the day an article writing it
|
|
16
|
+
* silently gets nothing.
|
|
17
|
+
*
|
|
18
|
+
* A digit string, and deliberately not `Number`: that would take `'1e3'` and `'0x10'`,
|
|
19
|
+
* which nobody means to write in an article.
|
|
20
|
+
*/
|
|
21
|
+
export declare const VIDEO_VERB_ARGUMENTS: {
|
|
22
|
+
readonly 'jump-to': (arg: string) => boolean;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* The instructions a consumer writes in `whenVisible` / `whenHidden`.
|
|
26
|
+
*
|
|
27
|
+
* `'jump-to:500'` seeks to 500 ms. **A negative value counts from the end**:
|
|
28
|
+
* `'jump-to:-1'` is the last reachable timecode, and it is `-1` rather than `-0`
|
|
29
|
+
* because `-0 === 0` in JavaScript and would collide with the start. `'jump-start'`
|
|
30
|
+
* and `'jump-end'` are the shorthands for `'jump-to:0'` and `'jump-to:-1'`.
|
|
31
|
+
*
|
|
32
|
+
* Every one of them takes the `':once'` and `':force'` suffixes, which are the generic
|
|
33
|
+
* layer's and are not restated here.
|
|
34
|
+
*/
|
|
35
|
+
export type VideoAction = Exclude<VideoVerb, 'jump-to'> | `jump-to:${number}`;
|
|
36
|
+
/**
|
|
37
|
+
* What a verb competes with, so that taking one control over doesn't silence the rest.
|
|
38
|
+
*
|
|
39
|
+
* Seeking belongs to `'playback'`: a reader who drags the timeline has said where they
|
|
40
|
+
* want to be, and an automatic jump would take it back from them.
|
|
41
|
+
*/
|
|
42
|
+
export type VideoDomain = 'playback' | 'sound';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The verbs a video answers to, as the table keys them.
|
|
3
|
+
*
|
|
4
|
+
* `jump-to` is the only one taking an argument, and the table is keyed by the verb
|
|
5
|
+
* alone — the argument is parsed out of the instruction before the lookup.
|
|
6
|
+
*/
|
|
7
|
+
export const VIDEO_VERBS = [
|
|
8
|
+
'play',
|
|
9
|
+
'pause',
|
|
10
|
+
'loud',
|
|
11
|
+
'mute',
|
|
12
|
+
'jump-to',
|
|
13
|
+
'jump-start',
|
|
14
|
+
'jump-end'
|
|
15
|
+
];
|
|
16
|
+
/**
|
|
17
|
+
* How to read the argument of each verb that takes one.
|
|
18
|
+
*
|
|
19
|
+
* **Exported because a consumer has to validate what it is handed**, and a type gives a
|
|
20
|
+
* runtime validator nothing to compare against. lm-link reads an article's props out of
|
|
21
|
+
* static XML and has to reject `'jump-to:banana'` before it ever reaches here — with the
|
|
22
|
+
* list restated on its side, the day a verb is added is the day an article writing it
|
|
23
|
+
* silently gets nothing.
|
|
24
|
+
*
|
|
25
|
+
* A digit string, and deliberately not `Number`: that would take `'1e3'` and `'0x10'`,
|
|
26
|
+
* which nobody means to write in an article.
|
|
27
|
+
*/
|
|
28
|
+
export const VIDEO_VERB_ARGUMENTS = {
|
|
29
|
+
'jump-to': (arg) => /^-?\d+$/v.test(arg)
|
|
30
|
+
};
|
|
@@ -1,3 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two list shapes a `<video>` accepts as children, as records.
|
|
3
|
+
*
|
|
4
|
+
* They live here and not next to the component because nothing about them is React:
|
|
5
|
+
* they describe a `<source>` and a `<track>` element.
|
|
6
|
+
*
|
|
7
|
+
* **A video source is not a picture source**, which is why `Image` keeps a shape of its
|
|
8
|
+
* own rather than sharing this one: a `<source>` inside a `<video>` carries `src`, one
|
|
9
|
+
* inside a `<picture>` carries `srcSet`, `media` and `sizes`, and neither element accepts
|
|
10
|
+
* the other's attributes. What the two do share is how a list of them is read — see
|
|
11
|
+
* `parseSourceList` in `components/utils`.
|
|
12
|
+
*/
|
|
13
|
+
export type SourceData = {
|
|
14
|
+
src?: string;
|
|
15
|
+
type?: string;
|
|
16
|
+
};
|
|
17
|
+
export type TrackData = {
|
|
18
|
+
src?: string;
|
|
19
|
+
kind?: 'subtitles' | 'captions' | 'descriptions' | 'chapters' | 'metadata';
|
|
20
|
+
srclang?: string;
|
|
21
|
+
label?: string;
|
|
22
|
+
default?: boolean;
|
|
23
|
+
};
|
|
1
24
|
export declare const muteAttributeWorkaround: (video: HTMLVideoElement | null, shouldMute: boolean) => void;
|
|
2
25
|
export declare const forceMute: (video: HTMLVideoElement | null) => void;
|
|
3
26
|
export declare const forceLoud: (video: HTMLVideoElement | null) => void;
|
|
@@ -40,4 +63,18 @@ export declare const getTimelineClickProgress: (event: React.MouseEvent<HTMLDivE
|
|
|
40
63
|
* automatic mute the component still owed.
|
|
41
64
|
* @returns Whether to apply it now.
|
|
42
65
|
*/
|
|
43
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Seeks the element, in milliseconds, counting from the end when asked negatively.
|
|
68
|
+
*
|
|
69
|
+
* **A negative target counts back from the duration**, so `-1` is the last reachable
|
|
70
|
+
* timecode. It is `-1` and not `-0` because `-0 === 0` in JavaScript, which would make
|
|
71
|
+
* the end of a video indistinguishable from its start.
|
|
72
|
+
*
|
|
73
|
+
* Nothing happens before the metadata lands: the duration is `NaN` until then, and a
|
|
74
|
+
* seek computed against it would land anywhere. An instruction asking for the end of a
|
|
75
|
+
* video whose length is still unknown is simply not yet answerable.
|
|
76
|
+
*
|
|
77
|
+
* @param video - The element, or `null` before it mounts.
|
|
78
|
+
* @param targetMs - Where to go. Negative counts from the end.
|
|
79
|
+
*/
|
|
80
|
+
export declare const forceJumpTo: (video: HTMLVideoElement | null, targetMs: number) => void;
|
|
@@ -131,8 +131,27 @@ export const getTimelineClickProgress = (event) => {
|
|
|
131
131
|
* automatic mute the component still owed.
|
|
132
132
|
* @returns Whether to apply it now.
|
|
133
133
|
*/
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Seeks the element, in milliseconds, counting from the end when asked negatively.
|
|
136
|
+
*
|
|
137
|
+
* **A negative target counts back from the duration**, so `-1` is the last reachable
|
|
138
|
+
* timecode. It is `-1` and not `-0` because `-0 === 0` in JavaScript, which would make
|
|
139
|
+
* the end of a video indistinguishable from its start.
|
|
140
|
+
*
|
|
141
|
+
* Nothing happens before the metadata lands: the duration is `NaN` until then, and a
|
|
142
|
+
* seek computed against it would land anywhere. An instruction asking for the end of a
|
|
143
|
+
* video whose length is still unknown is simply not yet answerable.
|
|
144
|
+
*
|
|
145
|
+
* @param video - The element, or `null` before it mounts.
|
|
146
|
+
* @param targetMs - Where to go. Negative counts from the end.
|
|
147
|
+
*/
|
|
148
|
+
export const forceJumpTo = (video, targetMs) => {
|
|
149
|
+
if (video === null)
|
|
150
|
+
return;
|
|
151
|
+
const durationMs = secondsToMs(video.duration);
|
|
152
|
+
if (!Number.isFinite(durationMs))
|
|
153
|
+
return;
|
|
154
|
+
const absolute = targetMs < 0 ? durationMs + targetMs : targetMs;
|
|
155
|
+
// eslint-disable-next-line no-param-reassign
|
|
156
|
+
video.currentTime = msToSeconds(Math.min(Math.max(absolute, 0), durationMs));
|
|
138
157
|
};
|
package/components/index.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ export * as button from './Button/index.js'
|
|
|
3
3
|
export * as clippable from './Clippable/index.js'
|
|
4
4
|
export * as disclaimer from './Disclaimer/index.js'
|
|
5
5
|
export * as drawer from './Drawer/index.js'
|
|
6
|
-
export * as gallery from './Gallery/index.js'
|
|
7
6
|
export * as eventListener from './EventListener/index.js'
|
|
7
|
+
export * as gallery from './Gallery/index.js'
|
|
8
8
|
export * as iframe from './Iframe/index.js'
|
|
9
9
|
export * as image from './Image/index.js'
|
|
10
10
|
export * as input from './Input/index.js'
|
package/components/index.js
CHANGED
|
@@ -3,8 +3,8 @@ export * as button from './Button/index.js'
|
|
|
3
3
|
export * as clippable from './Clippable/index.js'
|
|
4
4
|
export * as disclaimer from './Disclaimer/index.js'
|
|
5
5
|
export * as drawer from './Drawer/index.js'
|
|
6
|
-
export * as gallery from './Gallery/index.js'
|
|
7
6
|
export * as eventListener from './EventListener/index.js'
|
|
7
|
+
export * as gallery from './Gallery/index.js'
|
|
8
8
|
export * as iframe from './Iframe/index.js'
|
|
9
9
|
export * as image from './Image/index.js'
|
|
10
10
|
export * as input from './Input/index.js'
|
|
@@ -21,3 +21,24 @@ export declare function mergeClassNames(...names: Array<string | null | undefine
|
|
|
21
21
|
* effect fires, so it is never a stale one.
|
|
22
22
|
*/
|
|
23
23
|
export declare function useChangeDispatch<T>(value: T, onChange?: (value: T) => void, isEqual?: (a: T, b: T) => boolean): void;
|
|
24
|
+
/**
|
|
25
|
+
* The three forms a `<source>` list is written in, reduced to the one a render uses.
|
|
26
|
+
*
|
|
27
|
+
* A bare string is a single source, an array of strings is several, an array of records
|
|
28
|
+
* is taken as given. **The array is read as homogeneous** — the first element decides for
|
|
29
|
+
* all of them —, which is what someone writing one by hand means anyway, and the only
|
|
30
|
+
* reading a static hyper-json array could support.
|
|
31
|
+
*
|
|
32
|
+
* `stringKey` is the whole reason this is shared rather than written twice. The parsing
|
|
33
|
+
* is identical for `Video` and `Image`, but **the shorthand does not name the same
|
|
34
|
+
* attribute**: a `<source>` inside a `<video>` carries `src`, one inside a `<picture>`
|
|
35
|
+
* carries `srcSet`. The two record shapes stay apart for the same reason — the picture
|
|
36
|
+
* source also takes `media` and `sizes`, which a video source has no use for, and neither
|
|
37
|
+
* element accepts the other's key. One element, one shape; only the reading is common.
|
|
38
|
+
*
|
|
39
|
+
* @template T - The record shape the caller's element accepts.
|
|
40
|
+
* @param given - What the consumer wrote.
|
|
41
|
+
* @param stringKey - Where a bare string goes.
|
|
42
|
+
* @returns The list as records, empty when there is nothing to render.
|
|
43
|
+
*/
|
|
44
|
+
export declare function parseSourceList<T extends Record<string, unknown>>(given: string | string[] | T[] | undefined, stringKey: keyof T & string): T[];
|
|
@@ -41,3 +41,43 @@ export function useChangeDispatch(value, onChange, isEqual = Object.is) {
|
|
|
41
41
|
onChange?.(value);
|
|
42
42
|
}, [value]);
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* The three forms a `<source>` list is written in, reduced to the one a render uses.
|
|
46
|
+
*
|
|
47
|
+
* A bare string is a single source, an array of strings is several, an array of records
|
|
48
|
+
* is taken as given. **The array is read as homogeneous** — the first element decides for
|
|
49
|
+
* all of them —, which is what someone writing one by hand means anyway, and the only
|
|
50
|
+
* reading a static hyper-json array could support.
|
|
51
|
+
*
|
|
52
|
+
* `stringKey` is the whole reason this is shared rather than written twice. The parsing
|
|
53
|
+
* is identical for `Video` and `Image`, but **the shorthand does not name the same
|
|
54
|
+
* attribute**: a `<source>` inside a `<video>` carries `src`, one inside a `<picture>`
|
|
55
|
+
* carries `srcSet`. The two record shapes stay apart for the same reason — the picture
|
|
56
|
+
* source also takes `media` and `sizes`, which a video source has no use for, and neither
|
|
57
|
+
* element accepts the other's key. One element, one shape; only the reading is common.
|
|
58
|
+
*
|
|
59
|
+
* @template T - The record shape the caller's element accepts.
|
|
60
|
+
* @param given - What the consumer wrote.
|
|
61
|
+
* @param stringKey - Where a bare string goes.
|
|
62
|
+
* @returns The list as records, empty when there is nothing to render.
|
|
63
|
+
*/
|
|
64
|
+
export function parseSourceList(given, stringKey) {
|
|
65
|
+
const fromString = (value) => {
|
|
66
|
+
const single = { [stringKey]: value };
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- a one-key record is the narrowest `T` a bare string can describe; every other field is optional by contract
|
|
68
|
+
return single;
|
|
69
|
+
};
|
|
70
|
+
if (given === undefined)
|
|
71
|
+
return [];
|
|
72
|
+
if (typeof given === 'string')
|
|
73
|
+
return [fromString(given)];
|
|
74
|
+
if (!Array.isArray(given))
|
|
75
|
+
return [];
|
|
76
|
+
if (given.length === 0)
|
|
77
|
+
return [];
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- first element sampled just above; array is read as homogeneous
|
|
79
|
+
if (typeof given[0] === 'string')
|
|
80
|
+
return given.map(fromString);
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- first element was checked not to be a string just above; array is read as homogeneous
|
|
82
|
+
return given;
|
|
83
|
+
}
|