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

Commit

Permalink
Remove popper when reset, change layout or component unmount (#146)
Browse files Browse the repository at this point in the history
* Remove popper when reset, change layout or component unmount

* Adding callback function when press dropdown button
  • Loading branch information
henrypalacios authored Jun 29, 2022
1 parent 1afd37e commit cbdaa22
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/apps/explorer/components/TransanctionBatchGraph/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ const updateLayout = (cy: Cytoscape.Core, layoutName: string, noAnimation = fals
cy.fit()
}

const removePopper = (popperInstance: React.MutableRefObject<PopperInstance | null>): void =>
popperInstance.current?.destroy()

function TransanctionBatchGraph({
txBatchData: { error, isLoading, txSettlement },
networkId,
Expand Down Expand Up @@ -308,6 +311,7 @@ function TransanctionBatchGraph({
if (resetZoom) {
updateLayout(cy, layout.name)
}
removePopper(cyPopperRef)
setResetZoom(null)
}, [error, isLoading, txSettlement, networkId, heightSize, resetZoom, layout.name])

Expand All @@ -329,7 +333,10 @@ function TransanctionBatchGraph({
})
cy.nodes().noOverlap({ padding: 5 })

return (): void => cy.removeAllListeners()
return (): void => {
cy.removeAllListeners()
removePopper(cyPopperRef)
}
}, [cytoscapeRef, elements.length])

if (isLoading)
Expand Down Expand Up @@ -366,6 +373,7 @@ function TransanctionBatchGraph({
dropdownButtonContentOpened={
<DropdownButtonContent icon={iconDice[currentLayoutIndex]} layout={LayoutNames[layout.name]} open />
}
callback={(): void => removePopper(cyPopperRef)}
items={Object.values(LayoutNames).map((layoutName) => (
<DropdownOption key={layoutName} onClick={(): void => setLayout(layouts[layoutName.toLowerCase()])}>
{layoutName}
Expand Down
7 changes: 5 additions & 2 deletions src/apps/explorer/components/common/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface DropdownProps extends DOMAttributes<HTMLDivElement> {
dropdownButtonContentOpened?: React.ReactNode | string
dropdownDirection?: DropdownDirection | undefined
dropdownPosition?: DropdownPosition | undefined
callback?: () => void
}

type CssString = FlattenSimpleInterpolation | string
Expand Down Expand Up @@ -107,18 +108,20 @@ export const Dropdown: React.FC<DropdownProps> = (props) => {
dropdownDirection,
dropdownPosition,
items,
callback,
} = props
const [isOpen, setIsOpen] = useState<boolean>(false)
const dropdownContainerRef = createRef<HTMLDivElement>()
useOnClickOutside(dropdownContainerRef, () => setIsOpen(false))

const onButtonClick = useCallback(
(e) => {
(e: React.MouseEvent<HTMLDivElement>) => {
e.stopPropagation()
if (disabled) return
setIsOpen(!isOpen)
callback && callback()
},
[disabled, isOpen],
[callback, disabled, isOpen],
)

return (
Expand Down

0 comments on commit cbdaa22

Please sign in to comment.