Skip to content

Commit

Permalink
fix: working autoplay in mobile (#652)
Browse files Browse the repository at this point in the history
* fix: autoplay works on player for mobile devices
  • Loading branch information
Kyzyl-ool authored Oct 20, 2023
1 parent 3774ae8 commit 61c9ade
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/blocks/Header/Header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ $backgroundWidth: 1440px;
height: 100%;

&_theme_dark {
--g-color-line-focus: var(--pc-color-line-focus-dark);

#{$block}__title,
#{$block}__overtitle {
color: var(--g-color-text-light-primary);
Expand Down
1 change: 1 addition & 0 deletions src/components/Media/Video/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const Video = (props: VideoAllProps) => {

return (
<ReactPlayerBlock
ref={ref}
className={b('react-player', videoClassName)}
src={src}
previewImgUrl={previewImg}
Expand Down
25 changes: 21 additions & 4 deletions src/components/ReactPlayer/ReactPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ interface PlayerPropgress {
played: number;
}

type ReactPlayerBlockRefType = ReactPlayerBlockHandler | undefined;
// eslint-disable-next-line react/display-name
export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactPlayerBlockProps>(
export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockRefType, ReactPlayerBlockProps>(
(props, originRef) => {
const isMobile = useContext(MobileContext);
const {metrika} = useContext(MetrikaContext);
Expand Down Expand Up @@ -130,9 +131,25 @@ export const ReactPlayerBlock = React.forwardRef<ReactPlayerBlockHandler, ReactP
}, [analyticsEvents]);
const handleAnalytics = useAnalytics(DefaultEventNames.ReactPlayerControls);

useImperativeHandle(originRef, () => ({
pause: () => setIsPlaying(false),
}));
useImperativeHandle(
originRef,
() => {
if (!playerRef) {
return;
}

const videoInstance = playerRef.getInternalPlayer() as HTMLVideoElement;
const {play, pause, addEventListener} = videoInstance;

// eslint-disable-next-line consistent-return
return {
play: play.bind(videoInstance),
pause: pause.bind(videoInstance),
addEventListener: addEventListener.bind(videoInstance),
};
},
[playerRef],
);

useEffect(() => {
if (ref.current && !playingVideoRef?.contains(ref.current)) {
Expand Down
5 changes: 2 additions & 3 deletions src/models/components.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type ArrowDirection = 'left' | 'right';

export interface ReactPlayerBlockHandler {
pause: () => void;
}
export interface ReactPlayerBlockHandler
extends Pick<HTMLVideoElement, 'play' | 'pause' | 'addEventListener'> {}

0 comments on commit 61c9ade

Please sign in to comment.