Snackbar
Brief, auto-dismissing notification messages that appear at the edge of the screen.
Import
import { Snackbar } from '@smart-tv/ui';
Basic Usage
import { useState } from 'react';
import { Snackbar, Button } from '@smart-tv/ui';
function App() {
const [open, setOpen] = useState(false);
const showSnackbar = () => {
setOpen(true);
};
return (
<>
<Button focusKey="show" onEnterPress={showSnackbar}>
Show Notification
</Button>
<Snackbar
open={open}
onClose={() => setOpen(false)}
message="Settings saved successfully!"
/>
</>
);
}
Auto-Hide Duration
Control how long the snackbar stays visible before auto-dismissing.
// Auto-hide after 3 seconds (default)
<Snackbar
open={open}
onClose={handleClose}
message="Quick notification"
duration={3000}
/>
// Auto-hide after 5 seconds
<Snackbar
open={open}
onClose={handleClose}
message="Longer notification"
duration={5000}
/>
// Don't auto-hide (null duration)
<Snackbar
open={open}
onClose={handleClose}
message="Stays until manually closed"
duration={null}
/>
Placement
Position the snackbar at different locations on screen.
// Bottom center (default)
<Snackbar
open={open}
placement="bottom-center"
message="Bottom center notification"
/>
// Bottom left
<Snackbar
open={open}
placement="bottom-left"
message="Bottom left notification"
/>
// Bottom right
<Snackbar
open={open}
placement="bottom-right"
message="Bottom right notification"
/>
// Top positions
<Snackbar
open={open}
placement="top-center"
message="Top center notification"
/>
<Snackbar
open={open}
placement="top-left"
message="Top left notification"
/>
<Snackbar
open={open}
placement="top-right"
message="Top right notification"
/>
With Action Button
Add an action button for user interaction.
<Snackbar
open={open}
onClose={handleClose}
message="Item deleted"
action={{
label: "Undo",
onClick: handleUndo,
focusKey: "undo-action"
}}
/>
// Action with custom styling
<Snackbar
open={open}
onClose={handleClose}
message="Download complete"
action={{
label: "Open",
onClick: handleOpen,
focusKey: "open-action",
className: "text-blue-400 font-semibold"
}}
/>
Pause on Hover/Focus
Prevent auto-hide when user interacts with the snackbar.
<Snackbar
open={open}
onClose={handleClose}
message="Pauses on interaction"
duration={5000}
pauseOnHover={true}
action={{
label: "Retry",
onClick: handleRetry,
focusKey: "retry-btn"
}}
/>
Variants
Different styles for different message types.
// Success message
<Snackbar
open={open}
message="Action completed successfully!"
variant="success"
className="bg-green-600 text-white"
/>
// Error message
<Snackbar
open={open}
message="Something went wrong"
variant="error"
className="bg-red-600 text-white"
/>
// Warning message
<Snackbar
open={open}
message="Please check your connection"
variant="warning"
className="bg-amber-600 text-white"
/>
// Info message
<Snackbar
open={open}
message="New features available"
variant="info"
className="bg-blue-600 text-white"
/>
Queue Management
Manage multiple notifications with a queue system.
import { useState } from 'react';
function App() {
const [snackbars, setSnackbars] = useState<Array<{
id: number;
message: string;
open: boolean;
}>>([]);
const showSnackbar = (message: string) => {
const id = Date.now();
setSnackbars(prev => [...prev, { id, message, open: true }]);
};
const closeSnackbar = (id: number) => {
setSnackbars(prev =>
prev.map(s => s.id === id ? { ...s, open: false } : s)
);
// Remove after animation
setTimeout(() => {
setSnackbars(prev => prev.filter(s => s.id !== id));
}, 300);
};
return (
<>
<Button onEnterPress={() => showSnackbar('First notification')}>
Show Notification
</Button>
{snackbars.map((snackbar, index) => (
<Snackbar
key={snackbar.id}
open={snackbar.open}
message={snackbar.message}
onClose={() => closeSnackbar(snackbar.id)}
style={{ bottom: `${(index * 70) + 20}px` }}
/>
))}
</>
);
}
Props
Prop | Type | Default | Description |
---|---|---|---|
open | boolean | required | Controls visibility |
message | string | ReactNode | required | Notification message |
onClose | () => void | required | Close handler |
duration | number | null | 3000 | Auto-hide duration (ms) |
placement | PlacementType | bottom-center | Screen position |
action | ActionConfig | - | Action button config |
pauseOnHover | boolean | false | Pause timer on hover |
variant | string | default | Visual variant |
className | string | - | CSS classes |
Action Config
Property | Type | Description |
---|---|---|
label | string | Button text |
onClick | () => void | Click handler |
focusKey | string | Focus key for button |
className | string | Button CSS classes |
Best Practices
- Keep messages brief and actionable
- Use 3-5 seconds duration for most notifications
- Provide action buttons for recoverable operations (Undo, Retry)
- Use appropriate variants/colors for different message types
- Enable pauseOnHover when snackbar has actions
- Position snackbars to not obscure important content
- Implement a queue system for multiple notifications