@cleartrip/ct-design-button 4.0.0-TEST.1 → 4.1.0-SNAPSHOT-native-main.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/style.ts ADDED
@@ -0,0 +1,293 @@
1
+ import { Theme } from '@cleartrip/ct-design-theme';
2
+ import { TypographyVariant } from '@cleartrip/ct-design-typography';
3
+ import { ButtonColorType, ButtonSizeType, ButtonVariantType } from './type';
4
+ import { ButtonColor, ButtonSize, ButtonVariant } from './constants';
5
+
6
+ export const getButtonSizeStyles = (theme: Theme, size: ButtonSizeType) => {
7
+ switch (size) {
8
+ case ButtonSize.SMALL: {
9
+ return {
10
+ height: theme.size[8],
11
+ paddingTop: theme.spacing[1],
12
+ paddingBottom: theme.spacing[1],
13
+ };
14
+ }
15
+ case ButtonSize.MEDIUM: {
16
+ return {
17
+ height: theme.size[10],
18
+ paddingTop: theme.spacing[2],
19
+ paddingBottom: theme.spacing[2],
20
+ };
21
+ }
22
+ case ButtonSize.LARGE: {
23
+ return {
24
+ height: theme.size[12],
25
+ paddingTop: theme.spacing[3],
26
+ paddingBottom: theme.spacing[3],
27
+ };
28
+ }
29
+ default: {
30
+ return {
31
+ height: theme.size[10],
32
+ paddingTop: theme.spacing[2],
33
+ paddingBottom: theme.spacing[2],
34
+ };
35
+ }
36
+ }
37
+ };
38
+
39
+ export const getButtonVariantStyles = (theme: Theme, variant: ButtonVariantType, disabled: boolean) => {
40
+ if (disabled) {
41
+ return {
42
+ borderWidth: theme.border.width.none,
43
+ };
44
+ }
45
+ switch (variant) {
46
+ case ButtonVariant.OUTLINE: {
47
+ return {
48
+ borderWidth: theme.border.width.sm,
49
+ borderStyle: theme.border.style.solid,
50
+ };
51
+ }
52
+ case ButtonVariant.CONTAINED:
53
+ case ButtonVariant.BARE: {
54
+ return {
55
+ borderWidth: theme.border.width.none,
56
+ };
57
+ }
58
+ default: {
59
+ return {
60
+ borderWidth: theme.border.width.none,
61
+ };
62
+ }
63
+ }
64
+ };
65
+
66
+ export const getContainedButtonColorStyles = (theme: Theme, color: ButtonColorType, disabled: boolean) => {
67
+ if (disabled) {
68
+ return {
69
+ backgroundColor: theme.color.button.containedDisabledBg,
70
+ };
71
+ }
72
+
73
+ switch (color) {
74
+ case ButtonColor.PRIMARY: {
75
+ return {
76
+ backgroundColor: theme.color.button.containedPrimaryBg,
77
+ };
78
+ }
79
+ case ButtonColor.SECONDARY: {
80
+ return {
81
+ backgroundColor: theme.color.button.containedSecondaryBg,
82
+ };
83
+ }
84
+ case ButtonColor.TERTIARY: {
85
+ return {
86
+ backgroundColor: theme.color.button.containedTertiaryBg,
87
+ };
88
+ }
89
+ default: {
90
+ return {
91
+ backgroundColor: theme.color.button.containedPrimaryBg,
92
+ };
93
+ }
94
+ }
95
+ };
96
+
97
+ export const getOutlinedButtonColorStyles = (theme: Theme, color: ButtonColorType, disabled: boolean) => {
98
+ if (disabled) {
99
+ return {
100
+ backgroundColor: theme.color.button.outlinedDisabledBg,
101
+ };
102
+ }
103
+
104
+ switch (color) {
105
+ case ButtonColor.PRIMARY: {
106
+ return {
107
+ backgroundColor: theme.color.button.outlinedPrimaryBg,
108
+ borderColor: theme.color.button.outlinedPrimaryBorder,
109
+ };
110
+ }
111
+ case ButtonColor.SECONDARY: {
112
+ return {
113
+ backgroundColor: theme.color.button.outlinedSecondaryBg,
114
+ borderColor: theme.color.button.outlinedSecondaryBorder,
115
+ };
116
+ }
117
+ case ButtonColor.TERTIARY: {
118
+ return {
119
+ backgroundColor: theme.color.button.outlinedTertiaryBg,
120
+ borderColor: theme.color.button.outlinedTertiaryBorder,
121
+ };
122
+ }
123
+ case ButtonColor.NEUTRAL: {
124
+ return {
125
+ backgroundColor: theme.color.button.outlinedNeutralBg,
126
+ borderColor: theme.color.button.outlinedNeutralBorder,
127
+ };
128
+ }
129
+ default: {
130
+ return {
131
+ backgroundColor: theme.color.button.outlinedPrimaryBg,
132
+ borderColor: theme.color.button.outlinedPrimaryBorder,
133
+ };
134
+ }
135
+ }
136
+ };
137
+
138
+ export const getButtonVariantColorStyles = (
139
+ theme: Theme,
140
+ color: ButtonColorType,
141
+ variant: ButtonVariantType,
142
+ disabled: boolean,
143
+ _isHover: boolean,
144
+ ) => {
145
+ switch (variant) {
146
+ case ButtonVariant.CONTAINED: {
147
+ return getContainedButtonColorStyles(theme, color, disabled);
148
+ }
149
+ case ButtonVariant.OUTLINE:
150
+ case ButtonVariant.BARE: {
151
+ return getOutlinedButtonColorStyles(theme, color, disabled);
152
+ }
153
+ default: {
154
+ return getContainedButtonColorStyles(theme, color, disabled);
155
+ }
156
+ }
157
+ };
158
+
159
+ export const getContainedButtonLabelColor = (color: ButtonColorType, disabled: boolean, theme: Theme) => {
160
+ if (disabled) {
161
+ return theme.color.button.containedDisabledLabel;
162
+ }
163
+
164
+ switch (color) {
165
+ case ButtonColor.PRIMARY: {
166
+ return theme.color.button.containedPrimaryLabel;
167
+ }
168
+ case ButtonColor.SECONDARY: {
169
+ return theme.color.button.containedSecondaryLabel;
170
+ }
171
+ case ButtonColor.TERTIARY: {
172
+ return theme.color.button.containedTertiaryLabel;
173
+ }
174
+ default: {
175
+ return theme.color.button.containedPrimaryLabel;
176
+ }
177
+ }
178
+ };
179
+
180
+ export const getOutlinedButtonLabelColor = (color: ButtonColorType, disabled: boolean, theme: Theme) => {
181
+ if (disabled) {
182
+ return theme.color.button.outlinedDisabledLabel;
183
+ }
184
+
185
+ switch (color) {
186
+ case ButtonColor.PRIMARY: {
187
+ return theme.color.button.outlinedPrimaryLabel;
188
+ }
189
+ case ButtonColor.SECONDARY: {
190
+ return theme.color.button.outlinedSecondaryLabel;
191
+ }
192
+ case ButtonColor.TERTIARY: {
193
+ return theme.color.button.outlinedTertiaryLabel;
194
+ }
195
+ case ButtonColor.NEUTRAL: {
196
+ return theme.color.button.outlinedNeutralLabel;
197
+ }
198
+ default: {
199
+ return theme.color.button.outlinedPrimaryLabel;
200
+ }
201
+ }
202
+ };
203
+
204
+ export const getButtonVariantLabelColor = (
205
+ variant: ButtonVariantType,
206
+ color: ButtonColorType,
207
+ disabled: boolean,
208
+ theme: Theme,
209
+ loading: boolean,
210
+ ) => {
211
+ switch (variant) {
212
+ case ButtonVariant.CONTAINED: {
213
+ return getContainedButtonLabelColor(color, disabled || loading, theme);
214
+ }
215
+ case ButtonVariant.OUTLINE: {
216
+ return getOutlinedButtonLabelColor(color, disabled || loading, theme);
217
+ }
218
+ case ButtonVariant.BARE: {
219
+ return getOutlinedButtonLabelColor(color, disabled, theme);
220
+ }
221
+ default: {
222
+ return getContainedButtonLabelColor(color, disabled || loading, theme);
223
+ }
224
+ }
225
+ };
226
+
227
+ export const getTypographyVariant = (size: ButtonSizeType) => {
228
+ switch (size) {
229
+ case ButtonSize.SMALL: {
230
+ return TypographyVariant.HM4;
231
+ }
232
+ case ButtonSize.MEDIUM: {
233
+ return TypographyVariant.HM4;
234
+ }
235
+ case ButtonSize.LARGE: {
236
+ return TypographyVariant.HM3;
237
+ }
238
+ default: {
239
+ return TypographyVariant.HM4;
240
+ }
241
+ }
242
+ };
243
+
244
+ export const getDefaultButtonStyles = (disabled: boolean) => {
245
+ if (disabled) {
246
+ return {
247
+ // cursor: 'not-allowed',
248
+ };
249
+ } else {
250
+ return {
251
+ // cursor: 'pointer',
252
+ };
253
+ }
254
+ };
255
+
256
+ export const getButtonStyles = ({
257
+ theme,
258
+ size,
259
+ variant,
260
+ color,
261
+ disabled,
262
+ isFullWidth,
263
+ loading,
264
+ isHover,
265
+ }: {
266
+ theme: Theme;
267
+ size: ButtonSizeType;
268
+ variant: ButtonVariantType;
269
+ color: ButtonColorType;
270
+ disabled: boolean;
271
+ isFullWidth: boolean;
272
+ loading: boolean;
273
+ isHover: boolean;
274
+ }) => {
275
+ return {
276
+ ...getButtonSizeStyles(theme, size),
277
+ ...getButtonVariantStyles(theme, variant, disabled || loading),
278
+ ...getButtonVariantColorStyles(theme, color, variant, disabled || loading, isHover),
279
+ ...getDefaultButtonStyles(disabled || loading),
280
+ minWidth: (isFullWidth ? '100%' : 'auto') as unknown as number,
281
+ };
282
+ };
283
+
284
+ export const getSpinnerDimmension = (size: ButtonSizeType, theme: Theme) => {
285
+ switch (size) {
286
+ case 'large':
287
+ return theme.size[5];
288
+ case 'medium':
289
+ return theme.size[4];
290
+ default:
291
+ return theme.size[3];
292
+ }
293
+ };
package/src/type.ts ADDED
@@ -0,0 +1,86 @@
1
+ import type { Styles } from '@cleartrip/ct-design-types';
2
+ import { TypographyStyleConfigProps } from '@cleartrip/ct-design-typography';
3
+ import { INativeUIEvent } from '@cleartrip/ct-design-types';
4
+
5
+ import { ButtonSize, ButtonVariant, ButtonColor } from './constants';
6
+
7
+ export type ButtonSizeType = `${ButtonSize}`;
8
+
9
+ export type ButtonVariantType = `${ButtonVariant}`;
10
+
11
+ export type ButtonColorType = `${ButtonColor}`;
12
+
13
+ export type IClickEvent = INativeUIEvent;
14
+
15
+ export type IButtonProps = Partial<{
16
+ children: React.ReactNode;
17
+ /**
18
+ * Set the size of Button
19
+ * Default size will take Medium
20
+ */
21
+ size: 'small' | 'medium' | 'large';
22
+ /**
23
+ * Set the style of Button
24
+ * Default variant will be Contained
25
+ */
26
+ variant: 'bare' | 'contained' | 'outline';
27
+ /**
28
+ * Set the background color of button
29
+ * Default will be primary from theme
30
+ */
31
+ color: 'primary' | 'secondary' | 'tertiary';
32
+ /**
33
+ * Check if it should take full width of container
34
+ */
35
+ isFullWidth: boolean;
36
+ /**
37
+ * Style loading state of button
38
+ */
39
+ loading: boolean;
40
+ /**
41
+ * Set the disabled style
42
+ */
43
+ disabled: boolean;
44
+ /**
45
+ * Prefix node of button
46
+ */
47
+ prefixIcon: React.ReactNode;
48
+ /**
49
+ * Suffix node of button
50
+ */
51
+ suffixIcon: React.ReactNode;
52
+ /**
53
+ * Callback when user clicks on the button
54
+ */
55
+ onClick: (event: IClickEvent) => void;
56
+ /**
57
+ * Custom style configuration for the button
58
+ */
59
+ styleConfig?: {
60
+ /**
61
+ * Style configuration for the root container of the button.
62
+ */
63
+ root?: Styles[];
64
+ /**
65
+ * Style configuration for the container of the button.
66
+ */
67
+ container?: Styles[];
68
+ /**
69
+ * Style configuration for the typography of the button.
70
+ */
71
+ typography?: TypographyStyleConfigProps;
72
+
73
+ /**
74
+ * Style configuration for the container of the button icon.
75
+ */
76
+ prefixIconContainer?: Styles[];
77
+ /**
78
+ * Style configuration for the container of the button icon.
79
+ */
80
+ suffixIconContainer?: Styles[];
81
+ };
82
+ /**
83
+ * No hover effect. Will override hover. However, this is a whereabout to the touch sticky issue with hover. JIRA: https://cleartrip.atlassian.net/browse/UIENG-3683, Referance: https://css-tricks.com/solving-sticky-hover-states-with-media-hover-hover/
84
+ */
85
+ noHover?: boolean;
86
+ }>;