Skip to content

Commit

Permalink
feat(component): wrap leave variant internally
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Oct 19, 2024
1 parent 12d1020 commit d20d133
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 24 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,17 +13,38 @@ 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 attrs = useAttrs()

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

const instance = 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) => instance.leave(done),
},
() => [props.present && node],
)

setNodeInstance(node, 0, style)
return wrapper
}

return node
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ 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[index]
}

return {
Expand Down

0 comments on commit d20d133

Please sign in to comment.