Skip to content

Commit cd99fda

Browse files
authored
feat: transition duration shorthand prop (#185)
1 parent 7a77272 commit cd99fda

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

docs/content/2.features/1.directive-usage.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ The `:variants` prop will be combined with all the other native variants propert
3030

3131
The rest of the variants properties can be found on the [Variants](/features/variants) page.
3232

33-
As a shorthand, you can use the `:delay` prop, that allows you to edit the delay from the element props.
33+
### Shorthand props
3434

35-
If you specified `visible`, `visible-once` or `enter` variant, the delay will be applied to each of them.
35+
For convenience we support the following shorthand props which allow you to quickly configure transition properties:
3636

37-
Otherwise, the delay will be applied on the `initial` [variant](/features/variants).
37+
- **`delay`**
38+
- **`duration`**
39+
40+
If you specified a `visible`, `visible-once` or `enter` variant, these shorthand properties will be applied to each of them.
41+
42+
Otherwise, they will be applied on the `initial` [variant](/features/variants) instead.
3843

3944
```vue
4045
<template>
@@ -45,6 +50,7 @@ Otherwise, the delay will be applied on the `initial` [variant](/features/varian
4550
:variants="{ custom: { scale: 2 } }"
4651
:hovered="{ scale: 1.2 }"
4752
:delay="200"
53+
:duration="1200"
4854
/>
4955
</template>
5056
```

src/components/Motion.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ export default defineComponent({
6969
type: [Number, String] as PropType<number | string>,
7070
required: false,
7171
},
72+
duration: {
73+
type: [Number, String] as PropType<number | string>,
74+
required: false,
75+
},
7276
},
7377
setup(props) {
7478
const slots = useSlots()
@@ -106,15 +110,17 @@ export default defineComponent({
106110
...(props.variants || {}),
107111
}
108112

109-
if (props.delay) {
110-
const delayNumber = parseInt(props.delay as string)
113+
for (const transitionKey of ['delay', 'duration'] as const) {
114+
if (!props[transitionKey]) continue
115+
116+
const transitionValueParsed = parseInt(props[transitionKey] as string)
111117

112-
// Apply delay to existing variants where applicable
118+
// Apply transition property to existing variants where applicable
113119
for (const configKey of ['enter', 'visible', 'visibleOnce']) {
114120
if (!config[configKey]) continue
115121

116122
config[configKey].transition ??= {}
117-
config[configKey].transition.delay = delayNumber
123+
config[configKey].transition[transitionKey] = transitionValueParsed
118124
}
119125
}
120126

src/utils/directive.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { isObject } from '@vueuse/core'
22
import type { Ref, VNode } from 'vue'
33
import type { MotionVariants } from '../types'
44

5-
const directivePropsKeys = ['initial', 'enter', 'leave', 'visible', 'visible-once', 'visibleOnce', 'hovered', 'tapped', 'focused', 'delay']
5+
const transitionKeys = ['delay', 'duration'] as const
6+
const directivePropsKeys = ['initial', 'enter', 'leave', 'visible', 'visible-once', 'visibleOnce', 'hovered', 'tapped', 'focused', ...transitionKeys] as const
7+
8+
function isTransitionKey(val: any): val is 'delay' | 'duration' {
9+
return transitionKeys.includes(val)
10+
}
611

712
export function resolveVariants<T extends string>(node: VNode<any, HTMLElement | SVGElement, Record<string, any>>, variantsRef: Ref<MotionVariants<T>>) {
813
// This is done to achieve compat with Vue 2 & 3
@@ -27,15 +32,16 @@ export function resolveVariants<T extends string>(node: VNode<any, HTMLElement |
2732
for (let key of directivePropsKeys) {
2833
if (!target || !target[key]) continue
2934

30-
if (key === 'delay' && typeof target[key] === 'number') {
31-
// Apply delay to existing variants where applicable
35+
if (isTransitionKey(key) && typeof target[key] === 'number') {
36+
// Apply transition property to existing variants where applicable
3237
for (const variantKey of ['enter', 'visible', 'visibleOnce'] as const) {
3338
const variantConfig = variantsRef.value[variantKey]
3439

3540
if (variantConfig == null) continue
3641

3742
variantConfig.transition ??= {}
38-
variantConfig.transition.delay = target[key]
43+
// @ts-expect-error `duration` does not exist on `inertia` type transitions
44+
variantConfig.transition[key] = target[key]
3945
}
4046

4147
continue

0 commit comments

Comments
 (0)