Commit 168e3092 authored by isstuev's avatar isstuev

animation fix

parent 87ab7a9e
import { Box, Flex, Text, Icon, VStack, useColorModeValue } from '@chakra-ui/react'; import { Box, Flex, Text, Icon, VStack, useColorModeValue } from '@chakra-ui/react';
import { AnimatePresence, animate, motion, useMotionValue } from 'framer-motion'; import { animate, motion, useMotionValue } from 'framer-motion';
import React, { useCallback } from 'react'; import React, { useCallback } from 'react';
import appConfig from 'configs/app/config'; import appConfig from 'configs/app/config';
...@@ -16,17 +16,19 @@ const NavigationMobile = () => { ...@@ -16,17 +16,19 @@ const NavigationMobile = () => {
const [ openedGroupIndex, setOpenedGroupIndex ] = React.useState(-1); const [ openedGroupIndex, setOpenedGroupIndex ] = React.useState(-1);
const x = useMotionValue(0); const mainX = useMotionValue(0);
const subX = useMotionValue(250);
const onGroupItemOpen = (index: number) => () => { const onGroupItemOpen = (index: number) => () => {
animate(x, -250);
setOpenedGroupIndex(index); setOpenedGroupIndex(index);
animate(mainX, -250, { ease: 'easeInOut' });
animate(subX, 0, { ease: 'easeInOut' });
}; };
const onGroupItemClose = useCallback(() => { const onGroupItemClose = useCallback(() => {
animate(x, 0); animate(mainX, 0, { ease: 'easeInOut' });
setOpenedGroupIndex(-1); animate(subX, 250, { ease: 'easeInOut', onComplete: () => setOpenedGroupIndex(-1) });
}, [ x ]); }, [ mainX, subX ]);
const isAuth = Boolean(cookies.get(cookies.NAMES.API_TOKEN)); const isAuth = Boolean(cookies.get(cookies.NAMES.API_TOKEN));
const hasAccount = appConfig.isAccountSupported && isAuth; const hasAccount = appConfig.isAccountSupported && isAuth;
...@@ -39,8 +41,7 @@ const NavigationMobile = () => { ...@@ -39,8 +41,7 @@ const NavigationMobile = () => {
<Box <Box
as={ motion.nav } as={ motion.nav }
mt={ 6 } mt={ 6 }
style={{ x }} style={{ x: mainX }}
transitionDuration="100ms"
> >
<VStack <VStack
w="100%" w="100%"
...@@ -61,43 +62,36 @@ const NavigationMobile = () => { ...@@ -61,43 +62,36 @@ const NavigationMobile = () => {
<Box <Box
as={ motion.nav } as={ motion.nav }
mt={ 6 } mt={ 6 }
style={{ x }} style={{ x: mainX }}
transitionDuration="100ms"
> >
<VStack as="ul" spacing="1" alignItems="flex-start"> <VStack as="ul" spacing="1" alignItems="flex-start">
{ accountNavItems.map((item) => <NavLink key={ item.text } { ...item }/>) } { accountNavItems.map((item) => <NavLink key={ item.text } { ...item }/>) }
</VStack> </VStack>
</Box> </Box>
) } ) }
<AnimatePresence> { openedGroupIndex >= 0 && (
{ openedGroupIndex >= 0 && ( <Box
<Box as={ motion.nav }
as={ motion.nav } mt={ 6 }
mt={ 6 } position="absolute"
position="absolute" top={ 0 }
top={ 0 } style={{ x: subX }}
initial={{ x: 250 }} key="sub"
animate={{ x: 0 }} >
exit={{ x: 250 }} <VStack
transitionDuration="100ms" w="100%"
transitionTimingFunction="linear" as="ul"
key="sub" spacing="1"
alignItems="flex-start"
> >
<VStack <Flex alignItems="center" px={ 3 } py={ 2.5 } w="100%" h="50px" onClick={ onGroupItemClose }>
w="100%" <Icon as={ chevronIcon } boxSize={ 6 } mr={ 2 } color={ iconColor }/>
as="ul" <Text variant="secondary" fontSize="sm">{ mainNavItems[openedGroupIndex].text }</Text>
spacing="1" </Flex>
alignItems="flex-start" { mainNavItems[openedGroupIndex].subItems?.map((item) => <NavLink key={ item.text } { ...item }/>) }
> </VStack>
<Flex alignItems="center" px={ 3 } py={ 2.5 } w="100%" h="50px" onClick={ onGroupItemClose }> </Box>
<Icon as={ chevronIcon } boxSize={ 6 } mr={ 2 } color={ iconColor }/> ) }
<Text variant="secondary" fontSize="sm">{ mainNavItems[openedGroupIndex].text }</Text>
</Flex>
{ mainNavItems[openedGroupIndex].subItems?.map((item) => <NavLink key={ item.text } { ...item }/>) }
</VStack>
</Box>
) }
</AnimatePresence>
</Box> </Box>
<NavFooter hasAccount={ hasAccount }/> <NavFooter hasAccount={ hasAccount }/>
</> </>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment