Skip to content

Commit b23c707

Browse files
committed
docs: improve <Audio> docs
1 parent ed6bb49 commit b23c707

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

docs/Audio.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ FaCC that creates an `<audio>` element to play audio tracks, re-renders on audio
55
## Usage
66

77
```jsx
8-
import {Audio} from 'mol-fe-react/lib/Audio';
8+
import {Audio} from 'libreact/lib/Audio';
99

1010
<Audio autoPlay src='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'>{(control, state) => {
1111
JSON.stringify(state, null, 4)
1212
}}</Audio>
1313
```
1414

15+
1516
## Props
1617

1718
In addition to props below also accepts all [React's media events](https://reactjs.org/docs/events.html#media-events).
1819

1920
```tsx
20-
export interface IAudioProps {
21+
interface IAudioProps {
2122
src: string;
2223
autoPlay?: boolean;
2324
loop?: boolean;
@@ -36,6 +37,56 @@ export interface IAudioProps {
3637
- `volume` - optional, number, audio volume in `[0..1]` range, defaults to `1`.
3738
- `noJs` - optional, React element(s) to render insided the `<audio>` tag.
3839

40+
41+
## Arguments
42+
43+
The `children` function receives two arguments, `audio` instance as *controller* and `state`.
44+
45+
```jsx
46+
<Audio autoPlay src={src}>{(audio, state) =>
47+
48+
}</Audio>
49+
```
50+
51+
First argument is the `<Audio>` component instance with the following public signature.
52+
53+
```ts
54+
interface IAudio {
55+
el: HTMLAudioElement;
56+
play();
57+
pause();
58+
seek(time: number);
59+
volume(value: number);
60+
mute();
61+
unmute();
62+
}
63+
```
64+
65+
, where
66+
67+
- `el` - `<audio>` element DOM node.
68+
69+
The second argument is the state of the `<Audio>` component with the following signature.
70+
71+
```ts
72+
interface IAudioState {
73+
time?: number;
74+
duration?: number;
75+
isPlaying?: boolean;
76+
muted?: boolean;
77+
volume?: number;
78+
}
79+
```
80+
81+
, where
82+
83+
- `time` - current time in seconds.
84+
- `duration` - total audio track duration in seconds.
85+
- `isPlaying` - whether the audio track is currently playing.
86+
- `muted` - whether `muted` attribute is set on the audio element.
87+
- `volume` - current volume in range `[0..1]`.
88+
89+
3990
## Example
4091

4192
Play a sample audio track with control buttons.

src/Audio/story.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ storiesOf('Generators/Audio', module)
3535
<button onClick={() => audio.volume(state.volume + 0.05)}>Volume +</button>
3636
<button onClick={audio.mute}>Mute</button>
3737
<button onClick={audio.unmute}>Unmute</button>
38+
3839
<pre style={{fontFamily: 'monospace'}}>
3940
{JSON.stringify(state, null, 4)}
4041
</pre>
42+
4143
<hr />
44+
4245
<pre style={{fontFamily: 'monospace'}}>
4346
{`
4447
<Audio src={src}>{(audio, state) =>

0 commit comments

Comments
 (0)