-
Notifications
You must be signed in to change notification settings - Fork 0
Rebuild useClickOutside #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SmorodinVik
wants to merge
3
commits into
master
Choose a base branch
from
feature/use-click-outside-update
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,3 +1,32 @@ | ||
| # `@byndyusoft-ui/use-click-outside` | ||
|
|
||
| > React hook to execute the callback when a click happens outside of component or components. | ||
| A React hook that invokes the given handler when a click (pointerdown) occurs outside the specified elements. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| npm i @byndyusoft-ui/use-click-outside | ||
| # or | ||
| yarn add @byndyusoft-ui/use-click-outside | ||
| ``` | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - `@byndyusoft-ui/use-latest-ref` | ||
|
|
||
| ## Behavior | ||
|
|
||
| - Subscribes to `pointerdown` on `document`. The handler is only called when the click occurs **outside all** elements passed in `refs` (i.e. `event.target` is not contained in any of `ref.current`). | ||
| - A single global listener on the document is used for all hook instances. | ||
| - The `disabled: true` option disables invoking the handler for that instance. | ||
| - The refs array may include `null` entries; they are ignored during the check. | ||
|
|
||
| ## Signature | ||
|
|
||
| ```ts | ||
| useClickOutside( | ||
| handler: (event: PointerEvent) => void, | ||
| refs: (RefObject<HTMLElement | null> | null)[], | ||
| options?: { disabled?: boolean } | ||
| ): void | ||
| ``` |
This file contains hidden or 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 hidden or 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 +1 @@ | ||
| export { default } from './useClickOutside'; | ||
| export { default, type IUseClickOutsideOptions } from './useClickOutside'; |
This file contains hidden or 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,33 @@ | ||
| import { Markdown, Meta, Canvas, Source } from '@storybook/blocks'; | ||
| import Readme from '../README.md?raw'; | ||
| import * as useClickOutsideStories from './useClickOutside.stories'; | ||
|
|
||
| <Meta title="hooks/useClickOutside" of={useClickOutsideStories} /> | ||
|
|
||
| <Markdown>{Readme}</Markdown> | ||
|
|
||
| ## Usage | ||
|
|
||
| 1. Import the hook: | ||
|
|
||
| <Source language="javascript" code="import useClickOutside from '@byndyusoft-ui/use-click-outside';" /> | ||
|
|
||
| 2. Call the hook with a handler and an array of refs for elements that should not trigger the handler when clicked inside. Pass options (e.g. `disabled`) if needed. | ||
|
|
||
| **Multiple refs** — the handler runs only when the click is outside all of the given elements: | ||
|
|
||
| <Canvas sourceState="shown" of={useClickOutsideStories.MultipleRefsStory} /> | ||
|
|
||
| **Disabled option** — when `disabled: true`, the handler is not called: | ||
|
|
||
| <Canvas sourceState="shown" of={useClickOutsideStories.DisabledStory} /> | ||
|
|
||
| **Dropdown** — typical use case: button + panel; clicking outside both closes the panel: | ||
|
|
||
| <Canvas sourceState="shown" of={useClickOutsideStories.DropdownStory} /> | ||
|
|
||
| ## Notes | ||
|
|
||
| - Uses the **pointerdown** event (mouse, touch, pen). The handler receives a `PointerEvent`. | ||
| - The `refs` array may be stable (e.g. via `useMemo`) or recreated on every render — the hook keeps handler and refs up to date via refs and does not require strict stabilization. | ||
| - Empty refs array: when clicking outside “nothing”, the handler is not called (no “inside” elements, so the “outside all” condition does not lead to a call). |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.