1
- import React , { useCallback } from 'react' ;
1
+ import React from 'react' ;
2
2
import styled from 'styled-components' ;
3
3
4
4
import { Colors , Radius } from '../../foundations' ;
5
- import { useHistory } from '../../history' ;
6
- import { RoutePath } from '../../routes' ;
7
- import { buttonReset } from '../../styles' ;
8
5
import { Text , TextProps } from './Text' ;
9
6
10
- export interface LinkProps extends Omit < TextProps < 'button' > , 'color' > {
11
- to : RoutePath ;
12
- color ?: Colors ;
13
- }
7
+ export type LinkProps < T extends React . ElementType = 'a' > = TextProps < T > & {
8
+ onClick ?: ( e : React . MouseEvent < HTMLAnchorElement > ) => void ;
9
+ } ;
14
10
15
11
const StyledText = styled ( Text ) < {
16
12
$hoverColor : Colors | undefined ;
17
13
} > ( ( props ) => ( {
18
- ...buttonReset ,
19
14
background : 'transparent' ,
15
+ cursor : 'default' ,
16
+ textDecoration : 'none' ,
17
+ display : 'inline-flex' ,
18
+ alignItems : 'center' ,
20
19
21
- '&:hover' : {
20
+ '&& :hover' : {
22
21
textDecorationLine : 'underline' ,
23
22
textUnderlineOffset : '2px' ,
24
23
color : props . $hoverColor ,
25
24
} ,
26
- '&:focus-visible' : {
25
+ '&& :focus-visible' : {
27
26
borderRadius : Radius . radius4 ,
28
27
outline : `2px solid ${ Colors . white } ` ,
29
28
outlineOffset : '2px' ,
@@ -39,25 +38,20 @@ const getHoverColor = (color: Colors | undefined) => {
39
38
}
40
39
} ;
41
40
42
- export const Link = ( { to, children, color, onClick, ...props } : LinkProps ) => {
43
- const history = useHistory ( ) ;
44
- const navigate = useCallback (
45
- ( e : React . MouseEvent < HTMLButtonElement > ) => {
46
- if ( onClick ) {
47
- onClick ( e ) ;
48
- }
49
- return history . push ( to ) ;
50
- } ,
51
- [ history , to , onClick ] ,
52
- ) ;
41
+ export const Link = < T extends React . ElementType = 'a' > ( {
42
+ as : forwardedAs ,
43
+ color,
44
+ ...props
45
+ } : LinkProps < T > ) => {
46
+ // If `as` is provided we need to pass it as `forwardedAs` for it to
47
+ // be correctly passed to the `Text` component.
48
+ const componentProps = forwardedAs ? { ...props , forwardedAs } : props ;
53
49
return (
54
50
< StyledText
55
- onClick = { navigate }
56
- as = { 'button' }
51
+ forwardedAs = "a"
57
52
color = { color }
58
53
$hoverColor = { getHoverColor ( color ) }
59
- { ...props } >
60
- { children }
61
- </ StyledText >
54
+ { ...componentProps }
55
+ />
62
56
) ;
63
57
} ;
0 commit comments