Skip to content

Commit

Permalink
fix(Popover): Fixed popover overlay bug
Browse files Browse the repository at this point in the history
  • Loading branch information
giubatt committed Jun 1, 2021
1 parent eacdb2a commit 92bcfee
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsxRuntime classic /
/** @jsx jsx */
import { jsx, SxStyleProp } from "theme-ui"
import React, { useCallback, useRef } from "react"
import React, { useCallback, useRef, useState } from "react"
import Tippy, { TippyProps } from "@tippyjs/react"
import { roundArrow } from "tippy.js"
import ReactDOM from "react-dom"
Expand Down Expand Up @@ -80,6 +80,7 @@ const Popover = ({
zIndexPopper = 9000,
zIndexOverlay = 8000,
}: PopoverProps) => {
const [visible, setVisible] = useState(false)
const overlayRef = useRef<HTMLDivElement>(ssrSafeCreateDiv())

const isControlled = isVisible !== undefined
Expand Down Expand Up @@ -181,21 +182,15 @@ const Popover = ({
onClickOutside={(instance, event) => onClickOutside?.(instance, event)}
onCreate={onCreate}
onShow={(instance) => {
onShow?.(instance)
setVisible(true)
if (overlayRef.current) {
overlayRef.current.style.opacity = "0"
document.body.append(overlayRef.current)
overlayRef.current.style.transition = "opacity 200ms"
}
setTimeout(() => {
if (overlayRef.current) overlayRef.current.style.opacity = "1"
})
onShow?.(instance)
}}
onHide={(instance) => {
if (overlayRef.current) {
overlayRef.current.style.transition = "opacity 200ms"
overlayRef.current.style.opacity = "0"
}
setVisible(false)

setTimeout(() => {
if (overlayRef.current) overlayRef.current.remove()
})
Expand Down Expand Up @@ -244,6 +239,25 @@ const Popover = ({
? ReactDOM.createPortal(
<div
sx={{
"@keyframes fadeIn": {
"0%": {
opacity: 0,
},
"100%": {
opacity: 1,
},
},
"@keyframes fadeOut": {
"0%": {
opacity: 1,
},
"100%": {
opacity: 0,
},
},
animation: visible
? `0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards fadeIn`
: `0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards fadeOut`,
position: "fixed",
top: 0,
left: 0,
Expand Down

1 comment on commit 92bcfee

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for herzui ready!

✅ Preview
https://herzui-celm2f7f5-micromed.vercel.app

Built with commit 92bcfee.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.