Skip to content

Commit

Permalink
fix: 修复 嵌套时动态模式定位异常 严格模式下重复创建
Browse files Browse the repository at this point in the history
  • Loading branch information
wanpan11 committed Jul 26, 2024
1 parent d7a8f2e commit 026d99d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/demo/customization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export default function Demo() {
console.log(e);
console.log(info);
});

return () => {
joystick.destroy();
};
}, []);

return (
Expand Down
4 changes: 4 additions & 0 deletions docs/demo/dynamic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default function Demo() {
useEffect(() => {
const joystick = new Joystick();
joystick.create({ mode: 'dynamic', zone: 'joystick' });

return () => {
joystick.destroy();
};
}, []);

return (
Expand Down
4 changes: 4 additions & 0 deletions docs/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export default function Demo() {
useEffect(() => {
const joystick = new Joystick();
joystick.create({ mode: 'static', zone: 'joystick' });

return () => {
joystick.destroy();
};
}, []);

return (
Expand Down
15 changes: 11 additions & 4 deletions src/joystick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Joystick {
front.setAttribute('class', 'front');

const uiStyle: CSS.Properties<string | number> = {
position: 'absolute',
position: mode === 'static' ? 'absolute' : 'fixed',
top:
mode === 'static'
? position.top
Expand Down Expand Up @@ -216,7 +216,7 @@ class Joystick {
endArr.forEach((key) => {
body.addEventListener(key, (e) => {
body.removeEventListener(toBind.move, this.move);
this.destroy();
this.reset();

this.callBack.end?.(e);
});
Expand Down Expand Up @@ -261,7 +261,7 @@ class Joystick {
e.preventDefault();
};

private destroy = (): void => {
private reset = (): void => {
if (this.currentJoystick.build) {
this.currentJoystick.front!.style.transform = 'translate(0px, 0px)';

Expand All @@ -271,13 +271,20 @@ class Joystick {
setTimeout(() => {
this.currentJoystick.build = false;
this.currentJoystick.ui!.remove();
}, 100);
}, 50);
} else {
this.currentJoystick.ui!.style.opacity = '0.5';
}
}
};

public destroy = (): void => {
if (this.currentJoystick.build) {
this.currentJoystick.ui!.remove();
this.currentJoystick.build = false;
}
};

public on = (type: CallBackType, cb: CallBack): void => {
const keys = Object.keys(this.callBack);

Expand Down

0 comments on commit 026d99d

Please sign in to comment.