Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(component): apply delay to visibility variants #184

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/components/Motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ export default defineComponent({
}

if (props.delay) {
config.enter.transition = { ...config.enter.transition } || {}
config.enter.transition.delay = parseInt(props.delay as string)
const delayNumber = parseInt(props.delay as string)

// Apply delay to existing variants where applicable
for (const configKey of ['enter', 'visible', 'visibleOnce']) {
if (!config[configKey]) continue

config[configKey].transition ??= {}
config[configKey].transition.delay = delayNumber
}
}

return config
})

Expand Down
45 changes: 12 additions & 33 deletions src/utils/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,29 @@ export function resolveVariants<T extends string>(node: VNode<any, HTMLElement |
}

// Loop on directive prop keys, add them to the local variantsRef if defined
directivePropsKeys.forEach((key) => {
if (key === 'delay') {
if (target && target[key] && typeof target[key] === 'number') {
const delay = target[key]
for (let key of directivePropsKeys) {
if (!target || !target[key]) continue

if (variantsRef && variantsRef.value) {
if (variantsRef.value.enter) {
if (!variantsRef.value.enter.transition) variantsRef.value.enter.transition = {}
if (key === 'delay' && typeof target[key] === 'number') {
// Apply delay to existing variants where applicable
for (const variantKey of ['enter', 'visible', 'visibleOnce'] as const) {
const variantConfig = variantsRef.value[variantKey]

variantsRef.value.enter.transition = {
delay,
...variantsRef.value.enter.transition,
}
}
if (variantConfig == null) continue

if (variantsRef.value.visible) {
if (!variantsRef.value.visible.transition) variantsRef.value.visible.transition = {}

variantsRef.value.visible.transition = {
delay,
...variantsRef.value.visible.transition,
}
}

if (variantsRef.value.visibleOnce) {
if (!variantsRef.value.visibleOnce.transition) variantsRef.value.visibleOnce.transition = {}

variantsRef.value.visibleOnce.transition = {
delay,
...variantsRef.value.visibleOnce.transition,
}
}
}
variantConfig.transition ??= {}
variantConfig.transition.delay = target[key]
}

return
continue
}

// @ts-expect-error - Fix errors later for typescript 5
if (target && target[key] && isObject(target[key])) {
if (isObject(target[key])) {
const prop = target[key]
if (key === 'visible-once') key = 'visibleOnce'
variantsRef.value[key as keyof MotionVariants<T>] = prop
}
})
}
}
}
Loading