@vidkit-cloud/player-react 0.0.0-stage → 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 VidKit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,111 @@
1
- # Temporary Holding Version
1
+ # @vidkit-cloud/player-react
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The [VidKit](https://vidkit.cloud) video player as a React component, with a hook for its methods and typed events.
4
+
5
+ ```tsx
6
+ 'use client'
7
+ import { useRef } from 'react'
8
+ import { VidKitPlayer, useVidKitPlayer, type PlayerRef } from '@vidkit-cloud/player-react'
9
+
10
+ export function Intro() {
11
+ const ref = useRef<PlayerRef>(null)
12
+ const player = useVidKitPlayer(ref)
13
+ return (
14
+ <>
15
+ <VidKitPlayer
16
+ ref={ref}
17
+ videoId="019a7e10-5b3c-7d21-a0f4-3e8b2c1d9f05"
18
+ muted
19
+ onEnded={() => console.log('watched to the end')}
20
+ onError={(e) => console.warn(e.code)}
21
+ />
22
+ <button disabled={!player.isReady} onClick={() => player.play()}>
23
+ Play
24
+ </button>
25
+ </>
26
+ )
27
+ }
28
+ ```
29
+
30
+ React 18 and 19. The player itself is loaded from VidKit when the component mounts, so you get player updates and your team's branding without upgrading the package. It is built on [`@vidkit-cloud/player-js`](https://www.npmjs.com/package/@vidkit-cloud/player-js), whose README describes the options, events and errors in full.
31
+
32
+ ## Install
33
+
34
+ ```sh
35
+ npm install @vidkit-cloud/player-react
36
+ ```
37
+
38
+ ## `<VidKitPlayer />`
39
+
40
+ It renders a `<div>` holding the player. Until the player is up, the `<div>` keeps the player's size, so nothing on the page moves.
41
+
42
+ **Props.** Every option of `createPlayer`, with the same name and type. Only `videoId` is required.
43
+
44
+ | Prop | Type | What it does |
45
+ | ---------------- | ------------------------ | ----------------------------------------------------------------------------------------- |
46
+ | `videoId` | `string` | The video to play. |
47
+ | `autoplay` | `boolean` | Start playing at once. Such a start is muted, as browsers require. |
48
+ | `muted` | `boolean` | Start without sound. |
49
+ | `loop` | `boolean` | Play again from the beginning at the end. |
50
+ | `start` | `number` | Where the first play starts, in seconds. |
51
+ | `aspectRatio` | `string` or `number` | The player's shape: `"16:9"`, or width divided by height. |
52
+ | `controlsLayout` | `"islands"`, `"classic"` | How the controls are laid out. |
53
+ | `color` | `string` | The accent colour, as `#rrggbb`. |
54
+ | `hideTitle` | `boolean` | Show no title on the poster. |
55
+ | `playerVersion` | `string` | Pins the player to one version, like `"1.2.0"`. Leave it out to get every player release. |
56
+ | `playerUrl` | `string` | Where the player script is loaded from. The first player on a page decides it. |
57
+ | `className` | `string` | For the `<div>` the component renders, never the player's own element. |
58
+ | `style` | `CSSProperties` | The same. |
59
+
60
+ A setting you leave out keeps what the video was published with in VidKit. `autoplay`, `muted`, `loop` and `hideTitle` can only be switched on: `false` is the same as leaving them out.
61
+
62
+ **Event props.** Each takes its event's payload, and every payload is an object.
63
+
64
+ | Prop | Payload | When |
65
+ | ------------------ | ----------------------------------- | ---------------------------------------------------------------- |
66
+ | `onReady` | `{ videoId, playerVersion }` | The player is up; again after a call-to-action swaps a video in. |
67
+ | `onError` | `{ code, message, fatal, videoId }` | The start failed, or playback failed. Check `fatal`. |
68
+ | `onPlay` | `{ cause }` | Playback starts: `"autoplay"`, `"user"` or `"programmatic"`. |
69
+ | `onPause` | `{}` | |
70
+ | `onEnded` | `{}` | |
71
+ | `onTimeUpdate` | `{ currentTime }` | About four times a second while playing. |
72
+ | `onDurationChange` | `{ duration }` | The duration became known, or changed. |
73
+ | `onWaiting` | `{}` | Playback stalled to buffer. |
74
+ | `onPlaying` | `{}` | Playback is moving again after a start or a stall. |
75
+ | `onRateChange` | `{ playbackRate }` | The speed changed. |
76
+ | `onVolumeChange` | `{ volume, muted }` | |
77
+ | `onVideoChange` | `{ videoId, previousVideoId }` | A call-to-action swapped another video into the player. |
78
+
79
+ **Changing props.**
80
+
81
+ - A new `videoId` replaces the player with one for that video, in the same `<div>`.
82
+ - A new `loop`, `aspectRatio` or `controlsLayout` goes to the running player.
83
+ - Any other change is ignored, with a warning in development. A new `color` is among them for now: set it when the player is created.
84
+
85
+ **`ref`** holds a `PlayerRef`: the player's methods, getters and `on`/`off`, as `createPlayer` returns them. It stays the same object when a new `videoId` replaces the player, and is `null` before the component mounts.
86
+
87
+ ## `useVidKitPlayer(ref)`
88
+
89
+ It returns the same object on every render:
90
+
91
+ - **`isReady`**: whether the player is up. The component re-renders when it changes.
92
+ - **`error`**: what the player is failing with, as a `PlayerError`, or `null`. The component re-renders when it changes. A failure the player is still retrying (`fatal: false`) clears when playback moves again.
93
+ - **`play()`, `pause()`, `seek(seconds)`, `setVolume(volume)`, `setMuted(muted)`, `setPlaybackRate(rate)`**: they act on the player the ref holds now, and do nothing before it is up.
94
+
95
+ It holds no `currentTime`, which would re-render four times a second. Use `onTimeUpdate`, or read `ref.current?.currentTime`.
96
+
97
+ ## Server rendering
98
+
99
+ The package is marked `'use client'` for React Server Components. On a server it renders only the `<div>`, with the player's size reserved; the player is created in the browser after hydration, with no hydration warning.
100
+
101
+ ## Errors
102
+
103
+ `onError` and `error` carry the codes of `@vidkit-cloud/player-js`: `video_not_found`, `video_not_ready`, `loader_unavailable` and the others. A failed start leaves the `<div>` empty and its size reserved: the component draws no error screen, so you decide what to show. Removing the component while the player starts is not an error, and fires nothing.
104
+
105
+ ## Types
106
+
107
+ `PlayerRef`, `VidKitPlayerProps`, `VidKitPlayerEventProps`, `VidKitPlayerControls`, and from `@vidkit-cloud/player-js`: `Player`, `PlayerOptions`, `PlayerEvent`, `PlayerEventMap`, `PlayerError`, `PlayerErrorCode`, `PlayerErrorPayload`.
108
+
109
+ ## License
110
+
111
+ MIT
@@ -0,0 +1,15 @@
1
+ /** The prop that hears each event of the player. */
2
+ export declare const EVENT_PROPS: {
3
+ readonly ready: "onReady";
4
+ readonly error: "onError";
5
+ readonly play: "onPlay";
6
+ readonly pause: "onPause";
7
+ readonly ended: "onEnded";
8
+ readonly timeupdate: "onTimeUpdate";
9
+ readonly durationchange: "onDurationChange";
10
+ readonly waiting: "onWaiting";
11
+ readonly playing: "onPlaying";
12
+ readonly ratechange: "onRateChange";
13
+ readonly volumechange: "onVolumeChange";
14
+ readonly videochange: "onVideoChange";
15
+ };
@@ -0,0 +1,15 @@
1
+ /** The prop that hears each event of the player. */
2
+ export const EVENT_PROPS = {
3
+ ready: 'onReady',
4
+ error: 'onError',
5
+ play: 'onPlay',
6
+ pause: 'onPause',
7
+ ended: 'onEnded',
8
+ timeupdate: 'onTimeUpdate',
9
+ durationchange: 'onDurationChange',
10
+ waiting: 'onWaiting',
11
+ playing: 'onPlaying',
12
+ ratechange: 'onRateChange',
13
+ volumechange: 'onVolumeChange',
14
+ videochange: 'onVideoChange',
15
+ };
@@ -0,0 +1,7 @@
1
+ export { VidKitPlayer } from './vidkit-player.js';
2
+ export { useVidKitPlayer } from './use-vidkit-player.js';
3
+ export { PlayerError } from '@vidkit-cloud/player-js';
4
+ export type { PlayerRef } from './player-ref.js';
5
+ export type { VidKitPlayerProps, VidKitPlayerEventProps } from './vidkit-player-props.js';
6
+ export type { VidKitPlayerControls } from './vidkit-player-controls.js';
7
+ export type { Player, PlayerErrorCode, PlayerErrorPayload, PlayerEvent, PlayerEventMap, PlayerOptions, } from '@vidkit-cloud/player-js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ 'use client';
2
+ export { VidKitPlayer } from './vidkit-player.js';
3
+ export { useVidKitPlayer } from './use-vidkit-player.js';
4
+ export { PlayerError } from '@vidkit-cloud/player-js';
@@ -0,0 +1,3 @@
1
+ import type { PlayerController } from '@vidkit-cloud/player-js';
2
+ /** The controller a `PlayerRef` holds, or `null` when it holds none. */
3
+ export declare function controllerOf(value: unknown): PlayerController | null;
@@ -0,0 +1,9 @@
1
+ /** The controller a `PlayerRef` holds, or `null` when it holds none. */
2
+ export function controllerOf(value) {
3
+ if (!value || typeof value !== 'object')
4
+ return null;
5
+ const candidate = value;
6
+ return typeof candidate.subscribe === 'function' && typeof candidate.attach === 'function'
7
+ ? value
8
+ : null;
9
+ }
@@ -0,0 +1,7 @@
1
+ import type { PlayerOptions } from '@vidkit-cloud/player-js';
2
+ import type { VidKitPlayerProps } from '../vidkit-player-props.js';
3
+ /** Takes the player's options out of the component's props. */
4
+ export declare class PropsToOptions {
5
+ private static readonly NOT_OPTIONS;
6
+ static of(props: VidKitPlayerProps): PlayerOptions;
7
+ }
@@ -0,0 +1,17 @@
1
+ import { EVENT_PROPS } from '../event-props.js';
2
+ /** Takes the player's options out of the component's props. */
3
+ export class PropsToOptions {
4
+ static of(props) {
5
+ const options = {};
6
+ for (const [name, value] of Object.entries(props)) {
7
+ if (value !== undefined && !PropsToOptions.NOT_OPTIONS.has(name))
8
+ options[name] = value;
9
+ }
10
+ return options;
11
+ }
12
+ }
13
+ PropsToOptions.NOT_OPTIONS = new Set([
14
+ 'className',
15
+ 'style',
16
+ ...Object.values(EVENT_PROPS),
17
+ ]);
@@ -0,0 +1,7 @@
1
+ import type { Player } from '@vidkit-cloud/player-js';
2
+ /**
3
+ * What `ref` on `<VidKitPlayer>` holds: the `Player`, with its methods,
4
+ * getters and `on`/`off`. It stays the same object when a new `videoId`
5
+ * replaces the player behind it. `null` before the component has mounted.
6
+ */
7
+ export type PlayerRef = Player;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { PlayerRef } from './player-ref.js';
2
+ import type { VidKitPlayerControls } from './vidkit-player-controls.js';
3
+ /**
4
+ * The state and the methods of the player behind a `<VidKitPlayer>`'s ref.
5
+ * It holds no `currentTime`, which would re-render four times a second: use
6
+ * `onTimeUpdate`, or read `ref.current?.currentTime`.
7
+ */
8
+ export declare function useVidKitPlayer(ref: {
9
+ readonly current: PlayerRef | null;
10
+ }): VidKitPlayerControls;
@@ -0,0 +1,33 @@
1
+ 'use client';
2
+ import { useState, useSyncExternalStore } from 'react';
3
+ import { controllerOf } from './internal/controller-of.js';
4
+ /**
5
+ * The state and the methods of the player behind a `<VidKitPlayer>`'s ref.
6
+ * It holds no `currentTime`, which would re-render four times a second: use
7
+ * `onTimeUpdate`, or read `ref.current?.currentTime`.
8
+ */
9
+ export function useVidKitPlayer(ref) {
10
+ const [controls] = useState(() => createControls(ref));
11
+ // A new function on each render makes React subscribe again, so a player
12
+ // that mounts after this component's first render is followed too.
13
+ const subscribe = (notify) => controllerOf(ref.current)?.subscribe(notify) ?? (() => undefined);
14
+ useSyncExternalStore(subscribe, () => controls.isReady, () => false);
15
+ useSyncExternalStore(subscribe, () => controls.error, () => null);
16
+ return controls;
17
+ }
18
+ function createControls(ref) {
19
+ return {
20
+ get isReady() {
21
+ return controllerOf(ref.current)?.isReady ?? false;
22
+ },
23
+ get error() {
24
+ return controllerOf(ref.current)?.error ?? null;
25
+ },
26
+ play: () => ref.current?.play() ?? Promise.resolve(),
27
+ pause: () => ref.current?.pause(),
28
+ seek: (seconds) => ref.current?.seek(seconds),
29
+ setVolume: (volume) => ref.current?.setVolume(volume),
30
+ setMuted: (muted) => ref.current?.setMuted(muted),
31
+ setPlaybackRate: (rate) => ref.current?.setPlaybackRate(rate),
32
+ };
33
+ }
@@ -0,0 +1,19 @@
1
+ import type { PlayerError } from '@vidkit-cloud/player-js';
2
+ /** What `useVidKitPlayer` returns. The object is the same on every render. */
3
+ export interface VidKitPlayerControls {
4
+ /** Whether the player is up. Re-renders the component when it changes. */
5
+ readonly isReady: boolean;
6
+ /**
7
+ * What the player is failing with, or `null`. Re-renders the component when
8
+ * it changes. A failure the player is still retrying clears when playback
9
+ * moves again.
10
+ */
11
+ readonly error: PlayerError | null;
12
+ /** These act on the player the ref holds now, and do nothing before it is up. */
13
+ play(): Promise<void>;
14
+ pause(): void;
15
+ seek(seconds: number): void;
16
+ setVolume(volume: number): void;
17
+ setMuted(muted: boolean): void;
18
+ setPlaybackRate(rate: number): void;
19
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ import type { PlayerEvent, PlayerEventMap, PlayerOptions } from '@vidkit-cloud/player-js';
2
+ import type { CSSProperties } from 'react';
3
+ import type { EVENT_PROPS } from './event-props.js';
4
+ /** `onReady`, `onTimeUpdate` and the rest: each takes its event's payload. */
5
+ export type VidKitPlayerEventProps = {
6
+ [E in PlayerEvent as (typeof EVENT_PROPS)[E]]?: (payload: PlayerEventMap[E]) => void;
7
+ };
8
+ /**
9
+ * The props of `<VidKitPlayer>`: every option of `createPlayer`, with the
10
+ * same name and type, a prop for each event, and `className` and `style`
11
+ * for the outer element.
12
+ */
13
+ export type VidKitPlayerProps = PlayerOptions & VidKitPlayerEventProps & {
14
+ /** For the outer element the component renders, never the player's own. */
15
+ className?: string;
16
+ /** For the outer element the component renders, never the player's own. */
17
+ style?: CSSProperties;
18
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The VidKit player. It renders a `<div>` holding the player, with the
3
+ * player's size reserved until it is up, so nothing on the page moves.
4
+ *
5
+ * - A new `videoId` replaces the player with one for that video.
6
+ * - A new value of a live option (`loop`, `aspectRatio`, `controlsLayout`)
7
+ * goes to the running player; other changes are ignored, with a warning in
8
+ * development.
9
+ * - On a server it renders only the `<div>`; the player is created in the
10
+ * browser, after the page has hydrated.
11
+ */
12
+ export declare const VidKitPlayer: import("react").ForwardRefExoticComponent<import("@vidkit-cloud/player-js").PlayerOptions & import("./vidkit-player-props.js").VidKitPlayerEventProps & {
13
+ className?: string;
14
+ style?: import("react").CSSProperties;
15
+ } & import("react").RefAttributes<import("@vidkit-cloud/player-js").Player>>;
@@ -0,0 +1,52 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { createPlayerController, reservedBoxStyle } from '@vidkit-cloud/player-js';
4
+ import { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState, useSyncExternalStore, } from 'react';
5
+ import { EVENT_PROPS } from './event-props.js';
6
+ import { PropsToOptions } from './internal/props-to-options.js';
7
+ const EVENTS = Object.keys(EVENT_PROPS);
8
+ /**
9
+ * The VidKit player. It renders a `<div>` holding the player, with the
10
+ * player's size reserved until it is up, so nothing on the page moves.
11
+ *
12
+ * - A new `videoId` replaces the player with one for that video.
13
+ * - A new value of a live option (`loop`, `aspectRatio`, `controlsLayout`)
14
+ * goes to the running player; other changes are ignored, with a warning in
15
+ * development.
16
+ * - On a server it renders only the `<div>`; the player is created in the
17
+ * browser, after the page has hydrated.
18
+ */
19
+ export const VidKitPlayer = forwardRef(function VidKitPlayer(props, ref) {
20
+ const [controller] = useState(createPlayerController);
21
+ const box = useRef(null);
22
+ const latest = useRef(props);
23
+ const subscribe = useCallback((notify) => controller.subscribe(notify), [controller]);
24
+ const isReady = useSyncExternalStore(subscribe, () => controller.isReady, () => false);
25
+ useImperativeHandle(ref, () => controller, [controller]);
26
+ // The event props are read when an event arrives, so a new callback on
27
+ // each render needs no new subscription.
28
+ useEffect(() => {
29
+ latest.current = props;
30
+ });
31
+ useEffect(() => {
32
+ const offs = EVENTS.map((event) => controller.on(event, (payload) => {
33
+ const handler = latest.current[EVENT_PROPS[event]];
34
+ handler?.(payload);
35
+ }));
36
+ return () => offs.forEach((off) => off());
37
+ }, [controller]);
38
+ // The player lives from mount to unmount. React's StrictMode mounts twice
39
+ // in development; the second attach replaces the first, which never starts.
40
+ useEffect(() => {
41
+ const element = box.current;
42
+ if (!element)
43
+ return;
44
+ controller.attach(element, PropsToOptions.of(latest.current));
45
+ return () => controller.detach();
46
+ }, [controller]);
47
+ useEffect(() => {
48
+ controller.update(PropsToOptions.of(props));
49
+ });
50
+ const style = isReady ? props.style : { ...reservedBoxStyle(props.aspectRatio), ...props.style };
51
+ return _jsx("div", { ref: box, className: props.className, style: style });
52
+ });
package/package.json CHANGED
@@ -1,6 +1,76 @@
1
1
  {
2
2
  "name": "@vidkit-cloud/player-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "The VidKit video player as a React component, with a hook for its methods and typed events",
5
+ "author": "VidKit",
6
+ "license": "MIT",
7
+ "homepage": "https://vidkit.cloud",
8
+ "keywords": [
9
+ "vidkit",
10
+ "video",
11
+ "player",
12
+ "react",
13
+ "hls"
14
+ ],
15
+ "private": false,
16
+ "type": "module",
17
+ "sideEffects": false,
18
+ "main": "./dist/index.js",
19
+ "module": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "scripts": {
32
+ "dev": "vitest",
33
+ "build": "tsc -p tsconfig.build.json",
34
+ "typecheck": "tsc --noEmit",
35
+ "lint": "eslint .",
36
+ "format": "prettier --write .",
37
+ "format:check": "prettier --check .",
38
+ "test": "vitest run",
39
+ "test:watch": "vitest",
40
+ "test:cov": "vitest run --coverage",
41
+ "verify:package": "node scripts/verify-package.cjs",
42
+ "check-branch-name": "bash .husky/scripts/check-branch-name.sh",
43
+ "prepare": "husky",
44
+ "release": "semantic-release"
45
+ },
46
+ "dependencies": {
47
+ "@vidkit-cloud/player-js": "^1.0.0"
48
+ },
49
+ "peerDependencies": {
50
+ "react": "^18.0.0 || ^19.0.0"
51
+ },
52
+ "devDependencies": {
53
+ "@commitlint/cli": "^21.3.0",
54
+ "@commitlint/config-conventional": "^21.3.0",
55
+ "@eslint/js": "^10.0.1",
56
+ "@semantic-release/commit-analyzer": "^13.0.1",
57
+ "@semantic-release/github": "^12.0.11",
58
+ "@semantic-release/release-notes-generator": "^14.1.1",
59
+ "@testing-library/dom": "^10.4.2",
60
+ "@testing-library/react": "^16.3.3",
61
+ "@types/react": "^19.3.3",
62
+ "@types/react-dom": "^19.3.0",
63
+ "@vitest/coverage-v8": "^4.1.12",
64
+ "eslint": "^10.16.0",
65
+ "globals": "^17.14.0",
66
+ "husky": "^9.1.7",
67
+ "jsdom": "^31.0.1",
68
+ "prettier": "^3.10.1",
69
+ "react": "^19.3.1",
70
+ "react-dom": "^19.3.1",
71
+ "semantic-release": "^25.0.9",
72
+ "typescript": "^6.0.3",
73
+ "typescript-eslint": "^8.74.0",
74
+ "vitest": "^4.1.12"
75
+ }
76
+ }