Skip to content

Commit e049e96

Browse files
authored
Merge pull request #17 from riyaddecoder/feat/doc-update-add-ref
feat: doc updated, added ref
2 parents e756fd9 + 665c0e2 commit e049e96

File tree

6 files changed

+1945
-5
lines changed

6 files changed

+1945
-5
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<template>
2+
<div ref="el" />
3+
</template>
4+
5+
<script setup>
6+
import { createElement } from 'react';
7+
import { createRoot } from 'react-dom/client';
8+
import { ref, onMounted, defineProps } from 'vue';
9+
import AudioPlayerWithRef from './AudioPlayerWithRef.jsx';
10+
11+
const props = defineProps({
12+
src: {
13+
type: String,
14+
required: true
15+
}
16+
});
17+
18+
const el = ref();
19+
20+
onMounted(() => {
21+
const root = createRoot(el.value);
22+
root.render(createElement(AudioPlayerWithRef, { ...props }));
23+
});
24+
</script>
25+
<style>
26+
.audio-example-with-ref button {
27+
padding: 1px 10px;
28+
background: white;
29+
color: black;
30+
border-radius: 5px;
31+
margin-right: 3px;
32+
margin-top: 5px;
33+
box-shadow: 1px 1px 8px 3px #ececec;
34+
}
35+
36+
.dark .audio-example-with-ref button {
37+
box-shadow: none;
38+
}
39+
40+
.audio-example-with-ref button:hover {
41+
background: #ddd;
42+
}
43+
</style>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React, { useRef } from 'react';
2+
import { AudioPlayer } from 'react-audio-play';
3+
4+
export default function AudioPlayerWithRef(props) {
5+
const playerRef = useRef(null);
6+
7+
const handlePlay = () => {
8+
playerRef.current?.play();
9+
};
10+
11+
const handlePause = () => {
12+
playerRef.current?.pause();
13+
};
14+
15+
const handleStop = () => {
16+
playerRef.current?.stop();
17+
};
18+
19+
const handleFocus = () => {
20+
playerRef.current?.focus();
21+
};
22+
23+
return (
24+
<div className='audio-example-with-ref'>
25+
<AudioPlayer ref={playerRef} src={props.src} />
26+
<button onClick={handlePlay}>Play</button>
27+
<button onClick={handlePause}>Pause</button>
28+
<button onClick={handleStop}>Stop</button>
29+
<button onClick={handleFocus}>Focus</button>
30+
</div>
31+
);
32+
}

documentation/docs/examples.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,51 @@ Preview
8181

8282
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
8383

84-
## Example 4: Darkmode using basic style props
84+
## Example 4: Usage with ref (available from v1.0.4)
85+
86+
```tsx
87+
import { useRef } from 'react';
88+
import { AudioPlayer, AudioPlayerRef } from 'react-audio-play';
89+
90+
function App() {
91+
const playerRef = useRef<AudioPlayerRef>(null);
92+
93+
const handlePlay = () => {
94+
playerRef.current?.play();
95+
};
96+
97+
const handlePause = () => {
98+
playerRef.current?.pause();
99+
};
100+
101+
const handleStop = () => {
102+
playerRef.current?.stop();
103+
};
104+
105+
const handleFocus = () => {
106+
playerRef.current?.focus();
107+
};
108+
109+
return (
110+
<div>
111+
<AudioPlayer ref={playerRef} src="path/to/audio.mp3" autoPlay />
112+
<button onClick={handlePlay}>Play</button>
113+
<button onClick={handlePause}>Pause</button>
114+
<button onClick={handleStop}>Stop</button>
115+
<button onClick={handleFocus}>Focus</button>
116+
</div>
117+
);
118+
}
119+
120+
```
121+
122+
Preview
123+
124+
<AudioPlayerRefExample :src="music.nextMusic().url"/>
125+
126+
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
127+
128+
## Example 5: Darkmode using basic style props
85129

86130
```js
87131
import { AudioPlayer } from "react-audio-play";
@@ -104,7 +148,7 @@ Preview
104148

105149
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
106150

107-
## Example 5: Using Style Object
151+
## Example 6: Using Style Object
108152

109153
```js
110154
import { AudioPlayer } from "react-audio-play";
@@ -127,7 +171,7 @@ Preview
127171

128172
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
129173

130-
## Example 6: Using Custom CSS
174+
## Example 7: Using Custom CSS
131175

132176
::: code-group
133177

@@ -183,7 +227,7 @@ Preview
183227

184228
<small>Music Source: <a :href="music.getCurrentMusic().sourceLink" target="_blank">{{music.getCurrentMusic().title}}</a></small>
185229

186-
## Example 7: More Playing With CSS
230+
## Example 8: More Playing With CSS
187231

188232
::: code-group
189233

@@ -255,6 +299,7 @@ Preview
255299
<script setup>
256300
import { Music } from './.vitepress/lib/Music.ts';
257301
import AudioPlayerWrapper from './.vitepress/components/AudioPlayerWrapper.vue';
302+
import AudioPlayerRefExample from './.vitepress/components/AudioPlayerRefExample.vue';
258303

259304
const music = new Music();
260305
</script>

documentation/docs/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ The **AudioPlayer** component accepts the following props:
5252
| `width` | string, optional | The width of the audio player. For example, `"100%"`, `"300px"`, etc. |
5353
| `style` | object, optional | An object containing additional inline styles for the component. |
5454
55+
56+
## Advanced Usage
57+
58+
Starting with version `v1.0.4`, you can access certain actions of the `AudioPlayer` component programmatically using a `ref` with the following interface:
59+
60+
- `play`: Function to start audio playback.
61+
- `pause`: Function to pause audio playback.
62+
- `stop`: Function to stop the audio playback.
63+
- `focus`: Function to focus on the audio player component.
64+
65+
::: info
66+
[Example Here](/examples.html#example-4-usage-with-ref-available-from-v1-0-4)
67+
:::
68+
5569
## Keyboard Shortcuts
5670
5771
Below are the keyboard shortcuts available when the audio player is focused. They can be turned off by setting the `hasKeyBindings` prop to `false`.

0 commit comments

Comments
 (0)