Skip to content

Commit

Permalink
[docs] Add a customization example with ToggleButton (#17401)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrkroeker authored and oliviertassinari committed Sep 14, 2019
1 parent a0c9713 commit 8d5a09a
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
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

0 comments on commit 8d5a09a

Please sign in to comment.