1- // @ts -expect-error
21import { Link } from '@vkontakte/vkui' ;
32import { useHref } from '../hooks/useHref' ;
43import { RelativeRoutingType , To } from '@remix-run/router' ;
@@ -11,33 +10,19 @@ import {
1110} from 'react' ;
1211import { useLinkClickHandler } from '../hooks/useLinkClickHandler' ;
1312
14- export interface LinkProps
15- extends Omit < AnchorHTMLAttributes < HTMLAnchorElement > , 'href' > {
13+ export interface LinkProps extends Omit < AnchorHTMLAttributes < HTMLAnchorElement > , 'href' > {
1614 reloadDocument ?: boolean ;
1715 replace ?: boolean ;
1816 relative ?: RelativeRoutingType ;
1917 to : To ;
2018}
2119
22- export interface RouterLinkProps
23- extends Omit < LinkProps , 'className' | 'style' | 'children' > {
24- children ?:
25- | ReactNode
26- | ( ( props : { isActive : boolean ; isPending : boolean } ) => ReactNode ) ;
20+ export interface RouterLinkProps extends Omit < LinkProps , 'className' | 'style' | 'children' > {
21+ children ?: ReactNode ;
2722 caseSensitive ?: boolean ;
28- className ?:
29- | string
30- | ( ( props : {
31- isActive : boolean ;
32- isPending : boolean ;
33- } ) => string | undefined ) ;
23+ className ?: string ;
3424 end ?: boolean ;
35- style ?:
36- | CSSProperties
37- | ( ( props : {
38- isActive : boolean ;
39- isPending : boolean ;
40- } ) => CSSProperties | undefined ) ;
25+ style ?: CSSProperties ;
4126}
4227
4328const ABSOLUTE_URL_REGEX = / ^ (?: [ a - z ] [ a - z 0 - 9 + . - ] * : | \/ \/ ) / i;
@@ -47,69 +32,55 @@ const isBrowser =
4732 typeof window . document !== 'undefined' &&
4833 typeof window . document . createElement !== 'undefined' ;
4934
50- export const RouterLink = forwardRef < HTMLAnchorElement , RouterLinkProps > (
51- function (
52- {
53- to,
54- relative,
55- replace,
56- target,
57- reloadDocument,
58- onClick,
59- ...rest
60- } : RouterLinkProps ,
61- ref ,
62- ) {
63- // Rendered into <a href> for absolute URLs
64- let absoluteHref ;
65- let isExternal = false ;
35+ export const RouterLink = forwardRef < HTMLAnchorElement , RouterLinkProps > ( function (
36+ { to, relative, replace, target, reloadDocument, onClick, ...rest } : RouterLinkProps ,
37+ ref ,
38+ ) {
39+ // Rendered into <a href> for absolute URLs
40+ let absoluteHref ;
41+ let isExternal = false ;
6642
67- if ( typeof to === 'string' && ABSOLUTE_URL_REGEX . test ( to ) ) {
68- // Render the absolute href server- and client-side
69- absoluteHref = to ;
43+ if ( typeof to === 'string' && ABSOLUTE_URL_REGEX . test ( to ) ) {
44+ // Render the absolute href server- and client-side
45+ absoluteHref = to ;
7046
71- // Only check for external origins client-side
72- if ( isBrowser ) {
73- const currentUrl = new URL ( window . location . href ) ;
74- const targetUrl = to . startsWith ( '//' )
75- ? new URL ( currentUrl . protocol + to )
76- : new URL ( to ) ;
77- const path = targetUrl . pathname ;
47+ // Only check for external origins client-side
48+ if ( isBrowser ) {
49+ const currentUrl = new URL ( window . location . href ) ;
50+ const targetUrl = to . startsWith ( '//' ) ? new URL ( currentUrl . protocol + to ) : new URL ( to ) ;
51+ const path = targetUrl . pathname ;
7852
79- if ( targetUrl . origin === currentUrl . origin ) {
80- // Strip the protocol/origin/basename for same-origin absolute URLs
81- to = path + targetUrl . search + targetUrl . hash ;
82- } else {
83- isExternal = true ;
84- }
53+ if ( targetUrl . origin === currentUrl . origin ) {
54+ // Strip the protocol/origin/basename for same-origin absolute URLs
55+ to = path + targetUrl . search + targetUrl . hash ;
56+ } else {
57+ isExternal = true ;
8558 }
8659 }
87-
88- const href = useHref ( to , { relative } ) ;
60+ }
8961
90- const internalOnClick = useLinkClickHandler ( to , {
91- replace,
92- target,
93- relative,
94- } ) ;
62+ const href = useHref ( to , { relative } ) ;
9563
96- function handleClick (
97- event : ReactMouseEvent < HTMLAnchorElement , MouseEvent >
98- ) {
99- if ( onClick ) onClick ( event ) ;
100- if ( ! event . defaultPrevented ) {
101- internalOnClick ( event ) ;
102- }
103- }
64+ const internalOnClick = useLinkClickHandler ( to , {
65+ replace,
66+ target,
67+ relative,
68+ } ) ;
10469
105- return (
106- < Link
107- { ...rest }
108- href = { absoluteHref || href }
109- onClick = { isExternal || reloadDocument ? onClick : handleClick }
110- ref = { ref }
111- target = { target }
112- > </ Link >
113- ) ;
70+ function handleClick ( event : ReactMouseEvent < HTMLAnchorElement > ) {
71+ if ( onClick ) onClick ( event ) ;
72+ if ( ! event . defaultPrevented ) {
73+ internalOnClick ( event ) ;
74+ }
11475 }
115- ) ;
76+
77+ return (
78+ < Link
79+ { ...rest }
80+ href = { absoluteHref || href }
81+ onClick = { isExternal || reloadDocument ? onClick : handleClick }
82+ getRootRef = { ref }
83+ target = { target }
84+ > </ Link >
85+ ) ;
86+ } ) ;
0 commit comments