-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathArrowBlockButton.tsx
More file actions
160 lines (155 loc) · 4.68 KB
/
Copy pathArrowBlockButton.tsx
File metadata and controls
160 lines (155 loc) · 4.68 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { isValidElement } from 'react';
import type { Button, LocalizationKey } from '../customizables';
import { Flex, Icon, SimpleButton, Spinner, Text } from '../customizables';
import type { ElementDescriptor, ElementId } from '../customizables/elementDescriptors';
import { ArrowRight } from '../icons';
import type { PropsOfComponent, ThemableCssProp } from '../styledSystem';
type ArrowBlockButtonProps = PropsOfComponent<typeof Button> & {
rightIcon?: React.ComponentType | null;
rightIconSx?: ThemableCssProp;
leftIcon?: React.ComponentType | React.ReactElement;
leftIconSx?: ThemableCssProp;
childrenSx?: ThemableCssProp;
leftIconElementDescriptor?: ElementDescriptor;
leftIconElementId?: ElementId;
badge?: React.ReactElement;
textElementDescriptor?: ElementDescriptor;
textElementId?: ElementId;
arrowElementDescriptor?: ElementDescriptor;
arrowElementId?: ElementId;
spinnerElementDescriptor?: ElementDescriptor;
spinnerElementId?: ElementId;
textLocalizationKey?: LocalizationKey | string;
textVariant?: PropsOfComponent<typeof Text>['variant'];
};
export const ArrowBlockButton = React.forwardRef<HTMLButtonElement, ArrowBlockButtonProps>((props, ref) => {
const {
rightIcon = ArrowRight,
rightIconSx,
leftIcon,
leftIconSx,
leftIconElementId,
leftIconElementDescriptor,
isLoading,
children,
textElementDescriptor,
textElementId,
spinnerElementDescriptor,
spinnerElementId,
arrowElementDescriptor,
arrowElementId,
textLocalizationKey,
childrenSx,
badge,
textVariant = 'buttonSmall',
...rest
} = props;
const isIconElement = isValidElement(leftIcon);
return (
<SimpleButton
variant='outline'
block
isLoading={isLoading}
{...rest}
ref={ref}
sx={theme => [
{
gap: theme.space.$2,
position: 'relative',
justifyContent: 'center',
borderColor: theme.colors.$neutralAlpha100,
alignItems: 'center',
padding: `${theme.space.$1x5} ${theme.space.$3} ${theme.space.$1x5} ${theme.space.$2x5}`,
'--arrow-opacity': '0',
'--arrow-transform': `translateX(-${theme.space.$2})`,
'&:hover,&:focus-visible ': {
'--arrow-opacity': '0.5',
'--arrow-transform': 'translateX(0px)',
},
'[dir="rtl"] &': {
'--arrow-transform': `translateX(${theme.space.$2})`,
},
'[dir="rtl"] &:hover, [dir="rtl"] &:focus-visible': {
'--arrow-transform': 'translateX(0px)',
},
},
props.sx,
]}
>
{(isLoading || leftIcon) && (
<Flex
as='span'
sx={theme => ({ width: theme.space.$4, height: theme.space.$4, flex: 'none', alignItems: 'center' })}
>
{isLoading ? (
<Spinner
elementDescriptor={spinnerElementDescriptor}
elementId={spinnerElementId}
size={'md'}
/>
) : !isIconElement && leftIcon ? (
<Icon
elementDescriptor={leftIconElementDescriptor}
elementId={leftIconElementId}
icon={leftIcon as React.ComponentType}
sx={[
theme => ({
width: theme.sizes.$4,
// Fixes a bug in Safari where the icon shifts when navigating between routes.
transform: 'translateZ(0)',
}),
leftIconSx,
]}
/>
) : (
leftIcon
)}
</Flex>
)}
<Flex
gap={2}
as='span'
sx={[
{
overflow: 'hidden',
},
childrenSx,
]}
>
<Text
elementDescriptor={textElementDescriptor}
elementId={textElementId}
as='span'
truncate
variant={textVariant}
localizationKey={textLocalizationKey}
>
{children}
</Text>
{badge}
</Flex>
{rightIcon && (
<Icon
elementDescriptor={arrowElementDescriptor}
elementId={arrowElementId}
icon={rightIcon}
sx={[
theme => ({
transition: 'all 100ms ease',
minWidth: theme.sizes.$4,
minHeight: theme.sizes.$4,
width: '1em',
height: '1em',
opacity: `var(--arrow-opacity)`,
transform: `var(--arrow-transform)`,
'[dir="rtl"] &': {
transform: `var(--arrow-transform) scaleX(-1)`,
},
}),
rightIconSx,
]}
/>
)}
</SimpleButton>
);
});