forked from mui/material-ui
/
Link.tsx
335 lines (317 loc) · 9.74 KB
/
Link.tsx
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
334
335
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_composeClasses as composeClasses } from '@mui/base';
import { OverridableComponent } from '@mui/types';
import {
unstable_capitalize as capitalize,
unstable_useForkRef as useForkRef,
unstable_useIsFocusVisible as useIsFocusVisible,
} from '@mui/utils';
import { unstable_extendSxProp as extendSxProp } from '@mui/system';
import styled from '../styles/styled';
import useThemeProps from '../styles/useThemeProps';
import linkClasses, { getLinkUtilityClass } from './linkClasses';
import { LinkProps, LinkTypeMap } from './LinkProps';
import { TypographyContext } from '../Typography/Typography';
const useUtilityClasses = (ownerState: LinkProps) => {
const { level, color, variant, underline, focusVisible, disabled } = ownerState;
const slots = {
root: [
'root',
color && `color${capitalize(color)}`,
disabled && 'disabled',
focusVisible && 'focusVisible',
level,
underline && `underline${capitalize(underline)}`,
variant && `variant${capitalize(variant)}`,
],
startDecorator: ['startDecorator'],
endDecorator: ['endDecorator'],
};
return composeClasses(slots, getLinkUtilityClass, {});
};
const StartDecorator = styled('span', {
name: 'JoyLink',
slot: 'StartDecorator',
overridesResolver: (props, styles) => styles.startDecorator,
})<{ ownerState: LinkProps }>({
display: 'inline-flex',
marginInlineEnd: 'min(var(--Link-gap, 0.25em), 0.5rem)',
});
const EndDecorator = styled('span', {
name: 'JoyLink',
slot: 'endDecorator',
overridesResolver: (props, styles) => styles.endDecorator,
})<{ ownerState: LinkProps }>({
display: 'inline-flex',
marginInlineStart: 'min(var(--Link-gap, 0.25em), 0.5rem)',
});
const LinkRoot = styled('a', {
name: 'JoyLink',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})<{ ownerState: LinkProps }>(({ theme, ownerState }) => {
return [
{
'--Icon-fontSize': '1.25em',
...(ownerState.level && ownerState.level !== 'inherit' && theme.typography[ownerState.level]),
...(ownerState.level === 'inherit' && {
fontSize: 'inherit',
fontFamily: 'inherit',
lineHeight: 'inherit',
}),
...(ownerState.underline === 'none' && {
textDecoration: 'none',
}),
...(ownerState.underline === 'hover' && {
textDecoration: 'none',
'&:hover': {
textDecorationLine: 'underline',
},
}),
...(ownerState.underline === 'always' && {
textDecoration: 'underline',
}),
...(ownerState.startDecorator && {
verticalAlign: 'bottom', // to make the link align with the parent's content
}),
display: 'inline-flex',
alignItems: 'center',
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: theme.vars.radius.xs,
padding: 0, // Remove the padding in Firefox
textDecorationColor: `rgba(${
theme.vars.palette[ownerState.color!]?.mainChannel
} / var(--Link-underlineOpacity, 0.72))`,
...(ownerState.variant
? {
paddingInline: '0.25em', // better than left, right because it also works with writing mode.
marginInline: '-0.25em',
}
: {
color: theme.vars.palette[ownerState.color!]?.plainColor,
cursor: 'pointer',
[`&.${linkClasses.disabled}`]: {
pointerEvents: 'none',
color: theme.vars.palette[ownerState.color!]?.plainDisabledColor,
},
}),
userSelect: 'none',
MozAppearance: 'none', // Reset
WebkitAppearance: 'none', // Reset
'&::-moz-focus-inner': {
borderStyle: 'none', // Remove Firefox dotted outline.
},
...(ownerState.overlay
? {
position: 'initial',
'&::after': {
content: '""',
display: 'block',
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
borderRadius: `var(--Link-overlayRadius, var(--internal-action-radius, inherit))`,
margin: `var(--Link-overlayMargin)`,
},
[`${theme.focus.selector}`]: {
'&::after': theme.focus.default,
},
}
: {
position: 'relative',
[theme.focus.selector]: theme.focus.default,
}),
},
ownerState.variant && theme.variants[ownerState.variant]?.[ownerState.color!],
ownerState.variant && theme.variants[`${ownerState.variant}Hover`]?.[ownerState.color!],
ownerState.variant && theme.variants[`${ownerState.variant}Active`]?.[ownerState.color!],
ownerState.variant && theme.variants[`${ownerState.variant}Disabled`]?.[ownerState.color!],
];
});
const Link = React.forwardRef(function Link(inProps, ref) {
const {
color = 'primary',
textColor,
...themeProps
} = useThemeProps<typeof inProps & LinkProps>({
props: inProps,
name: 'JoyLink',
});
const nested = React.useContext(TypographyContext);
const props = extendSxProp({ ...themeProps, color: textColor }) as LinkProps;
const {
className,
component = 'a',
children,
disabled = false,
onBlur,
onFocus,
level: levelProp = 'body1',
overlay = false,
underline = 'hover',
variant,
endDecorator,
startDecorator,
...other
} = props;
const level = nested ? inProps.level || 'inherit' : levelProp;
const {
isFocusVisibleRef,
onBlur: handleBlurVisible,
onFocus: handleFocusVisible,
ref: focusVisibleRef,
} = useIsFocusVisible();
const [focusVisible, setFocusVisible] = React.useState<boolean>(false);
const handleRef = useForkRef(ref, focusVisibleRef) as React.Ref<HTMLAnchorElement>;
const handleBlur = (event: React.FocusEvent<HTMLAnchorElement>) => {
handleBlurVisible(event);
if (isFocusVisibleRef.current === false) {
setFocusVisible(false);
}
if (onBlur) {
onBlur(event);
}
};
const handleFocus = (event: React.FocusEvent<HTMLAnchorElement>) => {
handleFocusVisible(event);
if (isFocusVisibleRef.current === true) {
setFocusVisible(true);
}
if (onFocus) {
onFocus(event);
}
};
const ownerState = {
...props,
color,
component,
disabled,
focusVisible,
underline,
variant,
level,
overlay,
nested,
};
const classes = useUtilityClasses(ownerState);
return (
<LinkRoot
className={clsx(classes.root, className)}
as={component}
onBlur={handleBlur}
onFocus={handleFocus}
ref={handleRef}
ownerState={ownerState}
{...other}
>
{startDecorator && (
<StartDecorator ownerState={ownerState} className={classes.startDecorator}>
{startDecorator}
</StartDecorator>
)}
{children}
{endDecorator && (
<EndDecorator ownerState={ownerState} className={classes.endDecorator}>
{endDecorator}
</EndDecorator>
)}
</LinkRoot>
);
}) as OverridableComponent<LinkTypeMap>;
Link.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the link.
* @default 'primary'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['context', 'danger', 'info', 'neutral', 'primary', 'success', 'warning']),
PropTypes.string,
]),
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* Element placed after the children.
*/
endDecorator: PropTypes.node,
/**
* Applies the theme typography styles.
* @default 'body1'
*/
level: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['body1', 'body2', 'body3', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'inherit']),
PropTypes.string,
]),
/**
* @ignore
*/
onBlur: PropTypes.func,
/**
* @ignore
*/
onFocus: PropTypes.func,
/**
* If `true`, the ::after psuedo element is added to cover the area of interaction.
* The parent of the overlay Link should have `relative` CSS position.
* @default false
*/
overlay: PropTypes.bool,
/**
* Element placed before the children.
*/
startDecorator: PropTypes.node,
/**
* 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,
]),
/**
* The system color.
*/
textColor: PropTypes /* @typescript-to-proptypes-ignore */.any,
/**
* Controls when the link should have an underline.
* @default 'hover'
*/
underline: PropTypes.oneOf(['always', 'hover', 'none']),
/**
* Applies the theme link styles.
* @default 'plain'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
PropTypes.string,
]),
} as any;
export default Link;