Skip to content

Commit

Permalink
feat(hook): introduce useAudio hook (#383)
Browse files Browse the repository at this point in the history
* feat(hook): introduce useAudio hook

* chore: update readme

* chore: rename useSetState to useObjectState
  • Loading branch information
playerony authored Aug 9, 2022
1 parent 9d2ac1a commit 2e712d9
Show file tree
Hide file tree
Showing 16 changed files with 602 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ $ yarn add beautiful-react-hooks
* [useConditionalTimeout](docs/useConditionalTimeout.md)
* [useCookie](docs/useCookie.md)
* [useMutationObserver](docs/useMutationObserver.md)
* [useAudio](docs/useAudio.md)
* [useObjectState](docs/useObjectState.md)

<div>
<p align="center">
Expand Down
2 changes: 2 additions & 0 deletions docs/README.es-ES.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ $ yarn add beautiful-react-hooks
* [useConditionalTimeout](useConditionalTimeout.md)
* [useCookie](useCookie.md)
* [useMutationObserver](useMutationObserver.md)
* [useAudio](useAudio.md)
* [useObjectState](useObjectState.md)

<div>
<p align="center">
Expand Down
2 changes: 2 additions & 0 deletions docs/README.it-IT.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ $ yarn add beautiful-react-hooks
* [useConditionalTimeout](useConditionalTimeout.md)
* [useCookie](useCookie.md)
* [useMutationObserver](useMutationObserver.md)
* [useAudio](useAudio.md)
* [useObjectState](useObjectState.md)

<div>
<p align="center">
Expand Down
2 changes: 2 additions & 0 deletions docs/README.jp-JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ $ yarn add beautiful-react-hooks
* [useConditionalTimeout](useConditionalTimeout.md)
* [useCookie](useCookie.md)
* [useMutationObserver](useMutationObserver.md)
* [useAudio](useAudio.md)
* [useObjectState](useObjectState.md)

<div>
<p align="center">
Expand Down
2 changes: 2 additions & 0 deletions docs/README.pl-PL.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ $ yarn add beautiful-react-hooks
* [useConditionalTimeout](useConditionalTimeout.md)
* [useCookie](useCookie.md)
* [useMutationObserver](useMutationObserver.md)
* [useAudio](useAudio.md)
* [useObjectState](useObjectState.md)

<div>
<p align="center">
Expand Down
2 changes: 2 additions & 0 deletions docs/README.pt-BR.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ $ yarn add beautiful-react-hooks
* [useConditionalTimeout](useConditionalTimeout.md)
* [useCookie](useCookie.md)
* [useMutationObserver](useMutationObserver.md)
* [useAudio](useAudio.md)
* [useObjectState](useObjectState.md)

<div>
<p align="center">
Expand Down
2 changes: 2 additions & 0 deletions docs/README.uk-UA.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ $ yarn add beautiful-react-hooks
* [useConditionalTimeout](useConditionalTimeout.md)
* [useCookie](useCookie.md)
* [useMutationObserver](useMutationObserver.md)
* [useAudio](useAudio.md)
* [useObjectState](useObjectState.md)

<div>
<p align="center">
Expand Down
2 changes: 2 additions & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ $ yarn add beautiful-react-hooks
* [useConditionalTimeout](useConditionalTimeout.md)
* [useCookie](useCookie.md)
* [useMutationObserver](useMutationObserver.md)
* [useAudio](useAudio.md)
* [useObjectState](useObjectState.md)

<div>
<p align="center">
Expand Down
52 changes: 52 additions & 0 deletions docs/useAudio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# useAudio

Creates <audio> element, tracks its state and exposes playback controls.

### Why? 💡

- A quick way to use the `Audio` in your React components.

### Basic Usage:

```jsx harmony
import { useRef, useState } from 'react';
import { Button } from 'beautiful-react-ui';
import useAudio from 'beautiful-react-hooks/useAudio';

const UseAudioComponent = () => {
const [state, controls] = useAudio("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3", { autoPlay: true });

return (
<DisplayDemo>
{JSON.stringify(state, null, 2)}
<br />
<Button onClick={controls.play}>
Play
</Button>
<Button onClick={controls.pause}>
Pause
</Button>
<br />
<Button onClick={controls.mute}>
Mute
</Button>
<Button onClick={controls.unmute}>
Unmute
</Button>
<br />
<Button onClick={() => controls.setVolume(.1)}>Volume: 10%</Button>
<Button onClick={() => controls.setVolume(.5)}>Volume: 50%</Button>
<Button onClick={() => controls.setVolume(1)}>Volume: 100%</Button>
<br />
<Button onClick={() => controls.seek(state.currentTime + 10)}>
Jump 10 seconds
</Button>
<Button onClick={() => controls.seek(state.currentTime - 10)}>
Jump -10 seconds
</Button>
</DisplayDemo>
);
};

<UseAudioComponent />
```
49 changes: 49 additions & 0 deletions docs/useObjectState.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# useObjectState

React state hook that creates setState method which works similar to how useState works, merges object changes into the current state.

### Why? 💡

- takes care of automatically destruct the previous state and override this with a new one,

### Basic Usage:

```jsx harmony
import { useState, useRef } from 'react';
import { Button } from 'beautiful-react-ui';
import useObjectState from 'beautiful-react-hooks/useObjectState';

const UseObjectStateComponent = () => {
const [state, setState] = useObjectState({ count: 0, title: 'Test title' })

const reset = () => setState({ count: 0 })

const increment = () => setState({ count: state.count + 1 })

const decrement = () => setState({ count: state.count - 1 })

return (
<DisplayDemo>
<p>State:</p>
<p>{JSON.stringify(state, null, 2)}</p>
<Button onClick={increment}>
Increment counter
</Button>
<Button onClick={decrement}>
Decrement counter
</Button>
<Button onClick={reset}>
Reset counter
</Button>
</DisplayDemo>
);
};

<UseObjectStateComponent />
```

### Mastering the hook

#### ✅ When to use

- When in need of manage the state which is an object,
Loading

0 comments on commit 2e712d9

Please sign in to comment.