Skip to content

Commit

Permalink
refactor: extract setForwardedRef/useMixedRef
Browse files Browse the repository at this point in the history
  • Loading branch information
magicdawn committed Jan 7, 2025
1 parent b55028f commit 8613826
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
25 changes: 25 additions & 0 deletions src/common/hooks/mixed-ref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ForwardedRef } from 'react'

export function setForwardedRef<T>(forwardedRef: ForwardedRef<T>, value: T) {
if (!forwardedRef) return
if (typeof forwardedRef === 'function') {
forwardedRef(value)
} else {
forwardedRef.current = value
}
}

export function useMixedRef<T>(forwardedRef: ForwardedRef<T>) {
const ref = useRef<T | null>(null)
return useMemo(() => {
return {
get current() {
return ref.current
},
set current(val: T | null) {
ref.current = val
setForwardedRef(forwardedRef, val)
},
}
}, [ref])
}
24 changes: 5 additions & 19 deletions src/components/VideoCard/child-components/RecoverableVideo.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
import { useEventListener, useUnmount } from 'ahooks'
import { useMixedRef } from '$common/hooks/mixed-ref'
import { useEventListener } from 'ahooks'
import type { ComponentProps, ComponentRef, MutableRefObject } from 'react'

export const RecoverableVideo = forwardRef<
ComponentRef<'video'>,
ComponentProps<'video'> & { currentTimeRef: MutableRefObject<number | undefined> }
>(({ currentTimeRef, ...videoProps }, forwardRef) => {
const ref = useRef<ComponentRef<'video'>>(null)

const syncForwardedRef = () => {
forwardRef &&
(typeof forwardRef === 'function'
? forwardRef(ref.current)
: (forwardRef.current = ref.current))
}

>(({ currentTimeRef, ...videoProps }, forwardedRef) => {
const ref = useMixedRef<ComponentRef<'video'> | null>(forwardedRef)
const mounted = useRef(false)

useMount(() => {
syncForwardedRef()

// set initial time
if (ref.current && typeof currentTimeRef.current === 'number') {
ref.current.currentTime = currentTimeRef.current
}

mounted.current = true
})

useUnmount(() => {
syncForwardedRef()
})

useEventListener(
'timeupdate',
() => {
Expand All @@ -40,5 +26,5 @@ export const RecoverableVideo = forwardRef<
{ target: ref },
)

return <video ref={ref} {...videoProps} />
return <video {...videoProps} ref={ref} />
})
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setForwardedRef } from '$common/hooks/mixed-ref'
import { colorPrimaryValue } from '$components/css-vars'
import { css as _css, css } from '@emotion/react'
import { useHover } from 'ahooks'
Expand Down Expand Up @@ -92,13 +93,7 @@ export const VideoCardActionButton = memo(
{...divProps}
ref={(el) => {
triggerRef.current = el
if (forwardedRef) {
if (typeof forwardedRef === 'function') {
forwardedRef(el)
} else {
forwardedRef.current = el
}
}
setForwardedRef(forwardedRef, el)
}}
css={[S.button(visible, active)]}
className={clsx('action-button', className)}
Expand Down

0 comments on commit 8613826

Please sign in to comment.