Skip to content

Commit

Permalink
fix: cancelation on backdrop click
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Nov 24, 2022
1 parent 8a36627 commit 7d648ad
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
5 changes: 3 additions & 2 deletions packages/widget/src/components/BottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ import {
useCallback,
useImperativeHandle,
useRef,
useState,
useState
} from 'react';
import { getScrollableContainer } from '../../hooks';
import { backdropProps, modalProps, paperProps } from '../Dialog';
import type { BottomSheetBase, BottomSheetProps } from './types';

export const BottomSheet = forwardRef<BottomSheetBase, BottomSheetProps>(
({ elementRef, children, open }, ref) => {
({ elementRef, children, open, onClose }, ref) => {
const openRef = useRef(open);
const [drawerOpen, setDrawerOpen] = useState(open);

const close = useCallback(() => {
setDrawerOpen(false);
openRef.current = false;
onClose?.();
}, []);

useImperativeHandle(
Expand Down
3 changes: 2 additions & 1 deletion packages/widget/src/components/BottomSheet/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { DrawerProps } from '@mui/material';
import type { RefObject } from 'react';

export type BottomSheetProps = DrawerProps & {
export type BottomSheetProps = Omit<DrawerProps, 'onClose'> & {
elementRef?: RefObject<HTMLDivElement>;
onClose?(): void;
};

export interface BottomSheetBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Big from 'big.js';
import {
forwardRef,
MutableRefObject,
useCallback,
useImperativeHandle,
useRef,
useState
Expand Down Expand Up @@ -45,12 +46,12 @@ export const ExchangeRateBottomSheet = forwardRef<
onContinue?.();
};

const handleCancel = () => {
const handleCancel = useCallback(() => {
(ref as MutableRefObject<ExchangeRateBottomSheetBase>).current?.close(
false,
);
onCancel?.();
};
}, []);

useImperativeHandle(
ref,
Expand All @@ -70,7 +71,7 @@ export const ExchangeRateBottomSheet = forwardRef<
);

return (
<BottomSheet ref={bottomSheetRef}>
<BottomSheet ref={bottomSheetRef} onClose={handleCancel}>
<ExchangeRateBottomSheetContent
data={data}
onContinue={handleContinue}
Expand Down
9 changes: 4 additions & 5 deletions packages/widget/src/pages/SwapPage/TokenValueBottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import type { Route } from '@lifi/sdk';
import { WarningRounded as WarningIcon } from '@mui/icons-material';
import { Box, Button, Typography } from '@mui/material';
import Big from 'big.js';
import type { MutableRefObject } from 'react';
import { forwardRef, useRef } from 'react';
import { forwardRef, MutableRefObject, useCallback, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import type { BottomSheetBase } from '../../components/BottomSheet';
import { BottomSheet } from '../../components/BottomSheet';
Expand All @@ -21,13 +20,13 @@ export const TokenValueBottomSheet = forwardRef<
BottomSheetBase,
TokenValueBottomSheetProps
>(({ route, onContinue, onCancel }, ref) => {
const handleCancel = () => {
const handleCancel = useCallback(() => {
(ref as MutableRefObject<BottomSheetBase>).current?.close();
onCancel?.();
};
}, []);

return (
<BottomSheet ref={ref}>
<BottomSheet ref={ref} onClose={handleCancel}>
<TokenValueBottomSheetContent
route={route}
onContinue={onContinue}
Expand Down

0 comments on commit 7d648ad

Please sign in to comment.