Skip to content

Commit

Permalink
fix(hooks): wrapped onOpenProp/onCloseProp in useDisclosure with useC…
Browse files Browse the repository at this point in the history
…allbackRef
  • Loading branch information
takethefake committed Oct 14, 2021
1 parent 98fdadd commit 5fe9b55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-readers-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chakra-ui/hooks": patch
---

used useCallbackRef for onOpenProp/onCloseProp in useDisclosure
11 changes: 7 additions & 4 deletions packages/hooks/src/use-disclosure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { callAllHandlers } from "@chakra-ui/utils"
import * as React from "react"
import { useControllableProp } from "./use-controllable"
import { useId } from "./use-id"
import { useCallbackRef } from "./use-callback-ref"

export interface UseDisclosureProps {
isOpen?: boolean
Expand All @@ -19,6 +20,8 @@ export function useDisclosure(props: UseDisclosureProps = {}) {
id: idProp,
} = props

const onOpenPropCallbackRef = useCallbackRef(onOpenProp)
const onClosePropCallbackRef = useCallbackRef(onCloseProp)
const [isOpenState, setIsOpen] = React.useState(props.defaultIsOpen || false)
const [isControlled, isOpen] = useControllableProp(isOpenProp, isOpenState)

Expand All @@ -28,15 +31,15 @@ export function useDisclosure(props: UseDisclosureProps = {}) {
if (!isControlled) {
setIsOpen(false)
}
onCloseProp?.()
}, [isControlled, onCloseProp])
onClosePropCallbackRef?.()
}, [isControlled, onClosePropCallbackRef])

const onOpen = React.useCallback(() => {
if (!isControlled) {
setIsOpen(true)
}
onOpenProp?.()
}, [isControlled, onOpenProp])
onOpenPropCallbackRef?.()
}, [isControlled, onOpenPropCallbackRef])

const onToggle = React.useCallback(() => {
const action = isOpen ? onClose : onOpen
Expand Down

0 comments on commit 5fe9b55

Please sign in to comment.