@popperjs/core#createPopperLite TypeScript Examples

The following examples show how to use @popperjs/core#createPopperLite. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: usePopper.ts    From design-systems-cli with MIT License 4 votes vote down vote up
usePopper = (
  referenceElement: HTMLElement | null,
  popperElement: HTMLElement | null,
  options: Partial<PopperOptions> = {}
) => {
  const prevOptions = React.useRef<PopperOptions>();
  const optionsWithDefaults: PopperOptions = {
    ...defaultPopperOptions,
    ...options,
  };
  const [state, setState] = React.useState<
    Pick<PopperState, "styles" | "attributes">
  >({
    styles: {
      popper: {
        position: optionsWithDefaults.strategy,
        left: "0",
        top: "0",
      },
    },
    attributes: {},
  });

  const updateStateModifier = React.useMemo(
    () => ({
      name: "updateState" as const,
      enabled: true,
      phase: "write" as const,
      fn: ({
        state: newState,
      }: {
        /** The current popper state */
        state: PopperState;
      }) => {
        const elements = Object.keys(newState.elements);

        setState({
          styles: fromEntries(
            elements.map((element) => [element, newState.styles[element] || {}])
          ),
          attributes: fromEntries(
            elements.map((element) => [element, newState.attributes[element]])
          ),
        });
      },
      requires: ["computeStyles"],
    }),
    []
  );

  const popperOptions = React.useMemo(() => {
    const newOptions: PopperOptions = {
      ...optionsWithDefaults,
      modifiers: [
        ...optionsWithDefaults.modifiers,
        updateStateModifier,
        { name: "applyStyles", enabled: false },
      ],
    };

    if (isEqual(prevOptions.current, newOptions)) {
      return prevOptions.current || newOptions;
    }

    prevOptions.current = newOptions;
    return newOptions;
  }, [optionsWithDefaults, updateStateModifier]);

  const popperInstanceRef = React.useRef<Instance>();

  /** If the options change on the hook change them on the popper instance too */
  useLayoutEffect(() => {
    popperInstanceRef.current?.setOptions(popperOptions);
  }, [popperOptions]);

  /** Create the popper instance. Recreate it when the reference/popper elements change */
  useLayoutEffect(() => {
    if (!referenceElement || !popperElement) {
      return;
    }

    const popperInstance = createPopperLite(
      referenceElement,
      popperElement,
      popperOptions
    );

    popperInstanceRef.current = popperInstance;

    return () => {
      popperInstance.destroy();
      popperInstanceRef.current = undefined;
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [referenceElement, popperElement, createPopperLite]);

  return {
    styles: state.styles as { [key: string]: React.CSSProperties },
    attributes: state.attributes,
    popperInstanceRef,
  };
}