UNPKG

8.11 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import clsx from 'clsx';
6import chainPropTypes from '@mui/utils/chainPropTypes';
7import composeClasses from '@mui/utils/composeClasses';
8import { alpha } from '@mui/system/colorManipulator';
9import { styled } from "../zero-styled/index.js";
10import memoTheme from "../utils/memoTheme.js";
11import createSimplePaletteValueFilter from "../utils/createSimplePaletteValueFilter.js";
12import { useDefaultProps } from "../DefaultPropsProvider/index.js";
13import ButtonBase from "../ButtonBase/index.js";
14import capitalize from "../utils/capitalize.js";
15import iconButtonClasses, { getIconButtonUtilityClass } from "./iconButtonClasses.js";
16import { jsx as _jsx } from "react/jsx-runtime";
17const useUtilityClasses = ownerState => {
18 const {
19 classes,
20 disabled,
21 color,
22 edge,
23 size
24 } = ownerState;
25 const slots = {
26 root: ['root', disabled && 'disabled', color !== 'default' && `color${capitalize(color)}`, edge && `edge${capitalize(edge)}`, `size${capitalize(size)}`]
27 };
28 return composeClasses(slots, getIconButtonUtilityClass, classes);
29};
30const IconButtonRoot = styled(ButtonBase, {
31 name: 'MuiIconButton',
32 slot: 'Root',
33 overridesResolver: (props, styles) => {
34 const {
35 ownerState
36 } = props;
37 return [styles.root, ownerState.color !== 'default' && styles[`color${capitalize(ownerState.color)}`], ownerState.edge && styles[`edge${capitalize(ownerState.edge)}`], styles[`size${capitalize(ownerState.size)}`]];
38 }
39})(memoTheme(({
40 theme
41}) => ({
42 textAlign: 'center',
43 flex: '0 0 auto',
44 fontSize: theme.typography.pxToRem(24),
45 padding: 8,
46 borderRadius: '50%',
47 color: (theme.vars || theme).palette.action.active,
48 transition: theme.transitions.create('background-color', {
49 duration: theme.transitions.duration.shortest
50 }),
51 variants: [{
52 props: props => !props.disableRipple,
53 style: {
54 '--IconButton-hoverBg': theme.vars ? `rgba(${theme.vars.palette.action.activeChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha(theme.palette.action.active, theme.palette.action.hoverOpacity),
55 '&:hover': {
56 backgroundColor: 'var(--IconButton-hoverBg)',
57 // Reset on touch devices, it doesn't add specificity
58 '@media (hover: none)': {
59 backgroundColor: 'transparent'
60 }
61 }
62 }
63 }, {
64 props: {
65 edge: 'start'
66 },
67 style: {
68 marginLeft: -12
69 }
70 }, {
71 props: {
72 edge: 'start',
73 size: 'small'
74 },
75 style: {
76 marginLeft: -3
77 }
78 }, {
79 props: {
80 edge: 'end'
81 },
82 style: {
83 marginRight: -12
84 }
85 }, {
86 props: {
87 edge: 'end',
88 size: 'small'
89 },
90 style: {
91 marginRight: -3
92 }
93 }]
94})), memoTheme(({
95 theme
96}) => ({
97 variants: [{
98 props: {
99 color: 'inherit'
100 },
101 style: {
102 color: 'inherit'
103 }
104 }, ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()) // check all the used fields in the style below
105 .map(([color]) => ({
106 props: {
107 color
108 },
109 style: {
110 color: (theme.vars || theme).palette[color].main
111 }
112 })), ...Object.entries(theme.palette).filter(createSimplePaletteValueFilter()) // check all the used fields in the style below
113 .map(([color]) => ({
114 props: {
115 color
116 },
117 style: {
118 '--IconButton-hoverBg': theme.vars ? `rgba(${(theme.vars || theme).palette[color].mainChannel} / ${theme.vars.palette.action.hoverOpacity})` : alpha((theme.vars || theme).palette[color].main, theme.palette.action.hoverOpacity)
119 }
120 })), {
121 props: {
122 size: 'small'
123 },
124 style: {
125 padding: 5,
126 fontSize: theme.typography.pxToRem(18)
127 }
128 }, {
129 props: {
130 size: 'large'
131 },
132 style: {
133 padding: 12,
134 fontSize: theme.typography.pxToRem(28)
135 }
136 }],
137 [`&.${iconButtonClasses.disabled}`]: {
138 backgroundColor: 'transparent',
139 color: (theme.vars || theme).palette.action.disabled
140 }
141})));
142
143/**
144 * Refer to the [Icons](/material-ui/icons/) section of the documentation
145 * regarding the available icon options.
146 */
147const IconButton = /*#__PURE__*/React.forwardRef(function IconButton(inProps, ref) {
148 const props = useDefaultProps({
149 props: inProps,
150 name: 'MuiIconButton'
151 });
152 const {
153 edge = false,
154 children,
155 className,
156 color = 'default',
157 disabled = false,
158 disableFocusRipple = false,
159 size = 'medium',
160 ...other
161 } = props;
162 const ownerState = {
163 ...props,
164 edge,
165 color,
166 disabled,
167 disableFocusRipple,
168 size
169 };
170 const classes = useUtilityClasses(ownerState);
171 return /*#__PURE__*/_jsx(IconButtonRoot, {
172 className: clsx(classes.root, className),
173 centerRipple: true,
174 focusRipple: !disableFocusRipple,
175 disabled: disabled,
176 ref: ref,
177 ...other,
178 ownerState: ownerState,
179 children: children
180 });
181});
182process.env.NODE_ENV !== "production" ? IconButton.propTypes /* remove-proptypes */ = {
183 // ┌────────────────────────────── Warning ──────────────────────────────┐
184 // │ These PropTypes are generated from the TypeScript type definitions. │
185 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
186 // └─────────────────────────────────────────────────────────────────────┘
187 /**
188 * The icon to display.
189 */
190 children: chainPropTypes(PropTypes.node, props => {
191 const found = React.Children.toArray(props.children).some(child => /*#__PURE__*/React.isValidElement(child) && child.props.onClick);
192 if (found) {
193 return new Error(['MUI: You are providing an onClick event listener to a child of a button element.', 'Prefer applying it to the IconButton directly.', 'This guarantees that the whole <button> will be responsive to click events.'].join('\n'));
194 }
195 return null;
196 }),
197 /**
198 * Override or extend the styles applied to the component.
199 */
200 classes: PropTypes.object,
201 /**
202 * @ignore
203 */
204 className: PropTypes.string,
205 /**
206 * The color of the component.
207 * It supports both default and custom theme colors, which can be added as shown in the
208 * [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
209 * @default 'default'
210 */
211 color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['inherit', 'default', 'primary', 'secondary', 'error', 'info', 'success', 'warning']), PropTypes.string]),
212 /**
213 * If `true`, the component is disabled.
214 * @default false
215 */
216 disabled: PropTypes.bool,
217 /**
218 * If `true`, the keyboard focus ripple is disabled.
219 * @default false
220 */
221 disableFocusRipple: PropTypes.bool,
222 /**
223 * If `true`, the ripple effect is disabled.
224 *
225 * ⚠️ Without a ripple there is no styling for :focus-visible by default. Be sure
226 * to highlight the element by applying separate styles with the `.Mui-focusVisible` class.
227 * @default false
228 */
229 disableRipple: PropTypes.bool,
230 /**
231 * If given, uses a negative margin to counteract the padding on one
232 * side (this is often helpful for aligning the left or right
233 * side of the icon with content above or below, without ruining the border
234 * size and shape).
235 * @default false
236 */
237 edge: PropTypes.oneOf(['end', 'start', false]),
238 /**
239 * The size of the component.
240 * `small` is equivalent to the dense button styling.
241 * @default 'medium'
242 */
243 size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.oneOf(['small', 'medium', 'large']), PropTypes.string]),
244 /**
245 * The system prop that allows defining system overrides as well as additional CSS styles.
246 */
247 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
248} : void 0;
249export default IconButton;
\No newline at end of file