use-read-aloud 0.1.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/LICENSE +21 -0
- package/README.md +98 -0
- package/dist/index.d.ts +2 -0
- package/dist/use-read-aloud.cjs.js +1 -0
- package/dist/use-read-aloud.es.js +55 -0
- package/dist/useReadAloud.d.ts +13 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Siva Murugan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# use-read-aloud
|
|
2
|
+
|
|
3
|
+
A lightweight React hook that adds text-to-speech (read aloud) functionality using the Web Speech API.
|
|
4
|
+
|
|
5
|
+
This hook is designed for blogs, articles, and reading-focused applications.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install use-read-aloud
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Peer dependency
|
|
14
|
+
|
|
15
|
+
- react >= 18
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { useReadAloud } from "use-read-aloud";
|
|
21
|
+
|
|
22
|
+
function AudioPlayer({ text }: { text: string }) {
|
|
23
|
+
const { isReading, isPaused, toggle, start, reset } = useReadAloud(
|
|
24
|
+
() => text,
|
|
25
|
+
{ rate: 1 }
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<div>
|
|
30
|
+
<button onClick={toggle}>
|
|
31
|
+
{isReading && !isPaused ? "Pause" : "Play"}
|
|
32
|
+
</button>
|
|
33
|
+
|
|
34
|
+
{isReading && <button onClick={start}>Restart</button>}
|
|
35
|
+
<button onClick={reset}>Stop</button>
|
|
36
|
+
</div>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
useReadAloud(getText, options?)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Parameters
|
|
48
|
+
|
|
49
|
+
- getText - A function that returns the text to read.
|
|
50
|
+
|
|
51
|
+
- options
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
type Options = {
|
|
55
|
+
rate?: number; // default: 1
|
|
56
|
+
pitch?: number; // default: 1
|
|
57
|
+
};
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Returns
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
{
|
|
64
|
+
isReading: boolean;
|
|
65
|
+
isPaused: boolean;
|
|
66
|
+
start: () => void;
|
|
67
|
+
toggle: () => void;
|
|
68
|
+
reset: () => void;
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Behavior
|
|
73
|
+
|
|
74
|
+
- Long text is automatically split into smaller chunks for reliability
|
|
75
|
+
|
|
76
|
+
- Speech is cancelled automatically when the component unmounts
|
|
77
|
+
|
|
78
|
+
- Pause and resume are handled safely using internal session tracking
|
|
79
|
+
|
|
80
|
+
## Browser support
|
|
81
|
+
|
|
82
|
+
This hook uses the Web Speech API.
|
|
83
|
+
|
|
84
|
+
Supported:
|
|
85
|
+
|
|
86
|
+
- Chrome
|
|
87
|
+
|
|
88
|
+
- Edge
|
|
89
|
+
|
|
90
|
+
- Safari
|
|
91
|
+
|
|
92
|
+
Not supported:
|
|
93
|
+
|
|
94
|
+
- Firefox
|
|
95
|
+
|
|
96
|
+
## License
|
|
97
|
+
|
|
98
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react"),A=220;function I(o){const c=[];let s="";for(const n of o.split("."))(s+n+".").length>A?(c.push(s.trim()),s=n+"."):s+=n+".";return s.trim()&&c.push(s.trim()),c}function M(o,c={}){const{rate:s=1,pitch:n=1}=c,[d,k]=e.useState(!1),[b,f]=e.useState(!1),u=e.useRef(0),h=e.useRef([]),a=e.useRef(0),C=e.useCallback(()=>{h.current=[],a.current=0},[]),R=e.useCallback(()=>{const l=o();if(!l.trim())return;const t=l.split(/\n+/).map(y=>y.trim()).filter(Boolean);h.current=t.flatMap(I),a.current=0},[o]),p=e.useCallback(()=>{k(!1),f(!1)},[]),r=e.useCallback(()=>{speechSynthesis.cancel(),C(),p()},[C,p]),i=e.useCallback(()=>{const l=u.current;if(a.current>=h.current.length){p();return}const t=new SpeechSynthesisUtterance(h.current[a.current]);t.rate=s,t.pitch=n,t.onend=()=>{l===u.current&&(a.current+=1,i())},t.onerror=()=>{l===u.current&&r()},speechSynthesis.speak(t)},[p,r,n,s]),S=e.useCallback(()=>{r(),u.current+=1,k(!0),f(!1),R(),i()},[R,i]),g=e.useCallback(()=>{f(!0),speechSynthesis.pause()},[]),m=e.useCallback(()=>{f(!1),u.current+=1,speechSynthesis.cancel(),i()},[i]);return e.useEffect(()=>()=>r(),[r]),{isReading:d,isPaused:b,start:S,reset:r,pause:g,resume:m,toggle:d?b?m:g:S}}exports.useReadAloud=M;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useState as A, useRef as d, useCallback as t, useEffect as w } from "react";
|
|
2
|
+
const M = 220;
|
|
3
|
+
function P(f) {
|
|
4
|
+
const c = [];
|
|
5
|
+
let e = "";
|
|
6
|
+
for (const n of f.split("."))
|
|
7
|
+
(e + n + ".").length > M ? (c.push(e.trim()), e = n + ".") : e += n + ".";
|
|
8
|
+
return e.trim() && c.push(e.trim()), c;
|
|
9
|
+
}
|
|
10
|
+
function q(f, c = {}) {
|
|
11
|
+
const { rate: e = 1, pitch: n = 1 } = c, [m, R] = A(!1), [S, l] = A(!1), u = d(0), p = d([]), i = d(0), g = t(() => {
|
|
12
|
+
p.current = [], i.current = 0;
|
|
13
|
+
}, []), k = t(() => {
|
|
14
|
+
const a = f();
|
|
15
|
+
if (!a.trim()) return;
|
|
16
|
+
const s = a.split(/\n+/).map((b) => b.trim()).filter(Boolean);
|
|
17
|
+
p.current = s.flatMap(P), i.current = 0;
|
|
18
|
+
}, [f]), h = t(() => {
|
|
19
|
+
R(!1), l(!1);
|
|
20
|
+
}, []), r = t(() => {
|
|
21
|
+
speechSynthesis.cancel(), g(), h();
|
|
22
|
+
}, [g, h]), o = t(() => {
|
|
23
|
+
const a = u.current;
|
|
24
|
+
if (i.current >= p.current.length) {
|
|
25
|
+
h();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const s = new SpeechSynthesisUtterance(
|
|
29
|
+
p.current[i.current]
|
|
30
|
+
);
|
|
31
|
+
s.rate = e, s.pitch = n, s.onend = () => {
|
|
32
|
+
a === u.current && (i.current += 1, o());
|
|
33
|
+
}, s.onerror = () => {
|
|
34
|
+
a === u.current && r();
|
|
35
|
+
}, speechSynthesis.speak(s);
|
|
36
|
+
}, [h, r, n, e]), y = t(() => {
|
|
37
|
+
r(), u.current += 1, R(!0), l(!1), k(), o();
|
|
38
|
+
}, [k, o]), C = t(() => {
|
|
39
|
+
l(!0), speechSynthesis.pause();
|
|
40
|
+
}, []), I = t(() => {
|
|
41
|
+
l(!1), u.current += 1, speechSynthesis.cancel(), o();
|
|
42
|
+
}, [o]);
|
|
43
|
+
return w(() => () => r(), [r]), {
|
|
44
|
+
isReading: m,
|
|
45
|
+
isPaused: S,
|
|
46
|
+
start: y,
|
|
47
|
+
reset: r,
|
|
48
|
+
pause: C,
|
|
49
|
+
resume: I,
|
|
50
|
+
toggle: m ? S ? I : C : y
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
q as useReadAloud
|
|
55
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type Options = {
|
|
2
|
+
rate?: number;
|
|
3
|
+
pitch?: number;
|
|
4
|
+
};
|
|
5
|
+
export declare function useReadAloud(getText: () => string, options?: Options): {
|
|
6
|
+
isReading: boolean;
|
|
7
|
+
isPaused: boolean;
|
|
8
|
+
start: () => void;
|
|
9
|
+
reset: () => void;
|
|
10
|
+
pause: () => void;
|
|
11
|
+
resume: () => void;
|
|
12
|
+
toggle: () => void;
|
|
13
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-read-aloud",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "A React hook for reading text aloud using the Web Speech API",
|
|
6
|
+
"author": "Siva Murugan",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"react",
|
|
10
|
+
"react-hook",
|
|
11
|
+
"text-to-speech",
|
|
12
|
+
"speech-synthesis",
|
|
13
|
+
"read-aloud",
|
|
14
|
+
"web-speech-api"
|
|
15
|
+
],
|
|
16
|
+
"main": "./dist/use-read-aloud.cjs.js",
|
|
17
|
+
"module": "./dist/use-read-aloud.es.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"dev": "vite",
|
|
24
|
+
"types": "tsc --emitDeclarationOnly",
|
|
25
|
+
"build": "vite build && tsc --emitDeclarationOnly",
|
|
26
|
+
"lint": "eslint ."
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"react": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@eslint/js": "^9.39.1",
|
|
33
|
+
"@types/node": "^24.10.1",
|
|
34
|
+
"@types/react": "^19.2.5",
|
|
35
|
+
"@types/react-dom": "^19.2.3",
|
|
36
|
+
"@vitejs/plugin-react": "^5.1.1",
|
|
37
|
+
"eslint": "^9.39.1",
|
|
38
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
39
|
+
"eslint-plugin-react-refresh": "^0.4.24",
|
|
40
|
+
"globals": "^16.5.0",
|
|
41
|
+
"typescript": "~5.9.3",
|
|
42
|
+
"typescript-eslint": "^8.46.4",
|
|
43
|
+
"vite": "^7.2.4"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/SivaMurugan17/use-read-aloud.git"
|
|
48
|
+
}
|
|
49
|
+
}
|