Skip to content

Commit e5da75f

Browse files
committed
Prevent toggle animation on the initial update.
1 parent ccaacea commit e5da75f

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/ui/shared/ThreeWayToggle.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class ThreeWayToggle {
2222
valueA: string;
2323
valueB: string;
2424
private _value!: ThreeWayValue;
25+
private firstUpdate: boolean;
2526

2627
private onchange?: (source: ThreeWayToggle) => void;
2728
private tapHandler: TapHandler;
@@ -40,6 +41,7 @@ export class ThreeWayToggle {
4041
) {
4142
this.valueA = valueA;
4243
this.valueB = valueB;
44+
this.firstUpdate = true;
4345

4446
const toggle = createElement("div", "game-toggle threeway");
4547
this.element = toggle;
@@ -92,6 +94,10 @@ export class ThreeWayToggle {
9294
}
9395

9496
set value(v: ThreeWayValue | string) {
97+
if (!this.firstUpdate) {
98+
this.element.classList.add("animated");
99+
}
100+
this.firstUpdate = false;
95101
let state = null;
96102
if (v == ThreeWayOption.OptionA || v == this.valueA)
97103
state = ThreeWayOption.OptionA;

src/ui/shared/Toggle.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class Toggle {
1515
element: HTMLElement;
1616
private boxElement: HTMLElement;
1717
private _checked!: boolean;
18+
private firstUpdate: boolean;
1819

1920
private onchange?: (source: Toggle) => void;
2021
private tapHandler: TapHandler;
@@ -36,6 +37,7 @@ export class Toggle {
3637
iconEl.innerHTML = icon;
3738
iconEl.title = t(title);
3839

40+
this.firstUpdate = true;
3941
this.onchange = onchange;
4042
if (checked) {
4143
checked.then((value: boolean) => (this.checked = value));
@@ -58,6 +60,10 @@ export class Toggle {
5860
}
5961

6062
set checked(state: boolean) {
63+
if (!this.firstUpdate) {
64+
this.element.classList.add("animated");
65+
}
66+
this.firstUpdate = false;
6167
this.element.classList.toggle("enabled", state);
6268
if (this._checked != state) {
6369
this._checked = state;

style/components/toggles.scss

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@
5454
.toggle-box {
5555
background: transparent;
5656
height: calc($ball-width + max(0.3vw, 4px));
57-
transition:
58-
flex-shrink 100ms,
59-
flex-grow 100ms;
6057
}
6158
}
6259
&.threeway .toggle-box.a {
@@ -67,6 +64,11 @@
6764
justify-content: flex-end;
6865
background: transparent;
6966
}
67+
&.animated.threeway .wrap .toggle-box {
68+
transition:
69+
flex-shrink 100ms,
70+
flex-grow 100ms;
71+
}
7072

7173
.toggle-ball {
7274
border-radius: 100%;
@@ -76,6 +78,8 @@
7678
box-shadow: var(--button-box-shadow);
7779
position: absolute;
7880
right: calc(100% - $ball-width - $toggle-box-padding);
81+
}
82+
&.animated .toggle-ball {
7983
transition: 100ms right;
8084
}
8185

0 commit comments

Comments
 (0)