1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

161 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-01-17 02:50:57 -07:00
import React from "react";
2020-01-20 00:37:04 -07:00
import { Flex, IconButton, useColorMode } from "@chakra-ui/core";
import { motion, AnimatePresence } from "framer-motion";
import ResetButton from "~/components/ResetButton";
import useMedia from "~/components/MediaProvider";
2020-03-29 20:04:37 -07:00
import useConfig from "~/components/HyperglassProvider";
2020-01-20 00:37:04 -07:00
import Title from "~/components/Title";
2020-01-17 02:50:57 -07:00
const AnimatedFlex = motion.custom(Flex);
2020-01-20 00:37:04 -07:00
const AnimatedResetButton = motion.custom(ResetButton);
2020-01-17 02:50:57 -07:00
2020-01-20 00:37:04 -07:00
const titleVariants = {
sm: {
fullSize: { scale: 1, marginLeft: 0 },
2020-03-29 20:04:37 -07:00
smallLogo: { marginLeft: "auto" },
smallText: { marginLeft: "auto" }
2020-01-20 00:37:04 -07:00
},
md: {
fullSize: { scale: 1 },
2020-03-29 20:04:37 -07:00
smallLogo: { scale: 0.5 },
smallText: { scale: 0.8 }
2020-01-20 00:37:04 -07:00
},
lg: {
fullSize: { scale: 1 },
2020-03-29 20:04:37 -07:00
smallLogo: { scale: 0.5 },
smallText: { scale: 0.8 }
2020-01-20 00:37:04 -07:00
},
xl: {
fullSize: { scale: 1 },
2020-03-29 20:04:37 -07:00
smallLogo: { scale: 0.5 },
smallText: { scale: 0.8 }
2020-01-20 00:37:04 -07:00
}
};
const icon = { light: "moon", dark: "sun" };
const bg = { light: "white", dark: "black" };
const colorSwitch = { dark: "Switch to light mode", light: "Switch to dark mode" };
const headerTransition = { type: "spring", ease: "anticipate", damping: 15, stiffness: 100 };
2020-03-28 17:14:59 -07:00
const titleJustify = {
true: ["flex-end", "flex-end", "center", "center"],
false: ["flex-start", "flex-start", "center", "center"]
};
2020-03-29 20:04:37 -07:00
const titleHeight = {
true: null,
false: [null, "20vh", "20vh", "20vh"]
};
const resetButtonMl = { true: [null, 2, 2, 2], false: null };
const widthMap = {
text_only: "100%",
logo_only: ["90%", "90%", "25%", "25%"],
logo_subtitle: ["90%", "90%", "25%", "25%"],
all: ["90%", "90%", "25%", "25%"]
};
2020-01-20 00:37:04 -07:00
2020-03-29 20:04:37 -07:00
export default ({ isSubmitting, handleFormReset, ...props }) => {
2020-01-17 02:50:57 -07:00
const { colorMode, toggleColorMode } = useColorMode();
2020-03-29 20:04:37 -07:00
const { web } = useConfig();
2020-01-20 00:37:04 -07:00
const { mediaSize } = useMedia();
const resetButton = (
<AnimatePresence key="resetButton">
<AnimatedFlex
layoutTransition={headerTransition}
initial={{ opacity: 0, x: -50 }}
animate={{ opacity: 1, x: 0, width: "unset" }}
exit={{ opacity: 0, x: -50 }}
alignItems="center"
mb={[null, "auto"]}
2020-03-29 20:04:37 -07:00
ml={resetButtonMl[isSubmitting]}
display={isSubmitting ? "flex" : "none"}
2020-01-20 00:37:04 -07:00
>
<AnimatedResetButton isSubmitting={isSubmitting} onClick={handleFormReset} />
</AnimatedFlex>
</AnimatePresence>
);
const title = (
<AnimatedFlex
key="title"
px={1}
alignItems={isSubmitting ? "center" : ["center", "center", "flex-end", "flex-end"]}
positionTransition={headerTransition}
initial={{ scale: 0.5 }}
2020-03-29 20:04:37 -07:00
animate={
isSubmitting && web.text.title_mode === "text_only"
? "smallText"
: isSubmitting && web.text.title_mode !== "text_only"
? "smallLogo"
: "fullSize"
}
2020-01-20 00:37:04 -07:00
variants={titleVariants[mediaSize]}
2020-03-28 17:14:59 -07:00
justifyContent={titleJustify[isSubmitting]}
2020-01-20 00:37:04 -07:00
mb={[null, isSubmitting ? "auto" : null]}
mt={[null, isSubmitting ? null : "auto"]}
2020-03-29 20:04:37 -07:00
maxW={widthMap[web.text.title_mode]}
2020-03-22 09:55:06 -07:00
flex="1 0 0"
2020-03-29 20:04:37 -07:00
minH={titleHeight[isSubmitting]}
2020-01-20 00:37:04 -07:00
>
<Title isSubmitting={isSubmitting} onClick={handleFormReset} />
</AnimatedFlex>
);
const colorModeToggle = (
<AnimatedFlex
layoutTransition={headerTransition}
key="colorModeToggle"
alignItems="center"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
mb={[null, "auto"]}
2020-03-28 17:14:59 -07:00
mr={isSubmitting ? null : 2}
2020-01-20 00:37:04 -07:00
>
<IconButton
aria-label={colorSwitch[colorMode]}
variant="ghost"
color="current"
2020-03-29 20:04:37 -07:00
px={4}
p={null}
2020-01-20 00:37:04 -07:00
fontSize="20px"
onClick={toggleColorMode}
icon={icon[colorMode]}
/>
</AnimatedFlex>
);
const layout = {
false: {
sm: [title, resetButton, colorModeToggle],
md: [resetButton, title, colorModeToggle],
lg: [resetButton, title, colorModeToggle],
xl: [resetButton, title, colorModeToggle]
},
true: {
sm: [resetButton, colorModeToggle, title],
md: [resetButton, title, colorModeToggle],
lg: [resetButton, title, colorModeToggle],
xl: [resetButton, title, colorModeToggle]
}
};
2020-01-17 02:50:57 -07:00
return (
<Flex
2020-01-20 00:37:04 -07:00
px={2}
zIndex="4"
as="header"
2020-01-17 02:50:57 -07:00
width="full"
2020-03-29 20:04:37 -07:00
flex="0 1 auto"
2020-01-20 00:37:04 -07:00
bg={bg[colorMode]}
color="gray.500"
{...props}
2020-01-17 02:50:57 -07:00
>
2020-03-28 17:14:59 -07:00
<Flex
w="100%"
mx="auto"
2020-03-29 20:04:37 -07:00
pt={6}
2020-03-28 17:14:59 -07:00
justify="space-between"
2020-03-29 20:04:37 -07:00
flex="1 0 auto"
2020-03-28 17:14:59 -07:00
alignItems={isSubmitting ? "center" : "flex-start"}
>
2020-01-20 00:37:04 -07:00
{layout[isSubmitting][mediaSize]}
2020-01-17 02:50:57 -07:00
</Flex>
</Flex>
);
};