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 19, 2024
1 parent 7137d0a commit b9b01cb
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
<template>
<h1>Dinamic content:</h1>
<button @click="insert">Insert at random index</button>
<button @click="reset">Reset</button>

<StaggeredFadeAndSlide tag="ul" :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 tag="ul" :stagger="50">
<li key="1">Element to animate</li>
<li key="2">Element to animate</li>
Expand All @@ -7,5 +28,45 @@
</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>
1 change: 1 addition & 0 deletions packages/_vue3-migration-test/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export { default as TestBasePanel } from './panels/test-base-panel.vue';
export { default as TestBaseKeyboardNavigation } from './test-base-keyboard-navigation.vue';
export { default as TestBaseEventsModal } from './modals/test-base-events-modal.vue';
export { default as TestBaseIdModal } from './modals/test-base-id-modal.vue';
export { default as TestSearch } from './test-search.vue';
32 changes: 32 additions & 0 deletions packages/_vue3-migration-test/src/components/test-search.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<template>
<SearchInput />
<ClearSearchInput />
<SearchButton />
<MainScroll>
<ResultsList v-infinite-scroll:main-scroll>
<BaseVariableColumnGrid
style="--x-size-min-width-grid-item: 150px"
class="x-gap-12"
:columns="4"
>
<template #result="{ item: result }">
<MainScrollItem :item="result">
{{ result.name }}
</MainScrollItem>
</template>
</BaseVariableColumnGrid>
</ResultsList>
</MainScroll>
</template>

<script setup lang="ts">
import SearchInput from '../../../x-components/src/x-modules/search-box/components/search-input.vue';
import MainScroll from '../../../x-components/src/x-modules/scroll/components/main-scroll.vue';
import MainScrollItem from '../../../x-components/src/x-modules/scroll/components/main-scroll-item.vue';
import ResultsList from '../../../x-components/src/x-modules/search/components/results-list.vue';
import BaseVariableColumnGrid from '../../../x-components/src/components/base-variable-column-grid.vue';
import ClearSearchInput from '../../../x-components/src/x-modules/search-box/components/clear-search-input.vue';
import SearchButton from '../../../x-components/src/x-modules/search-box/components/search-button.vue';
</script>

<style></style>
5 changes: 3 additions & 2 deletions packages/_vue3-migration-test/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { XComponentsAdapter } from '@empathyco/x-types';
import { Component, configureCompat, createApp } from 'vue';
import { createStore } from 'vuex';
import { platformAdapter } from '../../x-adapter-platform/src/platform.adapter';
import { xPlugin } from '../../x-components/src/plugins/x-plugin';

import App from './App.vue';
import router from './router';
import { facetsXModule, nextQueriesXModule, scrollXModule, searchXModule } from './';
Expand All @@ -25,7 +26,7 @@ if (VUE_COMPAT_MODE === 2) {
});
}

const adapter = {} as XComponentsAdapter;
const adapter = platformAdapter;

const store = createStore({});

Expand Down
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 @@ -31,10 +31,16 @@ import {
TestPartialResultsList,
TestBaseEventsModal,
TestBaseIdModal,
TestStaggeredFadeAndSlide
TestStaggeredFadeAndSlide,
TestSearch
} from './';

const routes = [
{
path: '/',
name: 'Home',
component: TestSearch
},
{
path: '/animate-width',
name: 'AnimateWidth',
Expand Down
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,20 @@
<template>
<StaggeringTransitionGroup
v-on="$listeners"
<!-- eslint-disable vue/attributes-order -->
<TransitionGroup
v-bind="$attrs"
class="x-staggered-fade-and-slide"
:name="name"
v-on="$listeners"
@enter="enter"
@after-enter="afterEnter"
: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 +25,6 @@
*/
export default defineComponent({
name: 'StaggeredFadeAndSlide',
components: { StaggeringTransitionGroup },
inheritAttrs: false,
props: {
/**
Expand All @@ -33,45 +33,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 = 'x-staggered-fade-and-slide-';
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;
}
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 b9b01cb

Please sign in to comment.