Skip to content

Commit bbb8f68

Browse files
feat(Media): support ref for video element (#1297)
1 parent 24d4d1a commit bbb8f68

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

.storybook/stories/documentation/Types.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { Meta } from '@storybook/blocks';
5757
- `ariaLabel?: string` — ARIA label for accessibility
5858
- `contain?: boolean` — Whether video should be contained within bounds
5959
- `onVideoEnd?: () => void` — Callback when video ends
60+
- `ref?: React.Ref<HTMLVideoElement | null>` — Ref for video element
6061

6162
---
6263

memory-bank/usage/media.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ graph TD
6464
- `ImageObjectProps`: `{src: string, alt?: string, disableCompress?: boolean, hide?: boolean | Partial<Record<'desktop' | 'mobile' | 'tablet', boolean>>, fetchPriority?: 'high' | 'low' | 'auto', loading?: 'eager' | 'lazy', 'aria-describedby'?: string}`
6565
- `ImageDeviceProps`: `{desktop: string, mobile: string, tablet?: string, alt?: string, disableCompress?: boolean, hide?: boolean | Partial<Record<'desktop' | 'mobile' | 'tablet', boolean>>, fetchPriority?: 'high' | 'low' | 'auto', loading?: 'eager' | 'lazy', 'aria-describedby'?: string}`
6666
- `video`: MediaVideoProps - video configuration
67-
- `{src: string[], type?: 'default' | 'player', loop?: boolean | {start: number, end?: number}, muted?: boolean, autoplay?: boolean, elapsedTime?: number, playButton?: PlayButtonProps, controls?: 'default' | 'custom', customControlsOptions?: CustomControlsOptions, ariaLabel?: string, contain?: boolean, onVideoEnd?: () => void}`
67+
- `{src: string[], type?: 'default' | 'player', loop?: boolean | {start: number, end?: number}, muted?: boolean, autoplay?: boolean, elapsedTime?: number, playButton?: PlayButtonProps, controls?: 'default' | 'custom', customControlsOptions?: CustomControlsOptions, ariaLabel?: string, contain?: boolean, onVideoEnd?: () => void, ref?: React.Ref<HTMLVideoElement | null>}`
6868
- `PlayButtonProps`: `{type?: 'default' | 'text', theme?: 'blue' | 'grey', text?: string, className?: string}`
6969
- `CustomControlsOptions`: `{type?: 'with-mute-button' | 'with-play-pause-button', muteButtonShown?: boolean, positioning?: 'left' | 'right' | 'center'}`
7070
- `youtube`: string - YouTube video ID
@@ -593,6 +593,39 @@ The Media component integrates seamlessly with the page-constructor theme system
593593
/>
594594
```
595595

596+
### Video with Ref for Programmatic Control
597+
598+
```tsx
599+
const videoRef = React.useRef<HTMLVideoElement>(null);
600+
601+
const handlePlay = () => {
602+
if (videoRef.current) {
603+
videoRef.current.play();
604+
}
605+
};
606+
607+
const handlePause = () => {
608+
if (videoRef.current) {
609+
videoRef.current.pause();
610+
}
611+
};
612+
613+
const handleSeek = () => {
614+
if (videoRef.current) {
615+
videoRef.current.currentTime = 10; // Seek to 10 seconds
616+
}
617+
};
618+
619+
<Media
620+
video={{
621+
src: ['/path/to/video.mp4'],
622+
controls: MediaVideoControlsType.Default,
623+
ref: videoRef,
624+
}}
625+
height={400}
626+
/>;
627+
```
628+
596629
### Background Media with Parallax
597630

598631
```tsx

src/components/Media/Video/Video.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const Video = (props: VideoAllProps) => {
4646

4747
const ref = React.useRef<HTMLVideoElement>(null);
4848

49+
React.useImperativeHandle(video.ref, () => ref.current, []);
50+
4951
React.useEffect(() => {
5052
if (ref && ref.current) {
5153
const {loop} = video;

src/components/ReactPlayer/ReactPlayer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const ReactPlayer =
4141
: _ReactPlayer;
4242

4343
export interface ReactPlayerBlockProps
44-
extends Omit<MediaVideoProps, 'loop' | 'src'>,
44+
extends Omit<MediaVideoProps, 'loop' | 'src' | 'ref'>,
4545
ClassNameProps {
4646
src: string | string[];
4747
previewImgUrl?: string;

src/models/constructor-items/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export interface MediaVideoProps extends AnalyticsEventsBase {
190190
ariaLabel?: string;
191191
contain?: boolean;
192192
onVideoEnd?: () => void;
193+
ref?: React.Ref<HTMLVideoElement | null>;
193194
}
194195

195196
// links

0 commit comments

Comments
 (0)