Skip to content

Commit

Permalink
Update index.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
cgero-eth committed Oct 27, 2023
1 parent 7b67a14 commit 9333b51
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/components/bottomSheet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, {ReactNode} from 'react';
import {motion, PanInfo} from 'framer-motion';
import React, {useEffect, ReactNode} from 'react';
import {motion, PanInfo, useAnimationControls} from 'framer-motion';
import {Backdrop} from '@aragon/ods-old';
import styled from 'styled-components';

import usePrevious from 'hooks/usePrevious';

export type BottomSheetProps = {
children?: ReactNode;
isOpen: boolean;
Expand All @@ -20,25 +22,42 @@ export default function BottomSheet({
subtitle,
closeOnDrag = true,
}: BottomSheetProps) {
const prevIsOpen = usePrevious(isOpen);
const controls = useAnimationControls();

// For adding drag on bottom sheet
function onDragEnd(_: MouseEvent | TouchEvent | PointerEvent, info: PanInfo) {
if (!closeOnDrag) {
controls.start('visible');
return;
}

if (info.velocity.y > 20 || (info.velocity.y >= 0 && info.point.y > 45)) {
const shouldClose =
info.velocity.y > 20 || (info.velocity.y >= 0 && info.point.y > 45);
if (shouldClose) {
controls.start('hidden');
onClose?.();
} else {
controls.start('visible');
}
}
// For Run animation on each state change
useEffect(() => {
if (prevIsOpen && !isOpen) {
controls.start('hidden');
} else if (!prevIsOpen && isOpen) {
controls.start('visible');
}
}, [controls, isOpen, prevIsOpen]);

return (
<>
<Backdrop visible={isOpen} onClose={onClose} />
<StyledMotionContainer
drag="y"
onDragEnd={onDragEnd}
initial={isOpen ? 'visible' : 'hidden'}
animate={isOpen ? 'visible' : 'hidden'}
initial="hidden"
animate={controls}
transition={{
type: 'spring',
damping: 40,
Expand Down

0 comments on commit 9333b51

Please sign in to comment.