react-native-voice-ts 1.0.0 → 1.0.1

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.
Files changed (41) hide show
  1. package/README.md +914 -83
  2. package/dist/components/MicIcon.d.ts +23 -0
  3. package/dist/components/MicIcon.d.ts.map +1 -0
  4. package/dist/components/MicIcon.js +28 -0
  5. package/dist/components/MicIcon.js.map +1 -0
  6. package/dist/components/VoiceMicrophone.d.ts +81 -0
  7. package/dist/components/VoiceMicrophone.d.ts.map +1 -0
  8. package/dist/components/VoiceMicrophone.js +151 -0
  9. package/dist/components/VoiceMicrophone.js.map +1 -0
  10. package/dist/components/index.d.ts +5 -0
  11. package/dist/components/index.d.ts.map +1 -0
  12. package/dist/components/index.js +3 -0
  13. package/dist/components/index.js.map +1 -0
  14. package/dist/hooks/index.d.ts +3 -0
  15. package/dist/hooks/index.d.ts.map +1 -0
  16. package/dist/hooks/index.js +2 -0
  17. package/dist/hooks/index.js.map +1 -0
  18. package/dist/hooks/useVoiceRecognition.d.ts +77 -0
  19. package/dist/hooks/useVoiceRecognition.d.ts.map +1 -0
  20. package/dist/hooks/useVoiceRecognition.js +129 -0
  21. package/dist/hooks/useVoiceRecognition.js.map +1 -0
  22. package/dist/index.d.ts +4 -0
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +4 -0
  25. package/dist/index.js.map +1 -1
  26. package/ios/Voice.xcodeproj/project.xcworkspace/xcuserdata/olumayowadaniel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  27. package/ios/Voice.xcodeproj/project.xcworkspace/xcuserdata/rudie_shahinian.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  28. package/package.json +24 -6
  29. package/plugin/src/withVoice.ts +74 -0
  30. package/plugin/tsconfig.json +10 -0
  31. package/src/components/MicIcon.tsx +72 -0
  32. package/src/components/VoiceMicrophone.tsx +238 -0
  33. package/src/components/index.ts +4 -0
  34. package/src/hooks/index.ts +5 -0
  35. package/src/hooks/useVoiceRecognition.ts +217 -0
  36. package/src/images/mic.svg +16 -0
  37. package/src/index.ts +15 -0
  38. package/.nvmrc +0 -1
  39. package/.prettierrc +0 -5
  40. package/.releaserc +0 -15
  41. package/MIGRATION_SUMMARY.md +0 -510
