-
Notifications
You must be signed in to change notification settings - Fork 59
feat: Add <FocusTrap> docs
#258
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
11423b7
Add initial files
maximo-macchi-cb c8fa8e3
Add web docs
maximo-macchi-cb 0a97244
Touch up
maximo-macchi-cb 66da5df
Fix logic of includeTriggerInFocusTrap to focus on initial render
maximo-macchi-cb da37758
Add test
maximo-macchi-cb c862078
Fix build error
maximo-macchi-cb d66d9fa
Update changelogs
maximo-macchi-cb 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
122 changes: 122 additions & 0 deletions
122
apps/docs/docs/components/overlay/FocusTrap/_webExamples.mdx
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,122 @@ | ||
| :::note Before using FocusTrap | ||
| `<FocusTrap>` is intended to prevent keyboard users from interacting with elements on the page that a mouse user cannot interact with either. An example of this is `<Modal>` where the user cannot click the items behind the modal. If you want to shift focus based on UI events, consider using the [.focus()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus) method instead. | ||
| ::: | ||
|
|
||
| :::warning Accessibility | ||
| It is **required** that when using a `<FocusTrap>` there is logic using only key presses to escape the focus trap. Otherwise, keyboard users will be blocked after they enter a `<FocusTrap>`. | ||
| ::: | ||
|
|
||
| ## Basic example | ||
|
|
||
| :::note | ||
| All the examples have controls to enable / disable the focus trap so that page keyboard navigation isn't blocked. | ||
| ::: | ||
|
|
||
| When enabled, only the children of the `<FocusTrap>` will be able to receive focus. Otherwise, standard DOM focus order follows. | ||
|
|
||
| ```jsx live | ||
| function Example() { | ||
| const [focusTrapEnabled, setFocusTrapEnabled] = useState(false); | ||
|
|
||
| return ( | ||
| <VStack gap={2}> | ||
| <Checkbox | ||
| checked={focusTrapEnabled} | ||
| onChange={() => setFocusTrapEnabled((prev) => !prev)} | ||
| label="Focus trap enabled" | ||
| /> | ||
| {focusTrapEnabled && ( | ||
| <FocusTrap> | ||
| <VStack gap={2} background="bgAlternate" padding={2}> | ||
| <Text>Inside FocusTrap</Text> | ||
| <HStack gap={1}> | ||
| <Button>1</Button> | ||
| <Button>2</Button> | ||
| <Button>3</Button> | ||
| </HStack> | ||
| <Checkbox | ||
| checked={focusTrapEnabled} | ||
| onChange={() => setFocusTrapEnabled((prev) => !prev)} | ||
| label="Focus trap enabled" | ||
| /> | ||
| </VStack> | ||
| </FocusTrap> | ||
| )} | ||
| </VStack> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ## Include trigger in FocusTrap | ||
|
|
||
| The `includeTriggerInFocusTrap` prop includes the triggering element causing the `<FocusTrap>` to render as part of the focus order. | ||
|
|
||
| ```jsx live | ||
| function Example() { | ||
| const [focusTrapEnabled, setFocusTrapEnabled] = useState(false); | ||
|
|
||
| return ( | ||
| <VStack gap={2}> | ||
| <Checkbox | ||
| checked={focusTrapEnabled} | ||
| onChange={() => setFocusTrapEnabled((prev) => !prev)} | ||
| label="Focus trap enabled" | ||
| /> | ||
| {focusTrapEnabled && ( | ||
| <FocusTrap includeTriggerInFocusTrap> | ||
| <VStack gap={2} background="bgAlternate" padding={2}> | ||
| <Text>Inside FocusTrap</Text> | ||
| <HStack gap={1}> | ||
| <Button>1</Button> | ||
| <Button>2</Button> | ||
| <Button>3</Button> | ||
| </HStack> | ||
| <Checkbox | ||
| checked={focusTrapEnabled} | ||
| onChange={() => setFocusTrapEnabled((prev) => !prev)} | ||
| label="Focus trap enabled" | ||
| /> | ||
| </VStack> | ||
| </FocusTrap> | ||
| )} | ||
| </VStack> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ## Restore focus on unmount | ||
|
|
||
| The `restoreFocusOnUnmount` prop returns focus to the triggering element when the `<FocusTrap>` is unmounted. | ||
|
|
||
| ```jsx live | ||
| function Example() { | ||
| const [focusTrapEnabled, setFocusTrapEnabled] = useState(false); | ||
|
|
||
| return ( | ||
| <VStack gap={2}> | ||
| <Checkbox | ||
| checked={focusTrapEnabled} | ||
| onChange={() => setFocusTrapEnabled((prev) => !prev)} | ||
| label="Focus trap enabled" | ||
| /> | ||
| {focusTrapEnabled && ( | ||
| <FocusTrap restoreFocusOnUnmount> | ||
| <VStack gap={2} background="bgAlternate" padding={2}> | ||
| <Text>Inside FocusTrap</Text> | ||
| <HStack gap={1}> | ||
| <Button>1</Button> | ||
| <Button>2</Button> | ||
| <Button>3</Button> | ||
| </HStack> | ||
| <Checkbox | ||
| checked={focusTrapEnabled} | ||
| onChange={() => setFocusTrapEnabled((prev) => !prev)} | ||
| label="Focus trap enabled" | ||
| /> | ||
| </VStack> | ||
| </FocusTrap> | ||
| )} | ||
| </VStack> | ||
| ); | ||
| } | ||
| ``` | ||
10 changes: 10 additions & 0 deletions
10
apps/docs/docs/components/overlay/FocusTrap/_webPropsTable.mdx
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,10 @@ | ||
| import ComponentPropsTable from '@site/src/components/page/ComponentPropsTable'; | ||
| import webPropsData from ':docgen/web/overlays/FocusTrap/data'; | ||
| import { sharedParentTypes } from ':docgen/_types/sharedParentTypes'; | ||
| import { sharedTypeAliases } from ':docgen/_types/sharedTypeAliases'; | ||
|
|
||
| <ComponentPropsTable | ||
| props={webPropsData} | ||
| sharedTypeAliases={sharedTypeAliases} | ||
| sharedParentTypes={sharedParentTypes} | ||
| /> |
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,25 @@ | ||
| --- | ||
| id: focusTrap | ||
| title: FocusTrap | ||
| platform_switcher_options: { web: true, mobile: false } | ||
| hide_title: true | ||
| --- | ||
|
|
||
| import { VStack } from '@coinbase/cds-web/layout'; | ||
| import { ComponentHeader } from '@site/src/components/page/ComponentHeader'; | ||
| import { ComponentTabsContainer } from '@site/src/components/page/ComponentTabsContainer'; | ||
|
|
||
| import webPropsToc from ':docgen/web/overlays/FocusTrap/toc-props'; | ||
| import WebPropsTable from './_webPropsTable.mdx'; | ||
| import WebExamples, { toc as webExamplesToc } from './_webExamples.mdx'; | ||
| import webMetadata from './webMetadata.json'; | ||
|
|
||
| <VStack gap={5}> | ||
| <ComponentHeader title="FocusTrap" webMetadata={webMetadata} /> | ||
| <ComponentTabsContainer | ||
| webPropsTable={<WebPropsTable />} | ||
| webExamples={<WebExamples />} | ||
| webExamplesToc={webExamplesToc} | ||
| webPropsToc={webPropsToc} | ||
| /> | ||
| </VStack> |
15 changes: 15 additions & 0 deletions
15
apps/docs/docs/components/overlay/FocusTrap/webMetadata.json
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,15 @@ | ||
| { | ||
| "import": "import { FocusTrap } from '@coinbase/cds-web/overlays/FocusTrap'", | ||
| "source": "https://github.com/coinbase/cds/blob/master/packages/web/src/overlays/FocusTrap.tsx", | ||
| "description": "FocusTrap is a component that traps focus within its children.", | ||
| "relatedComponents": [ | ||
| { | ||
| "label": "Modal", | ||
| "url": "/components/overlay/Modal/" | ||
| }, | ||
| { | ||
| "label": "Tray", | ||
| "url": "/components/overlay/Tray/" | ||
| } | ||
| ] | ||
| } |
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
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
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
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
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
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
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.
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.
small nit: this note seems a little overkill with the accessibility warning shown above. But if you are worried about people removing the logic to exit the focus trap functionality I think it is fine to keep.
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.
I'd like to keep it since it's important for customers to implement exit logic. And
<FocusTrap>can't determine if exit logic has been added. If not implemented, keyboard nav users can be completely blocked resulting in critical bugs.