From 9352c997d5ce3f0e7087f80bbf41e58a95cb5aab Mon Sep 17 00:00:00 2001 From: Taya Leutina Date: Thu, 20 Jun 2024 18:01:35 +0300 Subject: [PATCH] fix(Sheet): fix resizing depending on the size of the window (#1671) --- src/components/Sheet/SheetContent.tsx | 44 +++++++++++++------ .../DefaultShowcase/DefaultShowcase.scss | 2 + .../WithMenuShowcase/WithMenuShowcase.scss | 5 +++ .../WithMenuShowcase.stories.tsx | 14 +++--- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/components/Sheet/SheetContent.tsx b/src/components/Sheet/SheetContent.tsx index ba1941f3a4..63e549acc8 100644 --- a/src/components/Sheet/SheetContent.tsx +++ b/src/components/Sheet/SheetContent.tsx @@ -16,6 +16,7 @@ const ACCELERATION_Y_MAX = 0.08; const ACCELERATION_Y_MIN = -0.02; // 90% from viewport const MAX_CONTENT_HEIGHT_FROM_VIEWPORT_COEFFICIENT = 0.9; +const WINDOW_RESIZE_TIMEOUT = 25; let hashHistory: string[] = []; @@ -74,6 +75,7 @@ class SheetContent extends React.Component(); velocityTracker = new VelocityTracker(); observer: ResizeObserver | null = null; + resizeWindowTimer: number | null = null; state: SheetContentState = { startScrollTop: 0, @@ -90,9 +92,12 @@ class SheetContent extends React.Component { + const availableViewportHeight = + window.innerHeight * MAX_CONTENT_HEIGHT_FROM_VIEWPORT_COEFFICIENT - this.sheetTopHeight; + + const resultHeight = + sheetHeight >= availableViewportHeight ? availableViewportHeight : sheetHeight; + + return resultHeight; + }; + private show = () => { this.setState({isAnimating: true}, () => { this.setStyles({status: 'showing'}); @@ -403,9 +421,13 @@ class SheetContent extends React.Component { this.setState({inWindowResizeScope: true}); - this.onResize(); + if (this.resizeWindowTimer) { + window.clearTimeout(this.resizeWindowTimer); + } - setTimeout(() => this.setState({inWindowResizeScope: false}), 0); + this.resizeWindowTimer = window.setTimeout(() => { + this.onResize(); + }, WINDOW_RESIZE_TIMEOUT); }; private onResize = () => { @@ -413,17 +435,13 @@ class SheetContent extends React.Component= availableViewportHeight ? availableViewportHeight : sheetHeight; + const resultHeight = this.getResultHeight(sheetHeight); this.sheetContentRef.current.style.transition = this.state.prevSheetHeight > sheetHeight @@ -432,7 +450,7 @@ class SheetContent extends React.Component = (args: SheetProps) => { const [visible, setVisible] = React.useState(false); + const generateMenuItems = () => { + return Array.from({length: 50}, (_, index) => { + return menu item 2.{index}; + }); + }; + return (
setVisible(false)}> - + menu item 1.1 menu item 1.2 menu item 1.3 - - menu item 2.1 - menu item 2.2 - menu item 2.3 - + {generateMenuItems()}