Skip to content

Commit

Permalink
fix(SheetContent): fix setting height for content (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Chernitsyn authored and amje committed Mar 17, 2023
1 parent f1227f0 commit 01f9add
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
29 changes: 25 additions & 4 deletions src/components/Sheet/SheetContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const DEFAULT_TRANSITION_DURATION = '0.3s';
const HIDE_THRESHOLD = 50;
const ACCELERATION_Y_MAX = 0.08;
const ACCELERATION_Y_MIN = -0.02;
// 90% from viewport
const MAX_CONTENT_HEIGHT_FROM_VIEWPORT_COEFFICIENT = 0.9;

let hashHistory: string[] = [];

Expand Down Expand Up @@ -64,6 +66,7 @@ class SheetContent extends React.Component<SheetContentInnerProps, SheetContentS
sheetTopRef = React.createRef<HTMLDivElement>();
sheetContentRef = React.createRef<HTMLDivElement>();
sheetInnerContentRef = React.createRef<HTMLDivElement>();
sheetTitleRef = React.createRef<HTMLDivElement>();
velocityTracker = new VelocityTracker();
observer: MutationObserver | null = null;
transitionDuration = DEFAULT_TRANSITION_DURATION;
Expand Down Expand Up @@ -161,7 +164,14 @@ class SheetContent extends React.Component<SheetContentInnerProps, SheetContentS
onTouchEnd={this.onContentTouchEnd}
onTransitionEnd={this.onContentTransitionEnd}
>
{title && <div className={sheetBlock('sheet-content-title')}>{title}</div>}
{title && (
<div
ref={this.sheetTitleRef}
className={sheetBlock('sheet-content-title')}
>
{title}
</div>
)}
<div ref={this.sheetInnerContentRef}>{content}</div>
</div>
</div>
Expand All @@ -185,6 +195,10 @@ class SheetContent extends React.Component<SheetContentInnerProps, SheetContentS
return this.sheetInnerContentRef.current?.getBoundingClientRect().height || 0;
}

private get sheetTitleHeight() {
return this.sheetTitleRef.current?.getBoundingClientRect().height || 0;
}

private get sheetScrollTop() {
return this.sheetContentRef.current?.scrollTop || 0;
}
Expand Down Expand Up @@ -392,12 +406,19 @@ class SheetContent extends React.Component<SheetContentInnerProps, SheetContentS
? `height 0s ease ${this.transitionDuration}`
: 'none';

this.setState({prevInnerContentHeight: this.innerContentHeight});
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 = `${this.innerContentHeight}px`;
this.sheetContentRef.current.style.height = `${resultHeight}px`;
this.sheetRef.current.style.transform = `translate3d(0, -${
this.innerContentHeight + this.sheetTopHeight
resultHeight + this.sheetTopHeight
}px, 0)`;
this.setState({prevInnerContentHeight: contentHeight});
};

private addListeners() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ const getRandomText = (length: number) => {
};

const EXTRA_OUTER_CONTENT = getRandomText(8000);
const EXTRA_INNER_CONTENT = getRandomText(1000);
const EXTRA_INNER_CONTENT = getRandomText(500);
const EXTRA_INNER_CONTENT_MORE_THAN_VIEWPORT = getRandomText(3000);

export const Showcase: Story<SheetProps> = (args: SheetProps) => {
const [visible, setVisible] = React.useState(false);
const [withExtraOuterContent, setWithExtraOuterContent] = React.useState(false);
const [withExtraInnerContent, setWithExtraInnerContent] = React.useState(false);
const [withExtraInnerContentMoreThenViewport, setWithExtraInnerContentMoreThenViewport] =
React.useState(false);
const [withTitle, setWithTitle] = React.useState(false);

return (
<div className={b()}>
Expand All @@ -40,22 +44,54 @@ export const Showcase: Story<SheetProps> = (args: SheetProps) => {
checked={withExtraOuterContent}
/>
</div>
<div className={b('content-item', b('checkbox'))}>
<Checkbox
content="Show title in sheet"
onUpdate={() => setWithTitle(!withTitle)}
checked={withTitle}
/>
</div>
{withExtraOuterContent && (
<div className={b('extra-content')}>{EXTRA_OUTER_CONTENT}</div>
)}
<Sheet {...args} visible={visible} onClose={() => setVisible(false)}>
<Sheet
{...args}
visible={visible}
onClose={() => setVisible(false)}
title={withTitle ? 'Sheet title' : undefined}
>
<div className={b('content-item', b('checkbox'))}>
<Checkbox
content="Extra content"
onUpdate={() => setWithExtraInnerContent(!withExtraInnerContent)}
onUpdate={() => {
setWithExtraInnerContent(!withExtraInnerContent);
setWithExtraInnerContentMoreThenViewport(false);
}}
checked={withExtraInnerContent}
/>
</div>
<div className={b('content-item', b('checkbox'))}>
<Checkbox
content="Extra content more then viewport"
onUpdate={() => {
setWithExtraInnerContentMoreThenViewport(
!withExtraInnerContentMoreThenViewport,
);
setWithExtraInnerContent(false);
}}
checked={withExtraInnerContentMoreThenViewport}
/>
</div>
{withExtraInnerContent && (
<div className={b('content-item', b('extra-content'))}>
{EXTRA_INNER_CONTENT}
</div>
)}
{withExtraInnerContentMoreThenViewport && (
<div className={b('content-item', b('extra-content'))}>
{EXTRA_INNER_CONTENT_MORE_THAN_VIEWPORT}
</div>
)}
<Button
view="action"
size="s"
Expand Down

0 comments on commit 01f9add

Please sign in to comment.