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 bba940d
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 70 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,20 @@
<template>
<StaggeringTransitionGroup class="x-staggered-fade-and-slide" :name="name" :appear="appear">
<!-- @slot (Required) Transition-group content -->
<!-- eslint-disable vue/attributes-order -->
<TransitionGroup
v-bind="$attrs"
@enter="enter"
@after-enter="afterEnter"
: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 +23,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
59 changes: 29 additions & 30 deletions packages/x-components/src/components/base-grid.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<template>
<component
:is="animation"
ref="gridEl"
:style="style"
class="x-base-grid"
:class="cssClasses"
Expand Down Expand Up @@ -114,6 +113,20 @@
};
const xBus = useXBus();
/**
* Number of columns rendered inside the grid.
*/
const renderedColumnsNumber = ref(0);
/**
* Reference to the root element of the grid.
*/
const rootEl = ref<Element | null>(null);
/**
* ResizeObserver instance to keep track of the number of columns rendered inside the grid.
*/
let resizeObserver: { stop: () => void };
/**
* It injects {@link ListItem} provided by an ancestor.
*
Expand All @@ -124,9 +137,6 @@
undefined
);
const gridEl = ref<ElementRef | HTMLElement | null>(null);
let renderedColumnsNumber = ref(0);
/**
* Emits the {@link XEventsTypes.RenderedColumnsNumberChanged}
* event whenever the number of columns rendered inside the grid changes.
Expand All @@ -136,9 +146,7 @@
watch(
renderedColumnsNumber,
() => xBus.emit('RenderedColumnsNumberChanged', renderedColumnsNumber.value),
{
immediate: false
}
{ immediate: false }
);
/**
Expand Down Expand Up @@ -205,49 +213,40 @@
})
);
/**
* Checks if a given value is an `ElementRef` object.
*
* @param value - The value to check.
* @returns `true` if the value is an `ElementRef` object, `false` otherwise.
*
* @internal
*/
const isElementRef = (value: any): value is ElementRef => {
return value && value.$el instanceof HTMLElement;
};
/**
* Updates the number of columns rendered inside the grid.
*
* @internal
*/
const updateRenderedColumnsNumber = (): void => {
const { gridTemplateColumns } = getComputedStyle(
isElementRef(gridEl.value) ? gridEl.value.$el : (gridEl.value as Element)
);
renderedColumnsNumber.value = gridTemplateColumns.split(' ').length;
if (rootEl.value) {
const { gridTemplateColumns } = getComputedStyle(rootEl.value);
renderedColumnsNumber.value = gridTemplateColumns.split(' ').length;
}
};
/**
* Initialises the rendered columns number and sets a ResizeObserver to keep it updated.
*
* @internal
*/
let resizeObserver: UseResizeObserverReturn;
onMounted(() => {
/**
* Using querySelector to get the root element of the grid instead of using the ref directly
* because with the introductions of Fragments, Vue returns every DOM element inside the component.
*/
rootEl.value = document.querySelector('.x-base-grid');
resizeObserver = useResizeObserver(
gridEl as MaybeComputedElementRef,
rootEl as MaybeComputedElementRef,
updateRenderedColumnsNumber
);
});
/**
* Stops the ResizeObserver when the component is unmounted.
*/
onBeforeUnmount(() => resizeObserver?.stop());
return {
gridItems,
cssClasses,
style,
gridEl,
slots
};
}
Expand Down
Loading

0 comments on commit bba940d

Please sign in to comment.