yandex-maps-unofficial / vue-yandex-map

Yandex Maps Component for VueJS
MIT License
359 stars 103 forks source link

Plugin doesn't work when I use custom element in vue 3 #328

Closed devmansurov closed 2 years ago

devmansurov commented 2 years ago

I have 2 projects with vue yandex map in vue 3:

  1. Project where work ymaps

In this project I connected plugin like this:

const { createApp } = require('vue');
import App from './App.vue';
import ymapPlugin from 'vue-yandex-maps/dist/vue-yandex-maps.esm.js';

const app = createApp(App);

app.config.isCustomElement = (tag) => tag.startsWith('y'); // <= This is doesn't work
app.use(ymapPlugin);
app.mount('#app');
  1. Project where vue yandex map doesn't work and return error with message:

Uncaught TypeError: Cannot read properties of null (reading 'offsetWidth')

In this project plugin connected like this:

import { defineCustomElement } from './defineCustomElementWithStyles'
import App from './App.ce.vue'
import store from './store'
import router from './router'
import ymapPlugin from 'vue-yandex-maps/dist/vue-yandex-maps.esm.js'

customElements.define(
  'app-root',
  defineCustomElement(App, {
    plugins: [store, router, ymapPlugin],
  })
)

You can see more details going by project link or in stackoverflow

PNKBizz commented 2 years ago

I'll see it later. Thanx

devmansurov commented 2 years ago

I'll see it later. Thanx

I get answer from stackoverflow:

vue-yandex-maps renders a map container with a randomly generated ID that is passed to the ymaps.Map constructor, which later uses it to query the document for the element. Unfortunately, the map container is rendered inside the Shadow DOM of the app-root custom element, which is hidden from document queries. The document.querySelector() thus returns null, and the ymaps.Map code tries to get the size of the container via the null reference, leading to the error you observed.

You would have to patch vue-yandex-maps yourself, or submit a GitHub issue to request a feature change, where you could pass in the map container element (from the custom element's Shadow DOM) instead of an ID. It looks like ymaps.Map already accepts either an element or a string ID, so no other change would be necessary.

Can you say something given this answer from the stackoverflow?