From a6c61fbeb8744cedf215791059991b9f5d288a1d Mon Sep 17 00:00:00 2001 From: Georges KABBOUCHI Date: Sun, 17 Jan 2021 13:27:54 +0200 Subject: [PATCH] pass tippy instance for the content slot and fix nested components --- package.json | 1 + playground/App.vue | 358 +--------------------- playground/components/Button.vue | 12 + playground/{ => components}/Counter.vue | 4 +- playground/components/Icon.vue | 11 + playground/main.ts | 21 +- playground/pages/Index.vue | 383 ++++++++++++++++++++++++ playground/pages/NestedComponents.vue | 40 +++ src/components/Tippy.ts | 23 +- yarn.lock | 5 + 10 files changed, 492 insertions(+), 366 deletions(-) create mode 100644 playground/components/Button.vue rename playground/{ => components}/Counter.vue (79%) create mode 100644 playground/components/Icon.vue create mode 100644 playground/pages/Index.vue create mode 100644 playground/pages/NestedComponents.vue diff --git a/package.json b/package.json index e38fc3d..3420e70 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "typescript": "^4.1.3", "vue": "^3.0.0", "vue-loader": "^16.0.0-beta.8", + "vue-router": "4", "webpack": "^4.44.1", "webpack-bundle-analyzer": "^3.8.0", "webpack-cli": "^3.3.12", diff --git a/playground/App.vue b/playground/App.vue index bfa576a..f31019e 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -1,359 +1,9 @@ \ No newline at end of file + diff --git a/playground/components/Button.vue b/playground/components/Button.vue new file mode 100644 index 0000000..d81b15d --- /dev/null +++ b/playground/components/Button.vue @@ -0,0 +1,12 @@ + + + \ No newline at end of file diff --git a/playground/Counter.vue b/playground/components/Counter.vue similarity index 79% rename from playground/Counter.vue rename to playground/components/Counter.vue index e17b6e1..0a310b9 100644 --- a/playground/Counter.vue +++ b/playground/components/Counter.vue @@ -1,5 +1,7 @@ \ No newline at end of file diff --git a/playground/main.ts b/playground/main.ts index e51762f..aa47d92 100644 --- a/playground/main.ts +++ b/playground/main.ts @@ -1,10 +1,29 @@ // necessary for webpack /// import { createApp } from 'vue' +import { createRouter, createWebHistory } from 'vue-router' import VueTippy from '../src' - +import "tippy.js/dist/tippy.css"; import App from './App.vue' +import PageIndex from './pages/Index.vue' +import PageNestedComponents from './pages/NestedComponents.vue' +import Counter from './components/Counter.vue' +import UiIcon from "./components/Icon.vue"; + +const router = createRouter({ + history: createWebHistory(), + routes :[ + { path: '/', component: PageIndex }, + { path: '/nested-components', component: PageNestedComponents }, + ] +}) + + const app = createApp(App) +app.component('counter', Counter) +app.component("ui-icon", UiIcon); + +app.use(router) app.use(VueTippy, { defaultProps: { placement: 'right' }, }) diff --git a/playground/pages/Index.vue b/playground/pages/Index.vue new file mode 100644 index 0000000..3c6dc9f --- /dev/null +++ b/playground/pages/Index.vue @@ -0,0 +1,383 @@ + + \ No newline at end of file diff --git a/playground/pages/NestedComponents.vue b/playground/pages/NestedComponents.vue new file mode 100644 index 0000000..6848ddb --- /dev/null +++ b/playground/pages/NestedComponents.vue @@ -0,0 +1,40 @@ + + + diff --git a/src/components/Tippy.ts b/src/components/Tippy.ts index 4123d66..a3c3f7d 100644 --- a/src/components/Tippy.ts +++ b/src/components/Tippy.ts @@ -1,4 +1,4 @@ -import { defineComponent, ref, h, ComponentObjectPropsOptions } from 'vue' +import { defineComponent, ref, h, ComponentObjectPropsOptions, onMounted } from 'vue' import { TippyOptions } from '../types' import { useTippy } from '../composables' import tippy, { DefaultProps } from 'tippy.js' @@ -55,6 +55,7 @@ const TippyComponent = defineComponent({ props, setup(props, { slots }) { const elem = ref() + const contentElem = ref() let options = { ...props } as TippyOptions & { to: String | Element | null | undefined @@ -63,12 +64,6 @@ const TippyComponent = defineComponent({ delete options.to } - if (slots.content != null && typeof slots.content != 'undefined') { - options.content = { - render: () => slots.content!(), - } - } - let target: any = elem if (props.to) { @@ -80,12 +75,20 @@ const TippyComponent = defineComponent({ } const tippy = useTippy(target, options) - return { elem, ...tippy } + + onMounted(() => { + if(slots.content) + tippy.setContent(() => contentElem.value) + }) + + return { elem, contentElem, ...tippy } }, render() { let slot = this.$slots.default ? this.$slots.default() : [] - - return h('span', { ref: 'elem' }, slot) + return h('span', { ref: 'elem' }, this.$slots.content ?[ + slot, + h('span', { ref : 'contentElem' }, this.$slots.content({ tippy : this.tippy})) + ] : slot) }, }) diff --git a/yarn.lock b/yarn.lock index e76fa70..c57c52e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5820,6 +5820,11 @@ vue-loader@^16.0.0-beta.8: hash-sum "^2.0.0" loader-utils "^2.0.0" +vue-router@4: + version "4.0.3" + resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-4.0.3.tgz#8b26050c88b2dec7e27a88835f71046b365823ec" + integrity sha512-AD1OjtVPyQHTSpoRsEGfPpxRQwhAhxcacOYO3zJ3KNkYP/r09mileSp6kdMQKhZWP2cFsPR3E2M3PZguSN5/ww== + vue@^3.0.0: version "3.0.5" resolved "https://registry.yarnpkg.com/vue/-/vue-3.0.5.tgz#de1b82eba24abfe71e0970fc9b8d4b2babdc3fe1"