Getting Started
Components
- Animated Number
- Auto Submit
- Carousel
- Character Counter
- Chartjs
- Checkbox Select All
- Clipboard
- Color Picker
- Confirmation
- Content Loader
- Dialog
- Dropdown
- Glow
- Hotkey
- Lightbox
- Notification
- Password Visibility
- Places Autocomplete
- Popover
- Prefetch
- Rails Nested Form
- Read More
- Remote Rails
- Reveal Controller
- Scroll Progress
- Scroll Reveal
- Scroll To
- Sortable
- Sound
- Speech Recognition
- Textarea Autogrow
- Timeago
Components
Speech Recognition
A Stimulus controller that uses the Web Speech API to capture speech and fill an input.
Installation
Install the package
Terminal$ yarn add @stimulus-components/speech-recognitionRegister the controller in your application
app/javascript/controllers/index.jsimport { Application } from '@hotwired/stimulus' import SpeechRecognition from '@stimulus-components/speech-recognition' const application = Application.start() application.register('speech-recognition', SpeechRecognition)
This component uses the Web Speech API. Support and accuracy depend on the browser and may require a secure context (HTTPS).
Example
Speech Recognition
Usage
<div data-controller="speech-recognition">
<textarea data-speech-recognition-target="input" placeholder="Click Start and speak..."></textarea>
<button
type="button"
data-speech-recognition-target="startButton"
data-action="speech-recognition#start"
class="hidden"
>
Start
</button>
<button
type="button"
data-speech-recognition-target="stopButton"
data-action="speech-recognition#stop"
class="hidden"
>
Stop
</button>
<span data-speech-recognition-target="indicator" class="hidden">Recording...</span>
</div>
The controller shows it only when the browser supports the Web Speech API. When the API is not supported, the start button, stop button, and indicator stay hidden.
Configuration
| Attribute | Default | Description | Optional |
|---|---|---|---|
data-speech-recognition-hidden-class | hidden | CSS class toggled to show/hide start, stop, and indicator. | ✅ |
Recognition runs with continuous: true and interimResults: true.
The full transcript (all results joined) is written to the input on each result.
Extending Controller
You can use inheritance to extend the functionality of any Stimulus component:
import SpeechRecognitionController from "@stimulus-components/speech-recognition"
export default class extends SpeechRecognitionController {
connect() {
super.connect()
console.log("Speech recognition controller connected.")
}
}
This controller will automatically have access to targets defined in the parent class.
If you override the connect, disconnect or any other methods from the parent, you'll want to call super.method() to make sure the parent functionality is executed.