@primer/react-brand
Version:
Primer Brand is a GitHub's design system for creating React-based marketing websites and digital experiences.
73 lines (72 loc) • 1.63 kB
JavaScript
import { useProvidedRefOrCreate as e } from "../hooks/useRef.js";
import { useCallback as t, useEffect as n, useRef as r, useState as i } from "react";
//#region src/MinimalVideoPlayer/useMinimalVideoPlayback.ts
function a({ autoPlay: a, forwardedRef: o, prefersReducedMotion: s }) {
let c = e(o), l = r(!1), u = r(!1), [d, f] = i(!1), p = t(async () => {
let e = c.current;
if (e) try {
await e.play();
} catch {
e.paused && f(!1);
}
}, [c]), m = t(() => f(!0), []), h = t(() => f(!1), []), g = t(() => {
l.current = !1, f(!1);
}, []);
return n(() => {
let e = c.current;
if (!e) return;
let t = (t) => {
if (t) {
if (l.current) {
l.current = !1, a && !s && p();
return;
}
if (!u.current && s) {
u.current = !0;
return;
}
!u.current && a && (u.current = !0, p());
return;
}
e.paused || (l.current = a && !s, e.pause());
};
if (typeof IntersectionObserver > "u") {
t(!0);
return;
}
let n = new IntersectionObserver(([e]) => {
t(e.intersectionRatio > 0);
});
return n.observe(e), () => n.disconnect();
}, [
a,
s,
p,
c
]), n(() => {
s && (u.current = !0, l.current = !1, c.current?.pause());
}, [s, c]), {
handlePlaybackEnded: g,
handlePlaybackPaused: h,
handlePlaybackStarted: m,
isPlaying: d,
togglePlayback: t(() => {
let e = c.current;
if (e) {
if (u.current = !0, l.current = !1, d) {
f(!1), e.pause();
return;
}
p();
}
}, [
d,
p,
c
]),
videoRef: c
};
}
//#endregion
export { a as useMinimalVideoPlayback };
//# sourceMappingURL=useMinimalVideoPlayback.js.map