3.5.0 • Published 3 months ago

@highlight-ui/utils-hooks v3.5.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

@highlight-ui/utils-hooks

Installation

yarn add @highlight-ui/utils-hooks

Hooks


useAutoPositioner

This component is deprecated

A wrapper for the usePopper from the react-popper library.

Usage

Accepts a Popper.js options object as an argument and returns the styles and attributes values from the usePopper hook. For convenience, the hook also returns setReferenceElement and setPopperElement functions.

import { useAutoPositioner } from '@highlight-ui/utils-hooks';

const MyComponent = () => {
  const { setReferenceElement, setPopperElement, styles, attributes } =
    useAutoPositioner();

  return (
    <div ref={setReferenceElement}>
      <div
        ref={setPopperElement}
        style={{ ...styles.popper }}
        {...attributes.popper}
      ></div>
    </div>
  );
};

useAutoPosition

Calculating position of "floating" elements like tooltips, popovers, dropdowns, menus, and more.

Usage

Accepts a config as an argument and returns the styles, triggerRef and floatRef functions.

type Options = {
  // Position of the element to the trigger element. Default: bottom-start
  placement: Placement;
  // Automatic position based on the space around the trigger. This overrides flip and placement options. Default: Disabled
  auto: AutoConfig | null | false;
  // Flip position if there is not enough space on the placement area: Default: Enabled
  flip: FlipConfig | null | false;
  // Offset from the trigger element: Default: Enabled
  offset: OffsetConfig | null | false;
};
import { useAutoPosition } from '@highlight-ui/utils-hooks';

const MyComponent = () => {
  const { triggerRef, floatRef, styles } = useAutoPosition();

  return (
    <>
      <div ref={triggerRef}>Trigger</div>
      <div ref={floatRef} style={{ ...styles }}>
        Floating tooltip
      </div>
    </>
  );
};

useAutoIntegration

Wrap above useAutoPosition with interaction controls. Based on the config updated open property

Usage

Accepts a config as an argument and returns the styles, open, getTriggerProps, getFloatProps, triggerRef and floatRef functions.

type Options = AutoPositionOptions & {
  // Auto open on hover. Default: Enabled
  hover?: HoverConfig;
  // Auto open on click.  Default: Disabled
  click?: EnableConfig;
  // Auto open on focus.  Default: Enabled
  focus?: EnableConfig;
  // Set role to automatically add a11y params to floater. Default: Disabled
  role?: RoleConfig;
  // Auto close.  Default: Enabled
  dimish?: EnableConfig;
};
import { useAutoInteractions } from '@highlight-ui/utils-hooks';

const MyComponent = () => {
  const { triggerRef, floatRef, styles, open, getTriggerProps, getFloatProps } =
    useAutoInteractions();

  return (
    <>
      <div
        ref={triggerRef}
        // Functions like onClick, onHover, ... are at risk of being overriden
        // to prevent that we add then into the getTriggerProps that will join
        // the props/callbacks if it needs/uses it
        {...getTriggerProps({
          onClick: () => {
            console.log('clicked');
          },
        })}
      >
        Trigger
      </div>
      {open && (
        <div
          ref={floatRef}
          style={{ ...styles }}
          // Functions like onClick, onHover, ... are at risk of being overriden
          // to prevent that we add then into the getTriggerProps that will join
          // the props/callbacks if it needs/uses it
          {...getFloatProps({
            onClick: () => {
              console.log('clicked');
            },
          })}
        >
          Floating tooltip
        </div>
      )}
    </>
  );
};

useForkRef

Allows multiple ref objects/callbacks to be combined so that they can be passed as a single callback ref.

Usage

import React from 'react';
import { useForkRef } from '@highlight-ui/utils-hooks';

const MyComponent = () => {
  const firstRef = React.useRef();
  const secondRef = React.useRef();
  const finalRef = React.useRef([firstRef, secondRef]);

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

useClickOutside

Allows attaching a click-outside listener to a target element.

Usage

import { useClickOutside } from '@highlight-ui/utils-hooks';

const MyComponent = () => {
  const onClickOutside = () => {
    console.log('clicked outside');
  };
  const setElement = useClickOutside(onClickOutside);

  return (
    <div>
      <span>Outer Div</span>
      <div ref={setElement}>
        <span>Inner Div</span>
      </div>
    </div>
  );
};

useScrollObserver

Allows attaching a scroll listener to a target element.

Usage

import { useScrollObserver } from '@highlight-ui/utils-hooks';

const MyComponent = () => {
  const onScroll = () => {
    console.log('scrolled');
  };
  const { setElement } = useScrollObserver(onScroll);

  return (
    <div>
      <span>Outer Div</span>
      <div ref={setElement}>
        <span>Inner Div</span>
      </div>
    </div>
  );
};
3.5.0

3 months ago

3.4.3

5 months ago

3.2.24

10 months ago

3.2.23

10 months ago

3.2.26

10 months ago

3.2.25

10 months ago

3.2.28

10 months ago

3.2.27

10 months ago

3.4.0-r18.0

8 months ago

3.2.29

9 months ago

3.2.22

10 months ago

3.3.7

7 months ago

3.3.6

7 months ago

3.4.0

7 months ago

3.4.2

7 months ago

3.4.1

7 months ago

3.2.31

9 months ago

3.2.30

9 months ago

3.2.32

9 months ago

3.3.1

8 months ago

3.3.0

8 months ago

3.3.5

7 months ago

3.3.4

7 months ago

3.3.3

8 months ago

3.3.2

8 months ago

3.2.20

1 year ago

3.2.21

1 year ago

3.2.17

1 year ago

3.2.16

1 year ago

3.2.19

1 year ago

3.2.18

1 year ago

3.2.2

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.2.6

1 year ago

3.2.5

1 year ago

3.2.4

1 year ago

3.2.3

1 year ago

3.2.9

1 year ago

3.2.8

1 year ago

3.2.7

1 year ago

3.2.13

1 year ago

3.2.12

1 year ago

3.2.15

1 year ago

3.1.1

1 year ago

3.2.14

1 year ago

3.1.0

1 year ago

3.2.11

1 year ago

3.2.10

1 year ago

3.0.9

2 years ago

3.0.7

2 years ago

3.0.6

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.5

2 years ago

2.0.9-alpha.1

2 years ago

3.0.0

2 years ago

2.0.9-alpha.0

2 years ago

2.0.8

2 years ago

2.0.3

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.2

3 years ago

2.0.1

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

2.0.0

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago