add step for entrance-transition
This commit is contained in:
@@ -12,9 +12,3 @@
|
||||
opacity: 1;
|
||||
transform: translate3d(0, 0, 0);
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.entrance-fade-up-enter-active {
|
||||
transition: none;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,31 @@
|
||||
import type { ObjectDirective } from "vue";
|
||||
|
||||
const ENTRANCE_DURATION = 400;
|
||||
const ENTRANCE_DELAY_STEP = 70;
|
||||
const ACTIVE_CLASS = "entrance-fade-up-enter-active";
|
||||
const FROM_CLASS = "entrance-fade-up-enter-from";
|
||||
const TO_CLASS = "entrance-fade-up-enter-to";
|
||||
const STATE_KEY = Symbol("entrance-transition-state");
|
||||
const AUTO_ELEMENTS = new WeakSet<HTMLElement>();
|
||||
|
||||
/** 入场动画指令支持的延迟值。 */
|
||||
export type EntranceTransitionValue = number | string | { delay?: number | string } | undefined;
|
||||
/** 入场动画指令支持的延迟配置;省略时按同级元素顺序自动错开。 */
|
||||
export type EntranceTransitionValue =
|
||||
| number
|
||||
| string
|
||||
| {
|
||||
/** 固定延迟;设置 step 时启用自动步进,step 优先。 */
|
||||
delay?: number | string;
|
||||
/** 自动步进间隔,单位为毫秒。 */
|
||||
step?: number | string;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
interface EntranceTransitionState {
|
||||
frameId?: number;
|
||||
timerId?: number;
|
||||
delay: number;
|
||||
step: number;
|
||||
auto: boolean;
|
||||
}
|
||||
|
||||
type EntranceTransitionElement = HTMLElement & {
|
||||
@@ -29,22 +42,51 @@ function setDelay(element: EntranceTransitionElement, delay: number): void {
|
||||
element.style.setProperty("--entrance-delay", `${delay}ms`);
|
||||
}
|
||||
|
||||
function cleanup(element: EntranceTransitionElement): void {
|
||||
function isAuto(value: EntranceTransitionValue): boolean {
|
||||
return (
|
||||
value === undefined ||
|
||||
(typeof value === "object" && value !== null && value.step !== undefined)
|
||||
);
|
||||
}
|
||||
|
||||
function getAutoStep(value: EntranceTransitionValue): number {
|
||||
const rawStep =
|
||||
typeof value === "object" && value !== null
|
||||
? Number(value.step ?? ENTRANCE_DELAY_STEP)
|
||||
: ENTRANCE_DELAY_STEP;
|
||||
return Number.isFinite(rawStep) ? Math.max(0, rawStep) : ENTRANCE_DELAY_STEP;
|
||||
}
|
||||
|
||||
function getAutoDelay(element: EntranceTransitionElement, step: number): number {
|
||||
const parent = element.parentElement;
|
||||
if (!parent) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const siblings = Array.from(parent.children).filter(
|
||||
(sibling): sibling is HTMLElement => sibling instanceof HTMLElement && AUTO_ELEMENTS.has(sibling)
|
||||
);
|
||||
return Math.max(0, siblings.indexOf(element)) * step;
|
||||
}
|
||||
|
||||
function cleanup(element: EntranceTransitionElement, isUnmounting = false): void {
|
||||
const state = element[STATE_KEY];
|
||||
if (!state) {
|
||||
return;
|
||||
if (state) {
|
||||
if (state.frameId !== undefined) {
|
||||
window.cancelAnimationFrame(state.frameId);
|
||||
}
|
||||
if (state.timerId !== undefined) {
|
||||
window.clearTimeout(state.timerId);
|
||||
}
|
||||
|
||||
element.classList.remove(ACTIVE_CLASS, FROM_CLASS, TO_CLASS);
|
||||
element.style.removeProperty("--entrance-delay");
|
||||
delete element[STATE_KEY];
|
||||
}
|
||||
|
||||
if (state.frameId !== undefined) {
|
||||
window.cancelAnimationFrame(state.frameId);
|
||||
if (isUnmounting) {
|
||||
AUTO_ELEMENTS.delete(element);
|
||||
}
|
||||
if (state.timerId !== undefined) {
|
||||
window.clearTimeout(state.timerId);
|
||||
}
|
||||
|
||||
element.classList.remove(ACTIVE_CLASS, FROM_CLASS, TO_CLASS);
|
||||
element.style.removeProperty("--entrance-delay");
|
||||
delete element[STATE_KEY];
|
||||
}
|
||||
|
||||
/** 为元素添加一次性的渐入上移动画。 */
|
||||
@@ -55,8 +97,15 @@ export const VEntranceTransition: ObjectDirective<HTMLElement, EntranceTransitio
|
||||
}
|
||||
|
||||
const target = element as EntranceTransitionElement;
|
||||
const delay = getDelay(binding.value);
|
||||
target[STATE_KEY] = { delay };
|
||||
const auto = isAuto(binding.value);
|
||||
const step = auto ? getAutoStep(binding.value) : ENTRANCE_DELAY_STEP;
|
||||
const delay = auto ? 0 : getDelay(binding.value);
|
||||
target[STATE_KEY] = { delay, step, auto };
|
||||
if (auto) {
|
||||
AUTO_ELEMENTS.add(target);
|
||||
} else {
|
||||
AUTO_ELEMENTS.delete(target);
|
||||
}
|
||||
setDelay(target, delay);
|
||||
target.classList.add(ACTIVE_CLASS, FROM_CLASS);
|
||||
},
|
||||
@@ -75,6 +124,10 @@ export const VEntranceTransition: ObjectDirective<HTMLElement, EntranceTransitio
|
||||
state.frameId = window.requestAnimationFrame(() => {
|
||||
state.frameId = window.requestAnimationFrame(() => {
|
||||
state.frameId = undefined;
|
||||
if (state.auto) {
|
||||
state.delay = getAutoDelay(target, state.step);
|
||||
setDelay(target, state.delay);
|
||||
}
|
||||
target.classList.remove(FROM_CLASS);
|
||||
target.classList.add(TO_CLASS);
|
||||
state.timerId = window.setTimeout(() => cleanup(target), ENTRANCE_DURATION + state.delay);
|
||||
@@ -88,18 +141,31 @@ export const VEntranceTransition: ObjectDirective<HTMLElement, EntranceTransitio
|
||||
}
|
||||
|
||||
const target = element as EntranceTransitionElement;
|
||||
const auto = isAuto(binding.value);
|
||||
if (auto) {
|
||||
AUTO_ELEMENTS.add(target);
|
||||
} else {
|
||||
AUTO_ELEMENTS.delete(target);
|
||||
}
|
||||
|
||||
const state = target[STATE_KEY];
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
|
||||
state.delay = getDelay(binding.value);
|
||||
state.auto = auto;
|
||||
state.step = auto ? getAutoStep(binding.value) : ENTRANCE_DELAY_STEP;
|
||||
if (state.auto) {
|
||||
state.delay = getAutoDelay(target, state.step);
|
||||
} else {
|
||||
state.delay = getDelay(binding.value);
|
||||
}
|
||||
setDelay(target, state.delay);
|
||||
},
|
||||
|
||||
beforeUnmount(element) {
|
||||
if (element instanceof HTMLElement) {
|
||||
cleanup(element as EntranceTransitionElement);
|
||||
cleanup(element as EntranceTransitionElement, true);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user