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

79 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-12-19 15:59:14 -08:00
import type { ReactNode } from 'react';
import type { LinkProps } from 'react-router-dom';
import { Link } from 'react-router-dom';
import styled, { css } from 'styled-components';
interface ListItemProps {
children: ReactNode;
disabled?: boolean;
to?: string;
}
const StyledItem = styled.div`
display: flex;
width: 100%;
font-family: var(--content-font-family);
&:focus-visible {
border: 1px solid var(--primary-color);
}
`;
const ItemStyle = css`
display: flex;
width: 100%;
padding: 0.5rem 1rem;
2022-12-28 01:58:25 -08:00
color: var(--sidebar-fg);
2022-12-19 15:59:14 -08:00
border: 1px transparent solid;
transition: color 0.2s ease-in-out;
&:hover {
2022-12-28 01:58:25 -08:00
color: var(--sidebar-fg-hover);
2022-12-19 15:59:14 -08:00
}
`;
const Box = styled.div`
${ItemStyle}
`;
const ItemLink = styled(Link)<LinkProps & { disabled?: boolean }>`
opacity: ${(props) => props.disabled && 0.6};
pointer-events: ${(props) => props.disabled && 'none'};
&:focus-visible {
border: 1px solid var(--primary-color);
}
${ItemStyle}
`;
export const SidebarItem = ({ to, children, ...rest }: ListItemProps) => {
if (to) {
return (
<ItemLink
to={to}
{...rest}
>
{children}
</ItemLink>
);
}
return (
<StyledItem
tabIndex={0}
{...rest}
>
{children}
</StyledItem>
);
};
SidebarItem.Box = Box;
SidebarItem.Link = ItemLink;
SidebarItem.defaultProps = {
disabled: false,
to: undefined,
};