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

feat(component): wrap leave variant internally #239

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 25 additions & 3 deletions src/components/Motion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Component } from '@nuxt/schema'
import type { PropType } from 'vue'

import { defineComponent, h, useSlots } from 'vue'
import { Transition, defineComponent, h, useAttrs, useSlots } from 'vue'
import { variantToStyle } from '../utils/transform'
import { MotionComponentProps, setupMotionComponent } from '../utils/component'

Expand All @@ -13,18 +13,40 @@ export default defineComponent({
type: [String, Object] as PropType<string | Component>,
default: 'div',
},
// TODO: figure out if this is possible using `v-if`, otherwise find better prop name
present: {
type: Boolean,
default: true,
},
},
inheritAttrs: false,
setup(props) {
const slots = useSlots()

const { motionConfig, setNodeInstance } = setupMotionComponent(props)
const { instances, motionConfig, setNodeInstance }
= setupMotionComponent(props)

return () => {
const attrs = useAttrs()
const style = variantToStyle(motionConfig.value.initial || {})
const node = h(props.is, undefined, slots)
const node = h(props.is, attrs, slots)

setNodeInstance(node, 0, style)

// Wrap component in Transition if leave variant is set
if (props.leave) {
const wrapper = h(
Transition,
{
css: false,
onLeave: (_: any, done: any) => instances[0].leave(done),
},
() => [props.present && node],
)

return wrapper
}

return node
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/utils/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,15 @@ export function setupMotionComponent(
const styles = variantToStyle(instances[index].state as Variant)

for (const [key, val] of Object.entries(styles)) {
(el as any).style[key] = val
;(el as any).style[key] = val
}
}

return node
}

return {
instances,
motionConfig,
setNodeInstance,
}
Expand Down
Loading