@@ -0,0 +1,217 @@
1
+ import { useEffect, useState, useCallback } from 'react';
2
+ import Voice from '../index';
3
+ import type { SpeechErrorEvent, SpeechResultsEvent } from '../VoiceModuleTypes';
4
+
5
+ export interface UseVoiceRecognitionOptions {
6
+ /**
7
+ * Language locale for speech recognition
8
+ * @default 'en-US'
9
+ */
10
+ locale?: string;
11
+
12
+ /**
13
+ * Whether to enable partial results (real-time transcription)
14
+ * @default true
15
+ */
16
+ enablePartialResults?: boolean;
17
+
18
+ /**
19
+ * Callback fired when speech is recognized
20
+ */
21
+ onResult?: (text: string) => void;
22
+
23
+ /**
24
+ * Callback fired when an error occurs
25
+ */
26
+ onError?: (error: string) => void;
27
+ }
28
+
29
+ export interface UseVoiceRecognitionReturn {
30
+ /**
31
+ * Whether voice recognition is currently active
32
+ */
33
+ isRecording: boolean;
34
+
35
+ /**
36
+ * Final recognized text results
37
+ */
38
+ results: string[];
39
+
40
+ /**
41
+ * Partial results (real-time transcription)
42
+ */
43
+ partialResults: string[];
44
+
45
+ /**
46
+ * Error message if an error occurred
47
+ */
48
+ error: string | null;
49
+
50
+ /**
51
+ * Start voice recognition
52
+ */
53
+ start: () => Promise<void>;
54
+
55
+ /**
56
+ * Stop voice recognition and get final results
57
+ */
58
+ stop: () => Promise<void>;
59
+
60
+ /**
61
+ * Cancel voice recognition without getting results
62
+ */
63
+ cancel: () => Promise<void>;
64
+
65
+ /**
66
+ * Reset all state
67
+ */
68
+ reset: () => void;
69
+ }
70
+
71
+ /**
72
+ * Custom hook for voice recognition
73
+ *
74
+ * Provides a simple interface for speech-to-text functionality with automatic
75
+ * event listener setup and cleanup.
76
+ *
77
+ * @example
78
+ * ```tsx
79
+ * const { isRecording, results, start, stop } = useVoiceRecognition({
80
+ * locale: 'en-US',
81
+ * onResult: (text) => setSearchQuery(text),
82
+ * });
83
+ *
84
+ * // In your component
85
+ * <Button
86
+ * onPress={isRecording ? stop : start}
87
+ * title={isRecording ? 'Stop' : 'Start Recording'}
88
+ * />
89
+ * <Text>{results[0]}</Text>
90
+ * ```
91
+ */
92
+ export const useVoiceRecognition = (
93
+ options: UseVoiceRecognitionOptions = {},
94
+ ): UseVoiceRecognitionReturn => {
95
+ const {
96
+ locale = 'en-US',
97
+ enablePartialResults = true,
98
+ onResult,
99
+ onError,
100
+ } = options;
101
+
102
+ const [isRecording, setIsRecording] = useState(false);
103
+ const [results, setResults] = useState<string[]>([]);
104
+ const [partialResults, setPartialResults] = useState<string[]>([]);
105
+ const [error, setError] = useState<string | null>(null);
106
+
107
+ useEffect(() => {
108
+ // Set up event listeners
109
+ Voice.onSpeechStart = () => {
110
+ setIsRecording(true);
111
+ setError(null);
112
+ };
113
+
114
+ Voice.onSpeechEnd = () => {
115
+ setIsRecording(false);
116
+ };
117
+
118
+ Voice.onSpeechError = (e: SpeechErrorEvent) => {
119
+ const errorMessage = e.error?.message || 'Unknown error';
120
+ setError(errorMessage);
121
+ setIsRecording(false);
122
+ onError?.(errorMessage);
123
+ };
124
+
125
+ Voice.onSpeechResults = (e: SpeechResultsEvent) => {
126
+ if (e.value && e.value.length > 0) {
127
+ setResults(e.value);
128
+ const firstResult = e.value[0];
129
+ if (firstResult) {
130
+ onResult?.(firstResult);
131
+ }
132
+ }
133
+ };
134
+
135
+ if (enablePartialResults) {
136
+ Voice.onSpeechPartialResults = (e: SpeechResultsEvent) => {
137
+ if (e.value && e.value.length > 0) {
138
+ setPartialResults(e.value);
139
+ }
140
+ };
141
+ }
142
+
143
+ // Cleanup
144
+ return () => {
145
+ Voice.destroy().then(Voice.removeAllListeners);
146
+ };
147
+ }, [enablePartialResults, onResult, onError]);
148
+
149
+ const start = useCallback(async () => {
150
+ try {
151
+ setError(null);
152
+ setResults([]);
153
+ setPartialResults([]);
154
+
155
+ // Check permission (Android only)
156
+ const hasPermission = await Voice.checkMicrophonePermission();
157
+ if (!hasPermission) {
158
+ const granted = await Voice.requestMicrophonePermission();
159
+ if (!granted) {
160
+ setError('Microphone permission denied');
161
+ return;
162
+ }
163
+ }
164
+
165
+ await Voice.start(locale, {
166
+ EXTRA_PARTIAL_RESULTS: enablePartialResults,
167
+ });
168
+ } catch (e) {
169
+ const errorMessage =
170
+ e instanceof Error ? e.message : 'Failed to start recording';
171
+ setError(errorMessage);
172
+ onError?.(errorMessage);
173
+ }
174
+ }, [locale, enablePartialResults, onError]);
175
+
176
+ const stop = useCallback(async () => {
177
+ try {
178
+ await Voice.stop();
179
+ } catch (e) {
180
+ const errorMessage =
181
+ e instanceof Error ? e.message : 'Failed to stop recording';
182
+ setError(errorMessage);
183
+ onError?.(errorMessage);
184
+ }
185
+ }, [onError]);
186
+
187
+ const cancel = useCallback(async () => {
188
+ try {
189
+ await Voice.cancel();
190
+ setResults([]);
191
+ setPartialResults([]);
192
+ } catch (e) {
193
+ const errorMessage =
194
+ e instanceof Error ? e.message : 'Failed to cancel recording';
195
+ setError(errorMessage);
196
+ onError?.(errorMessage);
197
+ }
198
+ }, [onError]);
199
+
200
+ const reset = useCallback(() => {
201
+ setResults([]);
202
+ setPartialResults([]);
203
+ setError(null);
204
+ setIsRecording(false);
205
+ }, []);
206
+
207
+ return {
208
+ isRecording,
209
+ results,
210
+ partialResults,
211
+ error,
212
+ start,
213
+ stop,
214
+ cancel,
215
+ reset,
216
+ };
217
+ };
@@ -0,0 +1,16 @@
1
+ <svg
2
+ xmlns="http://www.w3.org/2000/svg"
3
+ width="24"
4
+ height="24"
5
+ viewBox="0 0 24 24"
6
+ fill="none"
7
+ stroke="currentColor"
8
+ stroke-width="2"
9
+ stroke-linecap="round"
10
+ stroke-linejoin="round"
11
+ class="lucide lucide-mic-icon lucide-mic"
12
+ >
13
+ <path d="M12 19v3" />
14
+ <path d="M19 10v2a7 7 0 0 1-14 0v-2" />
15
+ <rect x="9" y="2" width="6" height="13" rx="3" />
16
+ </svg>;
package/src/index.ts CHANGED
@@ -497,4 +497,19 @@ export type {
497
497
  Language,
498
498
  } from './VoiceUtilTypes';
499
499
 
500
+ // Export components
501
+ export { VoiceMicrophone, MicIcon, MicOffIcon } from './components';
502
+ export type {
503
+ VoiceMicrophoneProps,
504
+ MicIconProps,
505
+ MicOffIconProps,
506
+ } from './components';
507
+
508
+ // Export hooks
509
+ export { useVoiceRecognition } from './hooks';
510
+ export type {
511
+ UseVoiceRecognitionOptions,
512
+ UseVoiceRecognitionReturn,
513
+ } from './hooks';
514
+
500
515
  export default new RCTVoice();
package/.nvmrc DELETED
@@ -1 +0,0 @@
1
- lts/fermium
package/.prettierrc DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "semi": true,
3
- "singleQuote": true,
4
- "trailingComma": "all"
5
- }
package/.releaserc DELETED
@@ -1,15 +0,0 @@
1
- {
2
- "plugins": [
3
- "@semantic-release/commit-analyzer",
4
- "@semantic-release/release-notes-generator",
5
- "@semantic-release/npm",
6
- "@semantic-release/github",
7
- [
8
- "@semantic-release/git",
9
- {
10
- "assets": "package.json",
11
- "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
12
- }
13
- ]
14
- ]
15
- }