Hooks Overview
Cloud UI provides a collection of React hooks for common UI patterns, state management, utilities, and lifecycle operations. These hooks are based on Mantine hooks with some custom enhancements.
Import
import { useDisclosure, useClipboard, useHotkeys } from '@tidbcloud/uikit'Hook Categories
DOM & UI
Hooks for interacting with the DOM and handling UI-related events:
useClickOutside- Detect clicks outside of elementuseSystemColorScheme- Detect user’s preferred color schemeuseElementSize- Get element width and heightuseEventListener- Add event listener to elementuseFocusReturn- Return focus to last focused elementuseFocusTrap- Trap focus within elementuseFocusWithin- Detect focus within elementuseFullscreen- Enter/exit fullscreen modeuseHotkeys- Listen for keyboard shortcutsuseHover- Detect hover stateuseInViewport- Check if element is in viewportuseIntersection- Observe element intersectionuseMediaQuery- Subscribe to media query changesuseMouse- Track mouse positionuseMove- Handle drag/move interactionsuseMutationObserver- Observe DOM mutationsuseOrientation- Track device orientationuseReducedMotion- Detect reduced motion preferenceuseResizeObserver- Observe element size changesuseScrollIntoView- Scroll element into viewuseScrollSpy- Track scroll position for navigationuseViewportSize- Get viewport dimensionsuseWindowEvent- Add window event listeneruseWindowScroll- Track window scroll position
State Management
Hooks for managing component state:
useCounter- Manage numeric counter stateuseDebouncedCallback- Create debounced callbackuseDebouncedState- Debounced state updatesuseDebouncedValue- Debounce value changesuseDisclosure- Manage boolean state (open/close)useId- Generate unique IDsuseInputState- Handle input state changesuseListState- Manage array stateuseLocalStorage- Persist state in localStorageuseMap- Manage Map stateusePagination- Manage pagination stateusePrevious- Get previous valueuseQueue- Manage queue stateuseSet- Manage Set stateuseSetState- Shallow merge state updatesuseStateHistory- Track state historyuseThrottledCallback- Create throttled callbackuseThrottledState- Throttled state updatesuseThrottledValue- Throttle value changesuseToggle- Toggle between valuesuseUncontrolled- Handle controlled/uncontrolled stateuseValidatedState- State with validation
Utilities
General utility hooks:
useClipboard- Copy to clipboard (enhanced with fallback)useDocumentTitle- Set document titleuseDocumentVisibility- Track document visibilityuseEyeDropper- Use EyeDropper APIuseFavicon- Set faviconuseFetch- Fetch datauseFileDialog- Open file dialoguseHash- Read/write URL hashuseHeadroom- Create headroom effectuseIdle- Detect user idle stateuseInterval- Run function at intervalsuseMergedRef- Merge multiple refsuseNetwork- Track network statususeOs- Detect operating systemusePageLeave- Detect page leaveuseTextSelection- Get text selectionuseTimeout- Run function after timeoutuseURLQueryState- Manage URL query state
Lifecycle
Hooks for component lifecycle:
useDidUpdate- Run effect only on updatesuseForceUpdate- Force component re-renderuseIsFirstRender- Check if first renderuseIsomorphicEffect- SSR-safe useLayoutEffectuseLogger- Log component lifecycleuseMounted- Check if component is mounteduseShallowEffect- Effect with shallow comparison