Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Sheet): fix Sheet resize when viewport size changes #1055

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 19 additions & 21 deletions src/components/Sheet/SheetContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ interface SheetContentState {
startScrollTop: number;
startY: number;
deltaY: number;
prevInnerContentHeight: number;
prevSheetHeight: number;
swipeAreaTouched: boolean;
contentTouched: boolean;
veilTouched: boolean;
Expand Down Expand Up @@ -78,7 +78,7 @@ class SheetContent extends React.Component<SheetContentInnerProps, SheetContentS
startScrollTop: 0,
startY: 0,
deltaY: 0,
prevInnerContentHeight: 0,
prevSheetHeight: 0,
swipeAreaTouched: false,
contentTouched: false,
veilTouched: false,
Expand All @@ -90,7 +90,9 @@ class SheetContent extends React.Component<SheetContentInnerProps, SheetContentS
this.addListeners();
this.show();
this.setInitialStyles();
this.setState({prevInnerContentHeight: this.innerContentHeight});
this.setState({
prevSheetHeight: this.sheetTitleHeight + this.innerContentHeight + this.sheetTopHeight,
});
}

componentDidUpdate(prevProps: SheetContentInnerProps) {
Expand Down Expand Up @@ -336,7 +338,7 @@ class SheetContent extends React.Component<SheetContentInnerProps, SheetContentS
accelerationY > ACCELERATION_Y_MAX
) {
this.hide();
} else {
} else if (deltaY !== 0) {
this.show();
}
};
Expand Down Expand Up @@ -399,30 +401,26 @@ class SheetContent extends React.Component<SheetContentInnerProps, SheetContentS
};

private onResize = () => {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -68,6 +68,9 @@ export const Showcase: StoryFn<SheetProps> = (args: SheetProps) => {
onClose={() => setVisible(false)}
title={withTitle ? 'Sheet title' : undefined}
>
<div className={b('content-item')}>
<TextInput />
</div>
<div className={b('content-item', b('checkbox'))}>
<Checkbox
content="Extra content"
Expand Down