From 56ddee444ad1203bc63ab52a6d658d431ae97b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20K=C3=BCgler?= Date: Mon, 31 Jul 2023 00:42:52 +0200 Subject: [PATCH] Extract content slot rendering The was used twice and we need this code only once, simlifying the actual rendering returns. --- src/components/Tippy.ts | 47 ++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/components/Tippy.ts b/src/components/Tippy.ts index 313faad..3ef2fd6 100644 --- a/src/components/Tippy.ts +++ b/src/components/Tippy.ts @@ -115,12 +115,13 @@ const TippyComponent = defineComponent({ } const tippy = useTippy(target, getOptions()) + const contentSlot = slots.content onMounted(() => { mounted.value = true nextTick(() => { - if (slots.content) + if (contentSlot) tippy.setContent(() => contentElem.value) }) }) @@ -132,7 +133,7 @@ const TippyComponent = defineComponent({ watch(() => props, () => { tippy.setProps(getOptions()) - if (slots.content) + if (contentSlot) tippy.setContent(() => contentElem.value) }, { deep: true }) @@ -149,41 +150,33 @@ const TippyComponent = defineComponent({ const slot = slots.default ? slots.default(exposed) : [] const contentTag = typeof props.contentTag === 'string' ? props.contentTag as string : props.contentTag + const content = contentSlot + ? h( + contentTag, + { + ref: contentElem, + style: { display: mounted.value ? 'inherit' : 'none' }, + class: props.contentClass, + }, + contentSlot(exposed) + ) + : null if (!props.tag) { const trigger = h(slot[0] as any, { ref: elem, 'data-v-tippy': '' }); - return slots.content ? - [ - trigger, h( - contentTag, - { - ref: contentElem, - style: { display: mounted.value ? 'inherit' : 'none' }, - class: props.contentClass - }, - slots.content(exposed) - ) - ] - : trigger + return content ? [trigger, content] : trigger } const tag = typeof props.tag === 'string' ? props.tag as string : props.tag - return h(tag, { ref: elem, 'data-v-tippy': '' }, slots.content ? [ - slot, - h( - contentTag, - { - ref: contentElem, - style: { display: mounted.value ? 'inherit' : 'none' }, - class: props.contentClass - }, - slots.content(exposed) - ), - ] : slot) + return h( + tag, + { ref: elem, 'data-v-tippy': '' }, + content ? [slot, content] : slot + ) } }, })