@woby/use
v0.0.26
Published
@woby/use
Maintainers
Readme
@woby/use
A collection of essential React hooks designed for both React and Voby environments. This library provides a set of utility hooks that help you build better applications with less code.
Installation
# Using npm
npm install @woby/use
# Using yarn
yarn add @woby/use
# Using pnpm
pnpm add @woby/useQuick Start
import { useToggle, useCounter, useLocalStorage } from '@woby/use';
function MyComponent() {
const [value, toggle] = useToggle(false);
const { count, increment, decrement } = useCounter(0);
const [name, setName] = useLocalStorage('name', '');
return (
<div>
<p>Toggle value: {() => $(value) ? 'ON' : 'OFF'}</p>
<button onClick={toggle}>Toggle</button>
<p>Count: {count}</p>
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
<input
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter your name"
/>
</div>
);
}Available Hooks
Core
use- Convert values to observables with optional cloning
State Management
useBoolean- Tracks state of a boolean valueuseCounter- Tracks numerical state with increment/decrement functionsuseToggle- Toggles between two valuesuseMap- Tracks state of a MapuseSet- Tracks state of a SetuseStep- Manages step navigationuseCountdown- Manages countdown timers
Browser Utilities
useLocalStorage- Manages localStorage valuesuseSessionStorage- Manages sessionStorage valuesuseReadLocalStorage- Reads localStorage valuesuseWindowSize- Tracks window size changesuseAspect- Calculates window aspect ratiouseViewportSize- Accesses visual viewport propertiesuseDarkMode- Tracks and toggles dark mode stateuseTernaryDarkMode- Advanced dark mode managementuseMediaQuery- Tracks media query matchesuseCopyToClipboard- Copies text to clipboarduseDocumentTitle- Sets the document titleuseLockedBody- Locks body scrollinguseElementSize- Tracks element sizeuseIntersectionObserver- Observes element intersectionsuseImageOnLoad- Handles image loadinguseScreen- Accesses screen informationuseGpsLocation- Accesses GPS locationuseLocation- Accesses browser locationuseScreenOrientation- Accesses screen orientationuseSelection- Accesses text selection
Events
useEventListener- Subscribes to eventsuseClickAnyWhere- Subscribes a callback to clicks anywhere on the pageuseClickAway- Detects clicks outside an elementuseOnClickOutside- Detects clicks outside an element
Effects
useDebounce- Debounces a value or functionuseTimeout- Sets up a timeout that runs a callbackuseInterval- Sets up an interval that runs a callbackuseIsomorphicLayoutEffect- useLayoutEffect in browser, useEffect in serveruseEffectOnce- Runs an effect only onceuseUpdateEffect- Runs an effect on updates only
Utility
useFetch- Fetches data from a URLuseScript- Loads external scriptsuseHover- Tracks hover state of an elementuseDestruct- Destructures objects and arraysuseEventCallback- Creates stable event callbacksuseId- Generates unique IDsuseInvert- Inverts boolean valuesusePause- Creates timed delaysuseTimer- Creates timersuseTry- Executes functions with error handling
Environment
useSsr- Handles server-side renderinguseIsClient- Checks if running on clientuseIsFirstRender- Checks if it's the first renderuseIsMounted- Checks if component is mounted
Documentation
Check out our full documentation for detailed information about each hook:
Development
# Clone the repository
git clone https://github.com/wobyjs/use
cd @woby/use
# Install dependencies
pnpm install
# Start development server
pnpm dev
# Build for production
pnpm build:web
# Run tests
pnpm test
# Serve documentation
pnpm docsFramework Compatibility
The hooks in this library are designed to work with:
- React (16.8+)
- Voby (with appropriate JSX configuration)
TypeScript Support
All hooks come with full TypeScript support and type definitions included.
Contributing
Contributions are welcome! Please read our contributing guide to get started.
License
MIT
