UNPKG

@interactive-video-labs/react

Version:

Thin React wrapper for the @interactive-video-labs/core engine. Enables cue-based interactive video playback in React apps.

107 lines (104 loc) 3.27 kB
/** * @interactive-video-labs/react v0.2.0 * Author: Taj * @license MIT */ "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.tsx var index_exports = {}; __export(index_exports, { InteractiveVideo: () => InteractiveVideo }); module.exports = __toCommonJS(index_exports); var import_react = require("react"); var import_core = require("@interactive-video-labs/core"); var import_jsx_runtime = require("react/jsx-runtime"); var generateUniqueId = () => `ivlabs-player-${Math.random().toString(36).slice(2, 9)}`; var InteractiveVideo = ({ videoUrl, onAnalyticsEvent, cues, translations, ...restOptions }) => { const containerRef = (0, import_react.useRef)(null); const playerRef = (0, import_react.useRef)(null); const uniqueIdRef = (0, import_react.useRef)(generateUniqueId()); (0, import_react.useEffect)(() => { if (!containerRef.current) return; const playerConfig = { videoUrl, ...restOptions }; try { const player = new import_core.IVLabsPlayer(uniqueIdRef.current, playerConfig); playerRef.current = player; if (onAnalyticsEvent) { const events = [ "PLAYER_LOADED", "VIDEO_STARTED", "VIDEO_PAUSED", "VIDEO_ENDED", "CUE_TRIGGERED", "INTERACTION_COMPLETED", "ERROR" ]; events.forEach((event) => { player.on( event, (payload) => onAnalyticsEvent(event, payload) ); }); } } catch (error) { console.error("Error initializing IVLabsPlayer:", error); } return () => { if (playerRef.current) { playerRef.current.destroy(); playerRef.current = null; } }; }, [videoUrl, onAnalyticsEvent, restOptions]); (0, import_react.useEffect)(() => { if (playerRef.current && cues) { playerRef.current.loadCues(cues); } }, [cues]); (0, import_react.useEffect)(() => { if (playerRef.current && translations) { const locale = restOptions.locale || "en"; playerRef.current.loadTranslations(locale, translations); } }, [translations, restOptions.locale]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "div", { ref: containerRef, id: uniqueIdRef.current, style: { width: "100%", height: "auto" }, "data-testid": "interactive-video-container" } ); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { InteractiveVideo });