Share
Links to a moment in the video, and opens at the moment it's given.
import { share } from "react-video-kit/share";Adds 920 B to a player2.1 KB on its ownSource
Adds a share button to the controls. It copies a link to the current position, and on a touch device it opens the system's share sheet instead. A page opened with that link starts from the same moment. Press the share button below.
Usage
import { VideoPlayer } from "react-video-kit/player";import { share } from "react-video-kit/share";const plugins = [share()];<VideoPlayer src="/video.mp4" plugins={plugins} />;The time goes in the t query parameter. It can be written as seconds or as a position, so ?t=90, ?t=1:30, and ?t=1m30s are all the same moment.
Change the parameter with param, and link somewhere other than the current page with url:
share({ param: "start", url: "https://example.com/watch/first-light",});Build a link yourself
link returns the link for any position, by default the current one.
"use client";import { useCommands } from "react-video-kit";import { share } from "react-video-kit/share";function ChapterLink({ start, title }: { start: number; title: string }) { const { link } = useCommands(share); return <a href={link(start)}>{title}</a>;}share does what the button does, and returns shared, copied, or failed.
Behaviour
- The time in the page's address applies once, as soon as the video's length is known. A time past the end is ignored, so a link to a video that has since been cut starts from the beginning.
- A link's time wins over a position saved by the persistence plugin.
- The link keeps the page's other query parameters.
- Cancelling the share sheet isn't a failure, and shows no message.
- If the clipboard isn't available, the viewer sees that the link couldn't be copied. The message is announced to screen readers.
- For a live stream, the button links to the stream without a time.
API
Options
| Option | Type | Description |
|---|---|---|
| param? | string | The query parameter that holds the time. |
| url? | string | The page to link to, instead of the current one. |
State
Read it with useVideoState(share, (state) => …).
| Field | Type | Description |
|---|---|---|
| notice | "failed" | "copied" | null | The message on show, or null. |
| noticeVisible | boolean | True until that message starts to fade. |
Commands
Call them with useCommands(share).
| Command | Type | Description |
|---|---|---|
| link | (time?: number) => string | The link to a moment, by default the current one. |
| share | () => Promise<"shared" | "copied" | "failed"> | Opens the system's share sheet on touch devices, and otherwise copies the link. |
Parts
| Part | Description |
|---|---|
| ShareButton | Shares a link to the current moment. |
| ShareNotice | Says whether the link was copied, also announced to screen readers. |