-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StartMediaButton and fix useStartVideo hook (#717)
- Loading branch information
Showing
7 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@livekit/components-core": patch | ||
"@livekit/components-react": patch | ||
--- | ||
|
||
Add StartMediaButton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/react/src/components/controls/StartMediaButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as React from 'react'; | ||
import { useRoomContext } from '../../context'; | ||
import { useStartAudio, useStartVideo } from '../../hooks'; | ||
|
||
/** @public */ | ||
export interface AllowMediaPlaybackProps extends React.ButtonHTMLAttributes<HTMLButtonElement> { | ||
label?: string; | ||
} | ||
|
||
/** | ||
* The `StartMediaButton` component is only visible when the browser blocks media playback. This is due to some browser implemented autoplay policies. | ||
* To start media playback, the user must perform a user-initiated event such as clicking this button. | ||
* As soon as media playback starts, the button hides itself again. | ||
* | ||
* @example | ||
* ```tsx | ||
* <LiveKitRoom> | ||
* <StartMediaButton label="Click to allow media playback" /> | ||
* </LiveKitRoom> | ||
* ``` | ||
* | ||
* @see Autoplay policy on MDN web docs: {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Best_practices#autoplay_policy} | ||
* @public | ||
*/ | ||
export function StartMediaButton({ label, ...props }: AllowMediaPlaybackProps) { | ||
const room = useRoomContext(); | ||
const { mergedProps: audioProps, canPlayAudio } = useStartAudio({ room, props }); | ||
const { mergedProps, canPlayVideo } = useStartVideo({ room, props: audioProps }); | ||
const { style, ...restProps } = mergedProps; | ||
style.display = canPlayAudio && canPlayVideo ? 'none' : 'block'; | ||
|
||
return ( | ||
<button style={style} {...restProps}> | ||
{label ?? `Start ${!canPlayAudio ? 'Audio' : 'Video'}`} | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters