Skip to content

Commit 9cd5a27

Browse files
authored
fix: the fancy button does not come back to its original state after the animation #246 (#247)
1 parent 2548f48 commit 9cd5a27

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"serve": "xs serve",
3939
"storybook": "storybook dev -p 6006",
4040
"storybook:build": "storybook build --output-dir docs/storybook",
41-
"test": "npx jest --silent",
41+
"test": "npx jest --silent --passWithNoTests",
4242
"test:coverage": "npx jest --coverage",
4343
"types": "xs types",
4444
"watch": "xs watch"
@@ -54,10 +54,10 @@
5454
"npm run types"
5555
],
5656
"src/*.{ts,js,mjs}": [
57-
"npm run test --silent --passWithNoTests"
57+
"npm run test"
5858
],
5959
"tests/components/*.test.ts": [
60-
"npm run test --silent --passWithNoTests"
60+
"npm run test"
6161
]
6262
},
6363
"extensionConfig": {

src/FancyButton.ts

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export type ButtonOptions = ViewsInput & {
128128
*/
129129
export class FancyButton extends ButtonContainer
130130
{
131-
protected animations: StateAnimations = {};
132-
protected originalInnerViewState: AnimationData = {};
131+
protected animations!: StateAnimations;
132+
protected originalInnerViewState!: AnimationData;
133133
protected defaultDuration = 100;
134134

135135
/** FancyButton options. */
@@ -253,6 +253,7 @@ export class FancyButton extends ButtonContainer
253253
if (animations)
254254
{
255255
this.animations = animations;
256+
this.setOriginalInnerViewState();
256257
Ticker.shared.add(() => Group.shared.update());
257258
}
258259

@@ -831,40 +832,6 @@ export class FancyButton extends ButtonContainer
831832
{
832833
if (!this.animations) return;
833834

834-
if (state === 'default' && !this.originalInnerViewState)
835-
{
836-
this.originalInnerViewState = {
837-
x: this.innerView.x,
838-
y: this.innerView.y,
839-
width: this.innerView.width,
840-
height: this.innerView.height,
841-
scale: {
842-
x: this.innerView.scale.x,
843-
y: this.innerView.scale.y,
844-
},
845-
};
846-
847-
// first animation state is default, so we don't need to animate it
848-
// this part will run only once, during initialization
849-
const defaultStateAnimation = this.animations?.default;
850-
851-
if (defaultStateAnimation)
852-
{
853-
this.innerView.x = defaultStateAnimation.props.x ?? this.originalInnerViewState.x ?? 0;
854-
this.innerView.y = defaultStateAnimation.props.y ?? this.originalInnerViewState.y ?? 0;
855-
this.innerView.width
856-
= defaultStateAnimation.props.width ?? this.originalInnerViewState.width ?? 0;
857-
this.innerView.height
858-
= defaultStateAnimation.props.height ?? this.originalInnerViewState.height ?? 0;
859-
this.innerView.scale.x
860-
= defaultStateAnimation.props.scale?.x ?? this.originalInnerViewState.scale?.x ?? 1;
861-
this.innerView.scale.y
862-
= defaultStateAnimation.props.scale?.y ?? this.originalInnerViewState.scale?.y ?? 1;
863-
864-
return;
865-
}
866-
}
867-
868835
const stateAnimation = this.animations[state] ?? this.animations.default;
869836

870837
if (stateAnimation)
@@ -882,6 +849,38 @@ export class FancyButton extends ButtonContainer
882849
new Tween(this.innerView).to(this.originalInnerViewState, this.defaultDuration).start();
883850
}
884851

852+
protected setOriginalInnerViewState()
853+
{
854+
this.originalInnerViewState = {
855+
x: this.innerView.x,
856+
y: this.innerView.y,
857+
width: this.innerView.width,
858+
height: this.innerView.height,
859+
scale: {
860+
x: this.innerView.scale.x,
861+
y: this.innerView.scale.y,
862+
},
863+
};
864+
865+
// first animation state is default, so we don't need to animate it
866+
// this part will run only once, during initialization
867+
const defaultStateAnimation = this.animations?.default;
868+
869+
if (defaultStateAnimation)
870+
{
871+
this.innerView.x = defaultStateAnimation.props.x ?? this.originalInnerViewState.x ?? 0;
872+
this.innerView.y = defaultStateAnimation.props.y ?? this.originalInnerViewState.y ?? 0;
873+
this.innerView.width
874+
= defaultStateAnimation.props.width ?? this.originalInnerViewState.width ?? 0;
875+
this.innerView.height
876+
= defaultStateAnimation.props.height ?? this.originalInnerViewState.height ?? 0;
877+
this.innerView.scale.x
878+
= defaultStateAnimation.props.scale?.x ?? this.originalInnerViewState.scale?.x ?? 1;
879+
this.innerView.scale.y
880+
= defaultStateAnimation.props.scale?.y ?? this.originalInnerViewState.scale?.y ?? 1;
881+
}
882+
}
883+
885884
protected initStateControl()
886885
{
887886
this.onDown.connect(() =>
@@ -891,7 +890,7 @@ export class FancyButton extends ButtonContainer
891890

892891
this.onUp.connect(() =>
893892
{
894-
isMobile.any ? this.setState('default') : this.setState('hover');
893+
this.setState(isMobile.any ? 'default' : 'hover');
895894
});
896895

897896
this.onUpOut.connect(() =>
@@ -909,14 +908,14 @@ export class FancyButton extends ButtonContainer
909908

910909
this.onPress.connect(() =>
911910
{
912-
isMobile.any ? this.setState('default') : this.setState('hover');
911+
this.setState(isMobile.any ? 'default' : 'hover');
913912
});
914913

915914
this.onHover.connect(() =>
916915
{
917916
if (!this.button.isDown)
918917
{
919-
isMobile.any ? this.setState('default') : this.setState('hover');
918+
this.setState(isMobile.any ? 'default' : 'hover');
920919
}
921920
});
922921
}

0 commit comments

Comments
 (0)