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

Commit 0dc52d2

Browse files
authored
fix: tweak hooks usage in usePopper (#357)
1 parent d764839 commit 0dc52d2

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

Diff for: .eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
root: true,
3+
reportUnusedDisableDirectives: true,
34
parser: 'babel-eslint',
45
env: {
56
browser: true,

Diff for: src/usePopper.js

+23-26
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const usePopper = (
4141
const [state, setState] = React.useState<State>({
4242
styles: {
4343
popper: {
44-
position: optionsWithDefaults.strategy || 'absolute',
44+
position: optionsWithDefaults.strategy,
4545
left: '0',
4646
top: '0',
4747
},
@@ -68,14 +68,14 @@ export const usePopper = (
6868
},
6969
requires: ['computeStyles'],
7070
}),
71-
[setState]
71+
[]
7272
);
7373

7474
const popperOptions = React.useMemo(() => {
7575
const newOptions = {
7676
onFirstUpdate: optionsWithDefaults.onFirstUpdate,
77-
placement: optionsWithDefaults.placement || 'bottom',
78-
strategy: optionsWithDefaults.strategy || 'absolute',
77+
placement: optionsWithDefaults.placement,
78+
strategy: optionsWithDefaults.strategy,
7979
modifiers: [
8080
...optionsWithDefaults.modifiers,
8181
updateStateModifier,
@@ -98,35 +98,32 @@ export const usePopper = (
9898
]);
9999

100100
const popperInstanceRef = React.useRef();
101-
const createPopper = React.useMemo(
102-
() => options.createPopper || defaultCreatePopper,
103-
[options.createPopper]
104-
);
105101

106102
useIsomorphicLayoutEffect(() => {
107-
let popperInstance = null;
108-
if (referenceElement != null && popperElement != null) {
109-
popperInstance = createPopper(
110-
referenceElement,
111-
popperElement,
112-
popperOptions
113-
);
114-
115-
popperInstanceRef.current = popperInstance;
103+
if (popperInstanceRef.current) {
104+
popperInstanceRef.current.setOptions(popperOptions);
105+
}
106+
}, [popperOptions]);
107+
108+
useIsomorphicLayoutEffect(() => {
109+
if (referenceElement == null || popperElement == null) {
110+
return;
116111
}
117112

113+
const createPopper = options.createPopper || defaultCreatePopper;
114+
const popperInstance = createPopper(
115+
referenceElement,
116+
popperElement,
117+
popperOptions
118+
);
119+
120+
popperInstanceRef.current = popperInstance;
121+
118122
return () => {
119-
popperInstance != null && popperInstance.destroy();
123+
popperInstance.destroy();
120124
popperInstanceRef.current = null;
121125
};
122-
// eslint-disable-next-line react-hooks/exhaustive-deps
123-
}, [referenceElement, popperElement, createPopper]);
124-
125-
useIsomorphicLayoutEffect(() => {
126-
if (popperInstanceRef.current) {
127-
popperInstanceRef.current.setOptions(popperOptions);
128-
}
129-
}, [popperOptions]);
126+
}, [referenceElement, popperElement, options.createPopper]);
130127

131128
return {
132129
state: popperInstanceRef.current ? popperInstanceRef.current.state : null,

0 commit comments

Comments
 (0)