Skip to content

Commit eea4300

Browse files
committed
refactor(): update gesture
1 parent 53075aa commit eea4300

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

src/index.ts

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { createGesture, GestureDetail, createAnimation } from '@ionic/core';
22
import type { Animation } from '@ionic/core/dist/types/utils/animation/animation-interface';
33
import { Gesture } from '@ionic/core/dist/types/utils/gesture';
4+
import { cloneElement, getTransform } from './utils';
5+
6+
const GestureName = 'tab-bar-gesture';
7+
const MinScale = 'scale(1.1)';
8+
const MiddleScale = 'scale(1.2)';
9+
const MaxScale = 'scale(1.3)';
10+
const OverScale = 'scale(1.4)';
411

512
export const registerTabBarEffect = (ionTabBar: HTMLElement): Gesture => {
613
let gesture!: Gesture;
@@ -9,12 +16,6 @@ export const registerTabBarEffect = (ionTabBar: HTMLElement): Gesture => {
916
let tabEffectElY: number | null;
1017

1118
const tabEffectEl = cloneElement('ion-tab-button');
12-
const GestureName = 'tab-bar-gesture';
13-
const MinScale = 'scale(1.1)';
14-
const MiddleScale = 'scale(1.2)';
15-
const MaxScale = 'scale(1.3)';
16-
const OverScale = 'scale(1.4)';
17-
1819
ionTabBar.addEventListener('pointerdown', () => clearActivated());
1920

2021
const createTabButtonGesture = () => {
@@ -61,21 +62,6 @@ export const registerTabBarEffect = (ionTabBar: HTMLElement): Gesture => {
6162
}
6263
};
6364

64-
const getTransform = (detailCurrentX: number, tabSelectedActual: Element): string => {
65-
const diff = -2;
66-
const currentX = detailCurrentX - tabSelectedActual.clientWidth / 2;
67-
const maxLeft = tabSelectedActual.getBoundingClientRect().left + diff;
68-
const maxRight = tabSelectedActual.getBoundingClientRect().right - diff - tabSelectedActual.clientWidth;
69-
70-
if (maxLeft < currentX && currentX < maxRight) {
71-
return `translate3d(${currentX}px, ${tabEffectElY}px, 0)`;
72-
}
73-
if (maxLeft > currentX) {
74-
return `translate3d(${maxLeft}px, ${tabEffectElY}px, 0)`;
75-
}
76-
return `translate3d(${maxRight}px, ${tabEffectElY}px, 0)`;
77-
};
78-
7965
const enterTabButtonAnimation = (detail: GestureDetail): Animation | undefined => {
8066
currentTouchedButton = (detail.event.target as HTMLElement).closest('ion-tab-button');
8167
const tabSelectedActual = ionTabBar.querySelector('ion-tab-button.tab-selected');
@@ -86,13 +72,15 @@ export const registerTabBarEffect = (ionTabBar: HTMLElement): Gesture => {
8672
tabEffectElY = tabSelectedActual.getBoundingClientRect().top;
8773
const startTransform = getTransform(
8874
tabSelectedActual.getBoundingClientRect().left + tabSelectedActual.clientWidth / 2,
75+
tabEffectElY,
8976
tabSelectedActual,
9077
);
9178
const middleTransform = getTransform(
9279
(tabSelectedActual.getBoundingClientRect().left + tabSelectedActual.clientWidth / 2 + detail.currentX) / 2,
80+
tabEffectElY,
9381
currentTouchedButton,
9482
);
95-
const endTransform = getTransform(detail.currentX, currentTouchedButton);
83+
const endTransform = getTransform(detail.currentX, tabEffectElY, currentTouchedButton);
9684
const tabButtonAnimation = createAnimation();
9785
tabButtonAnimation
9886
.addElement(tabEffectEl)
@@ -177,7 +165,7 @@ export const registerTabBarEffect = (ionTabBar: HTMLElement): Gesture => {
177165
return undefined;
178166
}
179167

180-
const transform = getTransform(detail.currentX, currentTouchedButton);
168+
const transform = getTransform(detail.currentX, tabEffectElY!, currentTouchedButton);
181169

182170
const tabButtonAnimation = createAnimation();
183171
tabButtonAnimation.addElement(tabEffectEl);
@@ -205,6 +193,7 @@ export const registerTabBarEffect = (ionTabBar: HTMLElement): Gesture => {
205193

206194
const endTransform = getTransform(
207195
currentTouchedButton.getBoundingClientRect().left + currentTouchedButton.clientWidth / 2,
196+
tabEffectElY!,
208197
currentTouchedButton,
209198
);
210199

@@ -228,17 +217,3 @@ export const registerTabBarEffect = (ionTabBar: HTMLElement): Gesture => {
228217

229218
return gesture;
230219
};
231-
232-
const cloneElement = (tagName: string): HTMLElement => {
233-
const getCachedEl = document.querySelector(`${tagName}.ion-cloned-element`);
234-
if (getCachedEl !== null) {
235-
return getCachedEl as HTMLElement;
236-
}
237-
238-
const clonedEl = document.createElement(tagName) as HTMLElement;
239-
clonedEl.classList.add('ion-cloned-element');
240-
clonedEl.style.setProperty('display', 'none');
241-
document.body.appendChild(clonedEl);
242-
243-
return clonedEl;
244-
};

src/utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export const cloneElement = (tagName: string): HTMLElement => {
2+
const getCachedEl = document.querySelector(`${tagName}.ion-cloned-element`);
3+
if (getCachedEl !== null) {
4+
return getCachedEl as HTMLElement;
5+
}
6+
7+
const clonedEl = document.createElement(tagName) as HTMLElement;
8+
clonedEl.classList.add('ion-cloned-element');
9+
clonedEl.style.setProperty('display', 'none');
10+
document.body.appendChild(clonedEl);
11+
12+
return clonedEl;
13+
};
14+
15+
export const getTransform = (detailCurrentX: number, tabEffectElY: number, tabSelectedActual: Element): string => {
16+
const diff = -2;
17+
const currentX = detailCurrentX - tabSelectedActual.clientWidth / 2;
18+
const maxLeft = tabSelectedActual.getBoundingClientRect().left + diff;
19+
const maxRight = tabSelectedActual.getBoundingClientRect().right - diff - tabSelectedActual.clientWidth;
20+
21+
if (maxLeft < currentX && currentX < maxRight) {
22+
return `translate3d(${currentX}px, ${tabEffectElY}px, 0)`;
23+
}
24+
if (maxLeft > currentX) {
25+
return `translate3d(${maxLeft}px, ${tabEffectElY}px, 0)`;
26+
}
27+
return `translate3d(${maxRight}px, ${tabEffectElY}px, 0)`;
28+
};

0 commit comments

Comments
 (0)