-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap exported components with React.forwardRef()
#6
Comments
Thanks @rileyjshaw, this is an excellent feature request and your reasoning is very understandable. I'll have a look into this and get back to you with updates. Cheers, |
@rileyjshaw I have a question for you about |
@Regaddi great question! I hadn’t considered that. Here are some quick thoughts. I was thinking through the problem while typing this, so Option 4 is my favourite. Feel free to skim the rest 😄 Option 1: Do nothingThe use case for forwarding refs to …but it would be really nice if Option 2: Define a set of acceptable props for both
|
Thanks a lot for the detailed input, @rileyjshaw! I really appreciate your thinking here, it's invaluable! I'd like to propose another, slightly adjusted, option, based on your options 2, 3 and 4: Instead of applying a plain interface DailyAudioHandle {
getAllAudio(): HTMLAudioElement[];
getActiveSpeakerAudio(): HTMLAudioElement;
getScreenAudio(): HTMLAudioElement[];
getAudioBySessionId(id: string): HTMLAudioElement;
getScreenAudioBySessionId(id: string): HTMLAudioElement;
}
export const DailyAudio = forwardRef<DailyAudioHandle, Props>((props, ref) => {
const containerRef = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => ({
getAllAudio: () => containerRef.current?.querySelectorAll('audio'),
getActiveSpeakerAudio: () =>
containerRef.current?.querySelector(
`audio[data-session-id="${activeSpeakerId}"][data-audio-type="audio"]`
),
getScreenAudio: () =>
containerRef.current?.querySelectorAll(
'audio[data-audio-type="screenAudio"]'
),
getAudioBySessionId: (id: string) =>
containerRef.current?.querySelector(
`audio[data-session-id="${id}"][data-audio-type="audio"]`
),
getScreenAudioBySessionId: (id: string) =>
containerRef.current?.querySelector(
`audio[data-session-id="${id}"][data-audio-type="screenAudio"]`
),
}));
return (
<div ref={containerRef}>
{/* rendered audio tracks *}
</div>
);
}); Rationale:
Let me know what you think, otherwise I'll probably go ahead with this approach |
@Regaddi this sounds so useful!! Great idea 😄 |
This change has shipped with daily-react 0.7.0 today! |
Feature request
It would be useful to wrap
DailyAudio
,DailyAudioTrack
, andDailyVideo
in React.forwardRef().Why you need this
I’d love to use
<DailyAudioTrack />
, but it’s useful to have a reference to the DOM node for HTMLMediaElements. For instance, it’s not possible to setvolume
under the current implementation, since you’d need access toaudioEl.current.volume
.Alternatives you've considered
I think it’s most useful to simply accept a ref and join it to the internal ref with something like
useMergeRefs
1. That way, the user can use the ref however they need.But if that’s not to your taste, you could also define specific methods with
useImperativeHandle
.It could also be nice to accept declarative props (like
volume
) and handle all of the effects internally, but it’s less flexible and more work on your end.Additional context
I also think it would be useful to accept arbitrary
props
and spread them onto the returned element, like so:That way, you can pass in arbitrary properties like
muted
.Footnotes
Reference implementation:
↩The text was updated successfully, but these errors were encountered: