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

@gunnarx2/hooks

v4.0.4

Published

Collection of React hooks

Downloads

2

Readme

@gunnarx2/hooks

npm version npm downloads npm bundle size npm license lerna

Collection of React hooks. Every hook supports TypeScript and Server-Side Rendering.

Installation

yarn add @gunnarx2/hooks

Usage

Event listener

Example - Event listener

import React from 'react';
import { useEventListener } from '@gunnarx2/hooks';

const Component = () => {
  const ref = useRef(null);

  useEventListener({
    type: 'click',
    listener: (event) => console.log(event),
    element: ref,
    options: { passive: true }
  });

  return <div ref={ref} />;
};

export default Component;

Reference - Event listener

Default value for element is window if isSSR returns false.

interface UseEventListener {
  type: keyof WindowEventMap;
  listener: EventListener;
  element?: RefObject<Element> | Document | Window | null;
  options?: AddEventListenerOptions;
}

const useEventListener = ({
  type,
  listener,
  element = isSSR ? undefined : window,
  options
}: UseEventListener): void => {};

Resize

Example - Resize

import React from 'react';
import { useResize } from '@gunnarx2/hooks';

const Component = () => {
  useResize((event) => {
    console.log(event);
  }, 666);

  return null;
};

export default Component;

Reference - Resize

It uses useEventListener() to listen for resize. Will trigger callback on window resize. Passed wait parameter will debounce the callback parameter.

const useResize = (
  callback: (event: Event) => void,
  wait: number = 250
): void => {};

Window size

Example - Window size

import React, { useEffect } from 'react';
import { useWindowSize } from '@gunnarx2/hooks';

const Component = () => {
  const { width, height } = useWindowSize(1337);

  useEffect(() => {
    console.log(width, height);
  }, [width, height]);

  return null;
};

export default Component;

Reference - Window size

It uses useResize() to listen for resize. Passed wait parameter will debounce the callback parameter.

const useWindowSize = (
  wait: number = 250
): {
  width?: number;
  height?: number;
} => {};

Click outside

Example - Click outside

import React, { useRef } from 'react';
import { useClickOutside } from '@gunnarx2/hooks';

const Component = () => {
  const ref = useRef(null);

  useClickOutside(ref, (event) => {
    console.log(event);
  });

  return <div ref={ref} />;
};

export default Component;

Reference - Click outside

It uses useEventListener() to listen for click.

const useClickOutside = (
  element: RefObject<Element> | null,
  callback: (event: MouseEvent) => void
): void => {};

Scroll

Example - Scroll

import React, { useEffect, useRef } from 'react';
import { useScroll } from '@gunnarx2/hooks';

const Component = () => {
  const ref = useRef(null);
  const { y, x, direction } = useScroll({
    wait: 420,
    element: ref
  });

  useEffect(() => {
    console.log(y, x, direction);
  }, [y, x, direction]);

  return <div ref={ref} />;
};

export default Component;

Reference - Scroll

It uses useEventListener() to listen for scroll, so default element is window. Passed wait parameter will throttle the callback parameter.

interface Scroll {
  y?: number;
  x?: number;
  direction?: 'up' | 'right' | 'down' | 'left';
}

interface UseScroll {
  wait?: number;
  element?: RefObject<Element> | Window | null;
}

export const useScroll = (options?: UseScroll): Scroll => {};

Trap focus

Example - Trap focus

import React, { useRef } from 'react';
import { useTrapFocus } from '@gunnarx2/hooks';

const Component = () => {
  const initialFocusRef = useRef(null);
  const trapRef = useTrapFocus({
    includeContainer: true,
    initialFocus: initialFocusRef.current,
    returnFocus: true,
    updateNodes: false
  });

  return (
    <div ref={trapRef} tabIndex={0}>
      Lorem ipsum{' '}
      <button type="button" ref={initialFocusRef}>
        dolor
      </button>
    </div>
  );
};

export default Component;

Reference - Trap focus

It uses useEventListener() to listen for keydown.

Include container

Include container in the tabbable nodes. In the example above it would result in trapRef.
Default: false.

Initial focus

Set node or 'container' as initial focus. For 'container' to work you need to set includeContainer: true.
Default: null.

Return focus

Return focus to the element that had focus before trapped. This will be executed when component unmounts.
Default: true.

Update nodes

Update tabbable nodes on each tab, can be useful if nodes is rendered dynamically in some way.
Default: false.

type Node = HTMLDivElement | null;

interface UseTrapFocus {
  includeContainer?: boolean;
  initialFocus?: 'container' | Node;
  returnFocus?: boolean;
  updateNodes?: boolean;
}

const useTrapFocus = (
  options?: UseTrapFocus
): MutableRefObject<Node> => {};