Documentation Index
Fetch the complete documentation index at: https://mintlify.com/kokonut-labs/kokonutui/llms.txt
Use this file to discover all available pages before exploring further.
A React hook that detects whether the current viewport is mobile-sized (below 768px width) using the window matchMedia API.
Installation
This hook can be installed via the CLI:- Tab Title
- Tab Title
- Tab Title
bunx --bun shadcn@latest add @kokonutui/use-mobile
/hooks/use-mobile.ts into your project.
Usage
API Reference
Parameters
This hook accepts no parameters.Return Value
Returns aboolean:
trueif viewport width is less than 768pxfalseif viewport width is 768px or greater
Features
- Responsive detection - Automatically updates on window resize
- Performance optimized - Uses native matchMedia API
- SSR safe - Returns
falseduring server-side rendering - Standard breakpoint - Uses 768px (common Tailwind
md:breakpoint)
Example: Conditional Rendering
Example: Different Layouts
Example: Modal Behavior
Use Cases
- Conditional component rendering for mobile/desktop
- Responsive navigation switching
- Different modal/drawer behaviors
- Touch vs. hover interaction patterns
- Layout adjustments beyond CSS media queries
- Feature toggling based on device size
Breakpoint
The hook uses a breakpoint of 768px, which corresponds to:- Tailwind CSS
md:breakpoint - Bootstrap
mdbreakpoint - iPad portrait width
Performance
- Uses
window.matchMedia()which is more efficient than resize event listeners - Automatically subscribes/unsubscribes to media query changes
- No polling or repeated calculations
- Minimal re-renders
SSR Considerations
- Returns
falseon initial server-side render - Hydrates with correct value on client
- Consider using CSS media queries for critical above-the-fold content to avoid layout shifts
Notes
- The hook starts with
undefinedstate and updates after first render - Returns
falseifwindowis not available (SSR) - Updates automatically on window resize
- Event listeners are properly cleaned up on unmount