UNPKG

@material-ui/core

Version:

React components that implement Google's Material Design.

186 lines (163 loc) 5.25 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; import React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import { capitalize } from '../utils/helpers'; import withStyles from '../styles/withStyles'; import { useIsFocusVisible } from '../utils/focusVisible'; import { useForkRef } from '../utils/reactHelpers'; import Typography from '../Typography'; export var 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', // Remove grey highlight WebkitTapHighlightColor: 'transparent', backgroundColor: 'transparent', // Reset default value // We disable the focus ring for mouse, touch and keyboard users. outline: 'none', 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: {} }; var Link = React.forwardRef(function Link(props, ref) { var classes = props.classes, className = props.className, _props$color = props.color, color = _props$color === void 0 ? 'primary' : _props$color, _props$component = props.component, component = _props$component === void 0 ? 'a' : _props$component, onBlur = props.onBlur, onFocus = props.onFocus, TypographyClasses = props.TypographyClasses, _props$underline = props.underline, underline = _props$underline === void 0 ? 'hover' : _props$underline, _props$variant = props.variant, variant = _props$variant === void 0 ? 'inherit' : _props$variant, other = _objectWithoutProperties(props, ["classes", "className", "color", "component", "onBlur", "onFocus", "TypographyClasses", "underline", "variant"]); var _useIsFocusVisible = useIsFocusVisible(), isFocusVisible = _useIsFocusVisible.isFocusVisible, onBlurVisible = _useIsFocusVisible.onBlurVisible, focusVisibleRef = _useIsFocusVisible.ref; var _React$useState = React.useState(false), _React$useState2 = _slicedToArray(_React$useState, 2), focusVisible = _React$useState2[0], setFocusVisible = _React$useState2[1]; var handlerRef = useForkRef(ref, focusVisibleRef); var handleBlur = function handleBlur(event) { if (focusVisible) { onBlurVisible(); setFocusVisible(false); } if (onBlur) { onBlur(event); } }; var handleFocus = function handleFocus(event) { if (isFocusVisible(event)) { setFocusVisible(true); } if (onFocus) { onFocus(event); } }; return React.createElement(Typography, _extends({ className: clsx(classes.root, classes["underline".concat(capitalize(underline))], className, focusVisible && classes.focusVisible, { button: classes.button }[component]), classes: TypographyClasses, color: color, component: component, onBlur: handleBlur, onFocus: handleFocus, ref: handlerRef, variant: variant }, other)); }); process.env.NODE_ENV !== "production" ? Link.propTypes = { /** * The content of the link. */ children: PropTypes.node.isRequired, /** * Override or extend the styles applied to the component. * See [CSS API](#css) below for more details. */ classes: PropTypes.object.isRequired, /** * @ignore */ className: PropTypes.string, /** * The color of the link. */ color: PropTypes.oneOf(['default', 'error', 'inherit', 'primary', 'secondary', 'textPrimary', 'textSecondary']), /** * The component used for the root node. * Either a string to use a DOM element or a component. */ component: PropTypes.elementType, /** * @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(['none', 'hover', 'always']), /** * Applies the theme typography styles. */ variant: PropTypes.string } : void 0; export default withStyles(styles, { name: 'MuiLink' })(Link);