react-hook-videojs 2.1.0-beta.2 → 3.0.2

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/dist/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import React, { HTMLProps } from "react";
2
- import videojs, { VideoJsPlayerOptions } from "video.js";
3
- declare type VideoType = (props: {
2
+ import { VideoJsPlayer, VideoJsPlayerOptions } from "video.js";
3
+ declare type VideoProps = {
4
4
  children?: React.ReactNode;
5
- } & Partial<HTMLProps<HTMLVideoElement>>) => JSX.Element;
5
+ } & Partial<HTMLProps<HTMLVideoElement>>;
6
+ declare type VideoType = (props: VideoProps) => JSX.Element;
6
7
  export declare const useVideoJS: (videoJsOptions: VideoJsPlayerOptions, classNames?: string) => {
7
8
  Video: VideoType;
8
9
  ready: boolean;
9
- player: videojs.Player | null;
10
+ player: VideoJsPlayer | null;
10
11
  };
11
12
  export {};
@@ -1,61 +1,70 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
- var __objRest = (source, exclude) => {
18
- var target = {};
19
- for (var prop in source)
20
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
21
- target[prop] = source[prop];
22
- if (source != null && __getOwnPropSymbols)
23
- for (var prop of __getOwnPropSymbols(source)) {
24
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
25
- target[prop] = source[prop];
26
- }
27
- return target;
28
- };
29
- import React, { useRef, useState, useEffect, useCallback } from "react";
1
+ import React, { forwardRef, useRef, useLayoutEffect, useState, useCallback } from "react";
30
2
  import videojs from "video.js";
