UNPKG

10.4 kBJavaScriptView Raw
1import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
3import _extends from "@babel/runtime/helpers/esm/extends";
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import clsx from 'clsx';
7import { unstable_composeClasses as composeClasses } from '@mui/base';
8import { alpha } from '@mui/system';
9import styled, { rootShouldForwardProp } from '../styles/styled';
10import useThemeProps from '../styles/useThemeProps';
11import ButtonBase from '../ButtonBase';
12import useEnhancedEffect from '../utils/useEnhancedEffect';
13import useForkRef from '../utils/useForkRef';
14import ListContext from '../List/ListContext';
15import listItemButtonClasses, { getListItemButtonUtilityClass } from './listItemButtonClasses';
16import { jsx as _jsx } from "react/jsx-runtime";
17export var overridesResolver = function overridesResolver(props, styles) {
18 var ownerState = props.ownerState;
19 return [styles.root, ownerState.dense && styles.dense, ownerState.alignItems === 'flex-start' && styles.alignItemsFlexStart, ownerState.divider && styles.divider, !ownerState.disableGutters && styles.gutters];
20};
21
22var useUtilityClasses = function useUtilityClasses(ownerState) {
23 var alignItems = ownerState.alignItems,
24 classes = ownerState.classes,
25 dense = ownerState.dense,
26 disabled = ownerState.disabled,
27 disableGutters = ownerState.disableGutters,
28 divider = ownerState.divider,
29 selected = ownerState.selected;
30 var slots = {
31 root: ['root', dense && 'dense', !disableGutters && 'gutters', divider && 'divider', disabled && 'disabled', alignItems === 'flex-start' && 'alignItemsFlexStart', selected && 'selected']
32 };
33 var composedClasses = composeClasses(slots, getListItemButtonUtilityClass, classes);
34 return _extends({}, classes, composedClasses);
35};
36
37var ListItemButtonRoot = styled(ButtonBase, {
38 shouldForwardProp: function shouldForwardProp(prop) {
39 return rootShouldForwardProp(prop) || prop === 'classes';
40 },
41 name: 'MuiListItemButton',
42 slot: 'Root',
43 overridesResolver: overridesResolver
44})(function (_ref) {
45 var _extends2;
46
47 var theme = _ref.theme,
48 ownerState = _ref.ownerState;
49 return _extends((_extends2 = {
50 display: 'flex',
51 flexGrow: 1,
52 justifyContent: 'flex-start',
53 alignItems: 'center',
54 position: 'relative',
55 textDecoration: 'none',
56 minWidth: 0,
57 boxSizing: 'border-box',
58 textAlign: 'left',
59 paddingTop: 8,
60 paddingBottom: 8,
61 transition: theme.transitions.create('background-color', {
62 duration: theme.transitions.duration.shortest
63 }),
64 '&:hover': {
65 textDecoration: 'none',
66 backgroundColor: (theme.vars || theme).palette.action.hover,
67 // Reset on touch devices, it doesn't add specificity
68 '@media (hover: none)': {
69 backgroundColor: 'transparent'
70 }
71 }
72 }, _defineProperty(_extends2, "&.".concat(listItemButtonClasses.selected), _defineProperty({
73 backgroundColor: theme.vars ? "rgba(".concat(theme.vars.palette.primary.mainChannel, " / ").concat(theme.vars.palette.action.selectedOpacity, ")") : alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity)
74 }, "&.".concat(listItemButtonClasses.focusVisible), {
75 backgroundColor: theme.vars ? "rgba(".concat(theme.vars.palette.primary.mainChannel, " / calc(").concat(theme.vars.palette.action.selectedOpacity, " + ").concat(theme.vars.palette.action.focusOpacity, "))") : alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity + theme.palette.action.focusOpacity)
76 })), _defineProperty(_extends2, "&.".concat(listItemButtonClasses.selected, ":hover"), {
77 backgroundColor: theme.vars ? "rgba(".concat(theme.vars.palette.primary.mainChannel, " / calc(").concat(theme.vars.palette.action.selectedOpacity, " + ").concat(theme.vars.palette.action.hoverOpacity, "))") : alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity),
78 // Reset on touch devices, it doesn't add specificity
79 '@media (hover: none)': {
80 backgroundColor: theme.vars ? "rgba(".concat(theme.vars.palette.primary.mainChannel, " / ").concat(theme.vars.palette.action.selectedOpacity, ")") : alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity)
81 }
82 }), _defineProperty(_extends2, "&.".concat(listItemButtonClasses.focusVisible), {
83 backgroundColor: (theme.vars || theme).palette.action.focus
84 }), _defineProperty(_extends2, "&.".concat(listItemButtonClasses.disabled), {
85 opacity: (theme.vars || theme).palette.action.disabledOpacity
86 }), _extends2), ownerState.divider && {
87 borderBottom: "1px solid ".concat((theme.vars || theme).palette.divider),
88 backgroundClip: 'padding-box'
89 }, ownerState.alignItems === 'flex-start' && {
90 alignItems: 'flex-start'
91 }, !ownerState.disableGutters && {
92 paddingLeft: 16,
93 paddingRight: 16
94 }, ownerState.dense && {
95 paddingTop: 4,
96 paddingBottom: 4
97 });
98});
99var ListItemButton = /*#__PURE__*/React.forwardRef(function ListItemButton(inProps, ref) {
100 var props = useThemeProps({
101 props: inProps,
102 name: 'MuiListItemButton'
103 });
104
105 var _props$alignItems = props.alignItems,
106 alignItems = _props$alignItems === void 0 ? 'center' : _props$alignItems,
107 _props$autoFocus = props.autoFocus,
108 autoFocus = _props$autoFocus === void 0 ? false : _props$autoFocus,
109 _props$component = props.component,
110 component = _props$component === void 0 ? 'div' : _props$component,
111 children = props.children,
112 _props$dense = props.dense,
113 dense = _props$dense === void 0 ? false : _props$dense,
114 _props$disableGutters = props.disableGutters,
115 disableGutters = _props$disableGutters === void 0 ? false : _props$disableGutters,
116 _props$divider = props.divider,
117 divider = _props$divider === void 0 ? false : _props$divider,
118 focusVisibleClassName = props.focusVisibleClassName,
119 _props$selected = props.selected,
120 selected = _props$selected === void 0 ? false : _props$selected,
121 other = _objectWithoutProperties(props, ["alignItems", "autoFocus", "component", "children", "dense", "disableGutters", "divider", "focusVisibleClassName", "selected"]);
122
123 var context = React.useContext(ListContext);
124 var childContext = {
125 dense: dense || context.dense || false,
126 alignItems: alignItems,
127 disableGutters: disableGutters
128 };
129 var listItemRef = React.useRef(null);
130 useEnhancedEffect(function () {
131 if (autoFocus) {
132 if (listItemRef.current) {
133 listItemRef.current.focus();
134 } else if (process.env.NODE_ENV !== 'production') {
135 console.error('MUI: Unable to set focus to a ListItemButton whose component has not been rendered.');
136 }
137 }
138 }, [autoFocus]);
139
140 var ownerState = _extends({}, props, {
141 alignItems: alignItems,
142 dense: childContext.dense,
143 disableGutters: disableGutters,
144 divider: divider,
145 selected: selected
146 });
147
148 var classes = useUtilityClasses(ownerState);
149 var handleRef = useForkRef(listItemRef, ref);
150 return /*#__PURE__*/_jsx(ListContext.Provider, {
151 value: childContext,
152 children: /*#__PURE__*/_jsx(ListItemButtonRoot, _extends({
153 ref: handleRef,
154 href: other.href || other.to,
155 component: (other.href || other.to) && component === 'div' ? 'a' : component,
156 focusVisibleClassName: clsx(classes.focusVisible, focusVisibleClassName),
157 ownerState: ownerState
158 }, other, {
159 classes: classes,
160 children: children
161 }))
162 });
163});
164process.env.NODE_ENV !== "production" ? ListItemButton.propTypes
165/* remove-proptypes */
166= {
167 // ----------------------------- Warning --------------------------------
168 // | These PropTypes are generated from the TypeScript type definitions |
169 // | To update them edit the d.ts file and run "yarn proptypes" |
170 // ----------------------------------------------------------------------
171
172 /**
173 * Defines the `align-items` style property.
174 * @default 'center'
175 */
176 alignItems: PropTypes.oneOf(['center', 'flex-start']),
177
178 /**
179 * If `true`, the list item is focused during the first mount.
180 * Focus will also be triggered if the value changes from false to true.
181 * @default false
182 */
183 autoFocus: PropTypes.bool,
184
185 /**
186 * The content of the component if a `ListItemSecondaryAction` is used it must
187 * be the last child.
188 */
189 children: PropTypes.node,
190
191 /**
192 * Override or extend the styles applied to the component.
193 */
194 classes: PropTypes.object,
195
196 /**
197 * The component used for the root node.
198 * Either a string to use a HTML element or a component.
199 */
200 component: PropTypes.elementType,
201
202 /**
203 * If `true`, compact vertical padding designed for keyboard and mouse input is used.
204 * The prop defaults to the value inherited from the parent List component.
205 * @default false
206 */
207 dense: PropTypes.bool,
208
209 /**
210 * If `true`, the component is disabled.
211 * @default false
212 */
213 disabled: PropTypes.bool,
214
215 /**
216 * If `true`, the left and right padding is removed.
217 * @default false
218 */
219 disableGutters: PropTypes.bool,
220
221 /**
222 * If `true`, a 1px light border is added to the bottom of the list item.
223 * @default false
224 */
225 divider: PropTypes.bool,
226
227 /**
228 * This prop can help identify which element has keyboard focus.
229 * The class name will be applied when the element gains the focus through keyboard interaction.
230 * It's a polyfill for the [CSS :focus-visible selector](https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo).
231 * The rationale for using this feature [is explained here](https://github.com/WICG/focus-visible/blob/HEAD/explainer.md).
232 * A [polyfill can be used](https://github.com/WICG/focus-visible) to apply a `focus-visible` class to other components
233 * if needed.
234 */
235 focusVisibleClassName: PropTypes.string,
236
237 /**
238 * @ignore
239 */
240 href: PropTypes.string,
241
242 /**
243 * Use to apply selected styling.
244 * @default false
245 */
246 selected: PropTypes.bool,
247
248 /**
249 * The system prop that allows defining system overrides as well as additional CSS styles.
250 */
251 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object])
252} : void 0;
253export default ListItemButton;
\No newline at end of file