-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
/
Copy pathLink.js
333 lines (319 loc) · 8.7 KB
/
Link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { alpha } from '@mui/system/colorManipulator';
import elementTypeAcceptingRef from '@mui/utils/elementTypeAcceptingRef';
import composeClasses from '@mui/utils/composeClasses';
import isFocusVisible from '@mui/utils/isFocusVisible';
import capitalize from '../utils/capitalize';
import { styled, useTheme } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import createSimplePaletteValueFilter from '../utils/createSimplePaletteValueFilter';
import { useDefaultProps } from '../DefaultPropsProvider';
import Typography from '../Typography';
import linkClasses, { getLinkUtilityClass } from './linkClasses';
import getTextDecoration from './getTextDecoration';
const v6Colors = {
primary: true,
secondary: true,
error: true,
info: true,
success: true,
warning: true,
textPrimary: true,
textSecondary: true,
textDisabled: true,
};
const useUtilityClasses = (ownerState) => {
const { classes, component, focusVisible, underline } = ownerState;
const slots = {
root: [
'root',
`underline${capitalize(underline)}`,
component === 'button' && 'button',
focusVisible && 'focusVisible',
],
};
return composeClasses(slots, getLinkUtilityClass, classes);
};
const LinkRoot = styled(Typography, {
name: 'MuiLink',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.root,
styles[`underline${capitalize(ownerState.underline)}`],
ownerState.component === 'button' && styles.button,
];
},
})(
memoTheme(({ theme }) => {
return {
variants: [
{
props: {
underline: 'none',
},
style: {
textDecoration: 'none',
},
},
{
props: {
underline: 'hover',
},
style: {
textDecoration: 'none',
'&:hover': {
textDecoration: 'underline',
},
},
},
{
props: {
underline: 'always',
},
style: {
textDecoration: 'underline',
'&:hover': {
textDecorationColor: 'inherit',
},
},
},
{
props: ({ underline, ownerState }) =>
underline === 'always' && ownerState.color !== 'inherit',
style: {
textDecorationColor: 'var(--Link-underlineColor)',
},
},
...Object.entries(theme.palette)
.filter(createSimplePaletteValueFilter())
.map(([color]) => ({
props: { underline: 'always', color },
style: {
'--Link-underlineColor': theme.vars
? `rgba(${theme.vars.palette[color].mainChannel} / 0.4)`
: alpha(theme.palette[color].main, 0.4),
},
})),
{
props: { underline: 'always', color: 'textPrimary' },
style: {
'--Link-underlineColor': theme.vars
? `rgba(${theme.vars.palette.text.primaryChannel} / 0.4)`
: alpha(theme.palette.text.primary, 0.4),
},
},
{
props: { underline: 'always', color: 'textSecondary' },
style: {
'--Link-underlineColor': theme.vars
? `rgba(${theme.vars.palette.text.secondaryChannel} / 0.4)`
: alpha(theme.palette.text.secondary, 0.4),
},
},
{
props: { underline: 'always', color: 'textDisabled' },
style: {
'--Link-underlineColor': (theme.vars || theme).palette.text.disabled,
},
},
{
props: {
component: 'button',
},
style: {
position: 'relative',
WebkitTapHighlightColor: 'transparent',
backgroundColor: 'transparent', // Reset default value
// We disable the focus ring for mouse, touch and keyboard users.
outline: 0,
border: 0,
margin: 0, // Remove the margin in Safari
borderRadius: 0,
padding: 0, // Remove the padding in Firefox
cursor: 'pointer',
userSelect: 'none',
verticalAlign: 'middle',
MozAppearance: 'none', // Reset
WebkitAppearance: 'none', // Reset
'&::-moz-focus-inner': {
borderStyle: 'none', // Remove Firefox dotted outline.
},
[`&.${linkClasses.focusVisible}`]: {
outline: 'auto',
},
},
},
],
};
}),
);
const Link = React.forwardRef(function Link(inProps, ref) {
const props = useDefaultProps({
props: inProps,
name: 'MuiLink',
});
const theme = useTheme();
const {
className,
color = 'primary',
component = 'a',
onBlur,
onFocus,
TypographyClasses,
underline = 'always',
variant = 'inherit',
sx,
...other
} = props;
const [focusVisible, setFocusVisible] = React.useState(false);
const handleBlur = (event) => {
if (!isFocusVisible(event.target)) {
setFocusVisible(false);
}
if (onBlur) {
onBlur(event);
}
};
const handleFocus = (event) => {
if (isFocusVisible(event.target)) {
setFocusVisible(true);
}
if (onFocus) {
onFocus(event);
}
};
const ownerState = {
...props,
color,
component,
focusVisible,
underline,
variant,
};
const classes = useUtilityClasses(ownerState);
return (
<LinkRoot
color={color}
className={clsx(classes.root, className)}
classes={TypographyClasses}
component={component}
onBlur={handleBlur}
onFocus={handleFocus}
ref={ref}
ownerState={ownerState}
variant={variant}
{...other}
sx={[
...(v6Colors[color] === undefined ? [{ color }] : []),
...(Array.isArray(sx) ? sx : [sx]),
]}
style={{
...other.style,
...(underline === 'always' &&
color !== 'inherit' &&
!v6Colors[color] && {
'--Link-underlineColor': getTextDecoration({ theme, ownerState }),
}),
}}
/>
);
});
Link.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the link.
* @default 'primary'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf([
'primary',
'secondary',
'success',
'error',
'info',
'warning',
'textPrimary',
'textSecondary',
'textDisabled',
]),
PropTypes.string,
]),
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: elementTypeAcceptingRef,
/**
* @ignore
*/
onBlur: PropTypes.func,
/**
* @ignore
*/
onFocus: PropTypes.func,
/**
* @ignore
*/
style: PropTypes.object,
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* `classes` prop applied to the [`Typography`](https://mui.com/material-ui/api/typography/) element.
*/
TypographyClasses: PropTypes.object,
/**
* Controls when the link should have an underline.
* @default 'always'
*/
underline: PropTypes.oneOf(['always', 'hover', 'none']),
/**
* Applies the theme typography styles.
* @default 'inherit'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf([
'body1',
'body2',
'button',
'caption',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'inherit',
'overline',
'subtitle1',
'subtitle2',
]),
PropTypes.string,
]),
};
export default Link;