PHP 8.2.30
Preview: usePopper.js.flow Size: 3.96 KB
/proc/thread-self/root/home/byroehnu/.trash/node_modules11/react-popper/lib/cjs/usePopper.js.flow

// @flow strict
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {
  createPopper as defaultCreatePopper,
  type Options as PopperOptions,
  type VirtualElement,
  type State as PopperState,
  type Instance as PopperInstance,
} from '@popperjs/core';
import isEqual from 'react-fast-compare';
import { fromEntries, useIsomorphicLayoutEffect } from './utils';

type Options = $Shape<{
  ...PopperOptions,
  createPopper: typeof defaultCreatePopper,
}>;

type Styles = {
  [key: string]: $Shape<CSSStyleDeclaration>,
};

type Attributes = {
  [key: string]: { [key: string]: string },
};

type State = {
  styles: Styles,
  attributes: Attributes,
};

const EMPTY_MODIFIERS = [];

type UsePopperResult = $ReadOnly<{
  state: ?PopperState,
  styles: Styles,
  attributes: Attributes,
  update: ?$PropertyType<PopperInstance, 'update'>,
  forceUpdate: ?$PropertyType<PopperInstance, 'forceUpdate'>,
}>;

export const usePopper = (
  referenceElement: ?(Element | VirtualElement),
  popperElement: ?HTMLElement,
  options: Options = {}
): UsePopperResult => {
  const prevOptions = React.useRef<?PopperOptions>(null);

  const optionsWithDefaults = {
    onFirstUpdate: options.onFirstUpdate,
    placement: options.placement || 'bottom',
    strategy: options.strategy || 'absolute',
    modifiers: options.modifiers || EMPTY_MODIFIERS,
  };

  const [state, setState] = React.useState<State>({
    styles: {
      popper: {
        position: optionsWithDefaults.strategy,
        left: '0',
        top: '0',
      },
      arrow: {
        position: 'absolute',
      },
    },
    attributes: {},
  });

  const updateStateModifier = React.useMemo(
    () => ({
      name: 'updateState',
      enabled: true,
      phase: 'write',
      fn: ({ state }) => {
        const elements = Object.keys(state.elements);

        ReactDOM.flushSync(() => {
          setState({
            styles: fromEntries(
              elements.map((element) => [element, state.styles[element] || {}])
            ),
            attributes: fromEntries(
              elements.map((element) => [element, state.attributes[element]])
            ),
          });
        });
      },
      requires: ['computeStyles'],
    }),
    []
  );

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

    if (isEqual(prevOptions.current, newOptions)) {
      return prevOptions.current || newOptions;
    } else {
      prevOptions.current = newOptions;
      return newOptions;
    }
  }, [
    optionsWithDefaults.onFirstUpdate,
    optionsWithDefaults.placement,
    optionsWithDefaults.strategy,
    optionsWithDefaults.modifiers,
    updateStateModifier,
  ]);

  const popperInstanceRef = React.useRef();

  useIsomorphicLayoutEffect(() => {
    if (popperInstanceRef.current) {
      popperInstanceRef.current.setOptions(popperOptions);
    }
  }, [popperOptions]);

  useIsomorphicLayoutEffect(() => {
    if (referenceElement == null || popperElement == null) {
      return;
    }

    const createPopper = options.createPopper || defaultCreatePopper;
    const popperInstance = createPopper(
      referenceElement,
      popperElement,
      popperOptions
    );

    popperInstanceRef.current = popperInstance;

    return () => {
      popperInstance.destroy();
      popperInstanceRef.current = null;
    };
  }, [referenceElement, popperElement, options.createPopper]);

  return {
    state: popperInstanceRef.current ? popperInstanceRef.current.state : null,
    styles: state.styles,
    attributes: state.attributes,
    update: popperInstanceRef.current ? popperInstanceRef.current.update : null,
    forceUpdate: popperInstanceRef.current
      ? popperInstanceRef.current.forceUpdate
      : null,
  };
};

Directory Contents

Dirs: 1 × Files: 14

Name Size Perms Modified Actions
- drwxr-xr-x 2026-02-28 00:28:44
Edit Download
746 B lrw-r--r-- 2026-02-28 00:28:24
Edit Download
607 B lrw-r--r-- 2026-02-28 00:28:26
Edit Download
2.16 KB lrw-r--r-- 2026-02-28 00:27:44
Edit Download
1.03 KB lrw-r--r-- 2026-02-28 00:27:52
Edit Download
3.66 KB lrw-r--r-- 2026-02-28 00:28:00
Edit Download
3.04 KB lrw-r--r-- 2026-02-28 00:28:04
Edit Download
2.13 KB lrw-r--r-- 2026-02-28 00:28:16
Edit Download
1.12 KB lrw-r--r-- 2026-02-28 00:28:20
Edit Download
13 B lrw-r--r-- 2026-02-28 00:28:08
Edit Download
145 B lrw-r--r-- 2026-02-28 00:28:12
Edit Download
4.61 KB lrw-r--r-- 2026-02-28 00:28:28
Edit Download
3.96 KB lrw-r--r-- 2026-02-28 00:28:28
Edit Download
2.81 KB lrw-r--r-- 2026-02-28 00:28:30
Edit Download
1.46 KB lrw-r--r-- 2026-02-28 00:28:30
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).