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 a customization example with ToggleButton #17401

Merged
merged 5 commits into from
Sep 14, 2019
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
105 changes: 105 additions & 0 deletions docs/src/pages/components/toggle-button/CustomizedDividers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import React from 'react';
import { makeStyles, withStyles } from '@material-ui/core/styles';
import FormatAlignLeftIcon from '@material-ui/icons/FormatAlignLeft';
import FormatAlignCenterIcon from '@material-ui/icons/FormatAlignCenter';
import FormatAlignRightIcon from '@material-ui/icons/FormatAlignRight';
import FormatAlignJustifyIcon from '@material-ui/icons/FormatAlignJustify';
import FormatBoldIcon from '@material-ui/icons/FormatBold';
import FormatItalicIcon from '@material-ui/icons/FormatItalic';
import FormatUnderlinedIcon from '@material-ui/icons/FormatUnderlined';
import FormatColorFillIcon from '@material-ui/icons/FormatColorFill';
import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown';
import Divider from '@material-ui/core/Divider';
import Paper from '@material-ui/core/Paper';
import ToggleButton from '@material-ui/lab/ToggleButton';
import ToggleButtonGroup from '@material-ui/lab/ToggleButtonGroup';

const useStyles = makeStyles(theme => ({
paper: {
display: 'flex',
border: `1px solid ${theme.palette.divider}`,
flexWrap: 'wrap',
},
divider: {
alignSelf: 'stretch',
height: 'auto',
margin: theme.spacing(1, 0.5),
},
}));

const StyledToggleButtonGroup = withStyles(theme => ({
grouped: {
margin: theme.spacing(0.5),
border: 'none',
padding: theme.spacing(0, 1),
'&:not(:first-child)': {
borderRadius: theme.shape.borderRadius,
},
'&:first-child': {
borderRadius: theme.shape.borderRadius,
},
},
}))(ToggleButtonGroup);

export default function CustomizedDividers() {
const [alignment, setAlignment] = React.useState('left');
const [formats, setFormats] = React.useState(() => ['italic']);

const handleFormat = (event, newFormats) => {
setFormats(newFormats);
};

const handleAlignment = (event, newAlignment) => {
setAlignment(newAlignment);
};

const classes = useStyles();

return (
<div>
<Paper elevation={0} className={classes.paper}>
<StyledToggleButtonGroup
size="small"
value={alignment}
exclusive
onChange={handleAlignment}
aria-label="text alignment"
>
<ToggleButton value="left" aria-label="left aligned">
<FormatAlignLeftIcon />
</ToggleButton>
<ToggleButton value="center" aria-label="centered">
<FormatAlignCenterIcon />
</ToggleButton>
<ToggleButton value="right" aria-label="right aligned">
<FormatAlignRightIcon />
</ToggleButton>
<ToggleButton value="justify" aria-label="justified" disabled>
<FormatAlignJustifyIcon />
</ToggleButton>
</StyledToggleButtonGroup>
<Divider orientation="vertical" className={classes.divider} />
<StyledToggleButtonGroup
size="small"
value={formats}
onChange={handleFormat}
arial-label="text formatting"
>
<ToggleButton value="bold" aria-label="bold">
<FormatBoldIcon />
</ToggleButton>
<ToggleButton value="italic" aria-label="italic">
<FormatItalicIcon />
</ToggleButton>
<ToggleButton value="underlined" aria-label="underlined">
<FormatUnderlinedIcon />
</ToggleButton>
<ToggleButton value="color" aria-label="color" disabled>
<FormatColorFillIcon />
<ArrowDropDownIcon />
</ToggleButton>
</StyledToggleButtonGroup>
</Paper>
</div>
);
}
107 changes: 107 additions & 0 deletions docs/src/pages/components/toggle-button/CustomizedDividers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import React from 'react';
import { makeStyles, withStyles, Theme, createStyles } from '@material-ui/core/styles';
import FormatAlignLeftIcon from '@material-ui/icons/FormatAlignLeft';
import FormatAlignCenterIcon from '@material-ui/icons/FormatAlignCenter';
import FormatAlignRightIcon from '@material-ui/icons/FormatAlignRight';
import FormatAlignJustifyIcon from '@material-ui/icons/FormatAlignJustify';
import FormatBoldIcon from '@material-ui/icons/FormatBold';
import FormatItalicIcon from '@material-ui/icons/FormatItalic';
import FormatUnderlinedIcon from '@material-ui/icons/FormatUnderlined';
import FormatColorFillIcon from '@material-ui/icons/FormatColorFill';
import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown';
import Divider from '@material-ui/core/Divider';
import Paper from '@material-ui/core/Paper';
import ToggleButton from '@material-ui/lab/ToggleButton';
import ToggleButtonGroup from '@material-ui/lab/ToggleButtonGroup';

