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

@leafygreen-ui/hooks

v8.1.3

Published

LeafyGreen UI Kit Custom Hooks

Downloads

383,390

Readme

Hooks

npm (scoped)

Installation

Yarn

yarn add @leafygreen-ui/hooks

NPM

npm install @leafygreen-ui/hooks

useEventListener

Hook to create and remove eventListeners

Example

import { useEventListener } from '@leafygreen-ui/hooks';

useEventListener('click', handleClick, { enabled });

Properties

| Prop | Type | Description | Default | | ----------------------- | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------- | | type | Global Event Handler, string | Type of event to listen for. | | | eventCallback | function | Callback executed when event is triggered. | | | optional | object | Optional third argument passed to function with implementation specifications. | | | optional.options | AddEventListenerOptions excluding once | Parameter to specify options passed to the eventListener. To enable the once option, see optional.enabled. | | | optional.enabled | boolean, 'once' | Determines whether the event handler is attached or not. Setting this to "once" will ensure the event handler will be detached after the initial time an event is triggered. | true | | optional.dependencies | Array | Array to be passed to useEffect hook, such that the hook will only run if the array's values have changed. | [enabled, type] | | optional.element | Document, HTMLElement | The DOM node to attach the event handler to. Defaults to document. | document |

useEscapeKey

Hook that listens for EscapeKey press.

Example

import { useEscapeKey } from '@leafygreen-ui/hooks';

useEscapeKey(handleEscapeCallback);

Properties

| Prop | Type | Description | Default | | ---------- | ---------- | ---------------------------------------------------------------------------------------------------------------------- | ------- | | callback | function | Callback executed when EscapeKey is pressed. | | | optional | object | Optional argument passed to function with implementation specifications. See supported parameters for useEventHandler. | |

useMutationObserver

Example

import { useMutationObserver } from '@leafygreen-ui/hooks';

const lastTimeContentElMutated = useMutationObserver(
  target,
  mutationOptions,
  () => Date.now(),
  adjustOnMutation,
);

Properties

| Prop | Type | Description | Default | | ---------- | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | | target | HTMLElement, null | HTMLElement to subscribe to changes to. | | | options | MutationObserverInit | Object with information about what DOM changes to subscribe to. Docs here | | | callback | function | Callback function to execute inside of MutationObserver instance. | | | enabled | boolean | Determines whether the event handler is attached or not. | true |

useViewportSize

Hook to subscribe to changes in viewport size

Example

const viewportSize = useViewportSize();

usePoller

Hook to create a Poller that polls at a given interval.

If your onPoll handler returns a Promise it will wait for the Promise to resolve or reject before scheduling the next interval.

This hooks also makes use of the Page Visibility API. If the page is hidden then polling will stop. When a page becomes visible again then polling will resume.

Example

import { usePoller } from '@leafygreen-ui/hooks';

usePoller(onPoll, {
  interval: 30000,
  immediate: true,
  enabled: true,
});

Properties

| Prop | Type | Description | Default | | -------------------- | ---------- | -------------------------------------------------------------------- | ------- | | onPoll | function | Callback executed when poll interval occurs. | | | optional.interval | number | What interval the onPoll should be called. | 30000 | | optional.immediate | boolean | If we immediately poll, if false we wait till first interval occurs. | true | | optional.enabled | boolean | Is polling enabled. | true |

usePrevious

Hook to retrieve a value from the previous render.

Example

import { usePrevious } from '@leafygreen-ui/hooks';

const Example = ({nextValue: number}) => {
  const value = usePrevious(nextValue);

  return <div>{value}</div>;
}

// First render
<Example nextValue={42} />  // will render an empty `div`

// Second render
<Example nextValue={2020} />  // will render "42"

// Third render
<Example nextValue={0} />  // will render "2020"

useIsomorphicLayoutEffect

Drop-in replacement for useLayoutEffect that does not produce warnings during server-side rendering. If your project uses the react-hooks/exhaustive-deps ESLint rule, it's recommended to add useIsomorphicLayoutEffect to the additionalHooks of the rule. Example:

  rules: {
    'react-hooks/exhaustive-deps': [
      'warn',
      {
        additionalHooks: '(useIsomorphicLayoutEffect)'
      }
    ]
  }

useIdAllocator

Generates an SSR-compatible unique id based on a prefix string and an optional idProp parameter.

Example

import { useIdAllocator } from '@leafygreen-ui/hooks';

useIdAllocator({ prefix: 'select-component', id: idProp });

useForwardedRef

Returns a MutableRefObject from the forwarded ref.

Example

import { useForwardedRef } from '@leafygreen-ui/hooks';

const MyComponent = React.forwardRef((props, forwardedRef) => {
  const ref = useForwawrdedRef(forwardedRef, null);
});

Properties

| Prop | Type | Description | Default | | -------------------- | ------------------------------------------- | ------------------------ | ------- | | forwardedRefOrRefs | SettableRef, Array<SettableRef>, null | The forwarded ref | | | initialValue | T | Initial value of the ref | |

useDynamicRefs

useDynamicRefs is a hook that allows you to create multiple refs for dynamically created elements. Use the prefix property to create multiple sets of dynamic refs.

Example

import { useDynamicRefs } from '@leafygreen-ui/hooks';

const getRef = useDynamicRefs();
...
return ['a','b','c'].map(x => {
  const divRef = getRef(x)
  return <div ref={divRef}>{x}</div>
})

Properties

| Prop | Type | Description | Default | | -------- | -------- | --------------------------------------------------- | ------- | | prefix | string | Optional prefix to that genterates a ref name space | |