Skip to content

Commit c8a32e5

Browse files
committed
feat: allow to open multiple drawers while panning (like going from left to right) as it is done in discord app
1 parent 3473d03 commit c8a32e5

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

src/ui-drawer/index.ts

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class Drawer extends GridLayout {
167167
private mTranslationX: { [k in Side]: number } = { left: 0, right: 0 };
168168
private mTranslationY: { [k in VerticalSide]: number } = { bottom: 0, top: 0 };
169169
private mShowingSide: Side | VerticalSide = null;
170-
private mNeedToSetSide: Side | VerticalSide;
170+
// private mNeedToSetSide: Side | VerticalSide;
171171
private mModes: Partial<{ [k in Side | VerticalSide]: Mode }> = { left: 'slide', right: 'slide', bottom: 'slide', top: 'slide' };
172172

173173
translationFunction?: TranslationFunctionType;
@@ -301,33 +301,51 @@ export class Drawer extends GridLayout {
301301
needToSetSide = 'top';
302302
}
303303
if (needToSetSide && this[needToSetSide + 'ClosedDrawerAllowDraging']) {
304-
this.mNeedToSetSide = needToSetSide;
304+
// this.mNeedToSetSide = needToSetSide;
305305
return true;
306306
}
307307
}
308308
return false;
309309
}
310+
getDrawerToOpen(extraData) {
311+
if (extraData.translationX < 0 && this.rightDrawer) {
312+
return 'right';
313+
} else if (extraData.translationX > 0 && this.leftDrawer) {
314+
return 'left';
315+
} else if (extraData.translationY < 0 && this.bottomDrawer) {
316+
return 'bottom';
317+
} else if (extraData.translationY > 0 && this.topDrawer) {
318+
return 'top';
319+
}
320+
return null;
321+
}
310322
onGestureState(args: GestureStateEventData) {
311323
const { state, prevState, extraData, view } = args.data;
312324
if (state === GestureState.ACTIVE) {
313-
this.notify({ eventName: 'start', side: this.mNeedToSetSide });
314325
if (!this.mShowingSide) {
315-
if (this.mNeedToSetSide === 'left') {
316-
this.leftDrawer.visibility = 'visible';
317-
} else if (this.mNeedToSetSide === 'right') {
318-
this.rightDrawer.visibility = 'visible';
319-
} else if (this.mNeedToSetSide === 'bottom') {
320-
this.bottomDrawer.visibility = 'visible';
321-
} else if (this.mNeedToSetSide === 'top') {
322-
this.topDrawer.visibility = 'visible';
326+
const shouldShowSide = this.getDrawerToOpen(extraData);
327+
if (shouldShowSide && shouldShowSide !== this.mShowingSide) {
328+
this[shouldShowSide + 'Drawer'].visibility = 'visible';
329+
this.mShowingSide = shouldShowSide;
330+
this.notify({ eventName: 'start', side: this.mShowingSide });
323331
}
332+
// if (this.mNeedToSetSide === 'left') {
333+
// this.leftDrawer.visibility = 'visible';
334+
// } else if (this.mNeedToSetSide === 'right') {
335+
// this.rightDrawer.visibility = 'visible';
336+
// } else if (this.mNeedToSetSide === 'bottom') {
337+
// this.bottomDrawer.visibility = 'visible';
338+
// } else if (this.mNeedToSetSide === 'top') {
339+
// this.topDrawer.visibility = 'visible';
340+
// }
324341
}
325342
}
326343
this.updateIsPanning(state);
327344

328345
if (prevState === GestureState.ACTIVE) {
329-
const side = this.mShowingSide || this.mNeedToSetSide;
330-
this.mNeedToSetSide = null;
346+
const side = this.mShowingSide;
347+
// const side = this.mShowingSide || this.mNeedToSetSide;
348+
// this.mNeedToSetSide = null;
331349
if (!side || (this.shouldPan && !this.shouldPan(side))) {
332350
return;
333351
}
@@ -397,14 +415,36 @@ export class Drawer extends GridLayout {
397415
this.animateToPosition(side, destSnapPoint);
398416
}
399417
}
418+
isSideVisible(side: Side | VerticalSide) {
419+
if (side === 'left' || side === 'right') {
420+
return this.mViewWidth[side] - this.mTranslationX[side];
421+
} else {
422+
return this.mViewHeight[side] - this.mTranslationY[side];
423+
}
424+
}
400425
onGestureTouch(args: GestureTouchEventData) {
401426
const data = args.data;
402-
const side = this.mShowingSide || this.mNeedToSetSide;
403-
if (data.state !== GestureState.ACTIVE || !side || this.mIsAnimating) {
427+
const { state, extraData, view } = args.data;
428+
// const side = this.mShowingSide || this.mNeedToSetSide;
429+
if (data.state !== GestureState.ACTIVE || this.mIsAnimating) {
430+
return;
431+
}
432+
const shouldShowSide = this.getDrawerToOpen(extraData);
433+
if (shouldShowSide && (!this.mShowingSide || (shouldShowSide !== this.mShowingSide && !this.isSideVisible(this.mShowingSide)))) {
434+
if (this.mShowingSide) {
435+
this[this.mShowingSide + 'Drawer'].visibility = 'hidden';
436+
this.notify({ eventName: 'end', side: this.mShowingSide });
437+
}
438+
this[shouldShowSide + 'Drawer'].visibility = 'visible';
439+
this.mShowingSide = shouldShowSide;
440+
this.notify({ eventName: 'start', side: this.mShowingSide });
441+
}
442+
const side = this.mShowingSide;
443+
if (!side || this.mIsAnimating) {
404444
return;
405445
}
406446
if (side === 'left' || side === 'right') {
407-
const deltaX = data.extraData.translationX;
447+
const deltaX = extraData.translationX;
408448
if (this.mIsAnimating || !this.mIsPanning || deltaX === 0 || (this.shouldPan && !this.shouldPan(side))) {
409449
this.mPrevDeltaX = deltaX;
410450
return;
@@ -429,10 +469,10 @@ export class Drawer extends GridLayout {
429469
this.backDrop.visibility = trData.backDrop && trData.backDrop.opacity > 0 ? 'visible' : 'hidden';
430470
}
431471
this.applyTrData(trData, side);
432-
this.updateIsPanning(data.state);
472+
this.updateIsPanning(state);
433473
this.mPrevDeltaX = deltaX;
434474
} else {
435-
const deltaY = data.extraData.translationY;
475+
const deltaY = extraData.translationY;
436476
if (this.mIsAnimating || !this.mIsPanning || deltaY === 0 || (this.shouldPan && !this.shouldPan(side))) {
437477
this.mPrevDeltaY = deltaY;
438478
return;
@@ -457,7 +497,7 @@ export class Drawer extends GridLayout {
457497
this.backDrop.visibility = trData.backDrop && trData.backDrop.opacity > 0 ? 'visible' : 'hidden';
458498
}
459499
this.applyTrData(trData, side);
460-
this.updateIsPanning(data.state);
500+
this.updateIsPanning(state);
461501
this.mPrevDeltaY = deltaY;
462502
}
463503
}

0 commit comments

Comments
 (0)