Skip to content

Commit b5c69b2

Browse files
authored
feat(icons): add rotate property (#4433)
1 parent f995028 commit b5c69b2

File tree

11 files changed

+65
-74
lines changed

11 files changed

+65
-74
lines changed

packages/cli/src/templates/Canvas/index.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,7 @@ const Page = () => {
182182
{
183183
id: "toggle",
184184
label: minimize ? "Maximize" : "Minimize",
185-
icon: (
186-
<DropUpXS
187-
iconSize="XS"
188-
style={{ rotate: !minimize ? "180deg" : undefined }}
189-
/>
190-
),
185+
icon: <DropUpXS size="XS" rotate={!minimize} />,
191186
},
192187
];
193188

packages/core/src/Accordion/Accordion.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ export const HvAccordion = forwardRef<
9898
onClick={handleClick}
9999
variant={labelVariant}
100100
>
101-
<DropUpXS
102-
color="inherit"
103-
style={{ rotate: isOpen ? undefined : "180deg" }}
104-
/>
101+
<DropUpXS color="inherit" rotate={!isOpen} />
105102
{label}
106103
</HvTypography>
107104
);

packages/core/src/Card/stories/Expandable.tsx

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,15 @@ export const Expandable = () => {
4848
<HvTypography>This is my hidden content</HvTypography>
4949
</HvCardContent>
5050

51-
<div
52-
style={{ display: "flex", justifyContent: "center", paddingBottom: 0 }}
51+
<HvIconButton
52+
title={isOpen ? "Collapse" : "Expand"}
53+
onClick={() => setIsOpen(!isOpen)}
54+
aria-expanded={isOpen}
55+
aria-controls={cardContentId}
56+
style={{ width: "100%", height: 16, display: "flex", paddingBottom: 0 }}
5357
>
54-
<HvIconButton
55-
title={isOpen ? "Collapse" : "Expand"}
56-
onClick={() => setIsOpen(!isOpen)}
57-
aria-expanded={isOpen}
58-
aria-controls={cardContentId}
59-
>
60-
<DropDownXS
61-
style={{
62-
width: 16,
63-
height: 16,
64-
rotate: isOpen ? "180deg" : undefined,
65-
}}
66-
/>
67-
</HvIconButton>
68-
</div>
58+
<DropDownXS rotate={isOpen} size="xs" />
59+
</HvIconButton>
6960
</HvCard>
7061
);
7162
};

packages/core/src/DropdownButton/DropdownButton.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ export const HvDropdownButton = forwardRef<
4949

5050
const { classes, cx } = useClasses(classesProp);
5151

52-
const endIcon = icon ? undefined : (
53-
<DropDownXS iconSize="XS" style={{ rotate: open ? "180deg" : undefined }} />
54-
);
52+
const endIcon = icon ? undefined : <DropDownXS size="XS" rotate={open} />;
5553

