@thibault.sh/hooks
v0.0.1
Published
React hooks for browser storage (localStorage and sessionStorage)
Downloads
54
Maintainers
Readme
@thibault.sh/hooks
A collection of performant React hooks for common use cases. Built with TypeScript.
Installation
npm install @thibaultsh/hooks
Features
State Management
useLocalStorageState
- Persist state in localStorageuseSessionStorageState
- Session storage state managementuseCookieState
- Cookie state managementuseQueryParamsState
- URL query parameters state
UI/Interaction
useClickOutside
- Detect clicks outside elementsuseHover
- Track element hover stateuseKeyPress
- Keyboard input handlinguseKeyCombo
- Keyboard shortcutsuseLongPress
- Long press detection
Layout/Viewport
useMediaQuery
- Responsive design helperuseWindowSize
- Window dimensions trackinguseScrollPosition
- Scroll position trackinguseContainerScroll
- Container scroll managementuseElementSize
- Element size observeruseIntersectionObserver
- Viewport intersectionuseResizeObserver
- Element resize detection
Utility
useAsync
- Async operation wrapperuseDebounce
- Value debouncinguseThrottle
- Action throttlinguseTimer
- Timer managementuseInterval
- Interval handlinguseEventListener
- Event subscription
Usage
import { useLocalStorageState, useMediaQuery } from '@thibault.sh/hooks';
function App() {
const [theme, setTheme] = useLocalStorageState<'light' | 'dark'>('theme', 'light');
const isMobile = useMediaQuery('(max-width: 768px)');
return (
<div>
<button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
Toggle theme
</button>
{isMobile && <div>Mobile view</div>}
</div>
);
}
Documentation
For detailed documentation, visit thibault.sh/hooks
License
MIT © Thibault Mathian