-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lazy libs provider, add drawer drags animations
- Loading branch information
Showing
6 changed files
with
146 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/shared/lib/components/AnimationProvider/AnimationProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
createContext, ReactNode, useContext, useEffect, useMemo, useRef, useState, | ||
} from 'react'; | ||
|
||
type SpringType = typeof import('@react-spring/web'); | ||
type GestureType = typeof import('@use-gesture/react'); | ||
|
||
interface AnimationContextPayload { | ||
Gesture?: GestureType; | ||
Spring?: SpringType; | ||
isLoaded?: boolean; | ||
} | ||
|
||
const AnimationContext = createContext<AnimationContextPayload>({}); | ||
|
||
// Both libs depend on each other | ||
const getAsyncAnimationModules = async () => Promise.all([ | ||
import('@react-spring/web'), | ||
import('@use-gesture/react'), | ||
]); | ||
|
||
export const useAnimationLibs = () => useContext(AnimationContext) as Required<AnimationContextPayload>; | ||
|
||
export const AnimationProvider = ({ children }: {children: ReactNode}) => { | ||
const SpringRef = useRef<SpringType>(); | ||
const GestureRef = useRef<GestureType>(); | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
|
||
useEffect(() => { | ||
getAsyncAnimationModules().then(([Spring, Gesture]) => { | ||
SpringRef.current = Spring; | ||
GestureRef.current = Gesture; | ||
setIsLoaded(true); | ||
}); | ||
}, []); | ||
|
||
const value = useMemo(() => ({ | ||
Gesture: GestureRef.current, | ||
Spring: SpringRef.current, | ||
isLoaded, | ||
}), [isLoaded]); | ||
|
||
return ( | ||
<AnimationContext.Provider | ||
value={value} | ||
> | ||
{children} | ||
</AnimationContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { AnimationProvider, useAnimationLibs } from './AnimationProvider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,107 @@ | ||
import { classNames, Mods } from 'shared/lib/classNames/classNames'; | ||
import React, { memo } from 'react'; | ||
import { classNames } from 'shared/lib/classNames/classNames'; | ||
import React, { | ||
memo, ReactNode, useCallback, useEffect, | ||
} from 'react'; | ||
import { useTheme } from 'app/providers/ThemeProvider'; | ||
import { useModal } from 'shared/lib/hook/useModal/useModal'; | ||
import { Portal } from '../Portal/Portal'; | ||
import { useAnimationLibs } from 'shared/lib/components/AnimationProvider'; | ||
import { Overlay } from '../Overlay/Overlay'; | ||
import cls from './Drawer.module.scss'; | ||
import { Portal } from '../Portal/Portal'; | ||
|
||
interface DrawerProps { | ||
className?: string; | ||
children?: React.ReactNode; | ||
children: ReactNode; | ||
isOpen?: boolean; | ||
onClose?: () => void; | ||
lazy?: boolean; | ||
lazy?: boolean; | ||
} | ||
|
||
export const Drawer = memo((props: DrawerProps) => { | ||
const height = window.innerHeight - 100; | ||
|
||
export const DrawerContent = memo((props: DrawerProps) => { | ||
const { Spring, Gesture } = useAnimationLibs(); | ||
const [{ y }, api] = Spring.useSpring(() => ({ y: height })); | ||
const { theme } = useTheme(); | ||
const { | ||
className, | ||
children, | ||
isOpen, | ||
onClose, | ||
isOpen, | ||
lazy, | ||
} = props; | ||
|
||
const { theme } = useTheme(); | ||
const openDrawer = useCallback(() => { | ||
api.start({ y: 0, immediate: false }); | ||
}, [api]); | ||
|
||
const { | ||
isClosing, | ||
close, | ||
isMounted, | ||
} = useModal({ | ||
onClose, | ||
animationDelay: 300, | ||
isOpen, | ||
}); | ||
useEffect(() => { | ||
if (isOpen) { | ||
openDrawer(); | ||
} | ||
}, [api, isOpen, openDrawer]); | ||
|
||
const mods: Mods = { | ||
[cls.opened]: isOpen, | ||
[cls.isClosing]: isClosing, | ||
const close = (velocity = 0) => { | ||
api.start({ | ||
y: height, | ||
immediate: false, | ||
config: { ...Spring.config.stiff, velocity }, | ||
onResolve: onClose, | ||
}); | ||
}; | ||
|
||
if (lazy && !isMounted) { | ||
const bind = Gesture.useDrag( | ||
({ | ||
last, | ||
velocity: [, vy], | ||
direction: [, dy], | ||
movement: [, my], | ||
cancel, | ||
}) => { | ||
if (my < -70) cancel(); | ||
|
||
if (last) { | ||
if (my > height * 0.5 || (vy > 0.5 && dy > 0)) { | ||
close(); | ||
} else { | ||
openDrawer(); | ||
} | ||
} else { | ||
api.start({ y: my, immediate: true }); | ||
} | ||
}, | ||
{ | ||
from: () => [0, y.get()], filterTaps: true, bounds: { top: 0 }, rubberband: true, | ||
}, | ||
); | ||
|
||
if (!isOpen) { | ||
return null; | ||
} | ||
|
||
const display = y.to((py) => (py < height ? 'block' : 'none')); | ||
|
||
return ( | ||
<Portal> | ||
<div className={classNames(cls.Drawer, mods, [className, theme])}> | ||
<div className={classNames(cls.Drawer, {}, [className, theme, 'app_drawer'])}> | ||
<Overlay onClick={close} /> | ||
<div className={cls.content}> | ||
<Spring.a.div | ||
className={cls.sheet} | ||
style={{ display, bottom: `calc(-100vh + ${height - 100}px)`, y }} | ||
{...bind()} | ||
> | ||
{children} | ||
</div> | ||
</Spring.a.div> | ||
</div> | ||
</Portal> | ||
); | ||
}); | ||
|
||
export const Drawer = memo((props: DrawerProps) => { | ||
const { isLoaded } = useAnimationLibs(); | ||
|
||
if (!isLoaded) { | ||
return null; | ||
} | ||
|
||
return <DrawerContent {...props} />; | ||
}); |