5654
return (
5755
<HvButton

packages/core/src/Forms/Suggestions/Suggestions.stories.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,8 @@ export const CustomOpen: StoryObj<HvSuggestionsProps> = {
241241
}}
242242
endAdornment={
243243
<DropDownXS
244-
style={{
245-
cursor: "pointer",
246-
rotate: open ? "180deg" : undefined,
247-
}}
244+
rotate={open}
245+
style={{ cursor: "pointer" }}
248246
onClick={(evt) => {
249247
evt.stopPropagation();
250248
setOpen((v) => !v);

packages/core/src/Section/Section.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const HvSection = forwardRef<HTMLDivElement, HvSectionProps>(
9494
{...buttonProps}
9595
{...expandButtonProps}
9696
>
97-
<Down style={{ rotate: isOpen ? "180deg" : undefined }} />
97+
<Down rotate={isOpen} />
9898
</HvButton>
9999
)}
100100
{title}

packages/core/src/Table/hooks/useRowExpand.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const CellWithExpandButton = ({
6363
aria-expanded={row.isExpanded}
6464
onClick={rowProps?.onClick}
6565
>
66-
<DropDownXS style={{ rotate: row.isExpanded ? "180deg" : undefined }} />
66+
<DropDownXS rotate={row.isExpanded} />
6767
</HvButton>
6868
{cell?.value && (
6969
<HvTypography variant="label" component="span">

packages/core/src/VerticalNavigation/TreeView/TreeViewItem.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,7 @@ export const HvVerticalNavigationTreeViewItem = forwardRef(
522522
)}
523523

524524
{isOpen && expandable && (
525-
<DropDownXS
526-
color="currentcolor"
527-
style={{ rotate: expanded ? "180deg" : undefined }}
528-
/>
525+
<DropDownXS color="currentcolor" rotate={expanded} />
529526
)}
530527
</HvTypography>
531528
</HvTooltip>

packages/icons/src/IconBase.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
theme,
99
} from "@hitachivantara/uikit-styles";
1010

11-
import { HvIconContainer } from "./IconContainer";
11+
import { HvIconContainer, HvIconContainerProps } from "./IconContainer";
1212
import { getSizeStyles } from "./utils";
1313

1414
const getColorVars = (colorArray: string[]) => {
@@ -52,8 +52,7 @@ const getIconColors = (
5252

5353
export type IconSize = "XS" | "S" | "M" | "L";
5454

55-
export interface IconBaseProps
56-
extends Omit<React.HTMLAttributes<HTMLDivElement>, "color"> {
55+
export interface IconBaseProps extends Omit<HvIconContainerProps, "color"> {
5756
/**
5857
* A color or array of colors to override the default icon colors.
5958
* Accepts any valid CSS color or color from the UI Kit palette.

packages/icons/src/IconContainer.tsx

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
import { forwardRef, ReactNode } from "react";
1+
import { forwardRef } from "react";
22
import styled from "@emotion/styled";
33
import { getColor, HvColorAny, HvSize } from "@hitachivantara/uikit-styles";
44

5-
import { IconSize } from "./IconBase";
5+
import type { IconSize } from "./IconBase";
66
import { getSizeStyles } from "./utils";
77

8-
export const StyledIconBase = styled("div")({
8+
function getRotation(rotation?: HvIconContainerProps["rotate"]) {
9+
switch (rotation) {
10+
case "up":
11+
return "-90deg";
12+
case "down":
13+
return "90deg";
14+
case true:
15+
return "180deg";
16+
case false:
17+
default:
18+
return undefined;
19+
}
20+
}
21+
22+
const StyledIconContainer = styled("div")({
923
display: "flex",
24+
flex: "0 0 auto", // ensure icon doesn't flex grow/shrink
1025
fontSize: 16,
1126
// box has a minimum size of 32px (`xs` & `sm`)
1227
width: "var(--size, 32px)",
@@ -17,7 +32,7 @@ export const StyledIconBase = styled("div")({
1732
});
1833

1934
export interface HvIconContainerProps
20-
extends Omit<React.HTMLAttributes<HTMLDivElement>, "color"> {
35+
extends React.HTMLAttributes<HTMLDivElement> {
2136
/**
2237
* A color to override the default icon colors.
2338
* Accepts any valid CSS color or color from the UI Kit palette.
@@ -38,38 +53,39 @@ export interface HvIconContainerProps
3853
* @default "S"
3954
*/
4055
size?: HvSize | IconSize | number;
41-
/**
42-
* Icon to render.
43-
*/
44-
children?: ReactNode;
56+
/** Whether to rotate the icon @private WIP */
57+
rotate?: boolean | "up" | "down";
4558
}
4659

4760
/**
4861
* A component used to contain icons in the established margins.
49-
* This allows for the use of external icons with our components that expect an icon container of a specific size.
50-
* It also makes the use of our colors easier through the `color` prop.
62+
*
63+
* This allows using external icons UI Kit components that expect padding around the icon.
64+
* It also makes the use of theme colors easier through the `color` prop.
5165
*
5266
* @example
53-
* <HvIconContainer color="warning" size="lg" />
67+
* <HvIconContainer color="warning" size="lg">
5468
* <svg />
5569
* </HvIconContainer>
56-
*
5770
*/
58-
export const HvIconContainer = forwardRef<HTMLDivElement, HvIconContainerProps>(
59-
(props, ref) => {
60-
const { size, style: styleProp, color, children, ...others } = props;
61-
return (
62-
<StyledIconBase
63-
ref={ref}
64-
style={{
65-
...getSizeStyles("", size),
66-
color: getColor(color),
67-
...styleProp,
68-
}}
69-
{...others}
70-
>
71-
{children}
72-
</StyledIconBase>
73-
);
74-
},
75-
);
71+
export const HvIconContainer = forwardRef<
72+
// no-indent
73+
HTMLDivElement,
74+
HvIconContainerProps
75+
>(function HvIconContainer(props, ref) {
76+
const { size, style, color, rotate, children, ...others } = props;
77+
return (
78+
<StyledIconContainer
79+
ref={ref}
80+
style={{
81+
...getSizeStyles("", size),
82+
color: getColor(color),
83+
rotate: getRotation(rotate),
84+
...style,
85+
}}
86+
{...others}
87+
>
88+
{children}
89+
</StyledIconContainer>
90+
);
91+
});

0 commit comments

Comments
 (0)