Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

fix: flow type fixes for popper-core 2.3.3 #355

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[lints]

[options]
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectError
suppress_comment=\\(.\\|\n\\)*\\$\\(FlowExpectError\\|FlowFixMe\\)

[strict]

Expand Down
81 changes: 79 additions & 2 deletions src/__typings__/main-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// be found under `/typings/tests` please. Thanks! πŸ€—

import React from 'react';
import { Manager, Reference, Popper } from '..';
import { Manager, Reference, Popper, usePopper } from '..';
import type { Modifier, StrictModifiers } from '@popperjs/core';

export const Test = () => (
<Manager>
Expand Down Expand Up @@ -33,7 +34,10 @@ export const Test = () => (
}) => (
<div
ref={ref}
style={{ ...style, opacity: (isReferenceHidden || hasPopperEscaped) ? 0 : 1 }}
style={{
...style,
opacity: isReferenceHidden || hasPopperEscaped ? 0 : 1,
}}
data-placement={placement}
onClick={() => update()}
>
Expand All @@ -51,3 +55,76 @@ export const Test = () => (
</Popper>
</Manager>
);

export const HookTest = () => {
const [referenceElement, setReferenceElement] = React.useState<?Element>(
null
);
const [popperElement, setPopperElement] = React.useState<?HTMLElement>(null);
const [arrowElement, setArrowElement] = React.useState<?HTMLElement>(null);
const { styles, attributes, update } = usePopper(
referenceElement,
popperElement,
{
modifiers: [
{
name: 'arrow',
options: { element: arrowElement },
},
],
}
);

usePopper(referenceElement, popperElement, {
modifiers: [
// $FlowExpectError: offset tuple accepts only numbers
{
name: 'offset',
options: { offset: [0, ''] },
},
],
});

type CustomModifier = $Shape<Modifier<'custom', { foo: boolean }>>;
usePopper<StrictModifiers | CustomModifier>(referenceElement, popperElement, {
modifiers: [
{
name: 'custom',
options: { foo: true },
},
],
});

usePopper<StrictModifiers | CustomModifier>(
referenceElement,
popperElement,
// $FlowExpectError: foo should be boolean
{
modifiers: [
{
name: 'custom',
options: { foo: 'str' },
},
],
}
);

return (
<>
<button
type="button"
ref={setReferenceElement}
onClick={() => {
update && update();
}}
>
Reference element
</button>

<div {...attributes.popper} ref={setPopperElement} style={styles.popper}>
Popper element
<div ref={setArrowElement} style={styles.arrow} />
</div>
</>
);
};
33 changes: 22 additions & 11 deletions src/usePopper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
import * as React from 'react';
import {
createPopper as defaultCreatePopper,
type Options as PopperOptions,
type VirtualElement,
type Modifier,
type OptionsGeneric,
type StrictModifiers,
} from '@popperjs/core';
import isEqual from 'react-fast-compare';
import { fromEntries, useIsomorphicLayoutEffect } from './utils';

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

Expand All @@ -22,14 +24,20 @@ type State = {
},
};

type UpdateStateModifier = Modifier<'updateState', {||}>;

const EMPTY_MODIFIERS = [];

export const usePopper = (
type DefaultModifiers = StrictModifiers | $Shape<Modifier<any, any>>;

export const usePopper = <Modifiers: DefaultModifiers = DefaultModifiers>(
referenceElement: ?(Element | VirtualElement),
popperElement: ?HTMLElement,
options: Options = {}
options: Options<Modifiers> = {}
) => {
const prevOptions = React.useRef<?PopperOptions>(null);
type InternalModifiers = Modifiers | $Shape<UpdateStateModifier>;

const prevOptions = React.useRef<?OptionsGeneric<InternalModifiers>>(null);

const optionsWithDefaults = {
onFirstUpdate: options.onFirstUpdate,
Expand All @@ -49,7 +57,7 @@ export const usePopper = (
attributes: {},
});

const updateStateModifier = React.useMemo(
const updateStateModifier = React.useMemo<UpdateStateModifier>(
() => ({
name: 'updateState',
enabled: true,
Expand All @@ -71,7 +79,7 @@ export const usePopper = (
[]
);

const popperOptions = React.useMemo(() => {
const popperOptions = React.useMemo<OptionsGeneric<InternalModifiers>>(() => {
const newOptions = {
onFirstUpdate: optionsWithDefaults.onFirstUpdate,
placement: optionsWithDefaults.placement,
Expand All @@ -83,8 +91,11 @@ export const usePopper = (
],
};

if (isEqual(prevOptions.current, newOptions)) {
return prevOptions.current || newOptions;
if (
prevOptions.current != null &&
isEqual(prevOptions.current, newOptions)
) {
return prevOptions.current;
} else {
prevOptions.current = newOptions;
return newOptions;
Expand All @@ -111,7 +122,7 @@ export const usePopper = (
}

const createPopper = options.createPopper || defaultCreatePopper;
const popperInstance = createPopper(
const popperInstance = createPopper<InternalModifiers>(
referenceElement,
popperElement,
popperOptions
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@

"@popperjs/core@^2.3.3":
version "2.3.3"
resolved "https://registry.npmjs.org/@popperjs/core/-/core-2.3.3.tgz#8731722aeb7330e8fd9eb5d424be6b98dea7d6da"
resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.3.3.tgz#8731722aeb7330e8fd9eb5d424be6b98dea7d6da"
integrity sha512-yEvVC8RfhRPkD9TUn7cFcLcgoJePgZRAOR7T21rcRY5I8tpuhzeWfGa7We7tB14fe9R7wENdqUABcMdwD4SQLw==

"@rollup/plugin-commonjs@^11.0.2":
Expand Down