Skip to content
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

chore: Add focus-trap example #988

Merged
merged 5 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/ariakit/src/focus-trap/__examples__/focus-trap/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { FocusEvent, useRef } from "react";
import { Checkbox, useCheckboxState } from "ariakit/checkbox";
import { FocusTrap } from "ariakit/focus-trap";
import "./style.css";

export default function Example() {
const focusTrapped = useCheckboxState({ defaultValue: true });
const firstRef = useRef<HTMLButtonElement>(null);
const lastRef = useRef<HTMLButtonElement>(null);

const onTrapFocus = (event: FocusEvent) => {
if (event.relatedTarget === firstRef.current) {
lastRef.current?.focus();
} else {
firstRef.current?.focus();
}
};

return (
<>
{focusTrapped.value && <FocusTrap onFocus={onTrapFocus} />}
<div className="wrapper">
<label className="label">
<Checkbox state={focusTrapped} clickOnEnter className="checkbox" />
Trap focus
</label>
<button className="button" ref={firstRef}>
First
</button>
<button className="button" ref={lastRef}>
Last
</button>
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior is a little bit weird now when the focus is trapped. When pressing Tab, the focus never goes to the checkbox, when pressing Shift+Tab, the focus never goes to the last button.

I think we can just make the checkbox the first item ref to fix this:

Suggested change
<div className="wrapper">
<label className="label">
<Checkbox state={focusTrapped} clickOnEnter className="checkbox" />
Trap focus
</label>
<button className="button" ref={firstRef}>
First
</button>
<button className="button" ref={lastRef}>
Last
</button>
</div>
<div className="wrapper">
<label className="label">
<Checkbox state={focusTrapped} ref={firstRef} className="checkbox" />
Trap focus
</label>
<button className="button" ref={lastRef}>
Button
</button>
</div>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much @diegohaz !! Sorry for the delay! I was able to update the example with your suggestion. I've also added the focus trap test. The Ariakit test utils are really great! ❤️

{focusTrapped.value && <FocusTrap onFocus={onTrapFocus} />}
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import url("../../../button/__examples__/button/style.css");
@import url("../../../checkbox/__examples__/checkbox/style.css");

.wrapper {
@apply flex gap-2 flex-col rounded-lg border border-canvas-3 dark:border-canvas-3-dark p-3 w-56;
}
3 changes: 3 additions & 0 deletions packages/website/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export default function Home() {
<li>
<Link href="/examples/disclosure">Disclosure</Link>
</li>
<li>
<Link href="/examples/focus-trap">Focus trap</Link>
</li>
<li>
<Link href="/examples/form">Form</Link>
</li>
Expand Down