Files
grungnet/src/components/tile/TilesOverlay.js

191 lines
5.6 KiB
JavaScript
Raw Normal View History

2021-05-24 17:11:46 +10:00
import React, { useState } from "react";
2021-06-05 18:05:35 +10:00
import { Box, Close, Grid, useThemeUI, IconButton, Text, Flex } from "theme-ui";
2021-05-24 17:11:46 +10:00
import { useSpring, animated, config } from "react-spring";
import ReactResizeDetector from "react-resize-detector";
import SimpleBar from "simplebar-react";
import { useGroup } from "../../contexts/GroupContext";
import { UNGROUP_ID, ADD_TO_MAP_ID } from "../../contexts/TileDragContext";
import useResponsiveLayout from "../../hooks/useResponsiveLayout";
2021-06-05 18:05:35 +10:00
import ChangeNicknameIcon from "../../icons/ChangeNicknameIcon";
import GroupNameModal from "../../modals/GroupNameModal";
import { renameGroup } from "../../helpers/group";
import Droppable from "../drag/Droppable";
function TilesOverlay({ modalSize, children }) {
2021-06-05 18:05:35 +10:00
const {
groups,
openGroupId,
onGroupClose,
onGroupSelect,
onGroupsChange,
} = useGroup();
2021-05-24 17:11:46 +10:00
const { theme } = useThemeUI();
const layout = useResponsiveLayout();
2021-05-24 17:11:46 +10:00
const openAnimation = useSpring({
opacity: openGroupId ? 1 : 0,
2021-05-24 17:28:57 +10:00
transform: openGroupId ? "scale(1)" : "scale(0.99)",
2021-05-24 17:11:46 +10:00
config: config.gentle,
});
const [containerSize, setContinerSize] = useState({ width: 0, height: 0 });
function handleContainerResize(width, height) {
2021-05-24 17:11:46 +10:00
const size = Math.min(width, height) - 16;
setContinerSize({ width: size, height: size });
}
2021-06-05 18:05:35 +10:00
const [isGroupNameModalOpen, setIsGroupNameModalOpen] = useState(false);
function handleGroupNameChange(name) {
onGroupsChange(renameGroup(groups, openGroupId, name));
setIsGroupNameModalOpen(false);
}
const group = groups.find((group) => group.id === openGroupId);
if (!openGroupId) {
return null;
}
return (
2021-05-24 17:11:46 +10:00
<>
<Box
sx={{
position: "absolute",
width: "100%",
height: "100%",
top: 0,
}}
bg="overlay"
/>
<ReactResizeDetector
handleWidth
handleHeight
onResize={handleContainerResize}
>
2021-05-24 17:11:46 +10:00
<animated.div
style={{
...openAnimation,
position: "absolute",
width: "100%",
height: "100%",
top: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
cursor: "pointer",
pointerEvents: openGroupId ? undefined : "none",
}}
onClick={() => openGroupId && onGroupClose()}
>
<Box
sx={{
width: containerSize.width,
height: containerSize.height,
2021-05-24 17:11:46 +10:00
borderRadius: "8px",
border: "1px solid",
borderColor: "border",
cursor: "default",
display: "flex",
2021-06-05 18:05:35 +10:00
justifyContent: "flex-start",
alignItems: "center",
2021-05-24 17:11:46 +10:00
position: "relative",
2021-06-05 18:05:35 +10:00
flexDirection: "column",
2021-05-24 17:11:46 +10:00
}}
bg="background"
onClick={(e) => e.stopPropagation()}
>
2021-06-05 18:05:35 +10:00
<Flex my={1} sx={{ position: "relative" }}>
<Text as="p" my="2px">
{group?.name}
</Text>
<IconButton
sx={{
width: "24px",
height: "24px",
position: group?.name ? "absolute" : "relative",
left: group?.name ? "100%" : 0,
}}
title="Edit Group"
aria-label="Edit Group"
onClick={() => setIsGroupNameModalOpen(true)}
>
<ChangeNicknameIcon />
</IconButton>
</Flex>
2021-05-24 17:11:46 +10:00
<SimpleBar
style={{
width: containerSize.width - 16,
height: containerSize.height - 48,
marginBottom: "8px",
backgroundColor: theme.colors.muted,
2021-05-24 17:11:46 +10:00
}}
onClick={() => onGroupSelect()}
2021-05-24 17:11:46 +10:00
>
<Grid
sx={{
borderRadius: "4px",
overflow: "hidden",
position: "relative",
2021-05-24 17:11:46 +10:00
}}
gap={2}
columns={`repeat(${layout.groupGridColumns}, 1fr)`}
p={3}
2021-05-24 17:11:46 +10:00
>
<Droppable
id={ADD_TO_MAP_ID}
style={{
position: "absolute",
width: modalSize.width,
height: `calc(100% + ${
modalSize.height - containerSize.height + 48
}px)`,
left: `-${
(modalSize.width - containerSize.width) / 2 + 8
}px`,
top: `-${
(modalSize.height - containerSize.height) / 2 + 48
}px`,
zIndex: -1,
}}
/>
<Droppable
id={UNGROUP_ID}
style={{
position: "absolute",
2021-06-09 23:24:24 +10:00
top: 0,
bottom: 0,
left: 0,
right: 0,
zIndex: -1,
}}
/>
2021-05-24 17:11:46 +10:00
{children}
</Grid>
</SimpleBar>
<Close
onClick={() => onGroupClose()}
sx={{ position: "absolute", top: 0, right: 0 }}
/>
</Box>
</animated.div>
</ReactResizeDetector>
2021-06-05 18:05:35 +10:00
<GroupNameModal
isOpen={isGroupNameModalOpen}
name={group?.name}
onSubmit={handleGroupNameChange}
onRequestClose={() => setIsGroupNameModalOpen(false)}
/>
2021-05-24 17:11:46 +10:00
</>
);
}
export default TilesOverlay;