-
-
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
[TreeView] Simplify customization #21514
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,28 +15,7 @@ export const styles = (theme) => ({ | |
margin: 0, | ||
padding: 0, | ||
outline: 0, | ||
WebkitTapHighlightColor: 'transparent', | ||
'&:focus > $content $label': { | ||
backgroundColor: theme.palette.action.hover, | ||
}, | ||
'&$selected > $content $label': { | ||
backgroundColor: fade(theme.palette.primary.main, theme.palette.action.selectedOpacity), | ||
}, | ||
'&$selected > $content $label:hover, &$selected:focus > $content $label': { | ||
backgroundColor: fade( | ||
theme.palette.primary.main, | ||
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity, | ||
), | ||
// Reset on touch devices, it doesn't add specificity | ||
'@media (hover: none)': { | ||
backgroundColor: 'transparent', | ||
}, | ||
}, | ||
}, | ||
/* Pseudo-class applied to the root element when expanded. */ | ||
expanded: {}, | ||
/* Pseudo-class applied to the root element when selected. */ | ||
selected: {}, | ||
/* Styles applied to the `role="group"` element. */ | ||
group: { | ||
margin: 0, | ||
|
@@ -49,7 +28,43 @@ export const styles = (theme) => ({ | |
display: 'flex', | ||
alignItems: 'center', | ||
cursor: 'pointer', | ||
WebkitTapHighlightColor: 'transparent', | ||
'&:hover': { | ||
backgroundColor: theme.palette.action.hover, | ||
// Reset on touch devices, it doesn't add specificity | ||
'@media (hover: none)': { | ||
backgroundColor: 'transparent', | ||
}, | ||
}, | ||
'&$focused': { | ||
backgroundColor: theme.palette.action.focus, | ||
}, | ||
'&$selected': { | ||
backgroundColor: fade(theme.palette.primary.main, theme.palette.action.selectedOpacity), | ||
'&:hover': { | ||
backgroundColor: fade( | ||
theme.palette.primary.main, | ||
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity, | ||
), | ||
// Reset on touch devices, it doesn't add specificity | ||
'@media (hover: none)': { | ||
backgroundColor: fade(theme.palette.primary.main, theme.palette.action.selectedOpacity), | ||
}, | ||
}, | ||
'&$focused': { | ||
backgroundColor: fade( | ||
theme.palette.primary.main, | ||
theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity, | ||
), | ||
}, | ||
}, | ||
}, | ||
/* Pseudo-class applied to the content element when expanded. */ | ||
expanded: {}, | ||
/* Pseudo-class applied to the content element when selected. */ | ||
selected: {}, | ||
/* Pseudo-class applied to the content element when focused. */ | ||
focused: {}, | ||
/* Styles applied to the tree node icon and collapse/expand icon. */ | ||
iconContainer: { | ||
marginRight: 4, | ||
|
@@ -66,13 +81,6 @@ export const styles = (theme) => ({ | |
width: '100%', | ||
paddingLeft: 4, | ||
position: 'relative', | ||
'&:hover': { | ||
backgroundColor: theme.palette.action.hover, | ||
// Reset on touch devices, it doesn't add specificity | ||
'@media (hover: none)': { | ||
backgroundColor: 'transparent', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
|
@@ -189,6 +197,7 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) { | |
|
||
const handleMouseDown = (event) => { | ||
if (event.shiftKey || event.ctrlKey || event.metaKey) { | ||
// Prevent text selection | ||
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. Doesn't this also prevent focus in certain browsers or do those only focus if no meta keys are pressed? 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. You're right but because we don't depend on the focusing of a tree item for anything since we call |
||
event.preventDefault(); | ||
} | ||
|
||
|
@@ -363,16 +372,18 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) { | |
if (multiSelect) { | ||
ariaSelected = selected; | ||
} else if (selected) { | ||
// single-selection trees unset aria-selected | ||
/* single-selection trees unset aria-selected on un-selected items. | ||
* | ||
* If the tree does not support multiple selection, aria-selected | ||
* is set to true for the selected node and it is not present on any other node in the tree. | ||
* Source: https://www.w3.org/TR/wai-aria-practices/#TreeView | ||
*/ | ||
ariaSelected = true; | ||
} | ||
|
||
return ( | ||
<li | ||
className={clsx(classes.root, className, { | ||
[classes.expanded]: expanded, | ||
[classes.selected]: selected, | ||
})} | ||
className={clsx(classes.root, className)} | ||
role="treeitem" | ||
onKeyDown={handleKeyDown} | ||
onFocus={handleFocus} | ||
|
@@ -383,7 +394,11 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) { | |
{...other} | ||
> | ||
<div | ||
className={classes.content} | ||
className={clsx(classes.content, { | ||
[classes.expanded]: expanded, | ||
[classes.selected]: selected, | ||
[classes.focused]: focused, | ||
})} | ||
onClick={handleClick} | ||
onMouseDown={handleMouseDown} | ||
ref={contentRef} | ||
|
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.
Included from the other PR.