LeafyGreen UI Kit Custom Hooks
Last updated 2 months ago by brookescarlett .
Apache-2.0 · Original npm · Tarball · package.json
$ cnpm install @leafygreen-ui/hooks 
SYNC missed versions from official npm registry.


npm (scoped)



yarn add @leafygreen-ui/hooks


npm install @leafygreen-ui/hooks


Hook to create and remove eventListeners


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

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


Prop Type Description Default
type Global Event Handler or string Type of event to listen for.
eventCallback function Callback executed when event is triggered.
optional.options AddEventListenerOptions excluding once Parameter to specify options passed to the eventListener. To enable the once option, see optional.enabled.
optional.enabled boolean or "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 or HTMLElement The DOM node to attach the event handler to. Defaults to document. document


Hook that listens for EscapeKey press.


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



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.



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

const lastTimeContentElMutated = useMutationObserver(
  () => Date.now(),


Prop Type Description Default
target HTMLElement or 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


Hook to subscribe to changes in viewport size


const viewportSize = useViewportSize();


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.


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

usePoller(onPoll, {
  interval: 30000,
  immediate: true,
  enabled: true,


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


Hook to retrieve a value from the previous render.


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"


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': [
        additionalHooks: '(useIsomorphicLayoutEffect)'

Current Tags

  • 6.0.0                                ...           latest (2 months ago)

21 Versions

  • 6.0.0                                ...           2 months ago
  • 5.0.1                                ...           3 months ago
  • 5.0.0                                ...           3 months ago
  • 4.2.1                                ...           4 months ago
  • 4.2.0                                ...           5 months ago
  • 4.1.0                                ...           5 months ago
  • 4.0.1                                ...           5 months ago
  • 4.0.0                                ...           5 months ago
  • 3.0.0                                ...           6 months ago
  • 2.1.0                                ...           10 months ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.7                                ...           a year ago
  • 1.0.6                                ...           a year ago
  • 1.0.5                                ...           a year ago
  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago

Copyright 2014 - 2017 © taobao.org |