Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useTransition.md
1---2category: Animation3---45# useTransition67Transition between values89## Usage1011Define a source value to follow, and when changed the output will transition to the new value. If the source changes while a transition is in progress, a new transition will begin from where the previous one was interrupted.1213```ts14import { TransitionPresets, useTransition } from '@vueuse/core'15import { shallowRef } from 'vue'1617const source = shallowRef(0)1819const output = useTransition(source, {20duration: 1000,21easing: TransitionPresets.easeInOutCubic,22})23```2425Transition easing can be customized using [cubic bezier curves](https://developer.mozilla.org/en-US/docs/Web/CSS/easing-function/cubic-bezier#description).2627```ts28import { useTransition } from '@vueuse/core'29// ---cut---30useTransition(source, {31easing: [0.75, 0, 0.25, 1],32})33```3435The following transitions are available via the `TransitionPresets` constant.3637- [`linear`](https://cubic-bezier.com/#0,0,1,1)38- [`easeInSine`](https://cubic-bezier.com/#.12,0,.39,0)39- [`easeOutSine`](https://cubic-bezier.com/#.61,1,.88,1)40- [`easeInOutSine`](https://cubic-bezier.com/#.37,0,.63,1)41- [`easeInQuad`](https://cubic-bezier.com/#.11,0,.5,0)42- [`easeOutQuad`](https://cubic-bezier.com/#.5,1,.89,1)43- [`easeInOutQuad`](https://cubic-bezier.com/#.45,0,.55,1)44- [`easeInCubic`](https://cubic-bezier.com/#.32,0,.67,0)45- [`easeOutCubic`](https://cubic-bezier.com/#.33,1,.68,1)46- [`easeInOutCubic`](https://cubic-bezier.com/#.65,0,.35,1)47- [`easeInQuart`](https://cubic-bezier.com/#.5,0,.75,0)48- [`easeOutQuart`](https://cubic-bezier.com/#.25,1,.5,1)49- [`easeInOutQuart`](https://cubic-bezier.com/#.76,0,.24,1)50- [`easeInQuint`](https://cubic-bezier.com/#.64,0,.78,0)51- [`easeOutQuint`](https://cubic-bezier.com/#.22,1,.36,1)52- [`easeInOutQuint`](https://cubic-bezier.com/#.83,0,.17,1)53- [`easeInExpo`](https://cubic-bezier.com/#.7,0,.84,0)54- [`easeOutExpo`](https://cubic-bezier.com/#.16,1,.3,1)55- [`easeInOutExpo`](https://cubic-bezier.com/#.87,0,.13,1)56- [`easeInCirc`](https://cubic-bezier.com/#.55,0,1,.45)57- [`easeOutCirc`](https://cubic-bezier.com/#0,.55,.45,1)58- [`easeInOutCirc`](https://cubic-bezier.com/#.85,0,.15,1)59- [`easeInBack`](https://cubic-bezier.com/#.36,0,.66,-.56)60- [`easeOutBack`](https://cubic-bezier.com/#.34,1.56,.64,1)61- [`easeInOutBack`](https://cubic-bezier.com/#.68,-.6,.32,1.6)6263For more complex easing, a custom function can be provided.6465```ts66import { useTransition } from '@vueuse/core'67// ---cut---68function easeOutElastic(n) {69return n === 070? 071: n === 172? 173: (2 ** (-10 * n)) * Math.sin((n * 10 - 0.75) * ((2 * Math.PI) / 3)) + 174}7576useTransition(source, {77easing: easeOutElastic,78})79```8081By default the `source` must be a number, or array of numbers. For more complex values, define a custom `interpolation` function. For example, the following would transition a Three.js rotation.8283```ts84import { useTransition } from '@vueuse/core'85// ---cut---86import { Quaternion } from 'three'8788const source = ref(new Quaternion())8990const output = useTransition(source, {91interpolation: (q1, q2, t) => new Quaternion().slerpQuaternions(q1, q2, t)92})93```9495To control when a transition starts, set a `delay` value. To choreograph behavior around a transition, define `onStarted` or `onFinished` callbacks.9697```ts98import { useTransition } from '@vueuse/core'99// ---cut---100useTransition(source, {101delay: 1000,102onStarted() {103// called after the transition starts104},105onFinished() {106// called after the transition ends107},108})109```110111To stop transitioning, define a boolean `disabled` property. Be aware, this is not the same a `duration` of `0`. Disabled transitions track the source value **_synchronously_**. They do not respect a `delay`, and do not fire `onStarted` or `onFinished` callbacks.112113For even more control, transitions can be executed manually via the `transition` function. This function returns a promise that resolves when the transition is complete. Manual transitions can be cancelled by defining an `abort` function that returns a truthy value.114115```ts116import { transition } from '@vueuse/core'117118await transition(source, from, to, {119abort() {120if (shouldAbort)121return true122}123})124```125126## Type Declarations127128```ts129/**130* Cubic bezier points131*/132export type CubicBezierPoints = [number, number, number, number]133/**134* Easing function135*/136export type EasingFunction = (n: number) => number137/**138* Interpolation function139*/140export type InterpolationFunction<T> = (from: T, to: T, t: number) => T141/**142* Transition options143*/144export interface TransitionOptions<T> extends ConfigurableWindow {145/**146* Manually abort a transition147*/148abort?: () => any149/**150* Transition duration in milliseconds151*/152duration?: MaybeRef<number>153/**154* Easing function or cubic bezier points to calculate transition progress155*/156easing?: MaybeRef<EasingFunction | CubicBezierPoints>157/**158* Custom interpolation function159*/160interpolation?: InterpolationFunction<T>161/**162* Easing function or cubic bezier points to calculate transition progress163* @deprecated The `transition` option is deprecated, use `easing` instead.164*/165transition?: MaybeRef<EasingFunction | CubicBezierPoints>166}167export interface UseTransitionOptions<T> extends TransitionOptions<T> {168/**169* Milliseconds to wait before starting transition170*/171delay?: MaybeRef<number>172/**173* Disables the transition174*/175disabled?: MaybeRef<boolean>176/**177* Callback to execute after transition finishes178*/179onFinished?: () => void180/**181* Callback to execute after transition starts182*/183onStarted?: () => void184}185declare const _TransitionPresets: {186readonly easeInSine: readonly [0.12, 0, 0.39, 0]187readonly easeOutSine: readonly [0.61, 1, 0.88, 1]188readonly easeInOutSine: readonly [0.37, 0, 0.63, 1]189readonly easeInQuad: readonly [0.11, 0, 0.5, 0]190readonly easeOutQuad: readonly [0.5, 1, 0.89, 1]191readonly easeInOutQuad: readonly [0.45, 0, 0.55, 1]192readonly easeInCubic: readonly [0.32, 0, 0.67, 0]193readonly easeOutCubic: readonly [0.33, 1, 0.68, 1]194readonly easeInOutCubic: readonly [0.65, 0, 0.35, 1]195readonly easeInQuart: readonly [0.5, 0, 0.75, 0]196readonly easeOutQuart: readonly [0.25, 1, 0.5, 1]197readonly easeInOutQuart: readonly [0.76, 0, 0.24, 1]198readonly easeInQuint: readonly [0.64, 0, 0.78, 0]199readonly easeOutQuint: readonly [0.22, 1, 0.36, 1]200readonly easeInOutQuint: readonly [0.83, 0, 0.17, 1]201readonly easeInExpo: readonly [0.7, 0, 0.84, 0]202readonly easeOutExpo: readonly [0.16, 1, 0.3, 1]203readonly easeInOutExpo: readonly [0.87, 0, 0.13, 1]204readonly easeInCirc: readonly [0.55, 0, 1, 0.45]205readonly easeOutCirc: readonly [0, 0.55, 0.45, 1]206readonly easeInOutCirc: readonly [0.85, 0, 0.15, 1]207readonly easeInBack: readonly [0.36, 0, 0.66, -0.56]208readonly easeOutBack: readonly [0.34, 1.56, 0.64, 1]209readonly easeInOutBack: readonly [0.68, -0.6, 0.32, 1.6]210}211/**212* Common transitions213*214* @see https://easings.net215*/216export declare const TransitionPresets: Record<217keyof typeof _TransitionPresets,218CubicBezierPoints219> & {220linear: EasingFunction221}222/**223* Transition from one value to another.224*225* @param source226* @param from227* @param to228* @param options229*/230export declare function transition<T>(231source: Ref<T>,232from: MaybeRefOrGetter<T>,233to: MaybeRefOrGetter<T>,234options?: TransitionOptions<T>,235): PromiseLike<void>236/**237* Transition from one value to another.238* @deprecated The `executeTransition` function is deprecated, use `transition` instead.239*240* @param source241* @param from242* @param to243* @param options244*/245export declare function executeTransition<T>(246source: Ref<T>,247from: MaybeRefOrGetter<T>,248to: MaybeRefOrGetter<T>,249options?: TransitionOptions<T>,250): PromiseLike<void>251export declare function useTransition<T extends MaybeRefOrGetter<number>[]>(252source: [...T],253options?: UseTransitionOptions<T>,254): ComputedRef<{255[K in keyof T]: number256}>257export declare function useTransition<T extends MaybeRefOrGetter<number[]>>(258source: T,259options?: UseTransitionOptions<T>,260): ComputedRef<number[]>261export declare function useTransition<T>(262source: MaybeRefOrGetter<T>,263options?: UseTransitionOptions<T>,264): ComputedRef<T>265```266