-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[docs] Base ClickAwayListener style revisions and final review #32156
Merged
mapache-salvaje
merged 12 commits into
mui:master
from
mapache-salvaje:base-cal-style-review
Apr 29, 2022
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8d4d1a6
style consistency, some added details
samuelsycamore 796fcf5
clarifying the description
samuelsycamore 4de1812
update basic usage section
samuelsycamore baedd85
Update docs/data/base/components/click-away-listener/click-away-liste…
mapache-salvaje 9c296cf
Update docs/data/base/components/click-away-listener/click-away-liste…
mapache-salvaje 7d5b983
merge master
samuelsycamore 999a74d
clarity on portals, singular child
samuelsycamore 9438cb6
mixed up popper and portal
samuelsycamore 10f5258
line break
samuelsycamore 9d546bd
back ticks
samuelsycamore a87c43a
rearranging
samuelsycamore 3d29e61
link
samuelsycamore 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
33 changes: 17 additions & 16 deletions
33
docs/data/base/components/click-away-listener/click-away-listener.md
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 |
---|---|---|
@@ -1,58 +1,59 @@ | ||
--- | ||
product: base | ||
title: Detect click outside React component | ||
title: React ClickAwayListener component | ||
components: ClickAwayListener | ||
githubLabel: 'component: ClickAwayListener' | ||
packageName: '@mui/base' | ||
--- | ||
|
||
# Click-away listener | ||
|
||
<p class="description">Detect if a click event happened outside of an element. It listens for clicks that occur somewhere in the document.</p> | ||
<p class="description">The <code>ClickAwayListener</code> component detects when a click event happens outside of its children elements.</p> | ||
|
||
- 📦 [981 B gzipped](/size-snapshot). | ||
- 📦 [981 B gzipped](/size-snapshot) | ||
- ⚛️ Supports portals | ||
mapache-salvaje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
{{"component": "modules/components/ComponentLinkHeader.js", "design": false}} | ||
|
||
## Example | ||
## Basic usage | ||
|
||
For instance, if you need to hide a menu dropdown when people click anywhere else on your page: | ||
The following demo shows how to hide a menu dropdown when users click anywhere else on the page: | ||
|
||
{{"demo": "ClickAway.js"}} | ||
|
||
Notice that the component only accepts one child element. | ||
> **Note:** the `ClickAwayListener` component only accepts _one_ child element. | ||
|
||
You can find a more advanced demo on the [Menu documentation section](/components/menus/#menulist-composition). | ||
|
||
## Portal | ||
mapache-salvaje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The following demo uses [`Portal`](/base/react-portal/) to render the dropdown into a new "subtree" outside of current DOM hierarchy. | ||
The following demo uses the [`Portal`](/base/react-portal/) component to render the dropdown into a new subtree outside of the current DOM hierarchy: | ||
|
||
{{"demo": "PortalClickAway.js"}} | ||
|
||
## Leading edge | ||
|
||
By default, the component responds to the trailing events (click + touch end). | ||
However, you can configure it to respond to the leading events (mouse down + touch start). | ||
By default, the `ClickAwayListener` component responds to **trailing events**—the _end_ of a click or touch. | ||
|
||
You can set the component to listen for **leading events** (the start of a click or touch) using the `mouseEvent` and `touchEvent` props, as shown in the following demo: | ||
|
||
{{"demo": "LeadingClickAway.js"}} | ||
|
||
> ⚠️ In this mode, only interactions on the scrollbar of the document is ignored. | ||
> ⚠️ **Note:** When the component is set to listen for leading events, interactions with the scrollbar are ignored. | ||
|
||
## Accessibility | ||
|
||
By default `<ClickAwayListener />` will add an `onClick` handler to its children. | ||
This can result in e.g. screen readers announcing the children as clickable. | ||
However, the purpose of the `onClick` handler is not to make `children` interactive. | ||
By default, `ClickAwayListener` will add an `onClick` handler to its children. | ||
This can result in screen readers announcing that the children are clickable, even though this `onClick` handler has no effect on the childen elements themselves. | ||
mapache-salvaje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In order to prevent screen readers from marking non-interactive children as "clickable" add `role="presentation"` to the immediate children: | ||
To prevent this behavior, add `role="presentation"` to the immediate children: | ||
mapache-salvaje marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```tsx | ||
<ClickAwayListener> | ||
<div role="presentation"> | ||
<h1>non-interactive heading</h1> | ||
</div> | ||
</ClickAwayListern> | ||
</ClickAwayListener> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a general note, it can be great to search for other instances of the problem in the codebase. It's not rare to have the problem not isolated e.g. #33813 😁. |
||
``` | ||
|
||
This is also required to fix a quirk in NVDA when using Firefox that prevents announcement of alert messages (see [mui/material-ui#29080](https://github.com/mui/material-ui/issues/29080)). | ||
This is also required to fix a known issue in NVDA when using Firefox that prevents the announcement of alert messages—see [mui/material-ui#29080](https://github.com/mui/material-ui/issues/29080) for details. |
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.
This title was on purpose for SEO. See https://stackoverflow.com/questions/32553158/detect-click-outside-react-component for why. Sure it might not be for the title but we still need to rank for this search intent:
I have updated https://stackoverflow.com/a/59913572/2801714 to point to the latest URL.
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.
We can revert this title in the final pass of the Base docs. I'll make a note of it.