|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import type { InferMediaState, MediaUIComponent, StateAttrMap } from '@videojs/core'; |
| 3 | +import type { InferComponentState, InferMediaState, MediaUIComponent, StateAttrMap } from '@videojs/core'; |
4 | 4 | import { logMissingFeature } from '@videojs/core/dom'; |
5 | 5 | import type { Selector } from '@videojs/store'; |
6 | 6 | import type { ForwardedRef, ForwardRefExoticComponent, RefAttributes } from 'react'; |
7 | 7 | import { forwardRef, useState } from 'react'; |
8 | 8 |
|
9 | 9 | import { usePlayer } from '../player/context'; |
| 10 | +import type { renderElement as renderElementFn } from '../utils/use-render'; |
10 | 11 | import { renderElement } from '../utils/use-render'; |
11 | 12 | import { useButton } from './hooks/use-button'; |
12 | 13 |
|
13 | | -interface MediaButtonConfig<Core extends MediaUIComponent> { |
| 14 | +interface MediaButtonConfig<Core extends Required<MediaUIComponent>> { |
14 | 15 | displayName: string; |
15 | 16 | core: { new (): Core; defaultProps: Record<string, unknown> }; |
16 | | - stateAttrMap: StateAttrMap<object>; |
| 17 | + stateAttrMap: StateAttrMap<InferComponentState<Core>>; |
17 | 18 | selector: Selector<object, InferMediaState<Core> | undefined>; |
18 | 19 | action: (core: Core, state: InferMediaState<Core>) => void; |
19 | 20 | } |
20 | 21 |
|
21 | | -/** |
22 | | - * Creates a media button React component. Curried so `ComponentProps` is explicit |
23 | | - * while `Core` is inferred from the config. |
24 | | - */ |
25 | | -export function createMediaButton<ComponentProps extends object>() { |
26 | | - return <Core extends Required<MediaUIComponent>>( |
27 | | - config: MediaButtonConfig<Core> |
28 | | - ): ForwardRefExoticComponent<ComponentProps & RefAttributes<HTMLButtonElement>> => { |
29 | | - const { displayName, core: CoreClass, stateAttrMap, selector, action } = config; |
30 | | - |
31 | | - // Props that exist in the core's defaultProps are routed to setProps; the rest go to the DOM element. |
32 | | - const corePropKeys = new Set(Object.keys(CoreClass.defaultProps)); |
33 | | - |
34 | | - const Component = forwardRef(function MediaButton( |
35 | | - componentProps: Record<string, unknown>, |
36 | | - forwardedRef: ForwardedRef<HTMLButtonElement> |
37 | | - ) { |
38 | | - const { render, className, style, ...rest } = componentProps; |
39 | | - |
40 | | - const coreProps: Record<string, unknown> = {}; |
41 | | - const elementProps: Record<string, unknown> = {}; |
42 | | - |
43 | | - for (const key of Object.keys(rest)) { |
44 | | - if (corePropKeys.has(key)) { |
45 | | - coreProps[key] = rest[key]; |
46 | | - } else { |
47 | | - elementProps[key] = rest[key]; |
48 | | - } |
| 22 | +/** Creates a media button React component from a core class and config. */ |
| 23 | +export function createMediaButton<Core extends Required<MediaUIComponent>, Props extends object>( |
| 24 | + config: MediaButtonConfig<Core> |
| 25 | +): ForwardRefExoticComponent<Props & RefAttributes<HTMLButtonElement>> { |
| 26 | + const { displayName, core: CoreClass, stateAttrMap, selector, action } = config; |
| 27 | + |
| 28 | + // Props that exist in the core's defaultProps are routed to setProps; the rest go to the DOM element. |
| 29 | + const corePropKeys = new Set(Object.keys(CoreClass.defaultProps)); |
| 30 | + |
| 31 | + const Component = forwardRef(function MediaButton( |
| 32 | + componentProps: Record<string, unknown>, |
| 33 | + forwardedRef: ForwardedRef<HTMLButtonElement> |
| 34 | + ) { |
| 35 | + const { render, className, style, ...rest } = componentProps; |
| 36 | + |
| 37 | + const coreProps: Record<string, unknown> = {}; |
| 38 | + const elementProps: Record<string, unknown> = {}; |
| 39 | + |
| 40 | + for (const key of Object.keys(rest)) { |
| 41 | + if (corePropKeys.has(key)) { |
| 42 | + coreProps[key] = rest[key]; |
| 43 | + } else { |
| 44 | + elementProps[key] = rest[key]; |
49 | 45 | } |
| 46 | + } |
50 | 47 |
|
51 | | - const feature = usePlayer(selector); |
| 48 | + const feature = usePlayer(selector); |
52 | 49 |
|
53 | | - const [core] = useState(() => new CoreClass()); |
54 | | - core.setProps(coreProps); |
| 50 | + const [core] = useState(() => new CoreClass()); |
| 51 | + core.setProps(coreProps); |
55 | 52 |
|
56 | | - const { getButtonProps, buttonRef } = useButton({ |
57 | | - displayName, |
58 | | - onActivate: () => action(core, feature!), |
59 | | - isDisabled: () => !!coreProps.disabled || !feature, |
60 | | - }); |
| 53 | + const { getButtonProps, buttonRef } = useButton({ |
| 54 | + displayName, |
| 55 | + onActivate: () => action(core, feature!), |
| 56 | + isDisabled: () => !!coreProps.disabled || !feature, |
| 57 | + }); |
61 | 58 |
|
62 | | - if (!feature) { |
63 | | - if (__DEV__) logMissingFeature(displayName, selector.displayName ?? displayName); |
64 | | - return null; |
65 | | - } |
| 59 | + if (!feature) { |
| 60 | + if (__DEV__) logMissingFeature(displayName, selector.displayName ?? displayName); |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + type State = InferComponentState<Core>; |
66 | 65 |
|
67 | | - core.setMedia(feature); |
68 | | - const state = core.getState() as object & Record<string, unknown>; |
| 66 | + core.setMedia(feature); |
| 67 | + const state = core.getState() as State; |
69 | 68 |
|
70 | | - return renderElement('button', { render, className, style } as Parameters<typeof renderElement>[1], { |
| 69 | + return renderElement( |
| 70 | + 'button', |
| 71 | + { render, className, style } as renderElementFn.ComponentProps<InferComponentState<Core>>, |
| 72 | + { |
71 | 73 | state, |
72 | 74 | stateAttrMap, |
73 | 75 | ref: [forwardedRef, buttonRef], |
74 | 76 | props: [core.getAttrs(state), elementProps, getButtonProps()], |
75 | | - }); |
76 | | - }); |
| 77 | + } |
| 78 | + ); |
| 79 | + }); |
77 | 80 |
|
78 | | - Component.displayName = displayName; |
| 81 | + Component.displayName = displayName; |
79 | 82 |
|
80 | | - return Component as unknown as ForwardRefExoticComponent<ComponentProps & RefAttributes<HTMLButtonElement>>; |
81 | | - }; |
| 83 | + return Component as unknown as ForwardRefExoticComponent<Props & RefAttributes<HTMLButtonElement>>; |
82 | 84 | } |
0 commit comments