-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathMDButtonRoot.jsx
More file actions
276 lines (224 loc) · 7.53 KB
/
MDButtonRoot.jsx
File metadata and controls
276 lines (224 loc) · 7.53 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* eslint-disable prefer-destructuring */
/**
=========================================================
* Material Dashboard 2 React - v2.2.0
=========================================================
* Product Page: https://www.creative-tim.com/product/material-dashboard-react
* Copyright 2023 Creative Tim (https://www.creative-tim.com)
Coded by www.creative-tim.com
=========================================================
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
// @mui material components
import Button from "@mui/material/Button";
import { styled } from "@mui/material/styles";
export default styled(Button)(({ theme, ownerState }) => {
const { palette, functions, borders, boxShadows } = theme;
const { color, variant, size, circular, iconOnly, darkMode } = ownerState;
const { white, text, transparent, gradients, grey } = palette;
const { boxShadow, linearGradient, pxToRem, rgba } = functions;
const { borderRadius } = borders;
const { colored } = boxShadows;
// styles for the button with variant="contained"
const containedStyles = () => {
// background color value
const backgroundValue = palette[color] ? palette[color].main : white.main;
// backgroundColor value when button is focused
const focusedBackgroundValue = palette[color] ? palette[color].focus : white.focus;
// boxShadow value
const boxShadowValue = colored[color]
? `${boxShadow([0, 3], [3, 0], palette[color].main, 0.15)}, ${boxShadow(
[0, 3],
[1, -2],
palette[color].main,
0.2
)}, ${boxShadow([0, 1], [5, 0], palette[color].main, 0.15)}`
: "none";
// boxShadow value when button is hovered
const hoveredBoxShadowValue = colored[color]
? `${boxShadow([0, 14], [26, -12], palette[color].main, 0.4)}, ${boxShadow(
[0, 4],
[23, 0],
palette[color].main,
0.15
)}, ${boxShadow([0, 8], [10, -5], palette[color].main, 0.2)}`
: "none";
// color value
let colorValue = white.main;
if (!darkMode && (color === "white" || color === "light" || !palette[color])) {
colorValue = text.main;
} else if (darkMode && (color === "white" || color === "light" || !palette[color])) {
colorValue = grey[600];
}
// color value when button is focused
let focusedColorValue = white.main;
if (color === "white") {
focusedColorValue = text.main;
} else if (color === "primary" || color === "error" || color === "dark") {
focusedColorValue = white.main;
}
return {
background: backgroundValue,
color: colorValue,
boxShadow: boxShadowValue,
"&:hover": {
backgroundColor: backgroundValue,
boxShadow: hoveredBoxShadowValue,
},
"&:focus:not(:hover)": {
backgroundColor: focusedBackgroundValue,
boxShadow: palette[color]
? boxShadow([0, 0], [0, 3.2], palette[color].main, 0.5)
: boxShadow([0, 0], [0, 3.2], white.main, 0.5),
},
"&:disabled": {
backgroundColor: backgroundValue,
color: focusedColorValue,
},
};
};
// styles for the button with variant="outlined"
const outliedStyles = () => {
// background color value
const backgroundValue = color === "white" ? rgba(white.main, 0.1) : transparent.main;
// color value
const colorValue = palette[color] ? palette[color].main : white.main;
// boxShadow value
const boxShadowValue = palette[color]
? boxShadow([0, 0], [0, 3.2], palette[color].main, 0.5)
: boxShadow([0, 0], [0, 3.2], white.main, 0.5);
// border color value
let borderColorValue = palette[color] ? palette[color].main : rgba(white.main, 0.75);
if (color === "white") {
borderColorValue = rgba(white.main, 0.75);
}
return {
background: backgroundValue,
color: colorValue,
borderColor: borderColorValue,
"&:hover": {
background: transparent.main,
borderColor: colorValue,
},
"&:focus:not(:hover)": {
background: transparent.main,
boxShadow: boxShadowValue,
},
"&:active:not(:hover)": {
backgroundColor: colorValue,
color: white.main,
opacity: 0.85,
},
"&:disabled": {
color: colorValue,
borderColor: colorValue,
},
};
};
// styles for the button with variant="gradient"
const gradientStyles = () => {
// background value
const backgroundValue =
color === "white" || !gradients[color]
? white.main
: linearGradient(gradients[color].main, gradients[color].state);
// boxShadow value
const boxShadowValue = colored[color]
? `${boxShadow([0, 3], [3, 0], palette[color].main, 0.15)}, ${boxShadow(
[0, 3],
[1, -2],
palette[color].main,
0.2
)}, ${boxShadow([0, 1], [5, 0], palette[color].main, 0.15)}`
: "none";
// boxShadow value when button is hovered
const hoveredBoxShadowValue = colored[color]
? `${boxShadow([0, 14], [26, -12], palette[color].main, 0.4)}, ${boxShadow(
[0, 4],
[23, 0],
palette[color].main,
0.15
)}, ${boxShadow([0, 8], [10, -5], palette[color].main, 0.2)}`
: "none";
// color value
let colorValue = white.main;
if (color === "white") {
colorValue = text.main;
} else if (color === "light") {
colorValue = gradients.dark.state;
}
return {
background: backgroundValue,
color: colorValue,
boxShadow: boxShadowValue,
"&:hover": {
boxShadow: hoveredBoxShadowValue,
},
"&:focus:not(:hover)": {
boxShadow: boxShadowValue,
},
"&:disabled": {
background: backgroundValue,
color: colorValue,
},
};
};
// styles for the button with variant="text"
const textStyles = () => {
// color value
const colorValue = palette[color] ? palette[color].main : white.main;
// color value when button is focused
const focusedColorValue = palette[color] ? palette[color].focus : white.focus;
return {
color: colorValue,
"&:hover": {
color: focusedColorValue,
},
"&:focus:not(:hover)": {
color: focusedColorValue,
},
};
};
// styles for the button with circular={true}
const circularStyles = () => ({
borderRadius: borderRadius.section,
});
// styles for the button with iconOnly={true}
const iconOnlyStyles = () => {
// width, height, minWidth and minHeight values
let sizeValue = pxToRem(38);
if (size === "small") {
sizeValue = pxToRem(25.4);
} else if (size === "large") {
sizeValue = pxToRem(52);
}
// padding value
let paddingValue = `${pxToRem(11)} ${pxToRem(11)} ${pxToRem(10)}`;
if (size === "small") {
paddingValue = pxToRem(4.5);
} else if (size === "large") {
paddingValue = pxToRem(16);
}
return {
width: sizeValue,
minWidth: sizeValue,
height: sizeValue,
minHeight: sizeValue,
padding: paddingValue,
"& .material-icons": {
marginTop: 0,
},
"&:hover, &:focus, &:active": {
transform: "none",
},
};
};
return {
...(variant === "contained" && containedStyles()),
...(variant === "outlined" && outliedStyles()),
...(variant === "gradient" && gradientStyles()),
...(variant === "text" && textStyles()),
...(circular && circularStyles()),
...(iconOnly && iconOnlyStyles()),
};
});