Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
addressed last comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bmdalex committed May 31, 2019
1 parent 162be80 commit 8943460
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions packages/react/src/lib/positioner/Popper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ const Popper: React.FunctionComponent<PopperProps> = props => {

const popperRef = React.useRef<PopperJS>()
const contentRef = React.useRef<HTMLElement>(null)
const latestPlacement = React.useRef<PopperJS.Placement>()
const [computedPlacement, setComputedPlacement] = React.useState<PopperJS.Placement>()
const latestPlacement = React.useRef<PopperJS.Placement>(proposedPlacement)
const [computedPlacement, setComputedPlacement] = React.useState<PopperJS.Placement>(
proposedPlacement,
)

const computedModifiers: PopperJS.Modifiers = React.useMemo(
() =>
Expand All @@ -49,32 +51,18 @@ const Popper: React.FunctionComponent<PopperProps> = props => {
[rtl, offset, position],
)

const scheduleUpdate = React.useCallback(
() => {
if (popperRef.current) {
popperRef.current.scheduleUpdate()
}
},
[popperRef.current],
)

const destroyInstance = React.useCallback(
() => {
if (popperRef.current) {
popperRef.current.destroy()
popperRef.current = null
}
},
[popperRef.current],
)
const scheduleUpdate = React.useCallback(() => {
if (popperRef.current) {
popperRef.current.scheduleUpdate()
}
}, [])

const instanceDependencies = [
computedModifiers,
enabled,
userModifiers,
positionFixed,
proposedPlacement,
]
const destroyInstance = React.useCallback(() => {
if (popperRef.current) {
popperRef.current.destroy()
popperRef.current = null
}
}, [])

const createInstance = React.useCallback(
() => {
Expand Down Expand Up @@ -131,15 +119,15 @@ const Popper: React.FunctionComponent<PopperProps> = props => {

popperRef.current = createPopper(targetRef.current, contentRef.current, options)
},
[targetRef.current, contentRef.current, ...instanceDependencies],
[computedModifiers, enabled, userModifiers, positionFixed, proposedPlacement],
)

React.useEffect(
() => {
createInstance()
return destroyInstance
},
[computedModifiers, enabled, userModifiers, positionFixed, proposedPlacement],
[createInstance],
)

React.useEffect(scheduleUpdate, [...positioningDependencies, computedPlacement])
Expand Down

0 comments on commit 8943460

Please sign in to comment.