-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Button.tsx
173 lines (145 loc) · 4.11 KB
/
Button.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
import React, { forwardRef } from 'react';
import {
DefaultProps,
MantineSize,
MantineNumberSize,
MantineGradient,
MantineColor,
Selectors,
useComponentDefaultProps,
} from '@mantine/styles';
import { createPolymorphicComponent } from '@mantine/utils';
import { UnstyledButton } from '../UnstyledButton';
import { Loader, LoaderProps } from '../Loader';
import { ButtonGroup } from './ButtonGroup/ButtonGroup';
import useStyles, { sizes, ButtonVariant, ButtonStylesParams } from './Button.styles';
export type ButtonStylesNames = Selectors<typeof useStyles>;
export interface ButtonProps extends DefaultProps<ButtonStylesNames, ButtonStylesParams> {
/** Predefined button size */
size?: MantineSize;
/** Button type attribute */
type?: 'submit' | 'button' | 'reset';
/** Button color from theme */
color?: MantineColor;
/** Adds icon before button label */
leftIcon?: React.ReactNode;
/** Adds icon after button label */
rightIcon?: React.ReactNode;
/** Sets button width to 100% of parent element */
fullWidth?: boolean;
/** Button border-radius from theme or number to set border-radius in px */
radius?: MantineNumberSize;
/** Controls button appearance */
variant?: ButtonVariant;
/** Controls gradient settings in gradient variant only */
gradient?: MantineGradient;
/** Set text-transform to uppercase */
uppercase?: boolean;
/** Reduces vertical and horizontal spacing */
compact?: boolean;
/** Indicate loading state */
loading?: boolean;
/** Props spread to Loader component */
loaderProps?: LoaderProps;
/** Loader position relative to button label */
loaderPosition?: 'left' | 'right' | 'center';
/** Button label */
children?: React.ReactNode;
/** Disabled state */
disabled?: boolean;
}
const defaultProps: Partial<ButtonProps> = {
size: 'sm',
type: 'button',
variant: 'filled',
loaderPosition: 'left',
};
export const _Button = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => {
const {
className,
size,
color,
type,
disabled,
children,
leftIcon,
rightIcon,
fullWidth,
variant,
radius,
uppercase,
compact,
loading,
loaderPosition,
loaderProps,
gradient,
classNames,
styles,
unstyled,
...others
} = useComponentDefaultProps('Button', defaultProps, props);
const { classes, cx, theme } = useStyles(
{
radius,
color,
size,
fullWidth,
compact,
gradient,
variant,
withLeftIcon: !!leftIcon,
withRightIcon: !!rightIcon,
},
{ name: 'Button', unstyled, classNames, styles }
);
const colors = theme.fn.variant({ color, variant });
const loader = (
<Loader
color={colors.color}
size={theme.fn.size({ size, sizes }).height / 2}
{...loaderProps}
/>
);
return (
<UnstyledButton
className={cx(classes.root, className)}
type={type}
disabled={disabled}
data-button
data-disabled={disabled || undefined}
data-loading={loading || undefined}
ref={ref}
unstyled={unstyled}
{...others}
>
<div className={classes.inner}>
{(leftIcon || (loading && loaderPosition === 'left')) && (
<span className={cx(classes.icon, classes.leftIcon)}>
{loading && loaderPosition === 'left' ? loader : leftIcon}
</span>
)}
{loading && loaderPosition === 'center' && (
<span className={classes.centerLoader}>{loader}</span>
)}
<span
className={classes.label}
style={{ textTransform: uppercase ? 'uppercase' : undefined }}
>
{children}
</span>
{(rightIcon || (loading && loaderPosition === 'right')) && (
<span className={cx(classes.icon, classes.rightIcon)}>
{loading && loaderPosition === 'right' ? loader : rightIcon}
</span>
)}
</div>
</UnstyledButton>
);
}) as any;
_Button.displayName = '@mantine/core/Button';
_Button.Group = ButtonGroup;
export const Button = createPolymorphicComponent<
'button',
ButtonProps,
{ Group: typeof ButtonGroup }
>(_Button);