import classNames from 'classnames'; import React, { ComponentPropsWithoutRef, ElementType, SyntheticEvent, useCallback, } from 'react'; import { Link as RouterLink } from 'react-router-dom'; import styles from './Link.css'; export type LinkProps = ComponentPropsWithoutRef & { component?: C; to?: string; target?: string; isDisabled?: LinkProps['disabled']; noRouter?: boolean; onPress?(event: SyntheticEvent): void; }; export default function Link({ className, component, to, target, type, isDisabled, noRouter, onPress, ...otherProps }: LinkProps) { const Component = component || 'button'; const onClick = useCallback( (event: SyntheticEvent) => { if (isDisabled) { return; } onPress?.(event); }, [isDisabled, onPress] ); const linkClass = classNames( className, styles.link, to && styles.to, isDisabled && 'isDisabled' ); if (to) { const toLink = /\w+?:\/\//.test(to); if (toLink || noRouter) { return ( ); } return ( ); } return ( ); }