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

150 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-10-07 09:41:58 -07:00
import * as React from 'react';
import { Flex, useColorMode } from '@chakra-ui/core';
import { motion, AnimatePresence } from 'framer-motion';
import { useConfig, useHyperglassState, useMedia } from 'app/context';
import { Title, ResetButton, ColorModeToggle } from 'app/components';
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-10-07 09:41:58 -07:00
smallLogo: { marginLeft: 'auto' },
smallText: { marginLeft: 'auto' },
},
md: {
fullSize: { scale: 1 },
smallLogo: { scale: 0.5 },
2020-10-07 09:41:58 -07:00
smallText: { scale: 0.8 },
},
lg: {
fullSize: { scale: 1 },
smallLogo: { scale: 0.5 },
2020-10-07 09:41:58 -07:00
smallText: { scale: 0.8 },
},
xl: {
fullSize: { scale: 1 },
smallLogo: { scale: 0.5 },
2020-10-07 09:41:58 -07:00
smallText: { scale: 0.8 },
},
2020-01-20 00:37:04 -07:00
};
2020-10-07 09:41:58 -07:00
const bg = { light: 'white', dark: 'black' };
const headerTransition = {
2020-10-07 09:41:58 -07:00
type: 'spring',
ease: 'anticipate',
damping: 15,
2020-10-07 09:41:58 -07:00
stiffness: 100,
};
2020-03-28 17:14:59 -07:00
const titleJustify = {
2020-10-07 09:41:58 -07:00
true: ['flex-end', 'flex-end', 'center', 'center'],
false: ['flex-start', 'flex-start', 'center', 'center'],
2020-03-28 17:14:59 -07:00
};
2020-03-29 20:04:37 -07:00
const titleHeight = {
true: null,
2020-10-07 09:41:58 -07:00
false: [null, '20vh', '20vh', '20vh'],
2020-03-29 20:04:37 -07:00
};
const resetButtonMl = { true: [null, 2, 2, 2], false: null };
const widthMap = {
2020-10-07 09:41:58 -07:00
text_only: '100%',
logo_only: ['90%', '90%', '50%', '50%'],
logo_subtitle: ['90%', '90%', '50%', '50%'],
all: ['90%', '90%', '50%', '50%'],
2020-03-29 20:04:37 -07:00
};
2020-01-20 00:37:04 -07:00
export const Header = ({ layoutRef, ...props }) => {
const AnimatedFlex = motion.custom(Flex);
const AnimatedResetButton = motion.custom(ResetButton);
2020-07-04 14:59:05 -07:00
const { colorMode } = useColorMode();
const { web } = useConfig();
const { mediaSize } = useMedia();
const { isSubmitting, resetForm } = useHyperglassState();
const handleFormReset = () => {
resetForm(layoutRef);
};
const resetButton = (
<AnimatePresence key="resetButton">
<AnimatedFlex
layoutTransition={headerTransition}
initial={{ opacity: 0, x: -50 }}
2020-10-07 09:41:58 -07:00
animate={{ opacity: 1, x: 0, width: 'unset' }}
exit={{ opacity: 0, x: -50 }}
alignItems="center"
2020-10-07 09:41:58 -07:00
mb={[null, 'auto']}
ml={resetButtonMl[isSubmitting]}
2020-10-07 09:41:58 -07:00
display={isSubmitting ? 'flex' : 'none'}>
<AnimatedResetButton isSubmitting={isSubmitting} onClick={handleFormReset} />
</AnimatedFlex>
</AnimatePresence>
);
const title = (
<AnimatedFlex
key="title"
px={1}
2020-10-07 09:41:58 -07:00
alignItems={isSubmitting ? 'center' : ['center', 'center', 'flex-end', 'flex-end']}
positionTransition={headerTransition}
initial={{ scale: 0.5 }}
animate={
2020-10-07 09:41:58 -07:00
isSubmitting && web.text.title_mode === 'text_only'
? 'smallText'
: isSubmitting && web.text.title_mode !== 'text_only'
? 'smallLogo'
: 'fullSize'
}
variants={titleVariants[mediaSize]}
justifyContent={titleJustify[isSubmitting]}
2020-10-07 09:41:58 -07:00
mt={[null, isSubmitting ? null : 'auto']}
maxW={widthMap[web.text.title_mode]}
flex="1 0 0"
2020-10-07 09:41:58 -07:00
minH={titleHeight[isSubmitting]}>
<Title isSubmitting={isSubmitting} onClick={handleFormReset} />
</AnimatedFlex>
);
const colorModeToggle = (
<AnimatedFlex
layoutTransition={headerTransition}
key="colorModeToggle"
alignItems="center"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
2020-10-07 09:41:58 -07:00
mb={[null, 'auto']}
mr={isSubmitting ? null : 2}>
2020-07-04 14:59:05 -07:00
<ColorModeToggle />
</AnimatedFlex>
);
const layout = {
false: {
sm: [title, resetButton, colorModeToggle],
md: [resetButton, title, colorModeToggle],
lg: [resetButton, title, colorModeToggle],
2020-10-07 09:41:58 -07:00
xl: [resetButton, title, colorModeToggle],
},
true: {
sm: [resetButton, colorModeToggle, title],
md: [resetButton, title, colorModeToggle],
lg: [resetButton, title, colorModeToggle],
2020-10-07 09:41:58 -07:00
xl: [resetButton, title, colorModeToggle],
},
};
return (
<Flex
px={2}
zIndex="4"
as="header"
width="full"
flex="0 1 auto"
bg={bg[colorMode]}
color="gray.500"
2020-10-07 09:41:58 -07:00
{...props}>
<Flex
w="100%"
mx="auto"
pt={6}
justify="space-between"
flex="1 0 auto"
2020-10-07 09:41:58 -07:00
alignItems={isSubmitting ? 'center' : 'flex-start'}>
{layout[isSubmitting][mediaSize]}
</Flex>
</Flex>
);
2020-01-17 02:50:57 -07:00
};