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] Migrate TreeView demos #26146

Merged
merged 4 commits into from
May 6, 2021
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
67 changes: 21 additions & 46 deletions docs/src/pages/components/tree-view/BarTreeView.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,44 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { makeStyles, alpha } from '@material-ui/core/styles';
import { experimentalStyled as styled, alpha } from '@material-ui/core/styles';
import TreeView from '@material-ui/lab/TreeView';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import ChevronRightIcon from '@material-ui/icons/ChevronRight';
import TreeItem, { useTreeItem } from '@material-ui/lab/TreeItem';
import clsx from 'clsx';
import Typography from '@material-ui/core/Typography';

const useStyles = makeStyles({
root: {
height: 240,
flexGrow: 1,
maxWidth: 400,
position: 'relative',
const CustomContentRoot = styled('div')(({ theme }) => ({
WebkitTapHighlightColor: 'transparent',
'&:hover, &.Mui-disabled, &.Mui-focused, &.Mui-selected, &.Mui-selected.Mui-focused, &.Mui-selected:hover': {
backgroundColor: 'transparent',
},
});

const useContentStyles = makeStyles((theme) => ({
root: {
WebkitTapHighlightColor: 'transparent',
'&:hover, &$disabled, &$focused, &$selected, &$selected$focused, &$selected:hover': {
backgroundColor: 'transparent',
},
},
label: {
width: '100%',
paddingLeft: 4,
position: 'relative',
},
bar: {
[`& .MuiTreeItem-contentBar`]: {
position: 'absolute',
width: '100%',
height: 24,
left: 0,
'$root:hover &': {
'&:hover &': {
backgroundColor: theme.palette.action.hover,
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
backgroundColor: 'transparent',
},
},
'$root$disabled &': {
'&.Mui-disabled &': {
opacity: theme.palette.action.disabledOpacity,
backgroundColor: 'transparent',
},
'$root$focused &': {
'&.Mui-focused &': {
backgroundColor: theme.palette.action.focus,
},
'$root$selected &': {
'&.Mui-selected &': {
backgroundColor: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity,
),
},
'$root$selected:hover &': {
'&.Mui-selected:hover &': {
backgroundColor: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity,
Expand All @@ -67,17 +51,13 @@ const useContentStyles = makeStyles((theme) => ({
),
},
},
'$root$selected$focused &': {
'&.Mui-selected.Mui-focused &': {
backgroundColor: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity,
),
},
},
expanded: {},
selected: {},
focused: {},
disabled: {},
}));

const CustomContent = React.forwardRef(function CustomContent(props, ref) {
Expand All @@ -91,8 +71,6 @@ const CustomContent = React.forwardRef(function CustomContent(props, ref) {
displayIcon,
} = props;

const contentClasses = useContentStyles();

const {
disabled,
expanded,
Expand All @@ -115,24 +93,23 @@ const CustomContent = React.forwardRef(function CustomContent(props, ref) {
};

return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions
<div
className={clsx(className, classes.root, contentClasses.root, {
[contentClasses.expanded]: expanded,
[contentClasses.selected]: selected,
[contentClasses.focused]: focused,
[contentClasses.disabled]: disabled,
<CustomContentRoot
className={clsx(className, classes.root, {
'Mui-expanded': expanded,
'Mui-selected': selected,
'Mui-focused': focused,
'Mui-disabled': disabled,
Copy link
Member

Choose a reason for hiding this comment

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

Interesting, for another example I remember that I used another form that perhaps should be discussed.

https://github.com/mui-org/material-ui/blob/58c7e708a419bc8895774e964b8ef00b55d045ac/docs/src/pages/components/cards/RecipeReviewCard.tsx#L23

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it is a bit of a different use-case, but I see your point. I would say that for the pseudo states, we should always rely on the pseudo classes, as we do with the core components, as sometimes it would be impossible to override some css rules if we don't bump the specificity when overriding.

For example, for the disabled state, the core components are defining the styles with:

'&.Mui-disabled': { ... },

If we don't bump the specificity in the overrides, we won't be able to override.

})}
onClick={handleClick}
onMouseDown={handleMouseDown}
ref={ref}
>
<div className={contentClasses.bar} />
<div className="MuiTreeItem-contentBar" />
<div className={classes.iconContainer}>{icon}</div>
<Typography component="div" className={classes.label}>
{label}
</Typography>
</div>
</CustomContentRoot>
);
});

Expand Down Expand Up @@ -172,14 +149,12 @@ const CustomTreeItem = (props) => (
);

export default function BarTreeView() {
const classes = useStyles();

return (
<TreeView
aria-label="icon expansion"
className={classes.root}
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
sx={{ height: 240, flexGrow: 1, maxWidth: 400, position: 'relative' }}
>
<CustomTreeItem nodeId="1" label="Applications">
<CustomTreeItem nodeId="2" label="Calendar" />
Expand Down
67 changes: 21 additions & 46 deletions docs/src/pages/components/tree-view/BarTreeView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { makeStyles, alpha } from '@material-ui/core/styles';
import { experimentalStyled as styled, alpha } from '@material-ui/core/styles';
import TreeView from '@material-ui/lab/TreeView';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import ChevronRightIcon from '@material-ui/icons/ChevronRight';
Expand All @@ -11,53 +11,37 @@ import TreeItem, {
import clsx from 'clsx';
import Typography from '@material-ui/core/Typography';

const useStyles = makeStyles({
root: {
height: 240,
flexGrow: 1,
maxWidth: 400,
position: 'relative',
const CustomContentRoot = styled('div')(({ theme }) => ({
WebkitTapHighlightColor: 'transparent',
'&:hover, &.Mui-disabled, &.Mui-focused, &.Mui-selected, &.Mui-selected.Mui-focused, &.Mui-selected:hover': {
backgroundColor: 'transparent',
},
});

const useContentStyles = makeStyles((theme) => ({
root: {
WebkitTapHighlightColor: 'transparent',
'&:hover, &$disabled, &$focused, &$selected, &$selected$focused, &$selected:hover': {
backgroundColor: 'transparent',
},
},
label: {
width: '100%',
paddingLeft: 4,
position: 'relative',
},
bar: {
[`& .MuiTreeItem-contentBar`]: {
position: 'absolute',
width: '100%',
height: 24,
left: 0,
'$root:hover &': {
'&:hover &': {
backgroundColor: theme.palette.action.hover,
// Reset on touch devices, it doesn't add specificity
'@media (hover: none)': {
backgroundColor: 'transparent',
},
},
'$root$disabled &': {
'&.Mui-disabled &': {
opacity: theme.palette.action.disabledOpacity,
backgroundColor: 'transparent',
},
'$root$focused &': {
'&.Mui-focused &': {
backgroundColor: theme.palette.action.focus,
},
'$root$selected &': {
'&.Mui-selected &': {
backgroundColor: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity,
),
},
'$root$selected:hover &': {
'&.Mui-selected:hover &': {
backgroundColor: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity,
Expand All @@ -70,17 +54,13 @@ const useContentStyles = makeStyles((theme) => ({
),
},
},
'$root$selected$focused &': {
'&.Mui-selected.Mui-focused &': {
backgroundColor: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity,
),
},
},
expanded: {},
selected: {},
focused: {},
disabled: {},
}));

const CustomContent = React.forwardRef(function CustomContent(
Expand All @@ -97,8 +77,6 @@ const CustomContent = React.forwardRef(function CustomContent(
displayIcon,
} = props;

const contentClasses = useContentStyles();

const {
disabled,
expanded,
Expand All @@ -121,24 +99,23 @@ const CustomContent = React.forwardRef(function CustomContent(
};

return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions
<div
className={clsx(className, classes.root, contentClasses.root, {
[contentClasses.expanded]: expanded,
[contentClasses.selected]: selected,
[contentClasses.focused]: focused,
[contentClasses.disabled]: disabled,
<CustomContentRoot
className={clsx(className, classes.root, {
'Mui-expanded': expanded,
'Mui-selected': selected,
'Mui-focused': focused,
'Mui-disabled': disabled,
})}
onClick={handleClick}
onMouseDown={handleMouseDown}
ref={ref as React.Ref<HTMLDivElement>}
>
<div className={contentClasses.bar} />
<div className="MuiTreeItem-contentBar" />
<div className={classes.iconContainer}>{icon}</div>
<Typography component="div" className={classes.label}>
{label}
</Typography>
</div>
</CustomContentRoot>
);
});

Expand All @@ -147,14 +124,12 @@ const CustomTreeItem = (props: TreeItemProps) => (
);

export default function BarTreeView() {
const classes = useStyles();

return (
<TreeView
aria-label="icon expansion"
className={classes.root}
defaultCollapseIcon={<ExpandMoreIcon />}
defaultExpandIcon={<ChevronRightIcon />}
sx={{ height: 240, flexGrow: 1, maxWidth: 400, position: 'relative' }}
>
<CustomTreeItem nodeId="1" label="Applications">
<CustomTreeItem nodeId="2" label="Calendar" />
Expand Down
22 changes: 5 additions & 17 deletions docs/src/pages/components/tree-view/ControlledTreeView.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import * as React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Box from '@material-ui/core/Box';
import Button from '@material-ui/core/Button';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import ChevronRightIcon from '@material-ui/icons/ChevronRight';
import TreeView from '@material-ui/lab/TreeView';
import TreeItem from '@material-ui/lab/TreeItem';

const useStyles = makeStyles((theme) => ({
root: {
height: 270,
flexGrow: 1,
maxWidth: 400,
},
actions: {
marginBottom: theme.spacing(1),
},
}));

export default function ControlledTreeView() {
const classes = useStyles();
const [expanded, setExpanded] = React.useState([]);
const [selected, setSelected] = React.useState([]);

Expand All @@ -43,15 +31,15 @@ export default function ControlledTreeView() {
};

return (
<div className={classes.root}>
<div className={classes.actions}>
<Box sx={{ height: 270, flexGrow: 1, maxWidth: 400 }}>
<Box sx={{ mb: 1 }}>
<Button onClick={handleExpandClick}>
{expanded.length === 0 ? 'Expand all' : 'Collapse all'}
</Button>
<Button onClick={handleSelectClick}>
{selected.length === 0 ? 'Select all' : 'Unselect all'}
</Button>
</div>
</Box>
<TreeView
aria-label="controlled"
defaultCollapseIcon={<ExpandMoreIcon />}
Expand All @@ -76,6 +64,6 @@ export default function ControlledTreeView() {
</TreeItem>
</TreeItem>
</TreeView>
</div>
</Box>
);
}
Loading