-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from THEOplayer/react-utility-hooks
Add React utility hooks
- Loading branch information
Showing
16 changed files
with
283 additions
and
61 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from './useCurrentTime'; | ||
export * from './useDuration'; | ||
export * from './useMuted'; | ||
export * from './usePaused'; | ||
export * from './useSeeking'; | ||
export * from './useSource'; | ||
export * from './useVolume'; |
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,29 @@ | ||
import { useCallback, useContext, useSyncExternalStore } from 'react'; | ||
import { PlayerContext } from '../context'; | ||
import type { PlayerEventMap } from 'theoplayer'; | ||
|
||
const TIME_CHANGE_EVENTS = ['timeupdate', 'seeking', 'seeked', 'emptied'] satisfies ReadonlyArray<keyof PlayerEventMap>; | ||
|
||
/** | ||
* Returns {@link theoplayer!ChromelessPlayer.currentTime | the player's current time}, automatically updating whenever it changes. | ||
* | ||
* This hook must only be used in a component mounted inside a {@link DefaultUI} or {@link UIContainer}, | ||
* or alternatively any other component that provides a {@link PlayerContext}. | ||
* | ||
* @group Hooks | ||
*/ | ||
export function useCurrentTime(): number { | ||
const player = useContext(PlayerContext); | ||
const subscribe = useCallback( | ||
(callback: () => void) => { | ||
player?.addEventListener(TIME_CHANGE_EVENTS, callback); | ||
return () => player?.removeEventListener(TIME_CHANGE_EVENTS, callback); | ||
}, | ||
[player] | ||
); | ||
return useSyncExternalStore( | ||
subscribe, | ||
() => (player ? player.currentTime : 0), | ||
() => 0 | ||
); | ||
} |
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,29 @@ | ||
import { useCallback, useContext, useSyncExternalStore } from 'react'; | ||
import { PlayerContext } from '../context'; | ||
import type { PlayerEventMap } from 'theoplayer'; | ||
|
||
const DURATION_CHANGE_EVENTS = ['durationchange', 'emptied'] satisfies ReadonlyArray<keyof PlayerEventMap>; | ||
|
||
/** | ||
* Returns {@link theoplayer!ChromelessPlayer.duration | the player's duration}, automatically updating whenever it changes. | ||
* | ||
* This hook must only be used in a component mounted inside a {@link DefaultUI} or {@link UIContainer}, | ||
* or alternatively any other component that provides a {@link PlayerContext}. | ||
* | ||
* @group Hooks | ||
*/ | ||
export function useDuration(): number { | ||
const player = useContext(PlayerContext); | ||
const subscribe = useCallback( | ||
(callback: () => void) => { | ||
player?.addEventListener(DURATION_CHANGE_EVENTS, callback); | ||
return () => player?.removeEventListener(DURATION_CHANGE_EVENTS, callback); | ||
}, | ||
[player] | ||
); | ||
return useSyncExternalStore( | ||
subscribe, | ||
() => (player ? player.duration : NaN), | ||
() => NaN | ||
); | ||
} |
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,26 @@ | ||
import { useCallback, useContext, useSyncExternalStore } from 'react'; | ||
import { PlayerContext } from '../context'; | ||
|
||
/** | ||
* Returns {@link theoplayer!ChromelessPlayer.muted | the player's muted state}, automatically updating whenever it changes. | ||
* | ||
* This hook must only be used in a component mounted inside a {@link DefaultUI} or {@link UIContainer}, | ||
* or alternatively any other component that provides a {@link PlayerContext}. | ||
* | ||
* @group Hooks | ||
*/ | ||
export function useMuted(): boolean { | ||
const player = useContext(PlayerContext); | ||
const subscribe = useCallback( | ||
(callback: () => void) => { | ||
player?.addEventListener('volumechange', callback); | ||
return () => player?.removeEventListener('volumechange', callback); | ||
}, | ||
[player] | ||
); | ||
return useSyncExternalStore( | ||
subscribe, | ||
() => (player ? player.muted : false), | ||
() => false | ||
); | ||
} |
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,29 @@ | ||
import { useCallback, useContext, useSyncExternalStore } from 'react'; | ||
import { PlayerContext } from '../context'; | ||
import type { PlayerEventMap } from 'theoplayer'; | ||
|
||
const PAUSED_CHANGE_EVENTS = ['play', 'pause'] satisfies ReadonlyArray<keyof PlayerEventMap>; | ||
|
||
/** | ||
* Returns {@link theoplayer!ChromelessPlayer.paused | the player's paused state}, automatically updating whenever it changes. | ||
* | ||
* This hook must only be used in a component mounted inside a {@link DefaultUI} or {@link UIContainer}, | ||
* or alternatively any other component that provides a {@link PlayerContext}. | ||
* | ||
* @group Hooks | ||
*/ | ||
export function usePaused(): boolean { | ||
const player = useContext(PlayerContext); | ||
const subscribe = useCallback( | ||
(callback: () => void) => { | ||
player?.addEventListener(PAUSED_CHANGE_EVENTS, callback); | ||
return () => player?.removeEventListener(PAUSED_CHANGE_EVENTS, callback); | ||
}, | ||
[player] | ||
); | ||
return useSyncExternalStore( | ||
subscribe, | ||
() => (player ? player.paused : true), | ||
() => true | ||
); | ||
} |
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,29 @@ | ||
import { useCallback, useContext, useSyncExternalStore } from 'react'; | ||
import { PlayerContext } from '../context'; | ||
import type { PlayerEventMap } from 'theoplayer'; | ||
|
||
const SEEKING_CHANGE_EVENTS = ['seeking', 'seeked', 'emptied'] satisfies ReadonlyArray<keyof PlayerEventMap>; | ||
|
||
/** | ||
* Returns {@link theoplayer!ChromelessPlayer.seeking | the player's seeking state}, automatically updating whenever it changes. | ||
* | ||
* This hook must only be used in a component mounted inside a {@link DefaultUI} or {@link UIContainer}, | ||
* or alternatively any other component that provides a {@link PlayerContext}. | ||
* | ||
* @group Hooks | ||
*/ | ||
export function useSeeking(): boolean { | ||
const player = useContext(PlayerContext); | ||
const subscribe = useCallback( | ||
(callback: () => void) => { | ||
player?.addEventListener(SEEKING_CHANGE_EVENTS, callback); | ||
return () => player?.removeEventListener(SEEKING_CHANGE_EVENTS, callback); | ||
}, | ||
[player] | ||
); | ||
return useSyncExternalStore( | ||
subscribe, | ||
() => (player ? player.seeking : false), | ||
() => false | ||
); | ||
} |
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,27 @@ | ||
import { useCallback, useContext, useSyncExternalStore } from 'react'; | ||
import { PlayerContext } from '../context'; | ||
import type { SourceDescription } from 'theoplayer'; | ||
|
||
/** | ||
* Returns {@link theoplayer!ChromelessPlayer.source | the player's source}, automatically updating whenever it changes. | ||
* | ||
* This hook must only be used in a component mounted inside a {@link DefaultUI} or {@link UIContainer}, | ||
* or alternatively any other component that provides a {@link PlayerContext}. | ||
* | ||
* @group Hooks | ||
*/ | ||
export function useSource(): SourceDescription | undefined { | ||
const player = useContext(PlayerContext); | ||
const subscribe = useCallback( | ||
(callback: () => void) => { | ||
player?.addEventListener('sourcechange', callback); | ||
return () => player?.removeEventListener('sourcechange', callback); | ||
}, | ||
[player] | ||
); | ||
return useSyncExternalStore( | ||
subscribe, | ||
() => player?.source, | ||
() => undefined | ||
); | ||
} |
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,26 @@ | ||
import { useCallback, useContext, useSyncExternalStore } from 'react'; | ||
import { PlayerContext } from '../context'; | ||
|
||
/** | ||
* Returns {@link theoplayer!ChromelessPlayer.volume | the player's volume}, automatically updating whenever it changes. | ||
* | ||
* This hook must only be used in a component mounted inside a {@link DefaultUI} or {@link UIContainer}, | ||
* or alternatively any other component that provides a {@link PlayerContext}. | ||
* | ||
* @group Hooks | ||
*/ | ||
export function useVolume(): number { | ||
const player = useContext(PlayerContext); | ||
const subscribe = useCallback( | ||
(callback: () => void) => { | ||
player?.addEventListener('volumechange', callback); | ||
return () => player?.removeEventListener('volumechange', callback); | ||
}, | ||
[player] | ||
); | ||
return useSyncExternalStore( | ||
subscribe, | ||
() => (player ? player.volume : 1), | ||
() => 1 | ||
); | ||
} |
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
Oops, something went wrong.