8.1.3 • Published 1 month ago

@leafygreen-ui/hooks v8.1.3

Weekly downloads
768
License
Apache-2.0
Repository
github
Last release
1 month ago

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

PropTypeDescriptionDefault
typeGlobal Event Handler, stringType of event to listen for.
eventCallbackfunctionCallback executed when event is triggered.
optionalobjectOptional third argument passed to function with implementation specifications.
optional.optionsAddEventListenerOptions excluding onceParameter to specify options passed to the eventListener. To enable the once option, see optional.enabled.
optional.enabledboolean, '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.dependenciesArrayArray to be passed to useEffect hook, such that the hook will only run if the array's values have changed.[enabled, type]
optional.elementDocument, HTMLElementThe 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

PropTypeDescriptionDefault
callbackfunctionCallback executed when EscapeKey is pressed.
optionalobjectOptional 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

PropTypeDescriptionDefault
targetHTMLElement, nullHTMLElement to subscribe to changes to.
optionsMutationObserverInitObject with information about what DOM changes to subscribe to. Docs here
callbackfunctionCallback function to execute inside of MutationObserver instance.
enabledbooleanDetermines 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

PropTypeDescriptionDefault
onPollfunctionCallback executed when poll interval occurs.
optional.intervalnumberWhat interval the onPoll should be called.30000
optional.immediatebooleanIf we immediately poll, if false we wait till first interval occurs.true
optional.enabledbooleanIs 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

PropTypeDescriptionDefault
forwardedRefOrRefsSettableRef, Array<SettableRef>, nullThe forwarded ref
initialValueTInitial 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

PropTypeDescriptionDefault
prefixstringOptional prefix to that genterates a ref name space
8.1.3

1 month ago

8.1.2

2 months ago

8.1.1

3 months ago

8.1.0

4 months ago

8.0.0-alpha.0

10 months ago

8.0.0-alpha.1

10 months ago

8.0.1

6 months ago

8.0.0

9 months ago

7.7.8

9 months ago

7.7.7

9 months ago

7.7.6

10 months ago

7.7.4-next.7

12 months ago

7.7.4-next.6

12 months ago

7.7.4-next.5

12 months ago

7.7.4-next.4

12 months ago

7.7.5

11 months ago

7.7.4

11 months ago

7.7.3

12 months ago

7.7.2

12 months ago

7.7.4-next.3

12 months ago

7.7.4-next.2

12 months ago

7.7.4-next.1

12 months ago

7.7.4-next.0

12 months ago

7.7.1

1 year ago

7.7.0

1 year ago

7.6.0

1 year ago

7.5.0

1 year ago

7.4.0

1 year ago

7.3.3

2 years ago

7.3.2

2 years ago

7.3.2-next.0

2 years ago

7.3.2-next.1

2 years ago

7.3.2-next.2

2 years ago

7.3.1

2 years ago

7.3.2-test.0

2 years ago

7.3.0

2 years ago

7.2.0

2 years ago

7.1.1

2 years ago

7.1.0

2 years ago

7.0.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.0.1

4 years ago

5.0.0

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago