forked from Workday/canvas-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpandableIcon.tsx
80 lines (76 loc) · 2.46 KB
/
ExpandableIcon.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React from 'react';
import {createSubcomponent, ExtractProps} from '@workday/canvas-kit-react/common';
import {chevronUpIcon} from '@workday/canvas-system-icons-web';
import {CanvasSystemIcon} from '@workday/design-assets-types';
import {useExpandableIcon} from './hooks/useExpandableIcon';
import {SystemIcon, systemIconStencil} from '@workday/canvas-kit-react/icon';
import {IconPositions} from '@workday/canvas-kit-react/button';
import {useExpandableModel} from './hooks/useExpandableModel';
import {createStencil} from '@workday/canvas-kit-styling';
import {mergeStyles} from '@workday/canvas-kit-react/layout';
import {system} from '@workday/canvas-tokens-web';
export interface ExpandableIconProps extends Omit<ExtractProps<typeof SystemIcon, never>, 'icon'> {
/**
* Icon to display from `@workday/canvas-accent-icons-web`
* @default chevronUpIcon
*/
icon?: CanvasSystemIcon;
/**
* Button icon positions can either be `start` or `end`.
* If no value is provided, it defaults to `start`.
* @default 'start'
*/
iconPosition?: IconPositions;
}
export const expandableIconStencil = createStencil({
extends: systemIconStencil,
base: {
padding: system.space.x1,
},
compound: [
{
modifiers: {position: 'end', isExpanded: 'false'},
styles: {
marginInlineStart: 'auto',
transform: 'rotate(180deg)',
paddingInlineEnd: system.space.x3, // do I need these padding changes?
},
},
{
modifiers: {position: 'end', isExpanded: 'true'},
styles: {
marginInlineStart: 'auto',
paddingInlineStart: system.space.x3, // do I need these padding changes?
},
},
{
modifiers: {position: 'start', isExpanded: 'false'},
styles: {
marginInlineEnd: system.space.x2,
transform: 'rotate(90deg)',
},
},
{
modifiers: {position: 'start', isExpanded: 'true'},
styles: {
marginInlineEnd: system.space.x2,
transform: 'rotate(180deg)',
},
},
],
});
export const ExpandableIcon = createSubcomponent('span')({
modelHook: useExpandableModel,
elemPropsHook: useExpandableIcon,
})<ExpandableIconProps>(({icon, visible, iconPosition = 'start', ...elementProps}, Element) => {
return (
<SystemIcon
as={Element}
icon={icon || chevronUpIcon}
{...mergeStyles(
elementProps,
expandableIconStencil({position: iconPosition, isExpanded: visible ? 'true' : 'false'})
)}
/>
);
});