1+ import React from 'react'
2+ import PropTypes from 'prop-types'
3+ import styled from 'styled-components'
4+ import { system } from 'styled-system'
5+ import { COMMON , TYPOGRAPHY , get , SystemCommonProps , SystemTypographyProps } from './constants'
6+ import sx , { SxProp } from './sx'
7+ import theme from './theme'
8+ import { ComponentProps } from './utils/types' ;
9+
10+ type LinkBaseProps = {
11+ as ?: React . ReactNode ;
12+ href ?: string ;
13+ hoverColor ?: string ;
14+ muted ?: boolean ;
15+ underline ?: boolean ;
16+ } & SystemCommonProps & SxProp & SystemTypographyProps
17+
18+ const buttonStyles = {
19+ display : 'inline-block' ,
20+ padding : '0' ,
21+ fontSize : 'inherit' ,
22+ whiteSpace : 'nowrap' ,
23+ cursor : 'pointer' ,
24+ userSelect : 'none' ,
25+ backgroundColor : 'transparent' ,
26+ border : '0' ,
27+ appearance : 'none'
28+ }
29+
30+ const hoverColor = system ( {
31+ hoverColor : {
32+ property : 'color' ,
33+ scale : 'colors'
34+ }
35+ } )
36+
37+ const linkColor = ( { color, muted, ...props } : LinkBaseProps ) => ( {
38+ color : color ? color : muted ? 'gray.6' : 'blue.5'
39+ } )
40+
41+ const textDecoration = ( { underline, ...props } : LinkBaseProps ) => underline ? 'underline' : 'none'
42+ const hoverCss = ( { hoverColor, muted, ...props } : LinkBaseProps ) => hoverColor ? hoverColor : muted ? `color: ${ get ( 'colors.blue.5' ) ( theme ) } ` : ''
43+ const buttonCss = ( { as, ...props } : LinkBaseProps ) => as === 'button' ? buttonStyles : ''
44+
45+ const Link = styled . a . attrs ( linkColor ) `
46+ text-decoration: ${ textDecoration } ;
47+ &:hover {
48+ text-decoration: ${ textDecoration } ;
49+ ${ hoverCss } ;
50+ }
51+ ${ buttonCss } ;
52+ ${ TYPOGRAPHY } ${ COMMON } ;
53+ ${ sx } ;
54+ `
55+
56+ Link . defaultProps = {
57+ theme
58+ }
59+
60+ Link . propTypes = {
61+ as : PropTypes . elementType ,
62+ hoverColor : PropTypes . string ,
63+ href : PropTypes . string ,
64+ muted : PropTypes . bool ,
65+ theme : PropTypes . object ,
66+ underline : PropTypes . bool ,
67+ ...TYPOGRAPHY . propTypes ,
68+ ...COMMON . propTypes ,
69+ ...sx . propTypes
70+ }
71+
72+ export type LinkProps = ComponentProps < typeof Link >
73+ export default Link
0 commit comments