Skip to content

Commit

Permalink
Add common commponent for AnchorLink and ButtonLink
Browse files Browse the repository at this point in the history
  • Loading branch information
th3c0d3br34ker committed Sep 15, 2023
1 parent 05f2e2a commit 3071b33
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions packages/desktop-client/src/components/common/Link.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React, { type ReactNode, type ComponentProps } from 'react';
import { NavLink, useMatch, useNavigate } from 'react-router-dom';

import { css } from 'glamor';

import { type CSSProperties, styles } from '../../style';

import Button from './Button';

type ButtonLinkProps = ComponentProps<typeof Button> & {
to: string;
activeStyle?: CSSProperties;
};

type AnchorLinkProps = {
to: string;
style?: CSSProperties;
activeStyle?: CSSProperties;
children?: ReactNode;
};

const ButtonLink = ({
to,
style,
activeStyle,
onClick,
...props
}: ButtonLinkProps) => {
const navigate = useNavigate();
const match = useMatch({ path: to });

const handleClick = e => {
onClick?.(e);
navigate(to);
};

return (
<Button
style={{
...style,
...(match ? activeStyle : {}),
}}
activeStyle={activeStyle}
{...props}
onClick={handleClick}
/>
);
};

const AnchorLink = ({ to, style, activeStyle, children }: AnchorLinkProps) => {
const match = useMatch({ path: to });

return (
<NavLink
to={to}
className={`${css([
styles.smallText,
style,
match ? activeStyle : null,
])}`}
>
{children}
</NavLink>
);
};

type LinkProps = {
to: string;
linkType?: 'button' | 'anchor';
style?: CSSProperties;
activeStyle?: CSSProperties;
children?: ReactNode;
};

export default function Link({ linkType = 'anchor', ...props }: LinkProps) {

Check warning on line 75 in packages/desktop-client/src/components/common/Link.tsx

View workflow job for this annotation

GitHub Actions / lint

exported declaration 'default' not used within other modules
switch (linkType) {
case 'anchor':
return <AnchorLink {...props} />;

case 'button':
return <ButtonLink {...props} />;

default:
return null;
}
}

0 comments on commit 3071b33

Please sign in to comment.