const useStyles = makeStyles((theme: Theme) =>
createStyles({
paper: {
display: 'flex',
border: `1px solid ${theme.palette.divider}`,
flexWrap: 'wrap',
},
divider: {
alignSelf: 'stretch',
height: 'auto',
margin: theme.spacing(1, 0.5),
},
}),
);

const StyledToggleButtonGroup = withStyles(theme => ({
grouped: {
margin: theme.spacing(0.5),
border: 'none',
padding: theme.spacing(0, 1),
'&:not(:first-child)': {
borderRadius: theme.shape.borderRadius,
},
'&:first-child': {
borderRadius: theme.shape.borderRadius,
},
},
}))(ToggleButtonGroup);

export default function CustomizedDividers() {
const [alignment, setAlignment] = React.useState('left');
const [formats, setFormats] = React.useState(() => ['italic']);

const handleFormat = (event: React.MouseEvent<HTMLElement>, newFormats: string[]) => {
setFormats(newFormats);
};

const handleAlignment = (event: React.MouseEvent<HTMLElement>, newAlignment: string) => {
setAlignment(newAlignment);
};

const classes = useStyles();

return (
<div>
<Paper elevation={0} className={classes.paper}>
<StyledToggleButtonGroup
size="small"
value={alignment}
exclusive
onChange={handleAlignment}
aria-label="text alignment"
>
<ToggleButton value="left" aria-label="left aligned">
<FormatAlignLeftIcon />
</ToggleButton>
<ToggleButton value="center" aria-label="centered">
<FormatAlignCenterIcon />
</ToggleButton>
<ToggleButton value="right" aria-label="right aligned">
<FormatAlignRightIcon />
</ToggleButton>
<ToggleButton value="justify" aria-label="justified" disabled>
<FormatAlignJustifyIcon />
</ToggleButton>
</StyledToggleButtonGroup>
<Divider orientation="vertical" className={classes.divider} />
<StyledToggleButtonGroup
size="small"
value={formats}
onChange={handleFormat}
arial-label="text formatting"
>
<ToggleButton value="bold" aria-label="bold">
<FormatBoldIcon />
</ToggleButton>
<ToggleButton value="italic" aria-label="italic">
<FormatItalicIcon />
</ToggleButton>
<ToggleButton value="underlined" aria-label="underlined">
<FormatUnderlinedIcon />
</ToggleButton>
<ToggleButton value="color" aria-label="color" disabled>
<FormatColorFillIcon />
<ArrowDropDownIcon />
</ToggleButton>
</StyledToggleButtonGroup>
</Paper>
</div>
);
}
7 changes: 7 additions & 0 deletions docs/src/pages/components/toggle-button/toggle-button.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ Fancy larger or smaller buttons? Use the `size` property.

{{"demo": "pages/components/toggle-button/StandaloneToggleButton.js"}}

## Customized toggle button

Here is an example of customizing the component. You can learn more about this in the
[overrides documentation page](/customization/components/).

{{"demo": "pages/components/toggle-button/CustomizedDividers.js"}}

## Accessibility

ToggleButtonGroup has `role="group"`. You should provide an accessible label with `arial-label="label"`, `aria-labelledby="id"` or `<label>`.
Expand Down