UNPKG

12.9 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import composeClasses from '@mui/utils/composeClasses';
7import { alpha } from '@mui/system/colorManipulator';
8import getValidReactChildren from '@mui/utils/getValidReactChildren';
9import capitalize from "../utils/capitalize.js";
10import { styled } from "../zero-styled/index.js";
11import memoTheme from "../utils/memoTheme.js";
12import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
13import { useDefaultProps } from "../DefaultPropsProvider/index.js";
14import buttonGroupClasses, { getButtonGroupUtilityClass } from "./buttonGroupClasses.js";
15import ButtonGroupContext from "./ButtonGroupContext.js";
16import ButtonGroupButtonContext from "./ButtonGroupButtonContext.js";
17import { jsx as _jsx } from "react/jsx-runtime";
18const overridesResolver = (props, styles) => {
19 const {
20 ownerState
21 } = props;
22 return [{
23 [`& .${buttonGroupClasses.grouped}`]: styles.grouped
24 }, {
25 [`& .${buttonGroupClasses.grouped}`]: styles[`grouped${capitalize(ownerState.orientation)}`]
26 }, {
27 [`& .${buttonGroupClasses.grouped}`]: styles[`grouped${capitalize(ownerState.variant)}`]
28 }, {
29 [`& .${buttonGroupClasses.grouped}`]: styles[`grouped${capitalize(ownerState.variant)}${capitalize(ownerState.orientation)}`]
30 }, {
31 [`& .${buttonGroupClasses.grouped}`]: styles[`grouped${capitalize(ownerState.variant)}${capitalize(ownerState.color)}`]
32 }, {
33 [`& .${buttonGroupClasses.firstButton}`]: styles.firstButton
34 }, {
35 [`& .${buttonGroupClasses.lastButton}`]: styles.lastButton
36 }, {
37 [`& .${buttonGroupClasses.middleButton}`]: styles.middleButton
38 }, styles.root, styles[ownerState.variant], ownerState.disableElevation === true && styles.disableElevation, ownerState.fullWidth && styles.fullWidth, ownerState.orientation === 'vertical' && styles.vertical];
39};
40const useUtilityClasses = ownerState => {
41 const {
42 classes,
43 color,
44 disabled,
45 disableElevation,
46 fullWidth,
47 orientation,
48 variant
49 } = ownerState;
50 const slots = {
51 root: ['root', variant, orientation, fullWidth && 'fullWidth', disableElevation && 'disableElevation', `color${capitalize(color)}`],
52 grouped: ['grouped', `grouped${capitalize(orientation)}`, `grouped${capitalize(variant)}`, `grouped${capitalize(variant)}${capitalize(orientation)}`, `grouped${capitalize(variant)}${capitalize(color)}`, disabled && 'disabled'],
53 firstButton: ['firstButton'],
54 lastButton: ['lastButton'],
55 middleButton: ['middleButton']
56 };
57 return composeClasses(slots, getButtonGroupUtilityClass, classes);
58};
59const ButtonGroupRoot = styled('div', {
60 name: 'MuiButtonGroup',
61 slot: 'Root',
62 overridesResolver
63})(memoTheme(({
64 theme
65}) => ({
66 display: 'inline-flex',
67 borderRadius: (theme.vars || theme).shape.borderRadius,
68 variants: [{
69 props: {
70 variant: 'contained'
71 },
72 style: {
73 boxShadow: (theme.vars || theme).shadows[2]
74 }
75 }, {
76 props: {
77 disableElevation: true
78 },
79 style: {
80 boxShadow: 'none'
81 }
82 }, {
83 props: {
84 fullWidth: true
85 },
86 style: {
87 width: '100%'
88 }
89 }, {
90 props: {
91 orientation: 'vertical'
92 },
93 style: {
94 flexDirection: 'column',
95 [`& .${buttonGroupClasses.lastButton},& .${buttonGroupClasses.middleButton}`]: {
96 borderTopRightRadius: 0,
97 borderTopLeftRadius: 0
98 },
99 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
100 borderBottomRightRadius: 0,
101 borderBottomLeftRadius: 0
102 }
103 }
104 }, {
105 props: {
106 orientation: 'horizontal'
107 },
108 style: {
109 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
110 borderTopRightRadius: 0,
111 borderBottomRightRadius: 0
112 },
113 [`& .${buttonGroupClasses.lastButton},& .${buttonGroupClasses.middleButton}`]: {
114 borderTopLeftRadius: 0,
115 borderBottomLeftRadius: 0
116 }
117 }
118 }, {
119 props: {
120 variant: 'text',
121 orientation: 'horizontal'
122 },
123 style: {
124 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
125 borderRight: theme.vars ? `1px solid rgba(${theme.vars.palette.common.onBackgroundChannel} / 0.23)` : `1px solid ${theme.palette.mode === 'light' ? 'rgba(0, 0, 0, 0.23)' : 'rgba(255, 255, 255, 0.23)'}`,
126 [`&.${buttonGroupClasses.disabled}`]: {
127 borderRight: `1px solid ${(theme.vars || theme).palette.action.disabled}`
128 }
129 }
130 }
131 }, {
132 props: {
133 variant: 'text',
134 orientation: 'vertical'
135 },
136 style: {
137 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
138 borderBottom: theme.vars ? `1px solid rgba(${theme.vars.palette.common.onBackgroundChannel} / 0.23)` : `1px solid ${theme.palette.mode === 'light' ? 'rgba(0, 0, 0, 0.23)' : 'rgba(255, 255, 255, 0.23)'}`,
139 [`&.${buttonGroupClasses.disabled}`]: {
140 borderBottom: `1px solid ${(theme.vars || theme).palette.action.disabled}`
141 }
142 }
143 }
144 }, ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()).flatMap(([color]) => [{
145 props: {
146 variant: 'text',
147 color
148 },
149 style: {
150 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
151 borderColor: theme.vars ? `rgba(${theme.vars.palette[color].mainChannel} / 0.5)` : alpha(theme.palette[color].main, 0.5)
152 }
153 }
154 }]), {
155 props: {
156 variant: 'outlined',
157 orientation: 'horizontal'
158 },
159 style: {
160 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
161 borderRightColor: 'transparent',
162 '&:hover': {
163 borderRightColor: 'currentColor'
164 }
165 },
166 [`& .${buttonGroupClasses.lastButton},& .${buttonGroupClasses.middleButton}`]: {
167 marginLeft: -1
168 }
169 }
170 }, {
171 props: {
172 variant: 'outlined',
173 orientation: 'vertical'
174 },
175 style: {
176 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
177 borderBottomColor: 'transparent',
178 '&:hover': {
179 borderBottomColor: 'currentColor'
180 }
181 },
182 [`& .${buttonGroupClasses.lastButton},& .${buttonGroupClasses.middleButton}`]: {
183 marginTop: -1
184 }
185 }
186 }, {
187 props: {
188 variant: 'contained',
189 orientation: 'horizontal'
190 },
191 style: {
192 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
193 borderRight: `1px solid ${(theme.vars || theme).palette.grey[400]}`,
194 [`&.${buttonGroupClasses.disabled}`]: {
195 borderRight: `1px solid ${(theme.vars || theme).palette.action.disabled}`
196 }
197 }
198 }
199 }, {
200 props: {
201 variant: 'contained',
202 orientation: 'vertical'
203 },
204 style: {
205 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
206 borderBottom: `1px solid ${(theme.vars || theme).palette.grey[400]}`,
207 [`&.${buttonGroupClasses.disabled}`]: {
208 borderBottom: `1px solid ${(theme.vars || theme).palette.action.disabled}`
209 }
210 }
211 }
212 }, ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter(['dark'])).map(([color]) => ({
213 props: {
214 variant: 'contained',
215 color
216 },
217 style: {
218 [`& .${buttonGroupClasses.firstButton},& .${buttonGroupClasses.middleButton}`]: {
219 borderColor: (theme.vars || theme).palette[color].dark
220 }
221 }
222 }))],
223 [`& .${buttonGroupClasses.grouped}`]: {
224 minWidth: 40,
225 boxShadow: 'none',
226 props: {
227 variant: 'contained'
228 },
229 style: {
230 '&:hover': {
231 boxShadow: 'none'
232 }
233 }
234 }
235})));
236const ButtonGroup = /*#__PURE__*/React.forwardRef(function ButtonGroup(inProps, ref) {
237 const props = useDefaultProps({
238 props: inProps,
239 name: 'MuiButtonGroup'
240 });
241 const {
242 children,
243 className,
244 color = 'primary',
245 component = 'div',
246 disabled = false,
247 disableElevation = false,
248 disableFocusRipple = false,
249 disableRipple = false,
250 fullWidth = false,
251 orientation = 'horizontal',
252 size = 'medium',
253 variant = 'outlined',
254 ...other
255 } = props;
256 const ownerState = {
257 ...props,
258 color,
259 component,
260 disabled,
261 disableElevation,
262 disableFocusRipple,
263 disableRipple,
264 fullWidth,
265 orientation,
266 size,
267 variant
268 };
269 const classes = useUtilityClasses(ownerState);
270 const context = React.useMemo(() => ({
271 className: classes.grouped,
272 color,
273 disabled,
274 disableElevation,
275 disableFocusRipple,
276 disableRipple,
277 fullWidth,
278 size,
279 variant
280 }), [color, disabled, disableElevation, disableFocusRipple, disableRipple, fullWidth, size, variant, classes.grouped]);
281 const validChildren = getValidReactChildren(children);
282 const childrenCount = validChildren.length;
283 const getButtonPositionClassName = index => {
284 const isFirstButton = index === 0;
285 const isLastButton = index === childrenCount - 1;
286 if (isFirstButton && isLastButton) {
287 return '';
288 }
289 if (isFirstButton) {
290 return classes.firstButton;
291 }
292 if (isLastButton) {
293 return classes.lastButton;
294 }
295 return classes.middleButton;
296 };
297 return /*#__PURE__*/_jsx(ButtonGroupRoot, {
298 as: component,
299 role: "group",
300 className: clsx(classes.root, className),
301 ref: ref,
302 ownerState: ownerState,
303 ...other,
304 children: /*#__PURE__*/_jsx(ButtonGroupContext.Provider, {
305 value: context,
306 children: validChildren.map((child, index) => {
307 return /*#__PURE__*/_jsx(ButtonGroupButtonContext.Provider, {
308 value: getButtonPositionClassName(index),
309 children: child
310 }, index);
311 })
312 })
313 });
314});
315process.env.NODE_ENV !== "production" ? ButtonGroup.propTypes /* remove-proptypes */ = {
316 // ┌────────────────────────────── Warning ──────────────────────────────┐
317 // │ These PropTypes are generated from the TypeScript type definitions. │
318 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
319 // └─────────────────────────────────────────────────────────────────────┘
320 /**
321 * The content of the component.
322 */
323 children: PropTypes.node,
324 /**
325 * Override or extend the styles applied to the component.
326 */
327 classes: PropTypes.object,
328 /**
329 * @ignore
330 */
331 className: PropTypes.string,
332 /**
333 * The color of the component.
334 * It supports both default and custom theme colors, which can be added as shown in the
335 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
336 * @default 'primary'
337 */
338 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['inherit', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
339 /**
340 * The component used for the root node.
341 * Either a string to use a HTML element or a component.
342 */
343 component: PropTypes.elementType,
344 /**
345 * If `true`, the component is disabled.
346 * @default false
347 */
348 disabled: PropTypes.bool,
349 /**
350 * If `true`, no elevation is used.
351 * @default false
352 */
353 disableElevation: PropTypes.bool,
354 /**
355 * If `true`, the button keyboard focus ripple is disabled.
356 * @default false
357 */
358 disableFocusRipple: PropTypes.bool,
359 /**
360 * If `true`, the button ripple effect is disabled.
361 * @default false
362 */
363 disableRipple: PropTypes.bool,
364 /**
365 * If `true`, the buttons will take up the full width of its container.
366 * @default false
367 */
368 fullWidth: PropTypes.bool,
369 /**
370 * The component orientation (layout flow direction).
371 * @default 'horizontal'
372 */
373 orientation: PropTypes.oneOf(['horizontal', 'vertical']),
374 /**
375 * The size of the component.
376 * `small` is equivalent to the dense button styling.
377 * @default 'medium'
378 */
379 size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['small', 'medium', 'large']), PropTypes.string]),
380 /**
381 * The system prop that allows defining system overrides as well as additional CSS styles.
382 */
383 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
384 /**
385 * The variant to use.
386 * @default 'outlined'
387 */
388 variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['contained', 'outlined', 'text']), PropTypes.string])
389} : void 0;
390export default ButtonGroup;
\No newline at end of file