Overview
OfflineTube provides several custom React hooks to simplify common tasks like theme management, toast notifications, and responsive design.Available Hooks
useTheme
Manages application theme (dark/light mode) with localStorage persistence. Location:/workspace/source/src/lib/theme.tsx:241
Returns
Usage
Features
- Automatic persistence: Saves theme preference to
localStoragewith key'yt-theme' - Dynamic styling: Injects CSS overrides for light mode at runtime
- HTML class management: Adds/removes
'dark'and'light'classes on<html>element - SSR-safe: Handles initial theme load from localStorage in
useEffect
Theme Provider Setup
Wrap your app withThemeProvider to enable the hook:
Implementation Details
The hook uses React Context under the hood and applies theme changes by:- Adding/removing CSS classes on the HTML element
- Injecting or removing a runtime
<style>tag with light mode overrides - Persisting the selection to localStorage
bg-[#0f0f0f]) to work without crashing the CSS parser in development.
useToast
Displays toast notifications for user feedback (success, error, info messages). Location:/workspace/source/src/hooks/use-toast.ts:174
Inspired by: react-hot-toast
Returns
Usage
Using with Sonner
OfflineTube also usessonner for toast notifications. The recommended approach is to use sonner directly:
Toast Options
Managing Toasts
Advanced: Update Toast
useIsMobile
Detects if the current viewport is mobile-sized (< 768px). Location:/workspace/source/src/hooks/use-mobile.ts:5
Returns
Usage
Conditional Rendering
Breakpoint
The hook uses a breakpoint of 768px, which corresponds to Tailwind’smd: breakpoint.
Features
- Reactive: Updates when window is resized
- SSR-safe: Returns
undefinedinitially, then resolves after mount - Performant: Uses
matchMediaAPI with event listeners - Cleanup: Automatically removes listeners on unmount
Advanced: Custom Breakpoint
If you need a different breakpoint, create your own hook:Best Practices
Theme Hook
- Always wrap with ThemeProvider: Ensure
ThemeProviderwraps your app root - Use theme-aware classes: The hook manages CSS classes, so use conditional classNames based on theme
- Avoid manual theme detection: Use the hook instead of checking
document.documentElement.classList
Toast Hook
- Prefer sonner: Use
sonnerlibrary directly for simpler toast notifications - Keep messages concise: Toast messages should be brief and actionable
- Use appropriate variants:
defaultfor success/info,destructivefor errors - Dismiss programmatically: Store toast IDs if you need to dismiss them later
Mobile Hook
- Combine with Tailwind: Use for JS logic, but prefer Tailwind responsive classes for styling
- Handle undefined state: The initial value is
undefinedduring SSR - Avoid overuse: Use CSS media queries when possible for better performance
Hook Composition Example
Combine multiple hooks for complex components:Creating Custom Hooks
Follow these patterns when creating new hooks:Guidelines
- Prefix with
use: All hooks must start withuse - Return consistent types: Avoid changing return type based on conditions
- Document return values: Use JSDoc or TypeScript interfaces
- Handle cleanup: Always remove event listeners and clear timers
- Be mindful of dependencies: Include all dependencies in
useEffectarrays