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

154 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-12-19 15:59:14 -08:00
import type { ReactNode } from 'react';
2023-01-03 01:34:00 -08:00
import { createPolymorphicComponent, Flex, FlexProps, Group } from '@mantine/core';
import { RiPlayFill } from 'react-icons/ri';
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';
2023-01-03 01:34:00 -08:00
import { Button } from '/@/renderer/components';
import { textEllipsis } from '/@/renderer/styles';
2022-12-19 15:59:14 -08:00
interface ListItemProps extends FlexProps {
2022-12-19 15:59:14 -08:00
children: ReactNode;
disabled?: boolean;
to?: string;
}
const StyledItem = styled(Flex)`
2022-12-19 15:59:14 -08:00
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 _ItemLink = styled(StyledItem)<LinkProps & { disabled?: boolean }>`
2022-12-19 15:59:14 -08:00
opacity: ${(props) => props.disabled && 0.6};
pointer-events: ${(props) => props.disabled && 'none'};
&:focus-visible {
border: 1px solid var(--primary-color);
}
${ItemStyle}
`;
const ItemLink = createPolymorphicComponent<'a', ListItemProps>(_ItemLink);
export const SidebarItem = ({ to, children, ...props }: ListItemProps) => {
2022-12-19 15:59:14 -08:00
if (to) {
return (
<ItemLink
component={Link}
2022-12-19 15:59:14 -08:00
to={to}
{...props}
2022-12-19 15:59:14 -08:00
>
{children}
</ItemLink>
);
}
return (
<StyledItem
tabIndex={0}
{...props}
2022-12-19 15:59:14 -08:00
>
{children}
</StyledItem>
);
};
SidebarItem.Link = ItemLink;
SidebarItem.defaultProps = {
disabled: false,
to: undefined,
};
2023-01-03 01:34:00 -08:00
const _PlaylistItemLink = styled(StyledItem)<LinkProps & { disabled?: boolean }>`
display: block;
width: 80%;
padding: 0.3rem 0;
overflow: hidden;
color: var(--sidebar-fg);
opacity: ${(props) => props.disabled && 0.6};
transition: color 0.2s ease-in-out;
pointer-events: ${(props) => props.disabled && 'none'};
${textEllipsis}
&:hover {
color: var(--sidebar-fg-hover);
}
&:focus-visible {
border: 1px solid var(--primary-color);
}
`;
const PlaylistItemLink = createPolymorphicComponent<'a', ListItemProps>(_PlaylistItemLink);
export const PlaylistSidebarItem = ({
handlePlay,
to,
children,
...props
}: ListItemProps & { handlePlay: () => void }) => {
if (to) {
return (
<Group
noWrap
position="apart"
sx={{
'&:hover': {
button: {
display: 'block',
},
},
}}
>
<PlaylistItemLink
component={Link}
to={to}
{...props}
>
{children}
</PlaylistItemLink>
<Flex
align="center"
justify="flex-end"
>
<Button
compact
sx={{ display: 'none' }}
variant="filled"
onClick={handlePlay}
>
<RiPlayFill />
</Button>
</Flex>
</Group>
);
}
return (
<StyledItem
tabIndex={0}
{...props}
>
{children}
</StyledItem>
);
};