Skip to content

Commit

Permalink
fix(ui/menu): support ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Jan 6, 2022
1 parent eaf4d68 commit c50e26f
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions packages/varlet-ui/src/menu/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { defineComponent, ref, computed, watch, onMounted, onUnmounted, Transition, Teleport, nextTick } from 'vue'
import {
defineComponent,
ref,
computed,
watch,
onMounted,
onUnmounted,
Transition,
Teleport,
nextTick,
TeleportProps,
} from 'vue'
import { props } from './props'
import { getLeft, getTop, toSizeUnit } from '../utils/elements'
import { useZIndex } from '../context/zIndex'
Expand All @@ -15,6 +26,7 @@ export default defineComponent({
setup(props, { slots }) {
const host: Ref<null | HTMLElement> = ref(null)
const menu: Ref<null | HTMLElement> = ref(null)
const to: Ref<TeleportProps['to']> = ref()
const top: Ref<number> = ref(0)
const left: Ref<number> = ref(0)
const { zIndex } = useZIndex(() => props.show, 1)
Expand Down Expand Up @@ -89,7 +101,16 @@ export default defineComponent({
}
)

watch(
() => props.teleport,
(newValue) => {
to.value = newValue
}
)

onMounted(() => {
// Synchronously transfer the state to the mount hook to support server-side rendering to prevent hydration errors
to.value = props.teleport
resize()

document.addEventListener('click', handleMenuClose)
Expand All @@ -107,8 +128,8 @@ export default defineComponent({
<div class="var-menu" ref={host} onClick={handleClick}>
{slots.default?.()}

{props.teleport ? (
<Teleport to={props.teleport} disabled={disabled.value}>
{to.value ? (
<Teleport to={to.value} disabled={disabled.value}>
{renderTransition()}
</Teleport>
) : (
Expand Down

0 comments on commit c50e26f

Please sign in to comment.