Skip to content

Commit a36d9b8

Browse files
committed
refactor: remove waapi support
1 parent 0aafee7 commit a36d9b8

File tree

19 files changed

+197
-345
lines changed

19 files changed

+197
-345
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ I'm a big fan of smooth transitioning with a natural feel for DOM nodes. This li
7171
<br>
7272
To be honest, [`raf`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) won't achive that kind of performance, even when animating composite properties (transform, opacity). If you need to make a bulky request, be ready for some janky animations.
7373
<br>
74-
This is where `<UiTransition />` comes in 👨‍🏫. `<UiTransition />` uses the [`WAAPI`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API) if it is available, and falls back to using `CSS Keyframes`. Keyframes uses [off main thread animation](https://developer.mozilla.org/en-US/docs/Web/Performance/CSS_JavaScript_animation_performance#off_main_thread_animation) so you can be sure to do CPU draining tasks with your DOM node on the main thread, without having it interupted with calculations on every frame to create _spring animations_, ensuring a butter smooth animation with a natural feel.
74+
This is where `<UiTransition />` comes in 👨‍🏫. `<UiTransition />` uses `CSS Keyframes` which utilizes [off main thread animation](https://developer.mozilla.org/en-US/docs/Web/Performance/CSS_JavaScript_animation_performance#off_main_thread_animation) so you can be sure to do CPU draining tasks with your DOM node on the main thread, without having it interupted with calculations on every frame to create _spring animations_, ensuring a butter smooth animation with a natural feel.
7575

7676
## Props
7777

src/index.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default defineComponent({
3030
3131
props,
3232
33-
setup(_props, { slots, emit }) {
33+
setup(_props, { slots }) {
3434
const animPhase = ref<AnimPhase>("enter");
3535
3636
const props = computed(() => _props);
@@ -98,7 +98,6 @@ export default defineComponent({
9898
animPhase,
9999
appear: props.value.appear,
100100
configProp: getConfig,
101-
emit,
102101
getKeyframeName,
103102
getDuration,
104103
getDelay,

src/setup/hooks/beforeMount.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ export default function beforeMount() {
3535
if (!globalState.init) {
3636
globalState.init = true;
3737

38-
// set globalState.waapi
39-
globalState.waapi = typeof HTMLElement.prototype.animate === "function";
40-
41-
// or create a style tag.
42-
if (!globalState.waapi) {
43-
globalState.styleId = createStyleTag();
44-
}
38+
globalState.styleId = createStyleTag();
4539

4640
// create worker
4741
if (!globalState.webWorker) {

src/state/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { GlobalState } from "./types";
22

33
export const globalState: GlobalState = {
44
init: false,
5-
waapi: false,
65
webWorker: null,
76
styleId: "",
87
styleCreated: false,

src/state/transitions.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ let transitions = {
88
frame: (step, phase) => {
99
const build = {
1010
enter: {
11-
opacity: `${step(0, to)}`,
12-
transform: `scale3d(${step(from, to)}, ${step(from, to)}, 1)`,
11+
opacity: `${step(from, to)}`,
12+
willChange: "opacity",
1313
},
1414
leave: {
1515
opacity: `${step(to, from)}`,
16+
willChange: "opacity",
1617
},
1718
};
1819

src/state/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ export interface GlobalState {
44
styleId: string;
55
styleCreated: boolean;
66
webWorker: null | Worker;
7-
waapi: boolean;
87
init: boolean;
9-
keyframes: DynamicObject<number>;
8+
keyframes: DynamicObject<number | string>;
109
}
1110

1211
export type ConstructAnim = (...args: any[]) => BuildAnim;

src/types/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,12 @@ export interface Anim {
3434
}
3535

3636
export interface BuildAnim extends Anim {
37-
enter?: Anim;
38-
leave?: Anim;
37+
enter?: Anim | boolean;
38+
leave?: Anim | boolean;
3939
}
4040

4141
export type ConfigProp = BuildAnim | string | boolean;
4242

43-
export type Emit = (event: string, ...args: any[]) => void;
44-
4543
export interface InstallOptions {
4644
componentName?: string;
4745
globals?: (string | DynamicObject<string>)[];
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { RendererElement } from "vue";
2-
import { Emit } from "../../../types";
32
import { Hook } from "../types";
4-
import { $emit, resetEl } from "../utils";
5-
6-
export default function animcancelled(
7-
el: RendererElement,
8-
hook: Hook,
9-
emit: Emit
10-
) {
11-
$emit(emit, `${hook}Cancelled`, [el]);
3+
import { resetEl } from "../utils";
124

5+
export default function animcancelled(el: RendererElement, hook: Hook) {
136
resetEl(el);
147
}

src/utils/eventHooks/animDone/index.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
import { capitalize, ComputedRef, Ref, RendererElement } from "vue";
2-
import { AnimPhase, BuildAnim, Emit } from "../../../types";
1+
import { ComputedRef, Ref, RendererElement } from "vue";
2+
import { AnimPhase, BuildAnim } from "../../../types";
33
import { Hook, UiTransitionElement } from "../types";
4-
import {
5-
$emit,
6-
getState,
7-
resetEl,
8-
setAnimState,
9-
setProperties,
10-
} from "../utils";
4+
import { getState, resetEl, setAnimState, setProperties } from "../utils";
115

126
export default function animDone(
137
e: RendererElement,
148
hook: Hook,
15-
emit: Emit,
169
animPhase: Ref<AnimPhase>,
1710
configProp: ComputedRef<BuildAnim | null>,
1811
retainFinalStyle: boolean
@@ -21,11 +14,9 @@ export default function animDone(
2114

2215
setAnimState(state, animPhase);
2316

24-
$emit(emit, `after${capitalize(hook)}`, [e]);
25-
2617
resetEl(e);
2718

28-
if (retainFinalStyle && configProp.value) {
19+
if (retainFinalStyle && configProp.value && animPhase.value === "enter") {
2920
const el = e as unknown as UiTransitionElement;
3021

3122
const lastFrame = configProp.value.frame(
Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
import { ComputedRef, Ref, RendererElement } from "vue";
2-
import { AnimPhase, BuildAnim, Emit } from "../../../types";
3-
import sleep from "../../../worker/sleep";
2+
import { AnimPhase, BuildAnim } from "../../../types";
43
import { Hook, UiTransitionElement } from "../types";
5-
import { $emit, getState, setAnimState, setProperties } from "../utils";
6-
7-
import { globalState } from "../../../state";
4+
import { getState, setAnimState } from "../utils";
85

96
export default function animStart(
107
e: RendererElement,
118
done: (cancelled?: boolean) => void,
129
hook: Hook,
13-
emit: Emit,
1410
animPhase: Ref<AnimPhase>,
15-
configProp: ComputedRef<BuildAnim | null>,
16-
getKeyframeName: ComputedRef<string>,
17-
getDuration: ComputedRef<string>
11+
configProp: ComputedRef<BuildAnim | null>
1812
) {
19-
const { keyframes, waapi } = globalState;
20-
2113
const state = getState(hook);
2214

2315
setAnimState(state, animPhase);
2416

25-
$emit(emit, hook, [e]);
26-
2717
if (!configProp.value) {
2818
return done();
2919
}
@@ -35,32 +25,4 @@ export default function animStart(
3525

3626
delete el.__done;
3727
};
38-
39-
if (!waapi) {
40-
sleep().then(() => {
41-
const duration = () => {
42-
// check dataset
43-
if (el.dataset.uitDuration) {
44-
return parseFloat(el.dataset.uitDuration);
45-
}
46-
47-
// check the prop value
48-
if (typeof getDuration.value === "string" && !!getDuration.value) {
49-
return getDuration.value;
50-
}
51-
52-
// check duration in config object
53-
if (/string|number/.test(typeof configProp.value?.duration)) {
54-
return parseFloat(`${configProp.value?.duration}`) || 1;
55-
}
56-
57-
return keyframes[getKeyframeName.value];
58-
};
59-
60-
setProperties(el, {
61-
"--uit-anim-duration": `${duration()}ms`,
62-
"--uit-anim-name": getKeyframeName.value,
63-
});
64-
});
65-
}
6628
}

0 commit comments

Comments
 (0)