Skip to content

Commit be1f3a4

Browse files
committed
feat(flip): add enabled
- resolve #7
1 parent baa7abe commit be1f3a4

1 file changed

Lines changed: 59 additions & 51 deletions

File tree

src/Flip.tsx

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type ArrayOr<T> = T | T[];
2626
export interface FlipProps {
2727
id: string;
2828

29+
/* general props */
30+
enabled?: boolean;
31+
2932
/* animation props */
3033
duration?: number;
3134
easing?: string;
@@ -70,6 +73,7 @@ export const Flip = (props: FlipProps) => {
7073
local,
7174
] = splitProps(
7275
mergeProps({
76+
enabled: true,
7377
duration: defaultConfig.duration,
7478
easing: defaultConfig.easing,
7579
preserve: defaultConfig.preserve,
@@ -264,6 +268,8 @@ export const Flip = (props: FlipProps) => {
264268
};
265269

266270
const flip = () => {
271+
if (!local.enabled) return;
272+
267273
const childElement = child();
268274
if (!childElement) return;
269275

@@ -339,59 +345,61 @@ export const Flip = (props: FlipProps) => {
339345
const afterElement = childElement.nextElementSibling;
340346
const parentElement = childElement.parentElement;
341347

342-
queueMicrotask(() => {
343-
runWithOwner(owner, () => {
344-
if (exitClassName && parentElement) {
345-
const childElement = child();
346-
if (!childElement) return;
347-
348-
animation?.cancel();
349-
animation = null;
350-
childElement.classList.add(exitClassName);
351-
352-
if (afterElement) afterElement.insertAdjacentElement('beforebegin', childElement);
353-
else if (beforeElement) beforeElement.insertAdjacentElement('afterend', childElement);
354-
else parentElement.append(childElement);
355-
356-
const lastState = captureState(childElement, properties());
357-
const rect: Required<DOMRectInit> = {
358-
x: lastState.rect.x,
359-
y: lastState.rect.y,
360-
width: lastState.rect.width,
361-
height: lastState.rect.height,
362-
};
363-
364-
const isNewStatePositionAbsolute = newState.position === 'absolute' || newState.position === 'fixed';
365-
const isLastStatePositionAbsolute = lastState.position === 'absolute' || lastState.position === 'fixed';
366-
const options = {
367-
biasX: 0,
368-
biasY: 0,
369-
biasWidth: 1,
370-
biasHeight: 1,
371-
};
372-
373-
const isPositionPreserve = timingProps.preserve === 'position' || timingProps.preserve === 'all';
374-
const isScalePreserve = timingProps.preserve === 'scale' || timingProps.preserve === 'all';
375-
if (isNewStatePositionAbsolute && !isLastStatePositionAbsolute) {
376-
if (isPositionPreserve) options.biasX = lastState.rect.x - newState.rect.x;
377-
if (isPositionPreserve) options.biasY = lastState.rect.y - newState.rect.y;
378-
if (isScalePreserve) options.biasWidth = lastState.rect.width / newState.rect.width;
379-
if (isScalePreserve) options.biasHeight = lastState.rect.height / newState.rect.height;
348+
if (local.enabled) {
349+
queueMicrotask(() => {
350+
runWithOwner(owner, () => {
351+
if (exitClassName && parentElement) {
352+
const childElement = child();
353+
if (!childElement) return;
354+
355+
animation?.cancel();
356+
animation = null;
357+
childElement.classList.add(exitClassName);
358+
359+
if (afterElement) afterElement.insertAdjacentElement('beforebegin', childElement);
360+
else if (beforeElement) beforeElement.insertAdjacentElement('afterend', childElement);
361+
else parentElement.append(childElement);
362+
363+
const lastState = captureState(childElement, properties());
364+
const rect: Required<DOMRectInit> = {
365+
x: lastState.rect.x,
366+
y: lastState.rect.y,
367+
width: lastState.rect.width,
368+
height: lastState.rect.height,
369+
};
370+
371+
const isNewStatePositionAbsolute = newState.position === 'absolute' || newState.position === 'fixed';
372+
const isLastStatePositionAbsolute = lastState.position === 'absolute' || lastState.position === 'fixed';
373+
const options = {
374+
biasX: 0,
375+
biasY: 0,
376+
biasWidth: 1,
377+
biasHeight: 1,
378+
};
379+
380+
const isPositionPreserve = timingProps.preserve === 'position' || timingProps.preserve === 'all';
381+
const isScalePreserve = timingProps.preserve === 'scale' || timingProps.preserve === 'all';
382+
if (isNewStatePositionAbsolute && !isLastStatePositionAbsolute) {
383+
if (isPositionPreserve) options.biasX = lastState.rect.x - newState.rect.x;
384+
if (isPositionPreserve) options.biasY = lastState.rect.y - newState.rect.y;
385+
if (isScalePreserve) options.biasWidth = lastState.rect.width / newState.rect.width;
386+
if (isScalePreserve) options.biasHeight = lastState.rect.height / newState.rect.height;
387+
}
388+
if (!isNewStatePositionAbsolute && isLastStatePositionAbsolute) {
389+
if (isPositionPreserve) options.biasX = newState.rect.x - lastState.rect.x + (newState.rect.width - lastState.rect.width) / 2;
390+
if (isPositionPreserve) options.biasY = newState.rect.y - lastState.rect.y + (newState.rect.height - lastState.rect.height) / 2;
391+
if (isScalePreserve) options.biasWidth = newState.rect.width / lastState.rect.width;
392+
if (isScalePreserve) options.biasHeight = newState.rect.height / lastState.rect.height;
393+
}
394+
lastState.rect = DOMRect.fromRect(rect);
395+
396+
animate(newState, lastState, options)?.addEventListener('finish', () => {
397+
childElement.remove();
398+
});
380399
}
381-
if (!isNewStatePositionAbsolute && isLastStatePositionAbsolute) {
382-
if (isPositionPreserve) options.biasX = newState.rect.x - lastState.rect.x + (newState.rect.width - lastState.rect.width) / 2;
383-
if (isPositionPreserve) options.biasY = newState.rect.y - lastState.rect.y + (newState.rect.height - lastState.rect.height) / 2;
384-
if (isScalePreserve) options.biasWidth = newState.rect.width / lastState.rect.width;
385-
if (isScalePreserve) options.biasHeight = newState.rect.height / lastState.rect.height;
386-
}
387-
lastState.rect = DOMRect.fromRect(rect);
388-
389-
animate(newState, lastState, options)?.addEventListener('finish', () => {
390-
childElement.remove();
391-
});
392-
}
400+
});
393401
});
394-
});
402+
}
395403

396404
setTimeout(() => { // HACK: nextFrame
397405
runWithOwner(owner, () => {

0 commit comments

Comments
 (0)