Skip to content

Commit

Permalink
feat: return isOpen and improve effects (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Nov 2, 2024
1 parent f7381e3 commit 58986a7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ usePopover(
container?: HTMLElement
placement: PlacementType
arrowProps?: { size: number }
defaultOpen?: boolean
delayDuration?: number
}
): {
popover: ReactNode
Expand Down
25 changes: 19 additions & 6 deletions src/overlay-trigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export type PopoverOptions = {
container?: HTMLElement
placement: PlacementType
arrowProps?: { size: number }
defaultOpen?: boolean
delayDuration?: number
}

function usePopover(
Expand All @@ -55,13 +57,24 @@ function usePopover(
clickToggle,
hoverToggle,
focusToggle,
defaultOpen,
delayDuration,
}: PopoverOptions
) {
const triggerRef = useRef<HTMLElement>(null)
const overlayRef = useRef<HTMLElement>(null)
const adjustOverlayRef = useRef(() => {})
const [visible, setVisible] = useState(false)
const [isOpen, originSetOpen] = useState(!!defaultOpen)

function setOpen(value: boolean) {
const toggleOpenBasedOnDelay = delayDuration
? () => setTimeout(() => {
originSetOpen(value)
}, delayDuration)
: () => originSetOpen(value)

toggleOpenBasedOnDelay()
}

function handleMouseEnter() {
if (!isPointerEventSupported && !isTouchEventSupported) {
Expand All @@ -88,7 +101,7 @@ function usePopover(
}

function handleClick() {
if (visible) {
if (isOpen) {
close()
} else {
open()
Expand Down Expand Up @@ -129,11 +142,11 @@ function usePopover(
}

function open() {
setVisible(true)
setOpen(true)
}

function close() {
setVisible(false)
setOpen(false)
}

function getTrigger() {
Expand All @@ -160,7 +173,7 @@ function usePopover(
}, [container])

const overlay = useOverlay({
visible,
visible: isOpen,
container,
placement,
arrowProps,
Expand All @@ -172,7 +185,7 @@ function usePopover(
})

return {
isOpen: visible,
isOpen: isOpen,
popover: overlay,
triggerProps,
}
Expand Down

0 comments on commit 58986a7

Please sign in to comment.