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 Jun 17, 2024
1 parent 7137d0a commit e2d332c
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
<template>
<h1>Static content:</h1>
<StaggeredFadeAndSlide tag="ul" :stagger="50">
<li key="1">Element to animate</li>
<li key="2">Element to animate</li>
<li key="3">Element to animate</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>Dinamic content:</h1>
<button @click="insert">Insert at random index</button>
<button @click="reset">Reset</button>

<StaggeredFadeAndSlide tag="ul" :stagger="50">
<li v-for="item in items" :key="item" class="item">
{{ item }}
<button @click="remove(item)">x</button>
</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 } from '../../../../x-components/src/__stubs__';
const suggestions = getQuerySuggestionsStub('chip', 5);
const getInitialItems = () => [1, 2, 3, 4, 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, 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>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PromotedsList>
<BannersList>
<NextQueriesList>
<BaseGrid>
<BaseGrid :animation="StaggeredFadeAndSlide">
<template #result="{ item: result }">
{{ result.id }}
</template>
Expand Down Expand Up @@ -36,4 +36,5 @@
import BaseGrid from '../../../x-components/src/components/base-grid.vue';
import LocationProvider from '../../../x-components/src/components/location-provider.vue';
import NextQueriesList from '../../../x-components/src/x-modules/next-queries/components/next-queries-list.vue';
import StaggeredFadeAndSlide from '../../../x-components/src/components/animations/staggered-fade-and-slide.vue';
</script>
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<template>
<StaggeringTransitionGroup
<TransitionGroup
v-on="$listeners"
@enter="enter"
@after-enter="afterEnter"
v-bind="$attrs"
class="x-staggered-fade-and-slide"
:name="name"
:appear="appear"
:name="name"
:tag="tag"
>
<!-- @slot (Required) Transition-group content -->
<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';
/**
Expand All @@ -24,7 +24,6 @@
*/
export default defineComponent({
name: 'StaggeredFadeAndSlide',
components: { StaggeringTransitionGroup },
inheritAttrs: false,
props: {
/**
Expand All @@ -33,45 +32,115 @@
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 = ref('x-staggered-fade-and-slide');
/**
* The name of the animation.
*/
const animationName = 'x-staggered-fade-and-slide-';
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;
}
const { name } = useDisableAnimation(animationName);
/**
* 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 };
return {
name,
enter,
afterEnter
};
}
});
</script>

<style lang="scss" scoped>
<style lang="scss">
$transition-duration: 0.25s;
/* 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 {
z-index: 0;
&::v-deep .x-staggered-fade-and-slide {
&--enter-active,
&--leave-active {
transition: $transition-duration ease-out;
transition-property: opacity, transform;
}
.x-staggered-fade-and-slide-move {
transition: transform $transition-duration ease-out;
}
&--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);
}
&--enter,
&--leave-to {
transform: translate3d(0, 50%, 0);
opacity: 0;
z-index: -1;
}
}
/* 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
5 changes: 5 additions & 0 deletions packages/x-components/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const routes: RouteConfig[] = [
name: 'Home',
component: () => import('./views/home/Home.vue')
},
{
path: '/animations',
name: 'Animations Test',
component: () => import('./views/home/animations-test.vue')
},
{
path: '/products/:id',
name: 'Product page',
Expand Down
Loading

0 comments on commit e2d332c

Please sign in to comment.