Vibration
The Vibration component provides an interface to the Vibration API, allowing your Progressive Web App to trigger haptic feedback on devices that support it. This creates tactile sensations that enhance user experience and provide physical feedback for actions.
This component is particularly useful for:
Gaming applications requiring haptic feedback
Notification and alert systems
Form validation feedback
Interactive buttons and controls
Accessibility features for users with visual impairments
Timers and alarm applications
Browser Support
The Vibration API is supported on most Android devices in Chrome, Firefox, and Edge. iOS devices (iPhone, iPad) do not support the Vibration API due to platform restrictions.
Usage
Basic Vibration
<div {{ stimulus_controller('@pwa/vibration') }}>
<button {{ stimulus_action('@pwa/vibration', 'vibrate', 'click', {pattern: [200]}) }}>
Short Vibration (200ms)
</button>
<button {{ stimulus_action('@pwa/vibration', 'vibrate', 'click', {pattern: [500]}) }}>
Long Vibration (500ms)
</button>
</div>
<script>
document.addEventListener('pwa--vibration:triggered', () => {
console.log('Vibration started');
});
</script>Vibration Patterns
Persistent Vibration with Interval
Form Validation Feedback
Gaming Controls
Timer/Alarm Application
Accessibility Features
Parameters
None
Actions
vibrate
Triggers a vibration pattern on the device.
Parameters:
pattern(Array of numbers): Vibration pattern in milliseconds. Odd indices are vibration durations, even indices are pause durations.Single value:
[200]- vibrate for 200msPattern:
[100, 50, 100]- vibrate 100ms, pause 50ms, vibrate 100ms
interval(number, optional): Interval in milliseconds to repeat the pattern. If specified, the vibration will repeat until stopped or the page is closed.
With interval for repeating vibration:
stop
Stops any ongoing vibration, including persistent vibrations started with an interval.
Targets
None
Events
pwa--vibration:triggered
Dispatched when a vibration pattern is triggered.
No payload
Example:
pwa--vibration:stopped
Dispatched when vibration is explicitly stopped (not when a pattern naturally completes).
No payload
Example:
Best Practices
Be conservative: Use vibration sparingly to avoid annoying users
Keep it short: Most vibration patterns should be under 500ms total
Provide visual feedback: Always accompany vibration with visual feedback
Check support: Detect if the API is available before relying on it
Respect battery: Excessive vibration drains battery quickly
User preferences: Consider providing an option to disable vibrations
Context-appropriate: Use different patterns for different types of feedback
Common Vibration Patterns
Here are some commonly used vibration patterns:
Device Considerations
Android
Full support in Chrome, Firefox, and Edge
Vibration strength controlled by system settings
Users can disable vibration in system settings
iOS (iPhone/iPad)
Not supported - iOS does not expose the Vibration API to web applications
System haptics are only available to native apps
Always provide visual alternatives
Desktop
Limited support - most laptops don't have vibration motors
Some gaming devices with haptic feedback may work
Not reliable for desktop PWAs
Battery Impact
Vibration uses power from both the motor and the processor. Consider these guidelines:
Short vibrations (< 100ms): Minimal impact
Medium vibrations (100-500ms): Moderate impact
Long vibrations (> 500ms): Significant impact
Repeating vibrations: High impact, avoid if possible
Testing Vibration
Complete Example: Interactive Notification System
Last updated
Was this helpful?