|
1 | | -import { |
2 | | - Link as RouterLink, |
3 | | - type LinkProps as ReactRouterLinkProps, |
4 | | -} from 'react-router-dom'; |
| 1 | +import {type LinkProps as ReactRouterLinkProps} from 'react-router-dom'; |
5 | 2 | import isPropValid from '@emotion/is-prop-valid'; |
6 | 3 | import {css, type Theme} from '@emotion/react'; |
7 | 4 | import styled from '@emotion/styled'; |
8 | 5 | import type {LocationDescriptor} from 'history'; |
9 | 6 |
|
10 | | -import {locationDescriptorToTo} from 'sentry/utils/reactRouter6Compat/location'; |
11 | | -import normalizeUrl from 'sentry/utils/url/normalizeUrl'; |
12 | | -import {useLocation} from 'sentry/utils/useLocation'; |
| 7 | +import {useLinkBehavior} from './linkBehaviorContext'; |
13 | 8 |
|
14 | 9 | export interface LinkProps |
15 | 10 | extends React.RefAttributes<HTMLAnchorElement>, |
@@ -61,26 +56,19 @@ const getLinkStyles = ({ |
61 | 56 | `; |
62 | 57 |
|
63 | 58 | const Anchor = styled('a', { |
64 | | - shouldForwardProp: prop => |
65 | | - typeof prop === 'string' && isPropValid(prop) && prop !== 'disabled', |
| 59 | + shouldForwardProp: prop => isPropValid(prop) && prop !== 'disabled', |
66 | 60 | })<{disabled?: LinkProps['disabled']}>` |
67 | 61 | ${getLinkStyles} |
68 | 62 | `; |
69 | 63 |
|
70 | | -/** |
71 | | - * A context-aware version of Link (from react-router) that falls |
72 | | - * back to <a> if there is no router present |
73 | | - */ |
74 | | -export const Link = styled(({disabled, to, ...props}: LinkProps) => { |
75 | | - const location = useLocation(); |
| 64 | +export const Link = styled((props: LinkProps) => { |
| 65 | + const {Component, behavior} = useLinkBehavior(props); |
76 | 66 |
|
77 | | - if (disabled || !location) { |
| 67 | + if (props.disabled || Component === 'a') { |
78 | 68 | return <Anchor {...props} />; |
79 | 69 | } |
80 | 70 |
|
81 | | - return ( |
82 | | - <RouterLink to={locationDescriptorToTo(normalizeUrl(to, location))} {...props} /> |
83 | | - ); |
| 71 | + return <Component {...behavior()} />; |
84 | 72 | })` |
85 | 73 | ${getLinkStyles} |
86 | 74 | `; |
|
0 commit comments