-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: disclosure hook to package
- Loading branch information
1 parent
3b077d0
commit 6925f74
Showing
11 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
--- | ||
"@chakra-ui/react-use-size": patch | ||
"@chakra-ui/react-use-disclosure": patch | ||
--- | ||
|
||
Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# @chakra-ui/use-disclosure | ||
|
||
A Quick description of the component | ||
|
||
> This is an internal utility, not intended for public usage. | ||
## Installation | ||
|
||
```sh | ||
yarn add @chakra-ui/use-disclosure | ||
# or | ||
npm i @chakra-ui/use-disclosure | ||
``` | ||
|
||
## Contribution | ||
|
||
Yes please! See the | ||
[contributing guidelines](https://github.com/chakra-ui/chakra-ui/blob/master/CONTRIBUTING.md) | ||
for details. | ||
|
||
## Licence | ||
|
||
This project is licensed under the terms of the | ||
[MIT license](https://github.com/chakra-ui/chakra-ui/blob/master/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./src" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@chakra-ui/react-use-disclosure", | ||
"version": "1.0.0", | ||
"description": "", | ||
"keywords": [ | ||
"use-disclosure" | ||
], | ||
"author": "Segun Adebayo <sage@adebayosegun.com>", | ||
"homepage": "https://github.com/chakra-ui/chakra-ui#readme", | ||
"license": "MIT", | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.esm.js", | ||
"types": "dist/index.d.ts", | ||
"sideEffects": false, | ||
"files": [ | ||
"dist" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/chakra-ui/chakra-ui.git", | ||
"directory": "hooks/use-disclosure" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/chakra-ui/chakra-ui/issues" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --format=esm,cjs --dts", | ||
"dev": "pnpm build -- --watch", | ||
"clean": "rimraf dist .turbo", | ||
"typecheck": "tsc --noEmit", | ||
"build:fast": "tsup src/index.ts --format=esm,cjs" | ||
}, | ||
"dependencies": { | ||
"@chakra-ui/react-use-callback-ref": "workspace:*" | ||
}, | ||
"peerDependencies": { | ||
"react": ">=18" | ||
}, | ||
"devDependencies": { | ||
"react": "^18.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useCallbackRef } from "@chakra-ui/react-use-callback-ref" | ||
import React, { useCallback, useState, useId } from "react" | ||
|
||
export interface UseDisclosureProps { | ||
isOpen?: boolean | ||
defaultIsOpen?: boolean | ||
onClose?(): void | ||
onOpen?(): void | ||
id?: string | ||
} | ||
|
||
type HTMLProps = React.HTMLAttributes<HTMLElement> | ||
|
||
export function useDisclosure(props: UseDisclosureProps = {}) { | ||
const { | ||
onClose: onCloseProp, | ||
onOpen: onOpenProp, | ||
isOpen: isOpenProp, | ||
id: idProp, | ||
} = props | ||
|
||
const handleOpen = useCallbackRef(onOpenProp) | ||
const handleClose = useCallbackRef(onCloseProp) | ||
|
||
const [isOpenState, setIsOpen] = useState(props.defaultIsOpen || false) | ||
|
||
const isOpen = isOpenProp !== undefined ? isOpenProp : isOpenState | ||
|
||
const isControlled = isOpenProp !== undefined | ||
|
||
const id = idProp ?? `disclosure-${useId()}` | ||
|
||
const onClose = useCallback(() => { | ||
if (!isControlled) { | ||
setIsOpen(false) | ||
} | ||
handleClose?.() | ||
}, [isControlled, handleClose]) | ||
|
||
const onOpen = useCallback(() => { | ||
if (!isControlled) { | ||
setIsOpen(true) | ||
} | ||
handleOpen?.() | ||
}, [isControlled, handleOpen]) | ||
|
||
const onToggle = useCallback(() => { | ||
if (isOpen) { | ||
onClose() | ||
} else { | ||
onOpen() | ||
} | ||
}, [isOpen, onOpen, onClose]) | ||
|
||
function getButtonProps(props: HTMLProps = {}): HTMLProps { | ||
return { | ||
...props, | ||
"aria-expanded": isOpen, | ||
"aria-controls": id, | ||
onClick(event) { | ||
props.onClick?.(event) | ||
onToggle() | ||
}, | ||
} | ||
} | ||
|
||
function getDisclosureProps(props: HTMLProps = {}): HTMLProps { | ||
return { | ||
...props, | ||
hidden: !isOpen, | ||
id, | ||
} | ||
} | ||
|
||
return { | ||
isOpen, | ||
onOpen, | ||
onClose, | ||
onToggle, | ||
isControlled, | ||
getButtonProps, | ||
getDisclosureProps, | ||
} | ||
} | ||
|
||
export type UseDisclosureReturn = ReturnType<typeof useDisclosure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6925f74
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
chakra-ui-storybook – ./
chakra-ui-storybook-git-main-chakra-ui.vercel.app
storybook.chakra-ui.com
chakra-ui-storybook-chakra-ui.vercel.app
chakra-ui-storybook.vercel.app