Skip to content

Commit

Permalink
MOON-351: Add highlight state for TreeView and MenuItem (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffffffelix authored Nov 16, 2023
1 parent ce7221d commit 6ec65a4
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 357 deletions.
3 changes: 2 additions & 1 deletion src/components/Menu/Menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const Default: Story<MenuProps> = args => (
<MenuItem label="Variants" variant="title"/>
<MenuItem isHover label="Item3 - Hover"/>
<MenuItem isDisabled label="Item3 - Disabled"/>
<MenuItem isSelected label="Item3 - Selected"/>
<MenuItem isHighlighted label="Item3 - Highlighted"/>
<MenuItem isSelected label="Item4 - Selected"/>
</Menu>
);
Default.args = {
Expand Down
6 changes: 6 additions & 0 deletions src/components/Menu/MenuItem.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
background-color: var(--color-accent);
}

&.moonstone-highlighted {
color: var(--color-light);

background-color: var(--color-gray60);
}

&:focus {
outline: none;
box-shadow: inset 0 0 0 2px var(--color-accent);
Expand Down
4 changes: 3 additions & 1 deletion src/components/Menu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {MenuItemProps, MenuItemVariants} from './MenuItem.types';
export const MenuItem: React.FC<MenuItemProps> = ({
variant = MenuItemVariants.Default,
isHover,
isSelected,
isSelected = false,
isDisabled = false,
isHighlighted = false,
iconStart = null,
iconSize = null,
iconEnd = null,
Expand All @@ -27,6 +28,7 @@ export const MenuItem: React.FC<MenuItemProps> = ({
'moonstone-hover': isHover,
'moonstone-selected': isSelected,
'moonstone-disabled': isDisabled,
'moonstone-highlighted': isHighlighted && !isSelected,
'moonstone-title': variant === MenuItemVariants.Title
},
image && 'moonstone-menuItem-image',
Expand Down
5 changes: 5 additions & 0 deletions src/components/Menu/MenuItem.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export type MenuItemProps = {
*/
isDisabled?: boolean,

/**
* Is item highlighted, cannot be selected at the same time
*/
isHighlighted?: boolean,

/**
* MenuItem label
*/
Expand Down
13 changes: 9 additions & 4 deletions src/components/TreeView/ControlledTreeView.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';
import clsx from 'clsx';
import './TreeView.scss';
import {ControlledTreeViewProps} from './ControlledTreeView.types';
import {TreeViewData} from './TreeView.types';
import type {ControlledTreeViewProps, TreeViewData} from './TreeView.types';

import {Loading, ChevronDown, ChevronRight, CheckboxChecked, CheckboxUnchecked} from '~/icons';
import {Typography} from '~/components/Typography';
Expand Down Expand Up @@ -33,6 +32,7 @@ const ControlledTreeViewForwardRef: React.ForwardRefRenderFunction<HTMLUListElem
data,
openedItems = [],
selectedItems = [],
highlightedItems = [],
showCheckbox = false,
onClickItem = () => undefined,
onDoubleClickItem = () => undefined,
Expand All @@ -55,6 +55,7 @@ const ControlledTreeViewForwardRef: React.ForwardRefRenderFunction<HTMLUListElem
const isOpen = Boolean(openedItems.includes(node.id)) || !isClosable;
const isLoading = Boolean(node.isLoading);
const isSelected = Boolean(selectedItems.includes(node.id));
const isHighlighted = Boolean(highlightedItems.includes(node.id) && !isSelected);

// ---
// Manage clicks events
Expand Down Expand Up @@ -93,6 +94,7 @@ const ControlledTreeViewForwardRef: React.ForwardRefRenderFunction<HTMLUListElem
{
'moonstone-small': size === 'small',
'moonstone-selected': isSelected && !showCheckbox,
'moonstone-highlighted': isHighlighted,
'moonstone-reversed': isReversed,
'moonstone-disabled': node.isDisabled
}
Expand All @@ -103,7 +105,10 @@ const ControlledTreeViewForwardRef: React.ForwardRefRenderFunction<HTMLUListElem
itemComponent,
{
role: 'treeitem',
'aria-selected': isSelected,
'aria-expanded': isOpen,
'aria-busy': isLoading,
'aria-current': isHighlighted ? 'page' : null,
key: `${depth}-${node.id}`,
style: {'--treeItem-depth': depth},
...node.treeItemProps
Expand All @@ -129,7 +134,7 @@ const ControlledTreeViewForwardRef: React.ForwardRefRenderFunction<HTMLUListElem
onContextMenu={handleNodeContextMenu}
>
{showCheckbox ?
(isSelected ? <CheckboxChecked className="moonstone-treeView_itemIconStart" role="checkbox" color="blue"/> : <CheckboxUnchecked className="moonstone-treeView_itemIconStart" role="checkbox"/>) :
(isSelected ? <CheckboxChecked className="moonstone-treeView_itemIconStart" role="checkbox" color="blue" aria-checked="true"/> : <CheckboxUnchecked className="moonstone-treeView_itemIconStart" role="checkbox" aria-checked="false"/>) :
(displayIcon(node.iconStart, 'small', 'moonstone-treeView_itemIconStart', parentHasIconStart))}
<Typography isNowrap
className={clsx('flexFluid')}
Expand All @@ -149,7 +154,7 @@ const ControlledTreeViewForwardRef: React.ForwardRefRenderFunction<HTMLUListElem
}

// TreeView component
return React.createElement(component, {ref, role: 'tree', ...props}, generateLevelJSX(data, 0, false));
return React.createElement(component, {ref, role: 'tree', 'aria-multiselectable': showCheckbox, ...props}, generateLevelJSX(data, 0, false));
};

export const ControlledTreeView = React.forwardRef(ControlledTreeViewForwardRef);
70 changes: 0 additions & 70 deletions src/components/TreeView/ControlledTreeView.types.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/components/TreeView/TreeView.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
--treeItem-color: inherit;
--treeItem-color_hover: var(--color-dark);
--treeItem-color_selected: var(--color-light);
--treeItem-color_highlighted: var(--color-light);
--treeItem-color_selected_hover: var(--color-light);
--treeItem-background: inherit;
--treeItem-background_hover: var(--color-gray_light40);
--treeItem-background_selected: var(--color-accent);
--treeItem-background_selected_hover: var(--color-accent);
--treeItem-background_highlighted: var(--color-gray60);
}

.moonstone-treeView_item {
Expand Down Expand Up @@ -44,11 +46,18 @@
}
}

&.moonstone-highlighted {
color: var(--treeItem-color_highlighted);

background-color: var(--treeItem-background_highlighted);
}

// Theme reversed
&.moonstone-reversed {
--treeItem-color: var(--color-gray_light);
--treeItem-color_hover: var(--color-light);
--treeItem-background_hover: var(--color-gray40);
--treeItem-background_highlighted: var(--color-gray_light40);
}

&.moonstone-disabled {
Expand Down
Loading

0 comments on commit 6ec65a4

Please sign in to comment.