From 35a7b749493ec0377f5e013619d312f087801141 Mon Sep 17 00:00:00 2001 From: mournfulCoroner Date: Tue, 24 Oct 2023 14:25:39 +0300 Subject: [PATCH] fix(Sheet): fix Sheet resize when viewport size changes (#1055) --- CODEOWNERS | 2 +- src/components/Sheet/SheetContent.tsx | 40 +++++++++---------- .../DefaultShowcase.stories.tsx | 5 ++- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/CODEOWNERS b/CODEOWNERS index fcb6e65144..d76f2ca9b0 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -33,7 +33,7 @@ /src/components/User @DaffPunks /src/components/UserAvatar @DaffPunks /src/components/Select @korvin89 -/src/components/Sheet @korvin89 +/src/components/Sheet @mournfulCoroner /src/components/Skeleton @SeqviriouM /src/components/Spin @SeqviriouM /src/components/Stories @DarkGenius diff --git a/src/components/Sheet/SheetContent.tsx b/src/components/Sheet/SheetContent.tsx index 1a68d6cdee..5213a02b69 100644 --- a/src/components/Sheet/SheetContent.tsx +++ b/src/components/Sheet/SheetContent.tsx @@ -50,7 +50,7 @@ interface SheetContentState { startScrollTop: number; startY: number; deltaY: number; - prevInnerContentHeight: number; + prevSheetHeight: number; swipeAreaTouched: boolean; contentTouched: boolean; veilTouched: boolean; @@ -78,7 +78,7 @@ class SheetContent extends React.Component ACCELERATION_Y_MAX ) { this.hide(); - } else { + } else if (deltaY !== 0) { this.show(); } }; @@ -399,30 +401,26 @@ class SheetContent extends React.Component { - const heightChanged = this.state.prevInnerContentHeight !== this.innerContentHeight; - - if (!this.sheetRef.current || !this.sheetContentRef.current || !heightChanged) { + if (!this.sheetRef.current || !this.sheetContentRef.current) { return; } + const sheetHeight = this.sheetTitleHeight + this.innerContentHeight + this.sheetTopHeight; + + const availableViewportHeight = + window.innerHeight * MAX_CONTENT_HEIGHT_FROM_VIEWPORT_COEFFICIENT; + + const resultHeight = + sheetHeight >= availableViewportHeight ? availableViewportHeight : sheetHeight; + this.sheetContentRef.current.style.transition = - this.state.prevInnerContentHeight > this.innerContentHeight + this.state.prevSheetHeight > sheetHeight ? `height 0s ease ${this.transitionDuration}` : 'none'; - const contentHeight = this.sheetTitleHeight + this.innerContentHeight; - - const viewportHeight = window.innerHeight; - const resultHeight = - contentHeight >= viewportHeight - ? viewportHeight * MAX_CONTENT_HEIGHT_FROM_VIEWPORT_COEFFICIENT - : contentHeight; - - this.sheetContentRef.current.style.height = `${resultHeight}px`; - this.sheetRef.current.style.transform = `translate3d(0, -${ - resultHeight + this.sheetTopHeight - }px, 0)`; - this.setState({prevInnerContentHeight: contentHeight}); + this.sheetContentRef.current.style.height = `${resultHeight - this.sheetTopHeight}px`; + this.sheetRef.current.style.transform = `translate3d(0, -${resultHeight}px, 0)`; + this.setState({prevSheetHeight: sheetHeight}); }; private addListeners() { diff --git a/src/components/Sheet/__stories__/DefaultShowcase/DefaultShowcase.stories.tsx b/src/components/Sheet/__stories__/DefaultShowcase/DefaultShowcase.stories.tsx index 2d59b9f5af..29ff9f71a9 100644 --- a/src/components/Sheet/__stories__/DefaultShowcase/DefaultShowcase.stories.tsx +++ b/src/components/Sheet/__stories__/DefaultShowcase/DefaultShowcase.stories.tsx @@ -2,7 +2,7 @@ import React from 'react'; import type {Meta, StoryFn} from '@storybook/react'; -import {Button, Checkbox} from '../../../'; +import {Button, Checkbox, TextInput} from '../../../'; import {cn} from '../../../utils/cn'; import {Sheet} from '../../Sheet'; import type {SheetProps} from '../../Sheet'; @@ -68,6 +68,9 @@ export const Showcase: StoryFn = (args: SheetProps) => { onClose={() => setVisible(false)} title={withTitle ? 'Sheet title' : undefined} > +
+ +