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

[docs] Add "reset focus" control to demo tools #20724

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 35 additions & 1 deletion docs/src/modules/components/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import MenuItem from '@material-ui/core/MenuItem';
import MoreVertIcon from '@material-ui/icons/MoreVert';
import Tooltip from '@material-ui/core/Tooltip';
import RefreshIcon from '@material-ui/icons/Refresh';
import ResetFocusIcon from '@material-ui/icons/CenterFocusWeak';
import HighlightedCode from 'docs/src/modules/components/HighlightedCode';
import DemoSandboxed from 'docs/src/modules/components/DemoSandboxed';
import DemoLanguages from 'docs/src/modules/components/DemoLanguages';
Expand Down Expand Up @@ -61,6 +62,9 @@ const styles = (theme) => ({
[theme.breakpoints.up('sm')]: {
borderRadius: theme.shape.borderRadius,
},
'&:focus': {
outline: `2px dashed ${theme.palette.text.primary}`,
},
},
/* Isolate the demo with an outline. */
demoBgOutlined: {
Expand Down Expand Up @@ -335,18 +339,33 @@ function Demo(props) {
const demoSourceId = useUniqueId(`demo-`);
const openDemoSource = codeOpen || showPreview;

const initialFocusRef = React.useRef(null);
function handleResetFocusClick() {
initialFocusRef.current.focus();
}
function handleDemoMouseDown(event) {
// Otherwise clicking any non-focusable element in the demo focuses the demo container
// which is surprising and not how the code would behave outside of this page
event.preventDefault();
Copy link
Member

@oliviertassinari oliviertassinari Apr 26, 2020

Choose a reason for hiding this comment

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

I have discovered a negative side effect. Developers will no longer be able to copy and paste from the demos. For instance, try copy and paste the output of the playground in your project (my use case for making a new demo with #20755)

https://master--material-ui.netlify.app/components/popover/#anchor-playground

=> we can't.

Copy link
Member Author

Choose a reason for hiding this comment

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

Going back to the original implementation.

}

return (
<div className={classes.root}>
{/* We're actually preventing interaction here */}
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
<div
aria-label={t('initialFocusLabel')}
className={clsx(classes.demo, {
[classes.demoHiddenToolbar]: demoOptions.hideToolbar,
[classes.demoBgOutlined]: demoOptions.bg === 'outlined',
[classes.demoBgTrue]: demoOptions.bg === true,
[classes.demoBgInline]: demoOptions.bg === 'inline',
})}
tabIndex={-1}
onMouseEnter={handleDemoHover}
onMouseLeave={handleDemoHover}
onMouseDown={handleDemoMouseDown}
ref={initialFocusRef}
tabIndex={-1}
>
<DemoSandboxed
key={demoKey}
Expand Down Expand Up @@ -426,6 +445,21 @@ function Demo(props) {
<FileCopyIcon fontSize="small" />
</IconButton>
</Tooltip>
<Tooltip
classes={{ popper: classes.tooltip }}
title={t('resetFocus')}
placement="top"
>
<IconButton
aria-label={t('resetFocus')}
data-ga-event-category="demo"
data-ga-event-label={demoOptions.demo}
data-ga-event-action="reset-focus"
onClick={handleResetFocusClick}
>
<ResetFocusIcon fontSize="small" />
</IconButton>
</Tooltip>
<Tooltip classes={{ popper: classes.tooltip }} title={t('resetDemo')} placement="top">
<IconButton
aria-label={t('resetDemo')}
Expand Down
2 changes: 2 additions & 0 deletions docs/translations/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
"viewGitHub": "View the source on GitHub",
"visit": "Visit the website",
"whosUsing": "Who's using Material-UI?",
"initialFocusLabel": "A generic container that is programmatically focused to test keyboard navigation of our components.",
"resetFocus": "Reset focus to test keyboard navigation",
"pages": {
"/getting-started": "Getting Started",
"/getting-started/installation": "Installation",
Expand Down