- Preview
- Code
PlayPauseButton.jsx
"use client";
import { newEvent } from "@/lib/newEvent";
import { PauseCircleIcon, PlayCircleIcon } from "@/registry/react/Icons";
import { usePlayer, usePlayerSelector } from "@/registry/react/PlayerProvider";
export function PlayPauseButton() {
const player = usePlayer();
const playbackState = usePlayerSelector((p) => p.playbackState);
const isPlaying = playbackState === "playing";
return (
<button
type="button"
aria-label={isPlaying ? "Pause audio" : "Play audio"}
onClick={() => {
const name = isPlaying ? "Pause" : "Play";
player.onEvent(
newEvent({
type: `Pressed${name}`,
description: `The ${name.toLowerCase()} button was pressed.`,
}),
);
}}
>
{isPlaying ? <PauseCircleIcon size={40} /> : <PlayCircleIcon size={40} />}
</button>
);
}
Installation
npx @beyondwords/cli components add play-pause-button
pnpm dlx @beyondwords/cli components add play-pause-button
yarn dlx @beyondwords/cli components add play-pause-button
bunx --bun @beyondwords/cli components add play-pause-button
Usage
import { PlayerProvider } from '@/registry/react/PlayerProvider';
import { PlayPauseButton } from '@/registry/react/PlayPauseButton';
export function Player() {
return (
<PlayerProvider args={{ projectId: 9504, contentId: "ba00ef51-03ac-4a85-8b98-e45c957e8ef8", showUserInterface: false }}>
<PlayPauseButton />
</PlayerProvider>
);
}
playbackState from the surrounding
PlayerProvider. On click, it emits a
PressedPlay / PressedPause event built with
newEvent.
Reference
| Props | None |
| Player state read | playbackState |
| Events emitted | PressedPlay, PressedPause |
| Requires | a PlayerProvider ancestor |