Skip to content

Commit

Permalink
use Open/Closed context in Transition component (Vue)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Apr 30, 2021
1 parent fccde28 commit 1529b77
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions packages/@headlessui-vue/src/components/transitions/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { match } from '../../utils/match'
import { Features, render, RenderStrategy } from '../../utils/render'
import { Reason, transition } from './utils/transition'
import { dom } from '../../utils/dom'
import { useOpenClosedProvider, State, useOpenClosed } from '../../internal/open-closed'

type ID = ReturnType<typeof useId>

Expand Down Expand Up @@ -277,9 +278,16 @@ export let TransitionChild = defineComponent({
{ immediate: true }
)
})
// onUpdated(() => executeTransition(() => {}))

provide(NestingContext, nesting)
useOpenClosedProvider(
computed(() =>
match(state.value, {
[TreeStates.Visible]: State.Open,
[TreeStates.Hidden]: State.Closed,
})
)
)

return { el: container, state }
},
Expand Down Expand Up @@ -329,29 +337,42 @@ export let TransitionRoot = defineComponent({
})
},
setup(props) {
let usesOpenClosedState = useOpenClosed()

let show = computed(() => {
if (props.show === null && usesOpenClosedState !== null) {
return match(usesOpenClosedState.value, {
[State.Open]: true,
[State.Closed]: false,
})
}

return props.show
})

watchEffect(() => {
if (![true, false].includes(props.show)) {
if (![true, false].includes(show.value)) {
throw new Error('A <Transition /> is used but it is missing a `:show="true | false"` prop.')
}
})

let state = ref(props.show ? TreeStates.Visible : TreeStates.Hidden)
let state = ref(show.value ? TreeStates.Visible : TreeStates.Hidden)

let nestingBag = useNesting(() => {
state.value = TreeStates.Hidden
})

let initial = { value: true }
let transitionBag = {
show: computed(() => props.show),
show,
appear: computed(() => props.appear || !initial.value),
}

onMounted(() => {
watchEffect(() => {
initial.value = false

if (props.show) {
if (show.value) {
state.value = TreeStates.Visible
} else if (!hasChildren(nestingBag)) {
state.value = TreeStates.Hidden
Expand All @@ -362,6 +383,6 @@ export let TransitionRoot = defineComponent({
provide(NestingContext, nestingBag)
provide(TransitionContext, transitionBag)

return { state }
return { state, show }
},
})

0 comments on commit 1529b77

Please sign in to comment.