react-ui-sound-events 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +240 -0
- package/package.json +19 -0
- package/src/SoundAutoToast.jsx +21 -0
- package/src/SoundProvider.jsx +27 -0
- package/src/index.js +3 -0
- package/src/useSound.js +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# ðķ react-ui-sound-events
|
|
2
|
+
|
|
3
|
+
A lightweight and powerful sound-feedback system for **React & Next.js**
|
|
4
|
+
applications.\
|
|
5
|
+
Easily play different sounds for **login, logout, success, error, toast
|
|
6
|
+
notifications, and UI events** to enhance user experience and
|
|
7
|
+
accessibility.
|
|
8
|
+
|
|
9
|
+
------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
## ð Why react-ui-sound-events?
|
|
12
|
+
|
|
13
|
+
Modern web apps rely heavily on visual feedback, but **audio feedback
|
|
14
|
+
adds clarity, delight, and accessibility**.
|
|
15
|
+
|
|
16
|
+
This package helps you: - Avoid rewriting sound logic in every project -
|
|
17
|
+
Centralize UI sound management - Improve UX with minimal effort - Add
|
|
18
|
+
premium, app-like interactions
|
|
19
|
+
|
|
20
|
+
------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
## âĻ Features
|
|
23
|
+
|
|
24
|
+
- ðĩ Event-based sound playback
|
|
25
|
+
- âïļ Works with React & Next.js
|
|
26
|
+
- ð Toastify sound integration
|
|
27
|
+
- ð§ Centralized sound manager
|
|
28
|
+
- ð Volume control
|
|
29
|
+
- âŧïļ Sound reuse & preload
|
|
30
|
+
- ðŦ Browser autoplay-safe
|
|
31
|
+
- ðŠķ Lightweight & dependency-free
|
|
32
|
+
|
|
33
|
+
------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
## ðĶ Installation
|
|
36
|
+
|
|
37
|
+
``` bash
|
|
38
|
+
npm install react-ui-sound-events
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
or
|
|
42
|
+
|
|
43
|
+
``` bash
|
|
44
|
+
yarn add react-ui-sound-events
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
## âïļ Basic Setup (React)
|
|
50
|
+
|
|
51
|
+
Wrap your application once using `SoundProvider`.
|
|
52
|
+
|
|
53
|
+
``` jsx
|
|
54
|
+
import { SoundProvider } from "react-ui-sound-events";
|
|
55
|
+
|
|
56
|
+
<SoundProvider
|
|
57
|
+
sounds={{
|
|
58
|
+
login: "/sounds/login.mp3",
|
|
59
|
+
logout: "/sounds/logout.mp3",
|
|
60
|
+
success: "/sounds/success.mp3",
|
|
61
|
+
error: "/sounds/error.mp3",
|
|
62
|
+
info: "/sounds/info.mp3",
|
|
63
|
+
}}
|
|
64
|
+
volume={0.8}
|
|
65
|
+
>
|
|
66
|
+
<App />
|
|
67
|
+
</SoundProvider>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
## ðŪ Play Sounds Anywhere
|
|
73
|
+
|
|
74
|
+
Use the `useSound` hook inside any component.
|
|
75
|
+
|
|
76
|
+
``` jsx
|
|
77
|
+
import { useSound } from "react-ui-sound-events";
|
|
78
|
+
|
|
79
|
+
const LoginButton = () => {
|
|
80
|
+
const { play } = useSound();
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<button onClick={() => play("login")}>
|
|
84
|
+
Login
|
|
85
|
+
</button>
|
|
86
|
+
);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export default LoginButton;
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
------------------------------------------------------------------------
|
|
93
|
+
|
|
94
|
+
## ð Toastify Integration (Auto Sound)
|
|
95
|
+
|
|
96
|
+
Automatically play sounds when using `react-toastify`.
|
|
97
|
+
|
|
98
|
+
``` jsx
|
|
99
|
+
import { useSoundToast } from "react-ui-sound-events";
|
|
100
|
+
|
|
101
|
+
const Example = () => {
|
|
102
|
+
const toast = useSoundToast();
|
|
103
|
+
|
|
104
|
+
return (
|
|
105
|
+
<>
|
|
106
|
+
<button onClick={() => toast.success("Login successful!")}>
|
|
107
|
+
Success
|
|
108
|
+
</button>
|
|
109
|
+
|
|
110
|
+
<button onClick={() => toast.error("Something went wrong!")}>
|
|
111
|
+
Error
|
|
112
|
+
</button>
|
|
113
|
+
</>
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
## âïļ Next.js Usage
|
|
121
|
+
|
|
122
|
+
### App Router (`app/layout.js`)
|
|
123
|
+
|
|
124
|
+
``` jsx
|
|
125
|
+
"use client";
|
|
126
|
+
|
|
127
|
+
import { SoundProvider } from "react-ui-sound-events";
|
|
128
|
+
|
|
129
|
+
export default function RootLayout({ children }) {
|
|
130
|
+
return (
|
|
131
|
+
<SoundProvider
|
|
132
|
+
sounds={{
|
|
133
|
+
success: "/success.mp3",
|
|
134
|
+
error: "/error.mp3",
|
|
135
|
+
}}
|
|
136
|
+
>
|
|
137
|
+
{children}
|
|
138
|
+
</SoundProvider>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Pages Router (`pages/_app.js`)
|
|
144
|
+
|
|
145
|
+
``` jsx
|
|
146
|
+
import { SoundProvider } from "react-ui-sound-events";
|
|
147
|
+
|
|
148
|
+
function MyApp({ Component, pageProps }) {
|
|
149
|
+
return (
|
|
150
|
+
<SoundProvider sounds={{ success: "/success.mp3" }}>
|
|
151
|
+
<Component {...pageProps} />
|
|
152
|
+
</SoundProvider>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export default MyApp;
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
------------------------------------------------------------------------
|
|
160
|
+
|
|
161
|
+
## ð§ Supported Events
|
|
162
|
+
|
|
163
|
+
You can name sounds anything you want:
|
|
164
|
+
|
|
165
|
+
- login
|
|
166
|
+
- logout
|
|
167
|
+
- signup
|
|
168
|
+
- success
|
|
169
|
+
- error
|
|
170
|
+
- warning
|
|
171
|
+
- info
|
|
172
|
+
- click
|
|
173
|
+
- notification
|
|
174
|
+
|
|
175
|
+
``` js
|
|
176
|
+
play("success");
|
|
177
|
+
play("error");
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
------------------------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
## ð Volume Control
|
|
183
|
+
|
|
184
|
+
``` jsx
|
|
185
|
+
<SoundProvider volume={0.5}>
|
|
186
|
+
<App />
|
|
187
|
+
</SoundProvider>
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
Range: `0.0` -- `1.0`
|
|
191
|
+
|
|
192
|
+
------------------------------------------------------------------------
|
|
193
|
+
|
|
194
|
+
## ðŦ Browser Autoplay Policy
|
|
195
|
+
|
|
196
|
+
Due to browser security rules: - Sounds play **only after user
|
|
197
|
+
interaction** - First click enables sound playback - Mobile browsers may
|
|
198
|
+
restrict autoplay
|
|
199
|
+
|
|
200
|
+
This package safely follows these rules.
|
|
201
|
+
|
|
202
|
+
------------------------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
## ð ïļ Roadmap
|
|
205
|
+
|
|
206
|
+
- ð Global mute toggle
|
|
207
|
+
- ðĻ Sound themes (minimal, game, iOS-style)
|
|
208
|
+
- ðū Persist user preferences
|
|
209
|
+
- âŋ Accessibility mode
|
|
210
|
+
|
|
211
|
+
------------------------------------------------------------------------
|
|
212
|
+
|
|
213
|
+
## ðĄ Use Cases
|
|
214
|
+
|
|
215
|
+
- Login / Logout flows
|
|
216
|
+
- Admin dashboards
|
|
217
|
+
- Fintech & SaaS apps
|
|
218
|
+
- Toast notifications
|
|
219
|
+
- Games & kids apps
|
|
220
|
+
- Accessibility-focused UIs
|
|
221
|
+
|
|
222
|
+
------------------------------------------------------------------------
|
|
223
|
+
|
|
224
|
+
## ð§âðŧ Author
|
|
225
|
+
|
|
226
|
+
**Uttam Kumar**\
|
|
227
|
+
Frontend / MERN Stack Developer
|
|
228
|
+
|
|
229
|
+
------------------------------------------------------------------------
|
|
230
|
+
|
|
231
|
+
## â Support
|
|
232
|
+
|
|
233
|
+
If you like this project: - â Star it on GitHub - ðĶ Use it in your
|
|
234
|
+
projects - ð Report issues & suggest features
|
|
235
|
+
|
|
236
|
+
------------------------------------------------------------------------
|
|
237
|
+
|
|
238
|
+
## ð License
|
|
239
|
+
|
|
240
|
+
MIT License
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-ui-sound-events",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Event based sound system for React & Next.js UI",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"react",
|
|
8
|
+
"nextjs",
|
|
9
|
+
"sound",
|
|
10
|
+
"toast",
|
|
11
|
+
"ui",
|
|
12
|
+
"ux"
|
|
13
|
+
],
|
|
14
|
+
"author": "Uttam Kumar",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"react": ">=16"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { toast } from "react-toastify";
|
|
2
|
+
import { useSound } from "./SoundProvider";
|
|
3
|
+
|
|
4
|
+
export const useSoundToast = () => {
|
|
5
|
+
const { play } = useSound();
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
success: (msg) => {
|
|
9
|
+
play("success");
|
|
10
|
+
toast.success(msg);
|
|
11
|
+
},
|
|
12
|
+
error: (msg) => {
|
|
13
|
+
play("error");
|
|
14
|
+
toast.error(msg);
|
|
15
|
+
},
|
|
16
|
+
info: (msg) => {
|
|
17
|
+
play("info");
|
|
18
|
+
toast.info(msg);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React, { createContext, useContext, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
const SoundContext = createContext();
|
|
4
|
+
|
|
5
|
+
export const SoundProvider = ({ sounds = {}, volume = 1, children }) => {
|
|
6
|
+
const audioMap = useRef({});
|
|
7
|
+
|
|
8
|
+
const play = (name) => {
|
|
9
|
+
if (!sounds[name]) return;
|
|
10
|
+
|
|
11
|
+
if (!audioMap.current[name]) {
|
|
12
|
+
audioMap.current[name] = new Audio(sounds[name]);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
audioMap.current[name].volume = volume;
|
|
16
|
+
audioMap.current[name].currentTime = 0;
|
|
17
|
+
audioMap.current[name].play().catch(() => {});
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<SoundContext.Provider value={{ play }}>
|
|
22
|
+
{children}
|
|
23
|
+
</SoundContext.Provider>
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const useSound = () => useContext(SoundContext);
|
package/src/index.js
ADDED
package/src/useSound.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useSound } from "./SoundProvider";
|