forked from mui/material-ui
/
Link.js
187 lines (181 loc) · 4.59 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
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import capitalize from '../utils/capitalize';
import withStyles from '../styles/withStyles';
import { elementTypeAcceptingRef } from '@material-ui/utils';
import useIsFocusVisible from '../utils/useIsFocusVisible';
import useForkRef from '../utils/useForkRef';
import Typography from '../Typography';
export const styles = {
/* Styles applied to the root element. */
root: {},
/* Styles applied to the root element if `underline="none"`. */
underlineNone: {
textDecoration: 'none',
},
/* Styles applied to the root element if `underline="hover"`. */
underlineHover: {
textDecoration: 'none',
'&:hover': {
textDecoration: 'underline',
},
},
/* Styles applied to the root element if `underline="always"`. */
underlineAlways: {
textDecoration: 'underline',
},
// Same reset as ButtonBase.root
/* Styles applied to the root element if `component="button"`. */
button: {
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',
'-moz-appearance': 'none', // Reset
'-webkit-appearance': 'none', // Reset
'&::-moz-focus-inner': {
borderStyle: 'none', // Remove Firefox dotted outline.
},
'&$focusVisible': {
outline: 'auto',
},
},
/* Pseudo-class applied to the root element if the link is keyboard focused. */
focusVisible: {},
};
const Link = React.forwardRef(function Link(props, ref) {
const {
classes,
className,
color = 'primary',
component = 'a',
onBlur,
onFocus,
TypographyClasses,
underline = 'hover',
variant = 'inherit',
...other
} = props;
const { isFocusVisible, onBlurVisible, ref: focusVisibleRef } = useIsFocusVisible();
const [focusVisible, setFocusVisible] = React.useState(false);
const handlerRef = useForkRef(ref, focusVisibleRef);
const handleBlur = (event) => {
if (focusVisible) {
onBlurVisible();
setFocusVisible(false);
}
if (onBlur) {
onBlur(event);
}
};
const handleFocus = (event) => {
if (isFocusVisible(event)) {
setFocusVisible(true);
}
if (onFocus) {
onFocus(event);
}
};
return (
<Typography
className={clsx(
classes.root,
{
[classes.button]: component === 'button',
[classes.focusVisible]: focusVisible,
},
classes[`underline${capitalize(underline)}`],
className,
)}
classes={TypographyClasses}
color={color}
component={component}
onBlur={handleBlur}
onFocus={handleFocus}
ref={handlerRef}
variant={variant}
{...other}
/>
);
});
Link.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* The content of the link.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
* See [CSS API](#css) below for more details.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the link.
*/
color: PropTypes.oneOf([
'error',
'inherit',
'initial',
'primary',
'secondary',
'textPrimary',
'textSecondary',
]),
/**
* 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,
/**
* `classes` prop applied to the [`Typography`](/api/typography/) element.
*/
TypographyClasses: PropTypes.object,
/**
* Controls when the link should have an underline.
*/
underline: PropTypes.oneOf(['always', 'hover', 'none']),
/**
* Applies the theme typography styles.
*/
variant: PropTypes.oneOf([
'body1',
'body2',
'button',
'caption',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'inherit',
'overline',
'subtitle1',
'subtitle2',
]),
};
export default withStyles(styles, { name: 'MuiLink' })(Link);