Skip to content

Commit

Permalink
refactor(staggered-fade-and-slide): use Vue native staggered transition
Browse files Browse the repository at this point in the history
  • Loading branch information
victorcg88 committed Jul 29, 2024
1 parent 4b57f19 commit 1628cf6
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { default as TestCrossFade } from './test-cross-fade.vue';
export { default as TestFade } from './test-fade.vue';
export { default as TestFadeAndSlide } from './test-fade-and-slide.vue';
export { default as TestAnimationFactory } from './test-animation-factory.vue';
export { default as TestStaggeredFadeAndSlide } from './test-staggered-fade-and-slide.vue';
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<template>
<h1>Dinamic content:</h1>
<button @click="insert">Insert at random index</button>
<button @click="reset">Reset</button>

<StaggeredFadeAndSlide :stagger="500">
<li v-for="item in items" :key="item.id">
{{ item.id }} - {{ item.name }}
<button @click="remove(item)">x</button>
</li>
</StaggeredFadeAndSlide>

<br />
<h1>Animation as prop</h1>
<BaseSuggestions :suggestions="suggestions" :animation="StaggeredFadeAndSlide" :stagger="50">
<template #default="{ suggestion }">
<span>{{ suggestion.query }}</span>
</template>
</BaseSuggestions>

<br />
<h1>Static content:</h1>
<StaggeredFadeAndSlide :stagger="50">
<li key="1">Element to animate</li>
<li key="2">Element to animate</li>
<li key="3">Element to animate</li>
</StaggeredFadeAndSlide>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import StaggeredFadeAndSlide from '../../../../x-components/src/components/animations/staggered-fade-and-slide.vue';
import BaseSuggestions from '../../../../x-components/src/components/suggestions/base-suggestions.vue';
import {
getQuerySuggestionsStub,
createResultStub,
getResultsStub
} from '../../../../x-components/src/__stubs__';
const suggestions = getQuerySuggestionsStub('chip', 5);
const getInitialItems = () => getResultsStub(5);
const items = ref(getInitialItems());
let id = items.value.length + 1;
/**
* Insert a new item at a random index.
*/
function insert() {
const i = Math.round(Math.random() * items.value.length);
items.value.splice(i, 0, createResultStub(`Product ${id++}`));
}
/**
* Reset the list of items.
*/
function reset() {
items.value = getInitialItems();
id = items.value.length + 1;
}
/**
* Remove an item from the list.
*
* @param item - The item to remove.
*/
function remove(item: any) {
const i = items.value.indexOf(item);
if (i > -1) {
items.value.splice(i, 1);
}
}
</script>
8 changes: 7 additions & 1 deletion packages/_vue3-migration-test/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ import {
TestRenderlessExtraParam,
TestAnimationFactory,
TestIcons,
TestDisplayEmitter
TestDisplayEmitter,
TestStaggeredFadeAndSlide
} from './';

const routes = [
Expand Down Expand Up @@ -302,6 +303,11 @@ const routes = [
path: '/display-emitter',
name: 'DisplayEmitter',
component: TestDisplayEmitter
},
{
path: '/staggered-fade-and-slide',
name: 'StaggeredFadeAndSlide',
component: TestStaggeredFadeAndSlide
}
];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<template>
<StaggeringTransitionGroup class="x-staggered-fade-and-slide" :name="name" :appear="appear">
<!-- @slot (Required) Transition-group content -->
<TransitionGroup
@enter="enter"
@after-enter="afterEnter"
v-bind="$attrs"
:appear="appear"
:name="name"
:tag="tag"
>
<slot />
</StaggeringTransitionGroup>
</TransitionGroup>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import StaggeringTransitionGroup from './staggering-transition-group.vue';
import { defineComponent, ref } from 'vue';
import { useDisableAnimation } from './use-disable-animation';
/**
* Renders a transition group wrapping the elements passed in the default slot and animating
* them with a staggered fade and slide animation.
Expand All @@ -18,53 +22,116 @@
*/
export default defineComponent({
name: 'StaggeredFadeAndSlide',
components: { StaggeringTransitionGroup },
inheritAttrs: false,
props: {
/**
* Indicates if the transition must be applied on the initial render of the node.
*/
appear: {
type: Boolean,
default: true
},
/**
* The tag of the node to render to the DOM.
*/
tag: {
type: String,
default: 'div'
},
/**
* The time in ms to stagger each item.
*/
stagger: {
type: Number,
default: 25
}
},
setup: function () {
setup(props) {
/**
* The duration of the transition in ms.
*/
const transitionDuration = 250;
/**
* The counter to keep track of the staggered delay.
*/
const staggerCounter = ref(0);
/**
* The counter to keep track of the animations.
*/
const animationList = ref<HTMLElement[]>([]);
/**
* The name of the animation.
*/
const animationName = 'x-staggered-fade-and-slide-';
const { name } = useDisableAnimation(animationName);
return { name };
const animationName = ref('x-staggered-fade-and-slide');
/**
* The name of the animation.
*/
const { name } = useDisableAnimation(animationName.value);
/**
* Calculate the delay for the next element.
*
* @returns The delay in ms.
*/
function getNextTransitionDelay(): number {
return staggerCounter.value++ * props.stagger;
}
/**
* The enter transition.
*
* @param el
* @param done
*/
function enter(el: HTMLElement, done: () => void) {
animationList.value.push(el);
const delay = getNextTransitionDelay();
el.style.transitionDelay = `${delay}ms`;
setTimeout(() => {
el.style.transitionDelay = '0ms';
done();
}, transitionDuration + delay);
}
/**
* The after enter transition.
*
* @param el
*/
function afterEnter(el: HTMLElement) {
animationList.value = animationList.value.filter(item => item !== el);
if (animationList.value.length === 0) {
staggerCounter.value = 0;
}
}
return {
name,
enter,
afterEnter
};
}
});
</script>

<style lang="scss" scoped>
<style lang="scss">
$transition-duration: 0.25s;
.x-staggered-fade-and-slide {
z-index: 0;
&::v-deep .x-staggered-fade-and-slide {
&--enter-active,
&--leave-active {
transition: $transition-duration ease-out;
transition-property: opacity, transform;
}
&--move {
transition: transform $transition-duration ease-out;
}
&--enter,
&--leave-to {
transform: translate3d(0, 50%, 0);
opacity: 0;
z-index: -1;
}
}
/* 1. Declare transitions */
.x-staggered-fade-and-slide-enter-active,
.x-staggered-fade-and-slide-leave-active {
transition: $transition-duration ease-out;
transition-property: opacity, transform;
}
.x-staggered-fade-and-slide-move {
transition: transform $transition-duration ease-out;
}
/* 2. declare enter from and leave to state */
.x-staggered-fade-and-slide-enter,
.x-staggered-fade-and-slide-enter-from,
.x-staggered-fade-and-slide-leave-to {
opacity: 0;
transform: translate3d(0, 50%, 0);
}
/* 3. ensure leaving items are taken out of layout flow so that moving
animations can be calculated correctly. */
.x-staggered-fade-and-slide-leave-active {
position: absolute;
}
</style>

Expand Down
Loading

0 comments on commit 1628cf6

Please sign in to comment.