@npmteam2024/provident-placeat-repudiandae v1.0.1
React-Hooks
A collection of React Hooks.
tl;dr
- Install by executing
npm install @npmteam2024/provident-placeat-repudiandaeoryarn add @npmteam2024/provident-placeat-repudiandae. - Import by adding
import { useTick } from '@npmteam2024/provident-placeat-repudiandae'. - Do stuff with it!
const tick = useTick();
Server-side rendering
All hooks from this package support SSR. Some hooks use browser-only APIs, e.g. useCurrentPosition. Such hooks, if they are supposed to return a value, will return null instead.
User guide
Table of contents
useCurrentPositionuseDebouncedEffectuseDebouncedStateuseDebouncedValueuseEventListeneruseIntersectionObserveruseLocalStorageuseMatchMediauseMutationObserveruseOnLineusePermissionStateusePrefersColorSchemeDarkusePrefersColorSchemeLightusePrefersReducedMotionusePrefersReducedTransparencyuseResizeObserveruseScrollLeftuseScrollLeftPercentuseScrollTopuseScrollTopPercentuseSetIntervaluseSetTimeoutuseTickuseToggleuseWindowHeightuseWindowWidth
useCurrentPosition
Returns current position from Geolocation API.
Sample usage
import { useCurrentPosition } from '@npmteam2024/provident-placeat-repudiandae';
useCurrentPosition(); // { latitude: 0, longitude: 0 }useDebouncedEffect
Runs a given effect after a given delay.
Sample usage
import { useDebouncedEffect } from '@npmteam2024/provident-placeat-repudiandae';
useDebouncedEffect(
() => {
// This will run after 1 second of value not changing.
},
[value],
1000,
);useDebouncedState
Returns a debounced state and a function to update it.
Sample usage
import { useDebouncedState } from '@npmteam2024/provident-placeat-repudiandae';
const [value, setValue] = useDebouncedState('initialState', 1000); // ['initialState', Function]useDebouncedValue
Returns a debounced value.
Sample usage
import { useDebouncedValue } from '@npmteam2024/provident-placeat-repudiandae';
const debouncedValue = useDebouncedValue(value, 1000); // This value will be updated after 1 second of value not changing.useEventListener
Adds event listener to a given element.
Sample usage
import { useEventListener } from '@npmteam2024/provident-placeat-repudiandae';
useEventListener(element, 'click', onClick);useIntersectionObserver
Observes a given element using IntersectionObserver.
Sample usage
import { useIntersectionObserver } from '@npmteam2024/provident-placeat-repudiandae';
useIntersectionObserver(element, config, onIntersectionChange);useLocalStorage
Returns a stateful value synchronized with localStorage, and a function to update it.
Sample usage
import { useLocalStorage } from '@npmteam2024/provident-placeat-repudiandae';
const [value, setValue] = useLocalStorage('myKey', 'initialState'); // ['initialState', Function]useMatchMedia
Returns a flag which determines if the document matches the given media query string.
Sample usage
import { useMatchMedia } from '@npmteam2024/provident-placeat-repudiandae';
const isDesktop = useMatchMedia('screen and (min-width: 1024px)'); // true / falseuseMutationObserver
Observes a given element using MutationObserver.
Sample usage
import { useMutationObserver } from '@npmteam2024/provident-placeat-repudiandae';
useMutationObserver(element, config, onMutation);useOnLine
Returns the online status of the browser.
Sample usage
import { useOnLine } from '@npmteam2024/provident-placeat-repudiandae';
const onLine = useOnLine(); // trueusePermissionState
Returns permission state given permission name.
Sample usage
import { usePermissionState } from '@npmteam2024/provident-placeat-repudiandae';
const state = usePermissionState({ name: 'geolocation' }); // 'granted'usePrefersColorSchemeDark
Returns a flag which determines if the document matches (prefers-color-scheme: dark) media feature.
Sample usage
import { usePrefersColorSchemeDark } from '@npmteam2024/provident-placeat-repudiandae';
const prefersColorSchemeDark = usePrefersColorSchemeDark(); // trueusePrefersColorSchemeLight
Returns a flag which determines if the document matches (prefers-color-scheme: light) media feature.
Sample usage
import { usePrefersColorSchemeLight } from '@npmteam2024/provident-placeat-repudiandae';
const prefersColorSchemeLight = usePrefersColorSchemeLight(); // trueusePrefersReducedMotion
Returns a flag which determines if the document matches (prefers-reduced-motion: reduce) media feature.
Sample usage
import { usePrefersReducedMotion } from '@npmteam2024/provident-placeat-repudiandae';
const prefersReducedMotion = usePrefersReducedMotion(); // trueusePrefersReducedTransparency
Returns a flag which determines if the document matches (prefers-reduced-transparency: reduce) media feature.
Sample usage
import { usePrefersReducedTransparency } from '@npmteam2024/provident-placeat-repudiandae';
const prefersReducedTransparency = usePrefersReducedTransparency(); // trueuseResizeObserver
Observes a given element using ResizeObserver.
Sample usage
import { useResizeObserver } from '@npmteam2024/provident-placeat-repudiandae';
useResizeObserver(element, config, onResize);useScrollLeft
Returns current scroll left position in pixels.
Sample usage
import { useScrollLeft } from '@npmteam2024/provident-placeat-repudiandae';
const scrollLeft = useScrollLeft(); // 0useScrollLeftPercent
Returns current scroll left position in percentage.
Sample usage
import { useScrollLeftPercent } from '@npmteam2024/provident-placeat-repudiandae';
const scrollLeftPercent = useScrollLeftPercent(); // 0.5useScrollTop
Returns current scroll top position in pixels.
Sample usage
import { useScrollTop } from '@npmteam2024/provident-placeat-repudiandae';
const scrollTop = useScrollTop(); // 0useScrollTopPercent
Returns current scroll top position in percentage.
Sample usage
import { useScrollTopPercent } from '@npmteam2024/provident-placeat-repudiandae';
const scrollTopPercent = useScrollTopPercent(); // 0.5useSetInterval
Runs a given function every n milliseconds.
Sample usage
import { useSetInterval } from '@npmteam2024/provident-placeat-repudiandae';
useSetInterval(fn, 1000);useSetTimeout
Runs a given function after n milliseconds.
Sample usage
import { useSetTimeout } from '@npmteam2024/provident-placeat-repudiandae';
useSetTimeout(fn, 1000);useTick
Counts from 0, increasing the number returned every n milliseconds.
Sample usage
import { useTick } from '@npmteam2024/provident-placeat-repudiandae';
const tick = useTick(1000); // 0 … 🕐 … 1 … 🕑 … 2 …useToggle
Returns a flag and a function to toggle it.
Sample usage
import { useToggle } from '@npmteam2024/provident-placeat-repudiandae';
const [value, toggleValue] = useToggle(); // [false, Function]useWindowHeight
Returns the interior height of the window in pixels.
Sample usage
import { useWindowHeight } from '@npmteam2024/provident-placeat-repudiandae';
const windowWidth = useWindowHeight(); // 900useWindowWidth
Returns the interior width of the window in pixels.
Sample usage
import { useWindowWidth } from '@npmteam2024/provident-placeat-repudiandae';
const windowWidth = useWindowWidth(); // 1440License
The MIT License.