-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
4 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
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
88 changes: 88 additions & 0 deletions
88
src/pattern-library/components/patterns/hooks/UseClickAwayPage.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,88 @@ | ||
import Library from '../../Library'; | ||
|
||
export default function UseClickAwayPage() { | ||
return ( | ||
<Library.Page | ||
title="useClickAway" | ||
intro={ | ||
<p> | ||
<code>useClickAway</code> is a hook to listen for click events on{' '} | ||
<code>document.body</code>. If an event{"'"}s target is outside the | ||
container element, it invokes a callback. | ||
</p> | ||
} | ||
> | ||
<Library.Section> | ||
<Library.Pattern> | ||
<Library.Usage symbolName="useClickAway" /> | ||
<Library.Example> | ||
<Library.Demo | ||
withSource | ||
title="Basic useClickAway" | ||
exampleFile="use-click-away-basic" | ||
/> | ||
</Library.Example> | ||
</Library.Pattern> | ||
|
||
<Library.Pattern title="Hook arguments"> | ||
<Library.Example title="container"> | ||
<Library.Info> | ||
<Library.InfoItem label="description"> | ||
A Ref to the DOM element used to evaluate if clicking happened{' '} | ||
{'"'} | ||
away{'"'}. | ||
</Library.InfoItem> | ||
<Library.InfoItem label="type"> | ||
<code>{`RefObject<HTMLElement | undefined>`}</code> | ||
</Library.InfoItem> | ||
</Library.Info> | ||
</Library.Example> | ||
<Library.Example title="callback"> | ||
<Library.Info> | ||
<Library.InfoItem label="description"> | ||
The callback to be invoked when clicking away. The click event | ||
object is passed to it. | ||
</Library.InfoItem> | ||
<Library.InfoItem label="type"> | ||
<code>{`(e: Event) => void`}</code> | ||
</Library.InfoItem> | ||
</Library.Info> | ||
</Library.Example> | ||
<Library.Example title="options"> | ||
<Library.Info> | ||
<Library.InfoItem label="description"> | ||
Options to configure the hook{"'"}s behavior. | ||
</Library.InfoItem> | ||
<Library.InfoItem label="type"> | ||
See{' '} | ||
<Library.Link href="#hook-options">hook options</Library.Link>{' '} | ||
below for details. | ||
</Library.InfoItem> | ||
</Library.Info> | ||
</Library.Example> | ||
</Library.Pattern> | ||
|
||
<Library.Pattern title="Hook options" id="hook-options"> | ||
<Library.Example title="enabled"> | ||
<Library.Info> | ||
<Library.InfoItem label="description"> | ||
Allows the hook to be conditionally enabled or disabled, for | ||
example, if the container it handles is dynamically hidden or | ||
unmounted. | ||
</Library.InfoItem> | ||
<Library.InfoItem label="type"> | ||
<code>{`boolean`}</code> | ||
</Library.InfoItem> | ||
<Library.InfoItem label="default"> | ||
<code>{`true`}</code> | ||
</Library.InfoItem> | ||
</Library.Info> | ||
<Library.Code | ||
content={`useClickAway(containerRef, callback, { enabled: false })`} | ||
/> | ||
</Library.Example> | ||
</Library.Pattern> | ||
</Library.Section> | ||
</Library.Page> | ||
); | ||
} |
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,27 @@ | ||
import classnames from 'classnames'; | ||
import { useRef, useState } from 'preact/hooks'; | ||
|
||
import { Button } from '../../components/input'; | ||
import { useClickAway } from '../../hooks/use-click-away'; | ||
|
||
export default function App() { | ||
const [clickedAway, setClickedAway] = useState(false); | ||
const containerRef = useRef<HTMLDivElement | null>(null); | ||
|
||
useClickAway(containerRef, () => setClickedAway(true)); | ||
|
||
return ( | ||
<div | ||
ref={containerRef} | ||
className={classnames( | ||
'rounded-lg h-32 w-full p-2 flex items-center justify-center gap-2', | ||
{ 'bg-red-100': clickedAway, 'bg-amber-100': !clickedAway }, | ||
)} | ||
> | ||
{clickedAway ? 'You clicked outside the area' : 'Click outside this area'} | ||
{clickedAway && ( | ||
<Button onClick={() => setClickedAway(false)}>Reset</Button> | ||
)} | ||
</div> | ||
); | ||
} |
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