Skip to content

Commit

Permalink
LG-4422: Flip chevron icon and add animation (#2443)
Browse files Browse the repository at this point in the history
* Flip chevron icon and add animation for expand/collapse UIs

* Changeset for expandable-card and menu
  • Loading branch information
stephl3 authored Aug 9, 2024
1 parent c1b8b63 commit af96e39
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/many-pots-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@leafygreen-ui/expandable-card': patch
'@leafygreen-ui/menu': patch
---

[LG-4422](https://jira.mongodb.org/browse/LG-4422) Update chevron icon to properly indicate if interaction will expand or collapse and add animation
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const iconStyle = css`
grid-column: 2;
grid-row: 1;
color: ${palette.gray.base};
transition: transform ${transitionDuration}ms ease-in-out;
`;

export const iconThemeStyle: Record<Theme, string> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const ExpandableCard = ({
aria-label={`${isOpen ? 'collapse' : 'expand'} card`}
tabIndex={0}
>
<Icon glyph="ChevronUp" size={24} />
<Icon glyph="ChevronDown" size={24} />
</IconButton>
)}
</Transition>
Expand Down
21 changes: 17 additions & 4 deletions packages/menu/src/SubMenu/SubMenu.styles.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { css } from '@leafygreen-ui/emotion';
import { css, cx } from '@leafygreen-ui/emotion';
import { createUniqueClassName, Theme } from '@leafygreen-ui/lib';
import { spacing, transitionDuration } from '@leafygreen-ui/tokens';

import { LGIDs } from '../constants';
import { menuItemContainerStyles } from '../MenuItem/MenuItem.styles';
import { menuColor } from '../styles';

const TRANSITION_DURATION = transitionDuration.default;

export const subMenuContainerClassName = createUniqueClassName(LGIDs.submenu);
export const subMenuToggleClassName = createUniqueClassName(
LGIDs.submenu + '-trigger',
Expand All @@ -16,13 +18,15 @@ export const subMenuContainerStyles = css`
position: relative;
`;

export const getSubmenuToggleStyles = (theme: Theme) => css`
const getBaseSubmenuToggleStyles = (theme: Theme) => css`
position: absolute;
right: ${spacing[300]}px;
// Ensure the trigger is centered regardless of element height
top: 50%;
transform: translateY(-50%);
translate: 0 -50%;
rotate: 0deg;
color: ${menuColor[theme].icon.default};
transition: rotate ${TRANSITION_DURATION}ms ease-in-out;
&:hover {
color: ${menuColor[theme].icon.hover};
Expand All @@ -33,13 +37,22 @@ export const getSubmenuToggleStyles = (theme: Theme) => css`
}
`;

const submenuToggleExpandedStyle = css`
rotate: 180deg;
`;

export const getSubmenuToggleStyles = (theme: Theme, open: boolean) =>
cx(subMenuToggleClassName, getBaseSubmenuToggleStyles(theme), {
[submenuToggleExpandedStyle]: open,
});

export const getSubmenuListStyles = () => css`
list-style: none;
margin: 0;
padding: 0;
max-height: 0;
height: max-content;
overflow: hidden;
transition: max-height ${transitionDuration.default}ms ease-in-out;
transition: max-height ${TRANSITION_DURATION}ms ease-in-out;
position: relative;
`;
11 changes: 2 additions & 9 deletions packages/menu/src/SubMenu/SubMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import PropTypes from 'prop-types';
import { useDescendant } from '@leafygreen-ui/descendants';
import { css, cx } from '@leafygreen-ui/emotion';
import ChevronDownIcon from '@leafygreen-ui/icon/dist/ChevronDown';
import ChevronUpIcon from '@leafygreen-ui/icon/dist/ChevronUp';
import IconButton from '@leafygreen-ui/icon-button';
import { keyMap } from '@leafygreen-ui/lib';
import {
Expand All @@ -34,7 +33,6 @@ import {
getSubmenuToggleStyles,
subMenuContainerClassName,
subMenuContainerStyles,
subMenuToggleClassName,
} from './SubMenu.styles';
import { InternalSubMenuProps } from './SubMenu.types';
import { useChildrenHeight } from './useChildrenHeight';
Expand Down Expand Up @@ -100,8 +98,6 @@ export const SubMenu = InferredPolymorphic<InternalSubMenuProps, 'button'>(
const submenuTriggerRef = useRef<HTMLButtonElement>(null);
const subMenuHeight = useChildrenHeight(submenuRef, [open]);

const ChevronIcon = open ? ChevronDownIcon : ChevronUpIcon;

const handleClick: MouseEventHandler = e => {
if (onClick || rest.href) {
if (!disabled) {
Expand Down Expand Up @@ -210,12 +206,9 @@ export const SubMenu = InferredPolymorphic<InternalSubMenuProps, 'button'>(
aria-label={open ? 'Close Sub-menu' : 'Open Sub-menu'}
onClick={handleToggleClick}
onMouseDownCapture={handleToggleMouseDown}
className={cx(
subMenuToggleClassName,
getSubmenuToggleStyles(theme),
)}
className={getSubmenuToggleStyles(theme, open)}
>
<ChevronIcon role="presentation" />
<ChevronDownIcon role="presentation" />
</IconButton>
</li>
<SubMenuProvider depth={depth + 1} hasIcon={!!rest.glyph}>
Expand Down

0 comments on commit af96e39

Please sign in to comment.