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

Commit a8e1da4

Browse files
committed
fix: popper-core 2.3.3 type fixes
1 parent 1de36ab commit a8e1da4

File tree

3 files changed

+80
-13
lines changed

3 files changed

+80
-13
lines changed

src/__typings__/main-test.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// be found under `/typings/tests` please. Thanks! 🤗
55

66
import React from 'react';
7-
import { Manager, Reference, Popper } from '..';
7+
import { Manager, Reference, Popper, usePopper } from '..';
88

99
export const Test = () => (
1010
<Manager>
@@ -33,7 +33,10 @@ export const Test = () => (
3333
}) => (
3434
<div
3535
ref={ref}
36-
style={{ ...style, opacity: (isReferenceHidden || hasPopperEscaped) ? 0 : 1 }}
36+
style={{
37+
...style,
38+
opacity: isReferenceHidden || hasPopperEscaped ? 0 : 1,
39+
}}
3740
data-placement={placement}
3841
onClick={() => update()}
3942
>
@@ -51,3 +54,56 @@ export const Test = () => (
5154
</Popper>
5255
</Manager>
5356
);
57+
58+
export const HookTest = () => {
59+
const [referenceElement, setReferenceElement] = React.useState<?Element>(
60+
null
61+
);
62+
const [popperElement, setPopperElement] = React.useState<?HTMLElement>(null);
63+
const [arrowElement, setArrowElement] = React.useState<?HTMLElement>(null);
64+
const { styles, attributes, update } = usePopper(
65+
referenceElement,
66+
popperElement,
67+
{
68+
modifiers: [
69+
{
70+
name: 'arrow',
71+
options: { element: arrowElement || undefined },
72+
},
73+
],
74+
}
75+
);
76+
77+
usePopper(
78+
referenceElement,
79+
popperElement,
80+
// $FlowExpectError
81+
{
82+
modifiers: [
83+
{
84+
name: 'offset',
85+
options: { offset: [0, ''] },
86+
},
87+
],
88+
}
89+
);
90+
91+
return (
92+
<>
93+
<button
94+
type="button"
95+
ref={setReferenceElement}
96+
onClick={() => {
97+
update && update();
98+
}}
99+
>
100+
Reference element
101+
</button>
102+
103+
<div {...attributes.popper} ref={setPopperElement} style={styles.popper}>
104+
Popper element
105+
<div ref={setArrowElement} style={styles.arrow} />
106+
</div>
107+
</>
108+
);
109+
};

src/usePopper.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
import * as React from 'react';
33
import {
44
createPopper as defaultCreatePopper,
5-
type Options as PopperOptions,
65
type VirtualElement,
6+
type Modifier,
7+
type OptionsGeneric,
8+
type StrictModifiers,
79
} from '@popperjs/core';
810
import isEqual from 'react-fast-compare';
911
import { fromEntries, useIsomorphicLayoutEffect } from './utils';
1012

11-
type Options = $Shape<{
12-
...PopperOptions,
13+
type Options<TModifiers> = $Shape<{
14+
...OptionsGeneric<TModifiers>,
1315
createPopper: typeof defaultCreatePopper,
1416
}>;
1517

@@ -22,14 +24,20 @@ type State = {
2224
},
2325
};
2426

27+
type UpdateStateModifier = Modifier<'updateState', {||}>;
28+
2529
const EMPTY_MODIFIERS = [];
2630

27-
export const usePopper = (
31+
export const usePopper = <
32+
TModifiers: StrictModifiers | $Shape<Modifier<string, {}>>
33+
>(
2834
referenceElement: ?(Element | VirtualElement),
2935
popperElement: ?HTMLElement,
30-
options: Options = {}
36+
options: Options<TModifiers> = {}
3137
) => {
32-
const prevOptions = React.useRef<?PopperOptions>(null);
38+
type TExtendedModifier = TModifiers | $Shape<UpdateStateModifier>;
39+
40+
const prevOptions = React.useRef<?OptionsGeneric<TExtendedModifier>>(null);
3341

3442
const optionsWithDefaults = {
3543
onFirstUpdate: options.onFirstUpdate,
@@ -49,7 +57,7 @@ export const usePopper = (
4957
attributes: {},
5058
});
5159

52-
const updateStateModifier = React.useMemo(
60+
const updateStateModifier = React.useMemo<UpdateStateModifier>(
5361
() => ({
5462
name: 'updateState',
5563
enabled: true,
@@ -71,7 +79,7 @@ export const usePopper = (
7179
[]
7280
);
7381

74-
const popperOptions = React.useMemo(() => {
82+
const popperOptions = React.useMemo<OptionsGeneric<TExtendedModifier>>(() => {
7583
const newOptions = {
7684
onFirstUpdate: optionsWithDefaults.onFirstUpdate,
7785
placement: optionsWithDefaults.placement,
@@ -83,8 +91,11 @@ export const usePopper = (
8391
],
8492
};
8593

86-
if (isEqual(prevOptions.current, newOptions)) {
87-
return prevOptions.current || newOptions;
94+
if (
95+
prevOptions.current != null &&
96+
isEqual(prevOptions.current, newOptions)
97+
) {
98+
return prevOptions.current;
8899
} else {
89100
prevOptions.current = newOptions;
90101
return newOptions;

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@
12531253

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

12591259
"@rollup/plugin-commonjs@^11.0.2":

0 commit comments

Comments
 (0)