UNPKG

7.52 kBJavaScriptView Raw
1import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4import _extends from "@babel/runtime/helpers/esm/extends";
5import * as React from 'react';
6import PropTypes from 'prop-types';
7import clsx from 'clsx';
8import { elementTypeAcceptingRef } from '@mui/utils';
9import { unstable_composeClasses as composeClasses } from '@mui/base';
10import capitalize from '../utils/capitalize';
11import styled from '../styles/styled';
12import useThemeProps from '../styles/useThemeProps';
13import useIsFocusVisible from '../utils/useIsFocusVisible';
14import useForkRef from '../utils/useForkRef';
15import Typography from '../Typography';
16import linkClasses, { getLinkUtilityClass } from './linkClasses';
17import getTextDecoration, { colorTransformations } from './getTextDecoration';
18import { jsx as _jsx } from "react/jsx-runtime";
19
20var useUtilityClasses = function useUtilityClasses(ownerState) {
21 var classes = ownerState.classes,
22 component = ownerState.component,
23 focusVisible = ownerState.focusVisible,
24 underline = ownerState.underline;
25 var slots = {
26 root: ['root', "underline".concat(capitalize(underline)), component === 'button' && 'button', focusVisible && 'focusVisible']
27 };
28 return composeClasses(slots, getLinkUtilityClass, classes);
29};
30
31var LinkRoot = styled(Typography, {
32 name: 'MuiLink',
33 slot: 'Root',
34 overridesResolver: function overridesResolver(props, styles) {
35 var ownerState = props.ownerState;
36 return [styles.root, styles["underline".concat(capitalize(ownerState.underline))], ownerState.component === 'button' && styles.button];
37 }
38})(function (_ref) {
39 var theme = _ref.theme,
40 ownerState = _ref.ownerState;
41 return _extends({}, ownerState.underline === 'none' && {
42 textDecoration: 'none'
43 }, ownerState.underline === 'hover' && {
44 textDecoration: 'none',
45 '&:hover': {
46 textDecoration: 'underline'
47 }
48 }, ownerState.underline === 'always' && _extends({
49 textDecoration: 'underline'
50 }, ownerState.color !== 'inherit' && {
51 textDecorationColor: getTextDecoration({
52 theme: theme,
53 ownerState: ownerState
54 })
55 }, {
56 '&:hover': {
57 textDecorationColor: 'inherit'
58 }
59 }), ownerState.component === 'button' && _defineProperty({
60 position: 'relative',
61 WebkitTapHighlightColor: 'transparent',
62 backgroundColor: 'transparent',
63 // Reset default value
64 // We disable the focus ring for mouse, touch and keyboard users.
65 outline: 0,
66 border: 0,
67 margin: 0,
68 // Remove the margin in Safari
69 borderRadius: 0,
70 padding: 0,
71 // Remove the padding in Firefox
72 cursor: 'pointer',
73 userSelect: 'none',
74 verticalAlign: 'middle',
75 MozAppearance: 'none',
76 // Reset
77 WebkitAppearance: 'none',
78 // Reset
79 '&::-moz-focus-inner': {
80 borderStyle: 'none' // Remove Firefox dotted outline.
81
82 }
83 }, "&.".concat(linkClasses.focusVisible), {
84 outline: 'auto'
85 }));
86});
87var Link = /*#__PURE__*/React.forwardRef(function Link(inProps, ref) {
88 var props = useThemeProps({
89 props: inProps,
90 name: 'MuiLink'
91 });
92
93 var className = props.className,
94 _props$color = props.color,
95 color = _props$color === void 0 ? 'primary' : _props$color,
96 _props$component = props.component,
97 component = _props$component === void 0 ? 'a' : _props$component,
98 onBlur = props.onBlur,
99 onFocus = props.onFocus,
100 TypographyClasses = props.TypographyClasses,
101 _props$underline = props.underline,
102 underline = _props$underline === void 0 ? 'always' : _props$underline,
103 _props$variant = props.variant,
104 variant = _props$variant === void 0 ? 'inherit' : _props$variant,
105 sx = props.sx,
106 other = _objectWithoutProperties(props, ["className", "color", "component", "onBlur", "onFocus", "TypographyClasses", "underline", "variant", "sx"]);
107
108 var _useIsFocusVisible = useIsFocusVisible(),
109 isFocusVisibleRef = _useIsFocusVisible.isFocusVisibleRef,
110 handleBlurVisible = _useIsFocusVisible.onBlur,
111 handleFocusVisible = _useIsFocusVisible.onFocus,
112 focusVisibleRef = _useIsFocusVisible.ref;
113
114 var _React$useState = React.useState(false),
115 focusVisible = _React$useState[0],
116 setFocusVisible = _React$useState[1];
117
118 var handlerRef = useForkRef(ref, focusVisibleRef);
119
120 var handleBlur = function handleBlur(event) {
121 handleBlurVisible(event);
122
123 if (isFocusVisibleRef.current === false) {
124 setFocusVisible(false);
125 }
126
127 if (onBlur) {
128 onBlur(event);
129 }
130 };
131
132 var handleFocus = function handleFocus(event) {
133 handleFocusVisible(event);
134
135 if (isFocusVisibleRef.current === true) {
136 setFocusVisible(true);
137 }
138
139 if (onFocus) {
140 onFocus(event);
141 }
142 };
143
144 var ownerState = _extends({}, props, {
145 color: color,
146 component: component,
147 focusVisible: focusVisible,
148 underline: underline,
149 variant: variant
150 });
151
152 var classes = useUtilityClasses(ownerState);
153 return /*#__PURE__*/_jsx(LinkRoot, _extends({
154 color: color,
155 className: clsx(classes.root, className),
156 classes: TypographyClasses,
157 component: component,
158 onBlur: handleBlur,
159 onFocus: handleFocus,
160 ref: handlerRef,
161 ownerState: ownerState,
162 variant: variant,
163 sx: [].concat(_toConsumableArray(!Object.keys(colorTransformations).includes(color) ? [{
164 color: color
165 }] : []), _toConsumableArray(Array.isArray(sx) ? sx : [sx]))
166 }, other));
167});
168process.env.NODE_ENV !== "production" ? Link.propTypes
169/* remove-proptypes */
170= {
171 // ----------------------------- Warning --------------------------------
172 // | These PropTypes are generated from the TypeScript type definitions |
173 // | To update them edit the d.ts file and run "yarn proptypes" |
174 // ----------------------------------------------------------------------
175
176 /**
177 * The content of the component.
178 */
179 children: PropTypes.node,
180
181 /**
182 * Override or extend the styles applied to the component.
183 */
184 classes: PropTypes.object,
185
186 /**
187 * @ignore
188 */
189 className: PropTypes.string,
190
191 /**
192 * The color of the link.
193 * @default 'primary'
194 */
195 color: PropTypes
196 /* @typescript-to-proptypes-ignore */
197 .any,
198
199 /**
200 * The component used for the root node.
201 * Either a string to use a HTML element or a component.
202 */
203 component: elementTypeAcceptingRef,
204
205 /**
206 * @ignore
207 */
208 onBlur: PropTypes.func,
209
210 /**
211 * @ignore
212 */
213 onFocus: PropTypes.func,
214
215 /**
216 * The system prop that allows defining system overrides as well as additional CSS styles.
217 */
218 sx: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])), PropTypes.func, PropTypes.object]),
219
220 /**
221 * `classes` prop applied to the [`Typography`](/material-ui/api/typography/) element.
222 */
223 TypographyClasses: PropTypes.object,
224
225 /**
226 * Controls when the link should have an underline.
227 * @default 'always'
228 */
229 underline: PropTypes.oneOf(['always', 'hover', 'none']),
230
231 /**
232 * Applies the theme typography styles.
233 * @default 'inherit'
234 */
235 variant: PropTypes
236 /* @typescript-to-proptypes-ignore */
237 .oneOfType([PropTypes.oneOf(['body1', 'body2', 'button', 'caption', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'inherit', 'overline', 'subtitle1', 'subtitle2']), PropTypes.string])
238} : void 0;
239export default Link;
\No newline at end of file