feishin/src/renderer/features/sidebar/components/sidebar-item.tsx

77 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-12-19 15:59:14 -08:00
import type { ReactNode } from 'react';
import { createPolymorphicComponent, Flex, FlexProps } from '@mantine/core';
2022-12-19 15:59:14 -08:00
import type { LinkProps } from 'react-router-dom';
import { Link } from 'react-router-dom';
import styled, { css } from 'styled-components';
interface ListItemProps extends FlexProps {
2023-07-01 19:10:05 -07:00
children: ReactNode;
disabled?: boolean;
to?: string;
2022-12-19 15:59:14 -08:00
}
const StyledItem = styled(Flex)`
2023-07-01 19:10:05 -07:00
width: 100%;
font-family: var(--content-font-family);
2023-09-15 20:42:38 -07:00
font-weight: 600;
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
&:focus-visible {
border: 1px solid var(--primary-color);
}
2022-12-19 15:59:14 -08:00
`;
const ItemStyle = css`
2023-07-01 19:10:05 -07:00
display: flex;
width: 100%;
padding: 0.5rem 1rem;
color: var(--sidebar-fg);
border: 1px transparent solid;
transition: color 0.2s ease-in-out;
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
&:hover {
color: var(--sidebar-fg-hover);
}
2022-12-19 15:59:14 -08:00
`;
const _ItemLink = styled(StyledItem)<LinkProps & { disabled?: boolean }>`
2023-07-01 19:10:05 -07:00
pointer-events: ${(props) => props.disabled && 'none'};
2023-09-15 20:42:38 -07:00
opacity: ${(props) => props.disabled && 0.6};
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
&:focus-visible {
border: 1px solid var(--primary-color);
}
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
${ItemStyle}
2022-12-19 15:59:14 -08:00
`;
const ItemLink = createPolymorphicComponent<'a', ListItemProps>(_ItemLink);
export const SidebarItem = ({ to, children, ...props }: ListItemProps) => {
2023-07-01 19:10:05 -07:00
if (to) {
return (
<ItemLink
component={Link}
to={to}
{...props}
>
{children}
</ItemLink>
);
}
2022-12-19 15:59:14 -08:00
return (
2023-07-01 19:10:05 -07:00
<StyledItem
tabIndex={0}
{...props}
>
{children}
</StyledItem>
2022-12-19 15:59:14 -08:00
);
};
SidebarItem.Link = ItemLink;
SidebarItem.defaultProps = {
2023-07-01 19:10:05 -07:00
disabled: false,
to: undefined,
2022-12-19 15:59:14 -08:00
};