-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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 a11y section to Tabs #20965
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,102 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import AppBar from '@material-ui/core/AppBar'; | ||
import Tabs from '@material-ui/core/Tabs'; | ||
import Tab from '@material-ui/core/Tab'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import Box from '@material-ui/core/Box'; | ||
|
||
function TabPanel(props) { | ||
const { children, value, index, ...other } = props; | ||
|
||
return ( | ||
<div | ||
role="tabpanel" | ||
hidden={value !== index} | ||
id={`simple-tabpanel-${index}`} | ||
aria-labelledby={`simple-tab-${index}`} | ||
{...other} | ||
> | ||
{value === index && ( | ||
<Box p={3}> | ||
<Typography>{children}</Typography> | ||
</Box> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
TabPanel.propTypes = { | ||
children: PropTypes.node, | ||
index: PropTypes.number.isRequired, | ||
value: PropTypes.number.isRequired, | ||
}; | ||
|
||
function DemoTabs(props) { | ||
const { labelId, onChange, selectionFollowsFocus, value } = props; | ||
|
||
return ( | ||
<AppBar position="static"> | ||
<Tabs | ||
aria-labelledby={labelId} | ||
onChange={onChange} | ||
selectionFollowsFocus={selectionFollowsFocus} | ||
value={value} | ||
> | ||
<Tab label="Item One" aria-controls="simple-tabpanel-0" id="simple-tab-0" /> | ||
<Tab label="Item Two" aria-controls="simple-tabpanel-1" id="simple-tab-1" /> | ||
<Tab label="Item Three" aria-controls="simple-tabpanel-2" id="simple-tab-2" /> | ||
</Tabs> | ||
</AppBar> | ||
); | ||
} | ||
|
||
DemoTabs.propTypes = { | ||
labelId: PropTypes.string.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
selectionFollowsFocus: PropTypes.bool, | ||
value: PropTypes.number.isRequired, | ||
}; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
flexGrow: 1, | ||
}, | ||
}); | ||
|
||
export default function AccessibleTabs() { | ||
const classes = useStyles(); | ||
|
||
const [value, setValue] = React.useState(0); | ||
const handleChange = (event, newValue) => { | ||
setValue(newValue); | ||
}; | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<Typography id="demo-a11y-tabs-automatic-label"> | ||
Tabs where selection follows focus | ||
</Typography> | ||
<DemoTabs | ||
labelId="demo-a11y-tabs-automatic-label" | ||
selectionFollowsFocus | ||
onChange={handleChange} | ||
value={value} | ||
/> | ||
<Typography id="demo-a11y-tabs-manual-label"> | ||
Tabs where each tab needs to be selected manually | ||
</Typography> | ||
<DemoTabs labelId="demo-a11y-tabs-manual-label" onChange={handleChange} value={value} /> | ||
<TabPanel value={value} index={0}> | ||
Item One | ||
</TabPanel> | ||
<TabPanel value={value} index={1}> | ||
Item Two | ||
</TabPanel> | ||
<TabPanel value={value} index={2}> | ||
Item Three | ||
</TabPanel> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React from 'react'; | ||
import { makeStyles } from '@material-ui/core/styles'; | ||
import AppBar from '@material-ui/core/AppBar'; | ||
import Tabs from '@material-ui/core/Tabs'; | ||
import Tab from '@material-ui/core/Tab'; | ||
import Typography from '@material-ui/core/Typography'; | ||
import Box from '@material-ui/core/Box'; | ||
|
||
interface TabPanelProps { | ||
children?: React.ReactNode; | ||
index: number; | ||
value: number; | ||
} | ||
|
||
function TabPanel(props: TabPanelProps) { | ||
const { children, value, index, ...other } = props; | ||
|
||
return ( | ||
<div | ||
role="tabpanel" | ||
hidden={value !== index} | ||
id={`simple-tabpanel-${index}`} | ||
aria-labelledby={`simple-tab-${index}`} | ||
{...other} | ||
> | ||
{value === index && ( | ||
<Box p={3}> | ||
<Typography>{children}</Typography> | ||
</Box> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
interface DemoTabsProps { | ||
labelId: string; | ||
onChange: (event: unknown, value: number) => void; | ||
selectionFollowsFocus?: boolean; | ||
value: number; | ||
} | ||
function DemoTabs(props: DemoTabsProps) { | ||
const { labelId, onChange, selectionFollowsFocus, value } = props; | ||
|
||
return ( | ||
<AppBar position="static"> | ||
<Tabs | ||
aria-labelledby={labelId} | ||
onChange={onChange} | ||
selectionFollowsFocus={selectionFollowsFocus} | ||
value={value} | ||
> | ||
<Tab label="Item One" aria-controls="simple-tabpanel-0" id="simple-tab-0" /> | ||
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. It seems to duplicate with another id on the page https://validator.w3.org/nu/?doc=https%3A%2F%2Fmaster--material-ui.netlify.app%2Fcomponents%2Ftabs%2F. |
||
<Tab label="Item Two" aria-controls="simple-tabpanel-1" id="simple-tab-1" /> | ||
<Tab label="Item Three" aria-controls="simple-tabpanel-2" id="simple-tab-2" /> | ||
</Tabs> | ||
</AppBar> | ||
); | ||
} | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
flexGrow: 1, | ||
}, | ||
}); | ||
|
||
export default function AccessibleTabs() { | ||
const classes = useStyles(); | ||
|
||
const [value, setValue] = React.useState(0); | ||
const handleChange = (event: unknown, newValue: number) => { | ||
setValue(newValue); | ||
}; | ||
|
||
return ( | ||
<div className={classes.root}> | ||
<Typography id="demo-a11y-tabs-automatic-label"> | ||
Tabs where selection follows focus | ||
</Typography> | ||
<DemoTabs | ||
labelId="demo-a11y-tabs-automatic-label" | ||
selectionFollowsFocus | ||
onChange={handleChange} | ||
value={value} | ||
/> | ||
<Typography id="demo-a11y-tabs-manual-label"> | ||
Tabs where each tab needs to be selected manually | ||
</Typography> | ||
<DemoTabs labelId="demo-a11y-tabs-manual-label" onChange={handleChange} value={value} /> | ||
<TabPanel value={value} index={0}> | ||
Item One | ||
</TabPanel> | ||
<TabPanel value={value} index={1}> | ||
Item Two | ||
</TabPanel> | ||
<TabPanel value={value} index={2}> | ||
Item Three | ||
</TabPanel> | ||
</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
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.
Should it use the lab API instead? I will help to gain more feedback.
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 lean more towards this now, yes. For a11y attributes on the stable API we can refer to the previous demos.