Skip to content

Commit 9472018

Browse files
committed
fix: more fixes to allow more complex configurations
1 parent 72b80d6 commit 9472018

File tree

1 file changed

+67
-23
lines changed

1 file changed

+67
-23
lines changed

src/index.ts

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,21 @@ export class PersistentBottomSheet extends GridLayout {
7676
// isPanning = false;
7777
backdropColor = null;
7878

79-
steps: number[] = [70];
79+
_steps: number[] = [70];
80+
81+
get steps() {
82+
const result = this._steps || (this.bottomSheet && (this.bottomSheet as any).steps);
83+
return result;
84+
}
85+
set steps(value: number[]) {
86+
this._steps = value;
87+
}
8088
stepIndex = 0;
8189

8290
isAnimating = false;
8391
prevDeltaY = 0;
8492
viewHeight = 0;
93+
bottomViewHeight = 0;
8594
panGestureHandler: PanGestureHandler;
8695

8796
lastScrollY: number;
@@ -165,7 +174,8 @@ export class PersistentBottomSheet extends GridLayout {
165174
this._translationY = value;
166175
}
167176
get translationMaxOffset() {
168-
return this.steps[this.steps.length - 1];
177+
const steps = this.steps;
178+
return steps[steps.length - 1];
169179
}
170180
initNativeView() {
171181
super.initNativeView();
@@ -271,7 +281,8 @@ export class PersistentBottomSheet extends GridLayout {
271281
if (newValue) {
272282
newValue.iosOverflowSafeAreaEnabled = false;
273283
newValue.verticalAlignment = 'bottom';
274-
let index;
284+
newValue.on('layoutChanged', this.onBottomLayoutChange, this);
285+
let index;
275286
if (!newValue.parent) {
276287
index = this.getChildrenCount();
277288
this.addChild(newValue);
@@ -287,9 +298,8 @@ export class PersistentBottomSheet extends GridLayout {
287298
}
288299
}
289300

290-
computeTranslationData() {
291-
const value = this.translationY;
292-
const height = this.translationMaxOffset;
301+
computeTranslationData(height) {
302+
const value = this._translationY;
293303
const progress = 1 - value / height;
294304
if (this.translationFunction) {
295305
return this.translationFunction(height, value, progress);
@@ -307,11 +317,27 @@ export class PersistentBottomSheet extends GridLayout {
307317
const contentView = event.object as GridLayout;
308318
const height = Math.round(Utils.layout.toDeviceIndependentPixels(contentView.getMeasuredHeight()));
309319
this.viewHeight = height;
310-
if (this.translationY === -1) {
311-
const step = this.steps[this.stepIndex];
312-
const ty = this.translationMaxOffset - step;
320+
if (this.translationY === -1 && this.bottomViewHeight !== 0) {
321+
const height = this.bottomViewHeight;
322+
const steps = this.steps;
323+
const step = steps[this.stepIndex];
324+
const ty = height - step;
325+
this.translationY = ty;
326+
const data = this.computeTranslationData(height);
327+
this.applyTrData(data);
328+
}
329+
}
330+
onBottomLayoutChange(event: EventData) {
331+
const contentView = event.object as GridLayout;
332+
const height = Math.round(Utils.layout.toDeviceIndependentPixels(contentView.getMeasuredHeight()));
333+
this.bottomViewHeight = height;
334+
if (this.translationY === -1 && this.viewHeight !== 0) {
335+
const height = this.bottomViewHeight;
336+
const steps = this.steps;
337+
const step = steps[this.stepIndex];
338+
const ty = height - step;
313339
this.translationY = ty;
314-
const data = this.computeTranslationData();
340+
const data = this.computeTranslationData(height);
315341
this.applyTrData(data);
316342
}
317343
}
@@ -335,7 +361,10 @@ export class PersistentBottomSheet extends GridLayout {
335361
}
336362
set isScrollEnabled(value: boolean) {
337363
if (this._isScrollEnabled !== value) {
338-
this._isScrollEnabled = this.scrollView.isScrollEnabled = value;
364+
this._isScrollEnabled = value;
365+
if (this.scrollView) {
366+
this.scrollView.isScrollEnabled = value;
367+
}
339368
}
340369
}
341370
onTouch(event: TouchGestureEventData) {
@@ -389,9 +418,10 @@ export class PersistentBottomSheet extends GridLayout {
389418
// const viewY = this.translationY - height;
390419
const y = touchY - (this.lastTouchY || touchY);
391420
const trY = this.constrainY(this.translationY + y);
421+
const height = this.translationMaxOffset;
392422
// console.log('constraining on touch event', touchY, this.lastTouchY, y, trY);
393423
this.translationY = trY;
394-
const trData = this.computeTranslationData();
424+
const trData = this.computeTranslationData(height);
395425
this.applyTrData(trData);
396426
}
397427
this.lastTouchY = touchY;
@@ -456,15 +486,20 @@ export class PersistentBottomSheet extends GridLayout {
456486
const y = deltaY - this.prevDeltaY;
457487
const trY = this.constrainY(this.translationY + y);
458488
this.translationY = trY;
459-
const trData = this.computeTranslationData();
489+
const height = this.translationMaxOffset;
490+
const trData = this.computeTranslationData(height);
460491
this.applyTrData(trData);
461492
this.prevDeltaY = deltaY;
462493
}
463494

464495
applyTrData(trData: { [k: string]: any }) {
465496
Object.keys(trData).forEach((k) => {
497+
const { target, ...others } = trData[k];
498+
if (target) {
499+
Object.assign(target, others);
500+
}
466501
if (this[k]) {
467-
Object.assign(this[k], trData[k]);
502+
Object.assign(this[k], others);
468503
}
469504
});
470505
}
@@ -474,7 +509,6 @@ export class PersistentBottomSheet extends GridLayout {
474509
}
475510

476511
async animateToPosition(position, duration = OPEN_DURATION) {
477-
// console.log('animateToPosition', position, new Error().stack);
478512
if (this._scrollView && global.isAndroid) {
479513
// on android we get unwanted scroll effect while "swipping the view"
480514
// cancel the views touches before animation to prevent that
@@ -483,22 +517,32 @@ export class PersistentBottomSheet extends GridLayout {
483517
event.setAction(android.view.MotionEvent.ACTION_CANCEL);
484518
this.scrollView.nativeViewProtected.dispatchTouchEvent(event);
485519
}
486-
this.translationY = this.translationMaxOffset - position;
487-
const trData = this.computeTranslationData();
520+
const height = this.bottomViewHeight;
521+
this.translationY = height - position;
522+
const trData = this.computeTranslationData(height);
488523

489524
const params = Object.keys(trData)
490-
.map(
491-
(k) =>
492-
this[k] &&
493-
Object.assign(
525+
.map((k) => {
526+
const data = trData[k];
527+
if (data.target) {
528+
return Object.assign(
529+
{
530+
curve: AnimationCurve.easeInOut,
531+
duration,
532+
},
533+
transformAnimationValues(trData[k])
534+
);
535+
} else if (this[k]) {
536+
return Object.assign(
494537
{
495538
target: this[k],
496539
curve: AnimationCurve.easeInOut,
497540
duration,
498541
},
499542
transformAnimationValues(trData[k])
500-
)
501-
)
543+
);
544+
}
545+
})
502546
.filter((a) => !!a);
503547
try {
504548
await new Animation(params).play();

0 commit comments

Comments
 (0)