impress-2020/src/ItemList.js

174 lines
3.8 KiB
JavaScript
Raw Normal View History

import React from "react";
2020-04-25 15:25:51 -07:00
import { css } from "emotion";
import {
Box,
Flex,
IconButton,
Image,
Skeleton,
Tooltip,
2020-04-25 15:25:51 -07:00
useTheme,
} from "@chakra-ui/core";
import "./ItemList.css";
2020-04-25 15:25:51 -07:00
export function ItemListContainer({ children }) {
return <Flex direction="column">{children}</Flex>;
}
export function ItemListSkeleton({ count }) {
2020-04-22 15:24:38 -07:00
return (
2020-04-25 03:05:26 -07:00
<Flex direction="column">
2020-04-24 21:23:03 -07:00
{Array.from({ length: count }).map((_, i) => (
2020-04-25 19:02:30 -07:00
<ItemSkeleton key={i} />
2020-04-24 21:23:03 -07:00
))}
2020-04-25 03:05:26 -07:00
</Flex>
2020-04-22 15:24:38 -07:00
);
}
export function Item({ item, itemNameId, outfitState, dispatchToOutfit }) {
2020-04-25 15:25:51 -07:00
const { allItemIds } = outfitState;
const isInOutfit = allItemIds.includes(item.id);
2020-04-25 15:25:51 -07:00
const theme = useTheme();
return (
2020-04-25 19:02:30 -07:00
<ItemContainer>
2020-04-25 15:25:51 -07:00
<ItemThumbnail src={item.thumbnailUrl} />
<Box width="3" />
<ItemName id={itemNameId}>{item.name}</ItemName>
<Box flexGrow="1" />
{isInOutfit && (
<Tooltip label="Remove" placement="top">
<IconButton
icon="delete"
aria-label="Remove from outfit"
variant="ghost"
color="gray.400"
onClick={(e) => {
dispatchToOutfit({ type: "removeItem", itemId: item.id });
2020-04-25 20:21:51 -07:00
e.preventDefault();
}}
opacity="0"
transitionProperty="opacity color"
transitionDuration="0.2s"
className={css`
&:hover,
&:focus,
input:focus + .item-container & {
opacity: 1;
color: ${theme.colors.gray["800"]};
backgroundcolor: ${theme.colors.gray["200"]};
}
`}
/>
</Tooltip>
)}
2020-04-25 19:02:30 -07:00
</ItemContainer>
);
}
2020-04-22 15:24:38 -07:00
function ItemSkeleton() {
return (
2020-04-25 19:02:30 -07:00
<ItemContainer>
<Box d="flex" alignItems="center">
<Skeleton width="50px" height="50px" />
<Box width="3" />
<Skeleton height="1.5rem" width="12rem" />
</Box>
</ItemContainer>
);
}
function ItemContainer({ children }) {
const theme = useTheme();
return (
<Box
p="1"
my="1"
2020-04-25 19:02:30 -07:00
rounded="lg"
d="flex"
alignItems="center"
cursor="pointer"
border="1px"
borderColor="transparent"
className={
"item-container " +
css`
&:hover,
input:focus + & {
background-color: ${theme.colors.gray["100"]};
}
input:active + & {
border-color: ${theme.colors.green["400"]};
}
input:checked:focus + & {
border-color: ${theme.colors.green["800"]};
}
`
}
>
{children}
2020-04-22 15:24:38 -07:00
</Box>
);
}
2020-04-25 15:25:51 -07:00
function ItemThumbnail({ src }) {
const theme = useTheme();
return (
2020-04-25 15:25:51 -07:00
<Box
rounded="lg"
boxShadow="md"
border="1px"
2020-04-25 15:25:51 -07:00
borderColor="green.700"
width="50px"
height="50px"
overflow="hidden"
transition="all 0.15s"
transformOrigin="center"
2020-04-25 15:25:51 -07:00
transform="scale(0.8)"
className={css`
.item-container:hover & {
opacity: 0.9;
transform: scale(0.9);
bordercolor: ${theme.colors.green["600"]};
}
2020-04-25 15:25:51 -07:00
input:checked + .item-container & {
opacity: 1;
transform: none;
}
`}
>
<Image src={src} />
2020-04-25 15:25:51 -07:00
</Box>
);
}
function ItemName({ children, ...props }) {
2020-04-25 15:25:51 -07:00
const theme = useTheme();
return (
2020-04-25 15:25:51 -07:00
<Box
fontSize="md"
color="green.800"
transition="all 0.15s"
2020-04-25 15:25:51 -07:00
className={css`
.item-container:hover & {
opacity: 0.9;
font-weight: ${theme.fontWeights.medium};
}
2020-04-25 15:25:51 -07:00
input:checked + .item-container & {
opacity: 1;
font-weight: ${theme.fontWeights.bold};
}
`}
{...props}
>
{children}
2020-04-25 15:25:51 -07:00
</Box>
);
}