1
- import { forwardRef , useMemo } from 'react' ;
1
+ import { FunctionComponent , useMemo } from 'react' ;
2
2
import { Link as RouterLink , useLocation } from 'react-router-dom' ;
3
3
import MuiLink , { LinkProps as MuiLinkProps } from '@mui/material/Link' ;
4
4
import { APP_LINK_COLOR , APP_LINK_UNDERLINE } from '@/components/config' ;
@@ -19,58 +19,53 @@ export interface AppLinkProps extends MuiLinkProps {
19
19
* @param {string } [href] - external link URI
20
20
* @param {boolean } [openInNewTab] - link will be opened in new tab when true
21
21
*/
22
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
- const AppLink = forwardRef < any , AppLinkProps > (
24
- (
25
- {
26
- activeClassName = 'active' , // This class is applied to the Link component when the router.pathname matches the href/to prop
27
- children,
28
- color = APP_LINK_COLOR ,
29
- className : customClassName ,
30
- underline = APP_LINK_UNDERLINE ,
31
- to,
32
- href,
33
- openInNewTab, // Note: all external links are open in new Tab by default
34
- ...restOfProps
35
- } ,
36
- ref
37
- ) => {
38
- const location = useLocation ( ) ;
39
- const currentPath = location . pathname ;
40
- const destination = to || href || '' ; // Note: Don't use ?? here!!!
22
+ const AppLink : FunctionComponent < AppLinkProps > = ( {
23
+ activeClassName = 'active' , // This class is applied to the Link component when the router.pathname matches the href/to prop
24
+ children,
25
+ color = APP_LINK_COLOR ,
26
+ className : customClassName ,
27
+ underline = APP_LINK_UNDERLINE ,
28
+ to,
29
+ href,
30
+ openInNewTab, // Note: all external links are open in new Tab by default
31
+ ref,
32
+ ...restOfProps
33
+ } ) => {
34
+ const location = useLocation ( ) ;
35
+ const currentPath = location . pathname ;
36
+ const destination = to || href || '' ; // Note: Don't use ?? here!!!
41
37
42
- const className = useMemo (
43
- ( ) => [ customClassName , destination == currentPath && activeClassName ] . filter ( Boolean ) . join ( ' ' ) ,
44
- [ customClassName , activeClassName , destination , currentPath ]
45
- ) ;
38
+ const className = useMemo (
39
+ ( ) => [ customClassName , destination == currentPath && activeClassName ] . filter ( Boolean ) . join ( ' ' ) ,
40
+ [ customClassName , activeClassName , destination , currentPath ]
41
+ ) ;
46
42
47
- const isExternal = useMemo (
48
- ( ) =>
49
- destination . startsWith ( 'http' ) ||
50
- destination . startsWith ( '//' ) ||
51
- destination . startsWith ( 'mailto:' ) ||
52
- destination . startsWith ( 'tel:' ) ,
53
- [ destination ]
54
- ) ;
43
+ const isExternal = useMemo (
44
+ ( ) =>
45
+ destination . startsWith ( 'http' ) ||
46
+ destination . startsWith ( '//' ) ||
47
+ destination . startsWith ( 'mailto:' ) ||
48
+ destination . startsWith ( 'tel:' ) ,
49
+ [ destination ]
50
+ ) ;
55
51
56
- const propsToRender = {
57
- className,
58
- color,
59
- underline, // 'hover' | 'always' | 'none'
60
- ...( ( openInNewTab || ( isExternal && openInNewTab !== false ) ) && EXTERNAL_LINK_PROPS ) , // Open external links in new tab
61
- ...restOfProps ,
62
- } ;
52
+ const propsToRender = {
53
+ className,
54
+ color,
55
+ underline, // 'hover' | 'always' | 'none'
56
+ ...( ( openInNewTab || ( isExternal && openInNewTab !== false ) ) && EXTERNAL_LINK_PROPS ) , // Open external links in new tab
57
+ ...restOfProps ,
58
+ } ;
63
59
64
- return isExternal ? (
65
- < MuiLink ref = { ref } href = { destination } { ...propsToRender } >
66
- { children }
67
- </ MuiLink >
68
- ) : (
69
- < MuiLink ref = { ref } component = { RouterLink } to = { destination } { ...propsToRender } >
70
- { children }
71
- </ MuiLink >
72
- ) ;
73
- }
74
- ) ;
60
+ return isExternal ? (
61
+ < MuiLink ref = { ref } href = { destination } { ...propsToRender } >
62
+ { children }
63
+ </ MuiLink >
64
+ ) : (
65
+ < MuiLink ref = { ref } component = { RouterLink } to = { destination } { ...propsToRender } >
66
+ { children }
67
+ </ MuiLink >
68
+ ) ;
69
+ } ;
75
70
76
71
export default AppLink ;
0 commit comments