UNPKG

15.2 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import resolveProps from '@mui/utils/resolveProps';
7import composeClasses from '@mui/utils/composeClasses';
8import { alpha } from '@mui/system/colorManipulator';
9import rootShouldForwardProp from "../styles/rootShouldForwardProp.js";
10import { styled } from "../zero-styled/index.js";
11import memoTheme from "../utils/memoTheme.js";
12import { useDefaultProps } from "../DefaultPropsProvider/index.js";
13import ButtonBase from "../ButtonBase/index.js";
14import capitalize from "../utils/capitalize.js";
15import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
16import buttonClasses, { getButtonUtilityClass } from "./buttonClasses.js";
17import ButtonGroupContext from "../ButtonGroup/ButtonGroupContext.js";
18import ButtonGroupButtonContext from "../ButtonGroup/ButtonGroupButtonContext.js";
19import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
20const useUtilityClasses = ownerState => {
21 const {
22 color,
23 disableElevation,
24 fullWidth,
25 size,
26 variant,
27 classes
28 } = ownerState;
29 const slots = {
30 root: ['root', variant, `${variant}${capitalize(color)}`, `size${capitalize(size)}`, `${variant}Size${capitalize(size)}`, `color${capitalize(color)}`, disableElevation && 'disableElevation', fullWidth && 'fullWidth'],
31 label: ['label'],
32 startIcon: ['icon', 'startIcon', `iconSize${capitalize(size)}`],
33 endIcon: ['icon', 'endIcon', `iconSize${capitalize(size)}`]
34 };
35 const composedClasses = composeClasses(slots, getButtonUtilityClass, classes);
36 return {
37 ...classes,
38 // forward the focused, disabled, etc. classes to the ButtonBase
39 ...composedClasses
40 };
41};
42const commonIconStyles = [{
43 props: {
44 size: 'small'
45 },
46 style: {
47 '& > *:nth-of-type(1)': {
48 fontSize: 18
49 }
50 }
51}, {
52 props: {
53 size: 'medium'
54 },
55 style: {
56 '& > *:nth-of-type(1)': {
57 fontSize: 20
58 }
59 }
60}, {
61 props: {
62 size: 'large'
63 },
64 style: {
65 '& > *:nth-of-type(1)': {
66 fontSize: 22
67 }
68 }
69}];
70const ButtonRoot = styled(ButtonBase, {
71 shouldForwardProp: prop => rootShouldForwardProp(prop) || prop === 'classes',
72 name: 'MuiButton',
73 slot: 'Root',
74 overridesResolver: (props, styles) => {
75 const {
76 ownerState
77 } = props;
78 return [styles.root, styles[ownerState.variant], styles[`${ownerState.variant}${capitalize(ownerState.color)}`], styles[`size${capitalize(ownerState.size)}`], styles[`${ownerState.variant}Size${capitalize(ownerState.size)}`], ownerState.color === 'inherit' && styles.colorInherit, ownerState.disableElevation && styles.disableElevation, ownerState.fullWidth && styles.fullWidth];
79 }
80})(memoTheme(({
81 theme
82}) => {
83 const inheritContainedBackgroundColor = theme.palette.mode === 'light' ? theme.palette.grey[300] : theme.palette.grey[800];
84 const inheritContainedHoverBackgroundColor = theme.palette.mode === 'light' ? theme.palette.grey.A100 : theme.palette.grey[700];
85 return {
86 ...theme.typography.button,
87 minWidth: 64,
88 padding: '6px 16px',
89 border: 0,
90 borderRadius: (theme.vars || theme).shape.borderRadius,
91 transition: theme.transitions.create(['background-color', 'box-shadow', 'border-color', 'color'], {
92 duration: theme.transitions.duration.short
93 }),
94 '&:hover': {
95 textDecoration: 'none'
96 },
97 [`&.${buttonClasses.disabled}`]: {
98 color: (theme.vars || theme).palette.action.disabled
99 },
100 variants: [{
101 props: {
102 variant: 'contained'
103 },
104 style: {
105 color: `var(--variant-containedColor)`,
106 backgroundColor: `var(--variant-containedBg)`,
107 boxShadow: (theme.vars || theme).shadows[2],
108 '&:hover': {
109 boxShadow: (theme.vars || theme).shadows[4],
110 // Reset on touch devices, it doesn't add specificity
111 '@media (hover: none)': {
112 boxShadow: (theme.vars || theme).shadows[2]
113 }
114 },
115 '&:active': {
116 boxShadow: (theme.vars || theme).shadows[8]
117 },
118 [`&.${buttonClasses.focusVisible}`]: {
119 boxShadow: (theme.vars || theme).shadows[6]
120 },
121 [`&.${buttonClasses.disabled}`]: {
122 color: (theme.vars || theme).palette.action.disabled,
123 boxShadow: (theme.vars || theme).shadows[0],
124 backgroundColor: (theme.vars || theme).palette.action.disabledBackground
125 }
126 }
127 }, {
128 props: {
129 variant: 'outlined'
130 },
131 style: {
132 padding: '5px 15px',
133 border: '1px solid currentColor',
134 borderColor: `var(--variant-outlinedBorder, currentColor)`,
135 backgroundColor: `var(--variant-outlinedBg)`,
136 color: `var(--variant-outlinedColor)`,
137 [`&.${buttonClasses.disabled}`]: {
138 border: `1px solid ${(theme.vars || theme).palette.action.disabledBackground}`
139 }
140 }
141 }, {
142 props: {
143 variant: 'text'
144 },
145 style: {
146 padding: '6px 8px',
147 color: `var(--variant-textColor)`,
148 backgroundColor: `var(--variant-textBg)`
149 }
150 }, ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()).map(([color]) => ({
151 props: {
152 color
153 },
154 style: {
155 '--variant-textColor': (theme.vars || theme).palette[color].main,
156 '--variant-outlinedColor': (theme.vars || theme).palette[color].main,
157 '--variant-outlinedBorder': theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / 0.5)` : alpha(theme.palette[color].main, 0.5),
158 '--variant-containedColor': (theme.vars || theme).palette[color].contrastText,
159 '--variant-containedBg': (theme.vars || theme).palette[color].main,
160 '@media (hover: hover)': {
161 '&:hover': {
162 '--variant-containedBg': (theme.vars || theme).palette[color].dark,
163 '--variant-textBg': theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette[color].main, theme.palette.action.hoverOpacity),
164 '--variant-outlinedBorder': (theme.vars || theme).palette[color].main,
165 '--variant-outlinedBg': theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette[color].main, theme.palette.action.hoverOpacity)
166 }
167 }
168 }
169 })), {
170 props: {
171 color: 'inherit'
172 },
173 style: {
174 color: 'inherit',
175 borderColor: 'currentColor',
176 '--variant-containedBg': theme.vars ? theme.vars.palette.Button.inheritContainedBg : inheritContainedBackgroundColor,
177 '@media (hover: hover)': {
178 '&:hover': {
179 '--variant-containedBg': theme.vars ? theme.vars.palette.Button.inheritContainedHoverBg : inheritContainedHoverBackgroundColor,
180 '--variant-textBg': theme.vars ? `rgba(${theme.vars.palette.text.primaryChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette.text.primary, theme.palette.action.hoverOpacity),
181 '--variant-outlinedBg': theme.vars ? `rgba(${theme.vars.palette.text.primaryChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette.text.primary, theme.palette.action.hoverOpacity)
182 }
183 }
184 }
185 }, {
186 props: {
187 size: 'small',
188 variant: 'text'
189 },
190 style: {
191 padding: '4px 5px',
192 fontSize: theme.typography.pxToRem(13)
193 }
194 }, {
195 props: {
196 size: 'large',
197 variant: 'text'
198 },
199 style: {
200 padding: '8px 11px',
201 fontSize: theme.typography.pxToRem(15)
202 }
203 }, {
204 props: {
205 size: 'small',
206 variant: 'outlined'
207 },
208 style: {
209 padding: '3px 9px',
210 fontSize: theme.typography.pxToRem(13)
211 }
212 }, {
213 props: {
214 size: 'large',
215 variant: 'outlined'
216 },
217 style: {
218 padding: '7px 21px',
219 fontSize: theme.typography.pxToRem(15)
220 }
221 }, {
222 props: {
223 size: 'small',
224 variant: 'contained'
225 },
226 style: {
227 padding: '4px 10px',
228 fontSize: theme.typography.pxToRem(13)
229 }
230 }, {
231 props: {
232 size: 'large',
233 variant: 'contained'
234 },
235 style: {
236 padding: '8px 22px',
237 fontSize: theme.typography.pxToRem(15)
238 }
239 }, {
240 props: {
241 disableElevation: true
242 },
243 style: {
244 boxShadow: 'none',
245 '&:hover': {
246 boxShadow: 'none'
247 },
248 [`&.${buttonClasses.focusVisible}`]: {
249 boxShadow: 'none'
250 },
251 '&:active': {
252 boxShadow: 'none'
253 },
254 [`&.${buttonClasses.disabled}`]: {
255 boxShadow: 'none'
256 }
257 }
258 }, {
259 props: {
260 fullWidth: true
261 },
262 style: {
263 width: '100%'
264 }
265 }]
266 };
267}));
268const ButtonStartIcon = styled('span', {
269 name: 'MuiButton',
270 slot: 'StartIcon',
271 overridesResolver: (props, styles) => {
272 const {
273 ownerState
274 } = props;
275 return [styles.startIcon, styles[`iconSize${capitalize(ownerState.size)}`]];
276 }
277})({
278 display: 'inherit',
279 marginRight: 8,
280 marginLeft: -4,
281 variants: [{
282 props: {
283 size: 'small'
284 },
285 style: {
286 marginLeft: -2
287 }
288 }, ...commonIconStyles]
289});
290const ButtonEndIcon = styled('span', {
291 name: 'MuiButton',
292 slot: 'EndIcon',
293 overridesResolver: (props, styles) => {
294 const {
295 ownerState
296 } = props;
297 return [styles.endIcon, styles[`iconSize${capitalize(ownerState.size)}`]];
298 }
299})({
300 display: 'inherit',
301 marginRight: -4,
302 marginLeft: 8,
303 variants: [{
304 props: {
305 size: 'small'
306 },
307 style: {
308 marginRight: -2
309 }
310 }, ...commonIconStyles]
311});
312const Button = /*#__PURE__*/React.forwardRef(function Button(inProps, ref) {
313 // props priority: `inProps` > `contextProps` > `themeDefaultProps`
314 const contextProps = React.useContext(ButtonGroupContext);
315 const buttonGroupButtonContextPositionClassName = React.useContext(ButtonGroupButtonContext);
316 const resolvedProps = resolveProps(contextProps, inProps);
317 const props = useDefaultProps({
318 props: resolvedProps,
319 name: 'MuiButton'
320 });
321 const {
322 children,
323 color = 'primary',
324 component = 'button',
325 className,
326 disabled = false,
327 disableElevation = false,
328 disableFocusRipple = false,
329 endIcon: endIconProp,
330 focusVisibleClassName,
331 fullWidth = false,
332 size = 'medium',
333 startIcon: startIconProp,
334 type,
335 variant = 'text',
336 ...other
337 } = props;
338 const ownerState = {
339 ...props,
340 color,
341 component,
342 disabled,
343 disableElevation,
344 disableFocusRipple,
345 fullWidth,
346 size,
347 type,
348 variant
349 };
350 const classes = useUtilityClasses(ownerState);
351 const startIcon = startIconProp && /*#__PURE__*/_jsx(ButtonStartIcon, {
352 className: classes.startIcon,
353 ownerState: ownerState,
354 children: startIconProp
355 });
356 const endIcon = endIconProp && /*#__PURE__*/_jsx(ButtonEndIcon, {
357 className: classes.endIcon,
358 ownerState: ownerState,
359 children: endIconProp
360 });
361 const positionClassName = buttonGroupButtonContextPositionClassName || '';
362 return /*#__PURE__*/_jsxs(ButtonRoot, {
363 ownerState: ownerState,
364 className: clsx(contextProps.className, classes.root, className, positionClassName),
365 component: component,
366 disabled: disabled,
367 focusRipple: !disableFocusRipple,
368 focusVisibleClassName: clsx(classes.focusVisible, focusVisibleClassName),
369 ref: ref,
370 type: type,
371 ...other,
372 classes: classes,
373 children: [startIcon, children, endIcon]
374 });
375});
376process.env.NODE_ENV !== "production" ? Button.propTypes /* remove-proptypes */ = {
377 // ┌────────────────────────────── Warning ──────────────────────────────┐
378 // │ These PropTypes are generated from the TypeScript type definitions. │
379 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
380 // └─────────────────────────────────────────────────────────────────────┘
381 /**
382 * The content of the component.
383 */
384 children: PropTypes.node,
385 /**
386 * Override or extend the styles applied to the component.
387 */
388 classes: PropTypes.object,
389 /**
390 * @ignore
391 */
392 className: PropTypes.string,
393 /**
394 * The color of the component.
395 * It supports both default and custom theme colors, which can be added as shown in the
396 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
397 * @default 'primary'
398 */
399 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['inherit', 'primary', 'secondary', 'success', 'error', 'info', 'warning']), PropTypes.string]),
400 /**
401 * The component used for the root node.
402 * Either a string to use a HTML element or a component.
403 */
404 component: PropTypes.elementType,
405 /**
406 * If `true`, the component is disabled.
407 * @default false
408 */
409 disabled: PropTypes.bool,
410 /**
411 * If `true`, no elevation is used.
412 * @default false
413 */
414 disableElevation: PropTypes.bool,
415 /**
416 * If `true`, the keyboard focus ripple is disabled.
417 * @default false
418 */
419 disableFocusRipple: PropTypes.bool,
420 /**
421 * If `true`, the ripple effect is disabled.
422 *
423 * ⚠️ Without a ripple there is no styling for :focus-visible by default. Be sure
424 * to highlight the element by applying separate styles with the `.Mui-focusVisible` class.
425 * @default false
426 */
427 disableRipple: PropTypes.bool,
428 /**
429 * Element placed after the children.
430 */
431 endIcon: PropTypes.node,
432 /**
433 * @ignore
434 */
435 focusVisibleClassName: PropTypes.string,
436 /**
437 * If `true`, the button will take up the full width of its container.
438 * @default false
439 */
440 fullWidth: PropTypes.bool,
441 /**
442 * The URL to link to when the button is clicked.
443 * If defined, an `a` element will be used as the root node.
444 */
445 href: PropTypes.string,
446 /**
447 * The size of the component.
448 * `small` is equivalent to the dense button styling.
449 * @default 'medium'
450 */
451 size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['small', 'medium', 'large']), PropTypes.string]),
452 /**
453 * Element placed before the children.
454 */
455 startIcon: PropTypes.node,
456 /**
457 * The system prop that allows defining system overrides as well as additional CSS styles.
458 */
459 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
460 /**
461 * @ignore
462 */
463 type: PropTypes.oneOfType([PropTypes.oneOf(['button', 'reset', 'submit']), PropTypes.string]),
464 /**
465 * The variant to use.
466 * @default 'text'
467 */
468 variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['contained', 'outlined', 'text']), PropTypes.string])
469} : void 0;
470export default Button;
\No newline at end of file