npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@front-utils/react-hooks

v0.1.4

Published

template

Downloads

199

Readme

Front-utils/react-hooks

Hooks

| name | description | | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | useAnimatedValue | The useAnimatedValue function is a custom React hook that animates a numeric value from 0 to a specified finishValue over a given time period. It uses the requestAnimationFrame API to update the value smoothly and returns the animated value. | | useAutoScroll | The useAutoScroll is a custom React hook that automatically scrolls a parent element to center a target child element within its view. It uses useRef to reference the parent and target elements, and useCallback to calculate and set the scroll position based on the target's offset and the parent's height. | | useCountDownTimer | The useCountDownTimer is a custom React hook that calculates the remaining time in seconds until a specified end date. It optionally formats the result into hours, minutes, and seconds. The hook also tracks if the countdown has been cancelled. | | useElementSizeObserver | The useElementSizeObserver is a custom React hook that observes the size of a DOM element and returns its dimensions. It uses the ResizeObserver API to detect changes in the element's size and updates the state accordingly. | | useFontLoader | The useFontLoader is a React hook that loads a list of fonts asynchronously. It ensures that fonts are loaded only once by checking a global flag and uses the FontFace API to add fonts to the document. | | useIsMounted | The code defines a custom React hook useIsMounted that returns a reference object indicating whether a component is currently mounted. | | useNativeResizeObserver | The code defines a custom React hook useNativeResizeObserver that utilizes the ResizeObserver API to observe changes in the size of a specified DOM element. | | useNodeState | The useNodeState function is a custom React hook that manages the state of an HTMLElement or null. It provides a way to store and update a reference to a DOM node. | | usePrevious | The usePrevious function is a custom React hook that stores the previous value of a given state or prop. It uses the useRef hook to persist the value across renders and the useEffect hook to update the stored value whenever the input value changes. | | usePropsChanged | The usePropsChanged function is a custom React hook that determines if a given value has changed since the last render. It uses another custom hook usePrevious to track the previous value and a useRef to count renders. | | useResizeObserver | The useResizeObserver is a custom React hook that observes changes in the size of an HTML element and provides the current size. It uses a ResizeObserver to detect size changes and updates the state with debounced size information to prevent excessive re-renders. | | useScrollObserver | The useScrollObserver is a custom React hook that uses the Intersection Observer API to trigger a fetchData function when a specified DOM element (observerTarget) becomes visible in the viewport. | | useStateBool | The useStateBool function is a custom React hook that manages a boolean state. It provides an initial state and two functions to set the state to true or false. |

Examples

useAnimatedValue
const animatedValue = useAnimatedValue(100, { time: 2000, digitAfterDot: 1 });
useAutoScroll
const { parentElementRef, userElementRef } = useAutoScroll({
  itemHeight: 50,
  scrollTrigger: true,
});

return (
  <div ref={parentElementRef} style={{ overflowY: 'auto', height: '200px' }}>
    <div ref={userElementRef}>Target Element</div>
  </div>
);
useCountDownTimer
const { seconds, minutes, hours, isCancelled } = useCountDownTimer({
    endIsoDate: "2023-12-31T23:59:59Z",
    formatted: true,
});
useElementSizeObserver
const MyComponent = () => {
    const ref = useRef<HTMLDivElement>(null);
    const size = useElementSizeObserver({ isActive: true, node: ref.current });

    return (
        <div ref={ref}>
            {size ? `Width: ${size.width}, Height: ${size.height}` : 'Loading...'}
        </div>
    );
};
useFontLoader
const fonts = [
  { name: 'Roboto', url: Promise.resolve({ default: '/fonts/roboto.woff2' }), descriptors: { weight: '400' } },
  { name: 'Open Sans', url: Promise.resolve({ default: '/fonts/open-sans.woff2' }), descriptors: { weight: '700' } }
];

useFontLoader(fonts);
useIsMounted
const isMountedRef = useIsMounted();

useEffect(() => {
    if (isMountedRef.current) {
        console.log("Component is mounted");
    }
}, []);
useNativeResizeObserver
const MyComponent = () => {
    const ref = useRef<HTMLDivElement | null>(null);

    useNativeResizeObserver({
        node: ref.current,
        callback: (entries) => {
            entries.forEach(entry => {
                console.log('Size changed:', entry.contentRect);
            });
        }
    });

    return <div ref={ref}>Resizable content</div>;
};
useNodeState
const { node, updateNode } = useNodeState();

// To update the node
const someElement = document.getElementById('some-id');
updateNode(someElement);

// To access the current node
console.log(node); // Outputs: <HTMLElement> or null
usePrevious
import { usePrevious } from './usePrevious';

function MyComponent({ someProp }) {
    const prevProp = usePrevious(someProp);

    useEffect(() => {
        console.log('Previous prop:', prevProp);
        console.log('Current prop:', someProp);
    }, [someProp]);

    return <div>{someProp}</div>;
}
usePropsChanged
const { isChanged, prevVal } = usePropsChanged(someValue);
if (isChanged) {
    console.log("Value has changed from", prevVal, "to", someValue);
}
useResizeObserver
const MyComponent = () => {
    const { setNode, size } = useResizeObserver<HTMLDivElement>({ lazy: false });

    return (
        <div ref={setNode}>
            {size && (
                <p>
                    Width: {size.offsetWidth}, Height: {size.clientHeight}
                </p>
            )}
        </div>
    );
};
useScrollObserver
const ref = useRef<HTMLDivElement | null>(null);

useScrollObserver({
    observerTarget: ref,
    fetchData: () => console.log("Element is visible"),
});

return <div ref={ref}>Watch me!</div>;
useStateBool
const [isTrue, setTrue, setFalse] = useStateBool(false);

console.log(isTrue); // false
setTrue();
console.log(isTrue); // true
setFalse();
console.log(isTrue); // false