31
- const useVideoJS = (videoJsOptions, classNames = "") => {
3
+ import cloneDeep from "lodash.clonedeep";
4
+ import { dequal } from "dequal";
5
+ function useDeepCompareMemoize(value) {
6
+ const ref = React.useRef(value);
7
+ const signalRef = React.useRef(0);
8
+ if (!dequal(value, ref.current)) {
9
+ ref.current = value;
10
+ signalRef.current += 1;
11
+ }
12
+ return React.useMemo(() => ref.current, [signalRef.current]);
13
+ }
14
+ const VideoJsWrapper = forwardRef(({ children, videoJsOptions, onReady, onDispose, classNames, ...props }, playerRef) => {
15
+ const player = playerRef;
16
+ const videoJsOptionsCloned = cloneDeep(videoJsOptions);
32
17
  const videoNode = useRef(null);
33
- const [ready, setReady] = useState(false);
34
- const changedKey = JSON.stringify(videoJsOptions);
35
- const player = useRef(null);
36
- useEffect(() => {
37
- if (!videoNode.current)
18
+ const containerRef = useRef(null);
19
+ useLayoutEffect(() => {
20
+ var _a;
21
+ if (!((_a = videoNode.current) == null ? void 0 : _a.parentNode))
38
22
  return;
39
- player.current = videojs(videoNode.current, videoJsOptions);
40
- player.current.ready(() => {
41
- setReady(true);
42
- });
23
+ const originalVideoNodeParent = videoNode.current.parentNode.cloneNode(true);
24
+ if (!player.current) {
25
+ player.current = videojs(videoNode.current, videoJsOptionsCloned);
26
+ player.current.ready(() => {
27
+ onReady();
28
+ });
29
+ }
43
30
  return () => {
44
- var _a;
45
- (_a = player.current) == null ? void 0 : _a.dispose();
31
+ var _a2;
32
+ if (player.current) {
33
+ player.current.dispose();
34
+ if (containerRef.current && ((_a2 = videoNode.current) == null ? void 0 : _a2.parentNode) && !containerRef.current.contains(videoNode.current.parentNode)) {
35
+ containerRef.current.appendChild(originalVideoNodeParent);
36
+ videoNode.current = originalVideoNodeParent.firstChild;
37
+ }
38
+ player.current = null;
39
+ onDispose();
40
+ }
46
41
  };
47
- }, [changedKey]);
48
- const Video = useCallback((_a) => {
49
- var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
50
- return /* @__PURE__ */ React.createElement("div", {
51
- "data-vjs-player": true,
52
- key: changedKey
53
- }, /* @__PURE__ */ React.createElement("video", __spreadValues({
54
- ref: videoNode,
55
- className: `video-js ${classNames}`
56
- }, props), children));
57
- }, [changedKey]);
42
+ }, [useDeepCompareMemoize(videoJsOptions)]);
43
+ return /* @__PURE__ */ React.createElement("div", {
44
+ ref: containerRef
45
+ }, /* @__PURE__ */ React.createElement("div", {
46
+ "data-vjs-player": true
47
+ }, /* @__PURE__ */ React.createElement("video", {
48
+ ref: videoNode,
49
+ className: `video-js ${classNames}`,
50
+ ...props
51
+ }, children)));
52
+ });
53
+ VideoJsWrapper.displayName = "VideoJsWrapper";
54
+ const useVideoJS = (videoJsOptions, classNames = "") => {
55
+ const [ready, setReady] = useState(false);
56
+ const player = useRef(null);
57
+ const Video = useCallback(({ children, ...props }) => /* @__PURE__ */ React.createElement(VideoJsWrapper, {
58
+ videoJsOptions,
59
+ classNames,
60
+ onReady: () => setReady(true),
61
+ onDispose: () => setReady(false),
62
+ ...props,
63
+ ref: player
64
+ }, children), [useDeepCompareMemoize(videoJsOptions), classNames]);
58
65
  return { Video, ready, player: player.current };
59
66
  };
60
- export { useVideoJS };
67
+ export {
68
+ useVideoJS
69
+ };
61
70
  //# sourceMappingURL=react-hook-videojs.es.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"react-hook-videojs.es.js","sources":["../src/index.tsx"],"sourcesContent":["import React, {\n useRef,\n useState,\n useEffect,\n useCallback,\n HTMLProps,\n} from \"react\";\nimport videojs, { VideoJsPlayer, VideoJsPlayerOptions } from \"video.js\";\n\ntype VideoType = (\n props: {\n children?: React.ReactNode;\n } & Partial<HTMLProps<HTMLVideoElement>>\n) => JSX.Element;\n\nexport const useVideoJS = (\n videoJsOptions: VideoJsPlayerOptions,\n classNames = \"\"\n): {\n Video: VideoType;\n ready: boolean;\n player: videojs.Player | null;\n} => {\n const videoNode = useRef(null);\n const [ready, setReady] = useState(false);\n const changedKey = JSON.stringify(videoJsOptions);\n const player = useRef<VideoJsPlayer | null>(null);\n useEffect(() => {\n if (!videoNode.current) return;\n player.current = videojs(videoNode.current, videoJsOptions);\n player.current.ready(() => {\n setReady(true);\n });\n return () => {\n player.current?.dispose();\n };\n }, [changedKey]);\n\n const Video = useCallback<VideoType>(\n ({ children, ...props }) => (\n <div data-vjs-player={true} key={changedKey}>\n <video ref={videoNode} className={`video-js ${classNames}`} {...props}>\n {children}\n </video>\n </div>\n ),\n [changedKey]\n );\n return { Video, ready, player: player.current };\n};\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAea,aAAa,CACxB,gBACA,aAAa,OAKV;QACG,YAAY,OAAO,IAAI;QACvB,CAAC,OAAO,YAAY,SAAS,KAAK;QAClC,aAAa,KAAK,UAAU,cAAc;QAC1C,SAAS,OAA6B,IAAI;YACtC,MAAM;QACV,CAAC,UAAU;;WACR,UAAU,QAAQ,UAAU,SAAS,cAAc;WACnD,QAAQ,MAAM,MAAM;eAChB,IAAI;AAAA,KACd;WACM,MAAM;;mBACJ,+BAAS;AAAA;KAEjB,CAAC,UAAU,CAAC;QAET,QAAQ,YACZ,CAAC;AAAA,iBAAE,eAAF,IAAe,kBAAf,IAAe,CAAb;+CACA;MAAI,mBAAiB;AAAA,MAAM,KAAK;AAAA,2CAC9B;MAAM,KAAK;AAAA,MAAW,WAAW,YAAY;AAAA,OAAkB,QAC7D,QACH,CACF;AAAA,KAEF,CAAC,UAAU,CACb;SACO,EAAE,OAAO,OAAO,QAAQ,OAAO;AACxC;;"}
1
+ {"version":3,"file":"react-hook-videojs.es.js","sources":["../src/index.tsx"],"sourcesContent":["import React, {\n useRef,\n useState,\n useLayoutEffect,\n useCallback,\n forwardRef,\n HTMLProps,\n MutableRefObject,\n} from \"react\";\nimport videojs, { VideoJsPlayer, VideoJsPlayerOptions } from \"video.js\";\nimport cloneDeep from \"lodash.clonedeep\";\nimport { dequal } from \"dequal\";\n\n// Function copied from\n// https://github.com/kentcdodds/use-deep-compare-effect/blob/main/src/index.ts\nfunction useDeepCompareMemoize<T>(value: T): T {\n const ref = React.useRef<T>(value);\n const signalRef = React.useRef<number>(0);\n\n if (!dequal(value, ref.current)) {\n ref.current = value;\n signalRef.current += 1;\n }\n\n return React.useMemo(() => ref.current, [signalRef.current]);\n}\n\n// Integrating React and video.js is a bit tricky, especially when supporting\n// React 18 strict mode. We'll do our best to explain what happens in inline comments.\n\nconst VideoJsWrapper = forwardRef<\n VideoJsPlayer,\n {\n children: React.ReactNode;\n videoJsOptions: VideoJsPlayerOptions;\n onReady: () => void;\n onDispose: () => void;\n classNames: string;\n }\n>(\n (\n { children, videoJsOptions, onReady, onDispose, classNames, ...props },\n playerRef\n ) => {\n const player = playerRef as MutableRefObject<VideoJsPlayer | null>;\n // video.js sometimes mutates the provided options object.\n // We clone it to avoid mutation issues.\n const videoJsOptionsCloned = cloneDeep(videoJsOptions);\n const videoNode = useRef<HTMLVideoElement | null>(null);\n const containerRef = useRef<HTMLDivElement | null>(null);\n\n useLayoutEffect(() => {\n if (!videoNode.current?.parentNode) return;\n\n // Once we initialize the player, videojs will start mutating the DOM.\n // We need a snapshot of the state just before, so we know what state\n // to reset the DOM to.\n const originalVideoNodeParent =\n videoNode.current.parentNode.cloneNode(true);\n\n if (!player.current) {\n player.current = videojs(videoNode.current, videoJsOptionsCloned);\n player.current.ready(() => {\n onReady();\n });\n }\n\n return (): void => {\n // Whenever something changes in the options object, we\n // want to reinitialize video.js, and destroy the old player by calling `player.current.dispose()`\n\n if (player.current) {\n player.current.dispose();\n\n // Unfortunately, video.js heavily mutates the DOM in a way that React doesn't\n // like, so we need to readd the removed DOM elements directly after dispose.\n // More concretely, the node marked with `data-vjs-player` will be removed from the\n // DOM. We are readding the cloned original video node parent as it was when React first rendered it,\n // so it is once again synchronized with React.\n if (\n containerRef.current &&\n videoNode.current?.parentNode &&\n !containerRef.current.contains(videoNode.current.parentNode)\n ) {\n containerRef.current.appendChild(originalVideoNodeParent);\n videoNode.current =\n originalVideoNodeParent.firstChild as HTMLVideoElement;\n }\n\n player.current = null;\n onDispose();\n }\n };\n\n // We'll use the serialized videoJsOptions object as a dependency object\n }, [useDeepCompareMemoize(videoJsOptions)]);\n\n return (\n // TODO: can we get by withour introducing an extra div?\n <div ref={containerRef}>\n <div data-vjs-player>\n <video\n ref={videoNode}\n className={`video-js ${classNames}`}\n {...props}\n >\n {children}\n </video>\n </div>\n </div>\n );\n }\n);\n\nVideoJsWrapper.displayName = \"VideoJsWrapper\";\n\ntype VideoProps = {\n children?: React.ReactNode;\n} & Partial<HTMLProps<HTMLVideoElement>>;\n\ntype VideoType = (props: VideoProps) => JSX.Element;\n\nexport const useVideoJS = (\n videoJsOptions: VideoJsPlayerOptions,\n classNames = \"\"\n): {\n Video: VideoType;\n ready: boolean;\n player: VideoJsPlayer | null;\n} => {\n const [ready, setReady] = useState(false);\n\n // player will contain the video.js player object, once it is ready.\n const player = useRef<VideoJsPlayer>(null);\n const Video = useCallback(\n ({ children, ...props }: VideoProps) => (\n <VideoJsWrapper\n videoJsOptions={videoJsOptions}\n classNames={classNames}\n onReady={(): void => setReady(true)}\n onDispose={(): void => setReady(false)}\n {...props}\n ref={player}\n >\n {children}\n </VideoJsWrapper>\n ),\n [useDeepCompareMemoize(videoJsOptions), classNames]\n );\n\n return { Video, ready, player: player.current };\n};\n"],"names":[],"mappings":";;;;AAeA,+BAAkC,OAAa;AACvC,QAAA,MAAM,MAAM,OAAU,KAAK;AAC3B,QAAA,YAAY,MAAM,OAAe,CAAC;AAExC,MAAI,CAAC,OAAO,OAAO,IAAI,OAAO,GAAG;AAC/B,QAAI,UAAU;AACd,cAAU,WAAW;AAAA,EACvB;AAEO,SAAA,MAAM,QAAQ,MAAM,IAAI,SA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,aAAa,YACb,sBAAA,cAAA,gBAAA;AAAA,IACC;AAAA,IACA;AAAA,IACA,SAAS,MAAY,SAAS,IAAI;AAAA,IAClC,WAAW,MAAY,SAAS,KAAK;AAAA,IACpC,GAAG;AAAA,IACJ,KAAK;AAAA,EAAA,GAEJ,QACH,GAEF,CAAC,sBAAsB,cAAc,GAAG,UAAU,CACpD;AAEA,SAAO,EAAE,OAAO,OAAO,QAAQ,OAAO,QAAQ;AAChD;"}
@@ -1,71 +1,74 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
- var __hasOwnProp = Object.prototype.hasOwnProperty;
4
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __spreadValues = (a, b) => {
7
- for (var prop in b || (b = {}))
8
- if (__hasOwnProp.call(b, prop))
9
- __defNormalProp(a, prop, b[prop]);
10
- if (__getOwnPropSymbols)
11
- for (var prop of __getOwnPropSymbols(b)) {
12
- if (__propIsEnum.call(b, prop))
13
- __defNormalProp(a, prop, b[prop]);
14
- }
15
- return a;
16
- };
17
- var __objRest = (source, exclude) => {
18
- var target = {};
19
- for (var prop in source)
20
- if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
21
- target[prop] = source[prop];
22
- if (source != null && __getOwnPropSymbols)
23
- for (var prop of __getOwnPropSymbols(source)) {
24
- if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
25
- target[prop] = source[prop];
26
- }
27
- return target;
28
- };
29
1
  (function(global, factory) {
30
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react"), require("video.js")) : typeof define === "function" && define.amd ? define(["exports", "react", "video.js"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["react-hook-videojs"] = {}, global.React, global.videojs));
31
- })(this, function(exports2, React, videojs) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react"), require("video.js"), require("lodash.clonedeep"), require("dequal")) : typeof define === "function" && define.amd ? define(["exports", "react", "video.js", "lodash.clonedeep", "dequal"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global["react-hook-videojs"] = {}, global.react, global.video.js, global.lodash.clonedeep, global.dequal));
3
+ })(this, function(exports2, React, videojs, cloneDeep, dequal) {
32
4
  "use strict";
33
- function _interopDefaultLegacy(e) {
34
- return e && typeof e === "object" && "default" in e ? e : { "default": e };
5
+ const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
6
+ const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
7
+ const videojs__default = /* @__PURE__ */ _interopDefaultLegacy(videojs);
8
+ const cloneDeep__default = /* @__PURE__ */ _interopDefaultLegacy(cloneDeep);
9
+ function useDeepCompareMemoize(value) {
10
+ const ref = React__default.default.useRef(value);
11
+ const signalRef = React__default.default.useRef(0);
12
+ if (!dequal.dequal(value, ref.current)) {
13
+ ref.current = value;
14
+ signalRef.current += 1;
15
+ }
16
+ return React__default.default.useMemo(() => ref.current, [signalRef.current]);
35
17
  }
36
- var React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
37
- var videojs__default = /* @__PURE__ */ _interopDefaultLegacy(videojs);
38
- const useVideoJS = (videoJsOptions, classNames = "") => {
18
+ const VideoJsWrapper = React.forwardRef(({ children, videoJsOptions, onReady, onDispose, classNames, ...props }, playerRef) => {
19
+ const player = playerRef;
20
+ const videoJsOptionsCloned = cloneDeep__default.default(videoJsOptions);
39
21
  const videoNode = React.useRef(null);
40
- const [ready, setReady] = React.useState(false);
41
- const changedKey = JSON.stringify(videoJsOptions);
42
- const player = React.useRef(null);
43
- React.useEffect(() => {
44
- if (!videoNode.current)
22
+ const containerRef = React.useRef(null);
23
+ React.useLayoutEffect(() => {
24
+ var _a;
25
+ if (!((_a = videoNode.current) == null ? void 0 : _a.parentNode))
45
26
  return;
46
- player.current = videojs__default["default"](videoNode.current, videoJsOptions);
47
- player.current.ready(() => {
48
- setReady(true);
49
- });
27
+ const originalVideoNodeParent = videoNode.current.parentNode.cloneNode(true);
28
+ if (!player.current) {
29
+ player.current = videojs__default.default(videoNode.current, videoJsOptionsCloned);
30
+ player.current.ready(() => {
31
+ onReady();
32
+ });
33
+ }
50
34
  return () => {
51
- var _a;
52
- (_a = player.current) == null ? void 0 : _a.dispose();
35
+ var _a2;
36
+ if (player.current) {
37
+ player.current.dispose();
38
+ if (containerRef.current && ((_a2 = videoNode.current) == null ? void 0 : _a2.parentNode) && !containerRef.current.contains(videoNode.current.parentNode)) {
39
+ containerRef.current.appendChild(originalVideoNodeParent);
40
+ videoNode.current = originalVideoNodeParent.firstChild;
41
+ }
42
+ player.current = null;
43
+ onDispose();
44
+ }
53
45
  };
54
- }, [changedKey]);
55
- const Video = React.useCallback((_a) => {
56
- var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
57
- return /* @__PURE__ */ React__default["default"].createElement("div", {
58
- "data-vjs-player": true,
59
- key: changedKey
60
- }, /* @__PURE__ */ React__default["default"].createElement("video", __spreadValues({
61
- ref: videoNode,
62
- className: `video-js ${classNames}`
63
- }, props), children));
64
- }, [changedKey]);
46
+ }, [useDeepCompareMemoize(videoJsOptions)]);
47
+ return /* @__PURE__ */ React__default.default.createElement("div", {
48
+ ref: containerRef
49
+ }, /* @__PURE__ */ React__default.default.createElement("div", {
50
+ "data-vjs-player": true
51
+ }, /* @__PURE__ */ React__default.default.createElement("video", {
52
+ ref: videoNode,
53
+ className: `video-js ${classNames}`,
54
+ ...props
55
+ }, children)));
56
+ });
57
+ VideoJsWrapper.displayName = "VideoJsWrapper";
58
+ const useVideoJS = (videoJsOptions, classNames = "") => {
59
+ const [ready, setReady] = React.useState(false);
60
+ const player = React.useRef(null);
61
+ const Video = React.useCallback(({ children, ...props }) => /* @__PURE__ */ React__default.default.createElement(VideoJsWrapper, {
62
+ videoJsOptions,
63
+ classNames,
64
+ onReady: () => setReady(true),
65
+ onDispose: () => setReady(false),
66
+ ...props,
67
+ ref: player
68
+ }, children), [useDeepCompareMemoize(videoJsOptions), classNames]);
65
69
  return { Video, ready, player: player.current };
66
70
  };
67
71
  exports2.useVideoJS = useVideoJS;
68
- Object.defineProperty(exports2, "__esModule", { value: true });
69
- exports2[Symbol.toStringTag] = "Module";
72
+ Object.defineProperties(exports2, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
70
73
  });
71
74
  //# sourceMappingURL=react-hook-videojs.umd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"react-hook-videojs.umd.js","sources":["../src/index.tsx"],"sourcesContent":["import React, {\n useRef,\n useState,\n useEffect,\n useCallback,\n HTMLProps,\n} from \"react\";\nimport videojs, { VideoJsPlayer, VideoJsPlayerOptions } from \"video.js\";\n\ntype VideoType = (\n props: {\n children?: React.ReactNode;\n } & Partial<HTMLProps<HTMLVideoElement>>\n) => JSX.Element;\n\nexport const useVideoJS = (\n videoJsOptions: VideoJsPlayerOptions,\n classNames = \"\"\n): {\n Video: VideoType;\n ready: boolean;\n player: videojs.Player | null;\n} => {\n const videoNode = useRef(null);\n const [ready, setReady] = useState(false);\n const changedKey = JSON.stringify(videoJsOptions);\n const player = useRef<VideoJsPlayer | null>(null);\n useEffect(() => {\n if (!videoNode.current) return;\n player.current = videojs(videoNode.current, videoJsOptions);\n player.current.ready(() => {\n setReady(true);\n });\n return () => {\n player.current?.dispose();\n };\n }, [changedKey]);\n\n const Video = useCallback<VideoType>(\n ({ children, ...props }) => (\n <div data-vjs-player={true} key={changedKey}>\n <video ref={videoNode} className={`video-js ${classNames}`} {...props}>\n {children}\n </video>\n </div>\n ),\n [changedKey]\n );\n return { Video, ready, player: player.current };\n};\n"],"names":["useRef","useState","videojs","useCallback"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAea,aAAa,CACxB,gBACA,aAAa,OAKV;UACG,YAAYA,aAAO,IAAI;UACvB,CAAC,OAAO,YAAYC,eAAS,KAAK;UAClC,aAAa,KAAK,UAAU,cAAc;UAC1C,SAASD,aAA6B,IAAI;oBACtC,MAAM;UACV,CAAC,UAAU;;aACR,UAAUE,4BAAQ,UAAU,SAAS,cAAc;aACnD,QAAQ,MAAM,MAAM;iBAChB,IAAI;AAAA,OACd;aACM,MAAM;;qBACJ,+BAAS;AAAA;OAEjB,CAAC,UAAU,CAAC;UAET,QAAQC,kBACZ,CAAC;AAAA,mBAAE,eAAF,IAAe,kBAAf,IAAe,CAAb;qEACA;QAAI,mBAAiB;AAAA,QAAM,KAAK;AAAA,iEAC9B;QAAM,KAAK;AAAA,QAAW,WAAW,YAAY;AAAA,SAAkB,QAC7D,QACH,CACF;AAAA,OAEF,CAAC,UAAU,CACb;WACO,EAAE,OAAO,OAAO,QAAQ,OAAO;EACxC;;;;;"}
1
+ {"version":3,"file":"react-hook-videojs.umd.js","sources":["../src/index.tsx"],"sourcesContent":["import React, {\n useRef,\n useState,\n useLayoutEffect,\n useCallback,\n forwardRef,\n HTMLProps,\n MutableRefObject,\n} from \"react\";\nimport videojs, { VideoJsPlayer, VideoJsPlayerOptions } from \"video.js\";\nimport cloneDeep from \"lodash.clonedeep\";\nimport { dequal } from \"dequal\";\n\n// Function copied from\n// https://github.com/kentcdodds/use-deep-compare-effect/blob/main/src/index.ts\nfunction useDeepCompareMemoize<T>(value: T): T {\n const ref = React.useRef<T>(value);\n const signalRef = React.useRef<number>(0);\n\n if (!dequal(value, ref.current)) {\n ref.current = value;\n signalRef.current += 1;\n }\n\n return React.useMemo(() => ref.current, [signalRef.current]);\n}\n\n// Integrating React and video.js is a bit tricky, especially when supporting\n// React 18 strict mode. We'll do our best to explain what happens in inline comments.\n\nconst VideoJsWrapper = forwardRef<\n VideoJsPlayer,\n {\n children: React.ReactNode;\n videoJsOptions: VideoJsPlayerOptions;\n onReady: () => void;\n onDispose: () => void;\n classNames: string;\n }\n>(\n (\n { children, videoJsOptions, onReady, onDispose, classNames, ...props },\n playerRef\n ) => {\n const player = playerRef as MutableRefObject<VideoJsPlayer | null>;\n // video.js sometimes mutates the provided options object.\n // We clone it to avoid mutation issues.\n const videoJsOptionsCloned = cloneDeep(videoJsOptions);\n const videoNode = useRef<HTMLVideoElement | null>(null);\n const containerRef = useRef<HTMLDivElement | null>(null);\n\n useLayoutEffect(() => {\n if (!videoNode.current?.parentNode) return;\n\n // Once we initialize the player, videojs will start mutating the DOM.\n // We need a snapshot of the state just before, so we know what state\n // to reset the DOM to.\n const originalVideoNodeParent =\n videoNode.current.parentNode.cloneNode(true);\n\n if (!player.current) {\n player.current = videojs(videoNode.current, videoJsOptionsCloned);\n player.current.ready(() => {\n onReady();\n });\n }\n\n return (): void => {\n // Whenever something changes in the options object, we\n // want to reinitialize video.js, and destroy the old player by calling `player.current.dispose()`\n\n if (player.current) {\n player.current.dispose();\n\n // Unfortunately, video.js heavily mutates the DOM in a way that React doesn't\n // like, so we need to readd the removed DOM elements directly after dispose.\n // More concretely, the node marked with `data-vjs-player` will be removed from the\n // DOM. We are readding the cloned original video node parent as it was when React first rendered it,\n // so it is once again synchronized with React.\n if (\n containerRef.current &&\n videoNode.current?.parentNode &&\n !containerRef.current.contains(videoNode.current.parentNode)\n ) {\n containerRef.current.appendChild(originalVideoNodeParent);\n videoNode.current =\n originalVideoNodeParent.firstChild as HTMLVideoElement;\n }\n\n player.current = null;\n onDispose();\n }\n };\n\n // We'll use the serialized videoJsOptions object as a dependency object\n }, [useDeepCompareMemoize(videoJsOptions)]);\n\n return (\n // TODO: can we get by withour introducing an extra div?\n <div ref={containerRef}>\n <div data-vjs-player>\n <video\n ref={videoNode}\n className={`video-js ${classNames}`}\n {...props}\n >\n {children}\n </video>\n </div>\n </div>\n );\n }\n);\n\nVideoJsWrapper.displayName = \"VideoJsWrapper\";\n\ntype VideoProps = {\n children?: React.ReactNode;\n} & Partial<HTMLProps<HTMLVideoElement>>;\n\ntype VideoType = (props: VideoProps) => JSX.Element;\n\nexport const useVideoJS = (\n videoJsOptions: VideoJsPlayerOptions,\n classNames = \"\"\n): {\n Video: VideoType;\n ready: boolean;\n player: VideoJsPlayer | null;\n} => {\n const [ready, setReady] = useState(false);\n\n // player will contain the video.js player object, once it is ready.\n const player = useRef<VideoJsPlayer>(null);\n const Video = useCallback(\n ({ children, ...props }: VideoProps) => (\n <VideoJsWrapper\n videoJsOptions={videoJsOptions}\n classNames={classNames}\n onReady={(): void => setReady(true)}\n onDispose={(): void => setReady(false)}\n {...props}\n ref={player}\n >\n {children}\n </VideoJsWrapper>\n ),\n [useDeepCompareMemoize(videoJsOptions), classNames]\n );\n\n return { Video, ready, player: player.current };\n};\n"],"names":["React","dequal","forwardRef","cloneDeep","useRef","useLayoutEffect","videojs","useState","useCallback"],"mappings":";;;;;;;;AAeA,iCAAkC,OAAa;AACvC,UAAA,MAAMA,eAAAA,QAAM,OAAU,KAAK;AAC3B,UAAA,YAAYA,eAAAA,QAAM,OAAe,CAAC;AAExC,QAAI,CAACC,OAAAA,OAAO,OAAO,IAAI,OAAO,GAAG;AAC/B,UAAI,UAAU;AACd,gBAAU,WAAW;AAAA,IACvB;AAEO,WAAAD,eAAA,QAAM,QAAQ,MAAM,IAAI,SAAS,CAAC,UAAU,OAAO,CAAC;AAAA,EAC7D;AAKA,QAAM,iBAAiBE,MAUrB,WAAA,CACE,EAAE,UAAU,gBAAgB,SAAS,WAAW,eAAe,SAC/D,cACG;AACH,UAAM,SAAS;AAGT,UAAA,uBAAuBC,2BAAU,cAAc;AAC/C,UAAA,YAAYC,aAAgC,IAAI;AAChD,UAAA,eAAeA,aAA8B,IAAI;AAEvDC,UAAAA,gBAAgB,MAAM;;AAChB,UAAA,CAAC,iBAAU,YAAV,mBAAmB;AAAY;AAKpC,YAAM,0BACJ,UAAU,QAAQ,WAAW,UAAU,IAAI;AAEzC,UAAA,CAAC,OAAO,SAAS;AACnB,eAAO,UAAUC,iBAAA,QAAQ,UAAU,SAAS,oBAAoB;AACzD,eAAA,QAAQ,MAAM,MAAM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"description": "A simple react hook to easily integrate video.js with React",
5
5
  "author": "jimmycallin",
6
6
  "license": "MIT",
@@ -9,52 +9,50 @@
9
9
  "main": "dist/react-hook-videojs.umd.js",
10
10
  "module": "dist/react-hook-videojs.es.js",
11
11
  "source": "src/index.tsx",
12
+ "engineStrict": false,
12
13
  "engines": {
13
- "node": ">=12"
14
+ "node": ">=16"
14
15
  },
15
16
  "scripts": {
16
17
  "clean": "rimraf dist .tmp",
17
18
  "build": "vite build && tsc --project tsconfig.types.json",
18
- "test": "npm run test:lint && npm run test:jest && npm run test:formatting",
19
- "test:jest": "jest",
20
- "test:lint": "eslint ./src",
21
- "test:formatting": "prettier -c .",
19
+ "test": "npm run test:vitest",
20
+ "test:vitest": "vitest run",
22
21
  "format": "prettier --write .",
23
22
  "prepare": "npm run clean && npm run build",
24
- "prepublishOnly": "npm run test"
23
+ "prepublishOnly": "npm run test",
24
+ "dev": "vite build --watch"
25
25
  },
26
26
  "peerDependencies": {
27
- "react": "^16.8.0, ^17.0.0",
27
+ "react": ">=16.8.0 < 19",
28
+ "react-dom": ">=16.8.0 < 19",
28
29
  "video.js": "^7.0.0"
29
30
  },
30
31
  "devDependencies": {
31
- "@babel/core": "^7.14.6",
32
- "@babel/plugin-transform-typescript": "^7.18.8",
33
- "@babel/preset-env": "^7.14.7",
34
- "@babel/preset-react": "^7.14.5",
35
- "@testing-library/react": "^12.0.0",
36
- "@types/jest": "^28.1.5",
32
+ "@testing-library/react": "^13.3.0",
33
+ "@types/lodash.clonedeep": "^4.5.7",
37
34
  "@types/video.js": "^7.3.42",
38
- "@typescript-eslint/eslint-plugin": "^5.30.6",
39
- "@typescript-eslint/parser": "^5.30.6",
35
+ "@typescript-eslint/eslint-plugin": "^5.32.0",
36
+ "@typescript-eslint/parser": "^5.31.0",
40
37
  "@vitejs/plugin-react-refresh": "^1.3.6",
41
- "babel-jest": "^28.1.3",
42
- "eslint": "^8.0.0",
43
- "eslint-config-prettier": "^8.3.0",
44
- "eslint-plugin-jest": "^26.1.1",
45
- "eslint-plugin-react": "^7.24.0",
46
- "jest": "^28.1.3",
47
- "jest-environment-jsdom": "^28.1.3",
48
- "prettier": "^2.3.2",
49
- "react": "^17.0.2",
50
- "react-dom": "^17.0.2",
38
+ "eslint": "^8.20.0",
39
+ "eslint-config-prettier": "^8.5.0",
40
+ "eslint-plugin-react": "^7.30.1",
41
+ "jsdom": "^20.0.0",
42
+ "prettier": "^2.7.1",
43
+ "react": "^18.2.0",
44
+ "react-dom": "^18.2.0",
51
45
  "rimraf": "^3.0.2",
52
- "ts-jest": "^28.0.5",
53
46
  "typescript": "^4.7.4",
54
- "video.js": "^7.13.3",
55
- "vite": "^2.4.4"
47
+ "video.js": "^7.20.1",
48
+ "vite": "^3.0.4",
49
+ "vitest": "^0.20.2"
56
50
  },
57
51
  "files": [
58
52
  "dist"
59
- ]
53
+ ],
54
+ "dependencies": {
55
+ "dequal": "^2.0.3",
56
+ "lodash.clonedeep": "^4.5.0"
57
+ }
60
58
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2018 Jimmy Callin
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 DELETED
@@ -1,77 +0,0 @@
1
- # react-hook-videojs
2
-
3
- > A simple react hook to easily integrate video.js with React
4
-
5
- [![NPM](https://img.shields.io/npm/v/react-hook-videojs.svg)](https://www.npmjs.com/package/react-hook-videojs)
6
-
7
- # react-hook-videojs
8
-
9
- Due to how video.js mutates the DOM, integrating video.js with React can be a bit tricky. Especially if you want to support video.js component updates and correctly dispose of any old player.
10
-
11
- React Hooks helps us package this quite nicely, and all you have to do to use this package is:
12
-
13
- ```jsx
14
- import React from "react";
15
- import "video.js/dist/video-js.css";
16
- import { useVideoJS } from "react-hook-videojs";
17
-
18
- const App = () => {
19
- const videoUrl = "http://techslides.com/demos/sample-videos/small.mp4";
20
- const className = "my-class";
21
- const { Video, player, ready } = useVideoJS(
22
- { sources: [{ src: videoUrl }] },
23
- className // optional
24
- );
25
- if (ready) {
26
- // Do something with the video.js player object.
27
- }
28
- return (
29
- <div className="App">
30
- <Video />
31
- </div>
32
- );
33
- };
34
- ```
35
-
36
- `useVideoJS` takes an options argument, and passes it without modifications to video.js.
37
- You may also provide an optional second string argument that will be appended as class name on the `video` DOM node.
38
-
39
- See their [options reference](https://docs.videojs.com/tutorial-options.html) for further information on the options argument.
40
-
41
- ### Using with Tracks or other child components
42
-
43
- This hook now supports using features such as [tracks](https://docs.videojs.com/tutorial-tracks.html#text-tracks), and other child components of the `<video>` element.
44
-
45
- Example of using a text track:
46
-
47
- ```jsx
48
- const App = () => {
49
- // ...setup code from above
50
-
51
- return (
52
- <Video>
53
- <track
54
- kind="captions"
55
- src="//example.com/path/to/captions.vtt"
56
- srclang="en"
57
- label="English"
58
- default
59
- />
60
- </Video>
61
- );
62
- };
63
- ```
64
-
65
- _Note: Videojs supports adding `<track>` and `<sources>` elements programmatically_
66
-
67
- ### Support for all `<video>` element attributes
68
-
69
- This hook supports all attributes for the native `<video>` element directly on the `<Video>` component.
70
-
71
- ```jsx
72
- const App = () => {
73
- return <Video muted autopictureinpicture />;
74
- };
75
- ```
76
-
77
- _Note: video.js supports many of these (and registration of listeners) through the player options_