-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add `useClose` hook and `CloseButton` component * expose `useClose` hook and `CloseButton` components * use `CloseProvider` in the `Popover` component * use `CloseProvider` in the `Dialog` component * use `CloseProvider` in the `Disclosure` component * update changelog
- Loading branch information
1 parent
00f84ac
commit b86737b
Showing
8 changed files
with
87 additions
and
39 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
21 changes: 21 additions & 0 deletions
21
packages/@headlessui-react/src/components/close-button/close-button.tsx
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,21 @@ | ||
'use client' | ||
|
||
import React, { type ElementType, type Ref } from 'react' | ||
import { useClose } from '../../internal/close-provider' | ||
import { forwardRefWithAs, mergeProps } from '../../utils/render' | ||
import { Button, type ButtonProps, type _internal_ComponentButton } from '../button/button' | ||
|
||
let DEFAULT_BUTTON_TAG = 'button' as const | ||
|
||
export type CloseButtonProps<TTag extends ElementType = typeof DEFAULT_BUTTON_TAG> = | ||
ButtonProps<TTag> | ||
|
||
function CloseButtonFn<TTag extends ElementType = typeof DEFAULT_BUTTON_TAG>( | ||
props: ButtonProps<TTag>, | ||
ref: Ref<HTMLElement> | ||
) { | ||
let close = useClose() | ||
return <Button ref={ref} {...mergeProps({ onClick: close }, props)} /> | ||
} | ||
|
||
export let CloseButton = forwardRefWithAs(CloseButtonFn) as _internal_ComponentButton |
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
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
11 changes: 11 additions & 0 deletions
11
packages/@headlessui-react/src/internal/close-provider.tsx
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,11 @@ | ||
import React, { createContext, useContext } from 'react' | ||
|
||
let CloseContext = createContext(() => {}) | ||
|
||
export function useClose() { | ||
return useContext(CloseContext) | ||
} | ||
|
||
export function CloseProvider({ value, children }: React.PropsWithChildren<{ value: () => void }>) { | ||
return <CloseContext.Provider value={value}>{children}</CloseContext.Provider> | ||
} |