{"id":13701482,"url":"https://github.com/karol-f/vue-custom-element","last_synced_at":"2025-10-02T00:31:47.120Z","repository":{"id":40461992,"uuid":"70286414","full_name":"karol-f/vue-custom-element","owner":"karol-f","description":"Vue Custom Element - Web Components' Custom Elements for Vue.js","archived":false,"fork":true,"pushed_at":"2023-04-14T14:16:00.000Z","size":15320,"stargazers_count":1966,"open_issues_count":27,"forks_count":187,"subscribers_count":34,"default_branch":"master","last_synced_at":"2025-01-16T00:41:35.660Z","etag":null,"topics":["custom-elements","vue-custom-element","vuejs","vuejs2","web-components"],"latest_commit_sha":null,"homepage":"https://karol-f.github.io/vue-custom-element/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"vuejs/vue-element","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/karol-f.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-07T22:08:52.000Z","updated_at":"2024-12-11T15:32:28.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/karol-f/vue-custom-element","commit_stats":null,"previous_names":["karol-f/vue-element"],"tags_count":55,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karol-f%2Fvue-custom-element","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karol-f%2Fvue-custom-element/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karol-f%2Fvue-custom-element/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/karol-f%2Fvue-custom-element/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/karol-f","download_url":"https://codeload.github.com/karol-f/vue-custom-element/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234916159,"owners_count":18906643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["custom-elements","vue-custom-element","vuejs","vuejs2","web-components"],"created_at":"2024-08-02T20:01:41.922Z","updated_at":"2025-10-02T00:31:41.513Z","avatar_url":"https://github.com/karol-f.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","积分","Components \u0026 Libraries","Integrations","Integrations [🔝](#readme)"],"sub_categories":["付款","Integrations","Miscellaneous","Payment"],"readme":"## [Finally - official Web Components implementation! - check vuejs/vue-web-component-wrapper](https://github.com/vuejs/vue-web-component-wrapper)\n\n![Vue-custom-element](demo/assets/images/vue-custom-element-logo-text.png)\n\n## Table of contents\n\n- [Demo](#demo)\n- [Installation](#installation)\n- [Description](#description)\n- [Example](#example)\n- [ShadowDOM Example](#shadowdom-example)\n- [Browsers support](#browser-support)\n- [Options](#options)\n- [How does it work?](#how-does-it-work)\n- [Testing](#testing)\n- [Caveats](#caveats)\n\n## Demo\nYou can check out Vue-custom-element demos at **https://karol-f.github.io/vue-custom-element/**\n\n## Installation\n\n#### NPM\n```bash\nnpm install vue-custom-element --save\n```\n\n```javascript\nimport vueCustomElement from 'vue-custom-element'\n\nVue.use(vueCustomElement);\n```\n\nTo build your web-component using vue-cli, you have to use the following command:\n\n```bash\nvue-cli-service build --target lib --name your-component-name src/main.js\n```\n\nNote: the command ```vue-cli-service build --target wc``` does not work, since it using vue's own vue-web-component-wrapper.\n\n#### Direct include\n\nIf you are using Vue globally, just include `vue-custom-element.js` and it will automatically install the `Vue.customElement` method.\n\n```html\n\u003cscript src=\"path/to/vue-custom-element.js\"\u003e\u003c/script\u003e\n```\n\n#### Optional polyfill\nFor cross-browser compatibility (IE9+) use Custom Elements polyfill.\n\n```html\n\u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/document-register-element/1.4.1/document-register-element.js\"\u003e\u003c/script\u003e\n```\n\nor \n\n```\nimport 'document-register-element/build/document-register-element';\n```\n\n## Description\n\n`Vue-custom-element` is a tiny wrapper around Vue components. It provides a seamless way to use Vue components in HTML, plain JavaScript, Vue, React, Angular etc., without manually initialising Vue. It's using power of Web Components' Custom Elements.\n* Works with Vue 0.12.x, 1.x and 2.x\n* Small - 2.7 kb min+gzip, optional polyfill - 5 kb min+gzip\n\n### Why you might need `Vue-custom-element`?\n![Vue-custom-element](demo/assets/images/vue-custom-element-why.png)\n\nIt might be confusing for users to understand the different use cases of Vue components vs Custom Elements .\n \nWhy might you need `Vue-custom-element`? Simple, for your Vue components user's convenience. All they will need to do is include your JavaScript file and then they can:\n\n* include components as HTML tags (e.g. `\u003cmy-component\u003e\u003c/my-component\u003e`) at any time of the document lifecycle. You can use your elements in e.g. SPA application just by including HTML tag - no Vue initialization or JavaScript usage is needed. Custom Elements will auto initialize when mounted into document. You can include them in e.g. Vue, Angular or React projects and browser will take care of detecting it and initialization\n* use a simple API that allows for interacting with underlaying Vue instance by changing attributes, props or listening to events\n* take advantage of features like lazy-loading, that allows for loading components on demand, only when user add them to document\n\n### Features\n\n* **Simplicity** - only `tag-name` and Vue component `object` are needed for `Vue.customElement()` usage\n* **Compatibility** - using the optional polyfills a wide range of browsers is supported, including IE9+, Android and IOS\n* **Full featured** - you can use nesting, HMR, slots, lazy-loading, native Custom Elements callbacks.\n\t* reactive props and HTML attributes\n\t* automatic props casting (numbers, booleans) so they won't be available as strings but proper data types\n\t* listening to Vue component $emit'ed events\n\t* 'default' and 'named' slots are available for passing static content, check out the demo for an example\n\t* Hot Module Replacement for seamless developer experience (unminimized build, Vue 2.x+)\n\t* lazy-loading - you can download a component after it's attached to document. Useful for e.g. UI library authors. Check out the demo for an example\n\t* detect if detached callback is not invoked due to opening vue-custom-element in modal - element is then detached and attached to DOM again. It would be undesirable to destroy it immediately\n* **Custom Elements v1** - compatible with latest specifications. Vue-custom-element will use native implementation if supported\n\nCheck out the demo site to see it in action. \n\n## Example\nFor additional examples and detailed description check the demos page.\n\n###### Custom Element HTML\n``` html\n\u003cwidget-vue prop1=\"1\" prop2=\"string\" prop3=\"true\"\u003e\u003c/widget-vue\u003e\n```\n\n###### JavaScript - register with Vue-custom-element\n``` js\nVue.customElement('widget-vue', {\n  props: [\n    'prop1',\n    'prop2',\n    'prop3'\n  ],\n  data: {\n    message: 'Hello Vue!'\n  },\n  template: '\u003cp\u003e{{ message }}, {{ prop1  }}, {{prop2}}, {{prop3}}\u003c/p\u003e'\n});\n```\n\n###### JavaScript - element API usage\n``` js\ndocument.querySelector('widget-vue').prop2 // get prop value\ndocument.querySelector('widget-vue').prop2 = 'another string' // set prop value\n```\n\nYou can also change `\u003cwidget-vue\u003e` HTML attributes and changes will be instantly reflected.\n\n\n## ShadowDOM Example\nBy default, `vue-custom-element` does not mount underneath a `ShadowDOM`. While this is easier to develop and debug, CSS stylings for the parent can contaminate the component, and stylings for the component can contaminate the parent. This is most prevalent when using prebuilt UI libraries which assume the component is the only content on the page (i.e. Vuetify). A `ShadowDOM` prevents this contamination by isolating the components and stylings within an HTML document fragment.\n\nIn these situations, components should be mounted underneath a shadowDOM using the option\n``` js\nVue.customElement('widget-vue', CustomWidget, {\n  shadow: true,\n  beforeCreateVueInstance(root) {\n    const rootNode = root.el.getRootNode();\n\n    if (rootNode instanceof ShadowRoot) {\n      root.shadowRoot = rootNode;\n    } else {\n      root.shadowRoot = document.head;\n    }\n    return root;\n  },\n});\n```\n\nThe additional `beforeCreateVueInstance` is only required if your Vue component has bundled stylings and you are using `css-modules` with Webpack to bundle (which is most use cases). In addition, if you are using `vue-loader` and `vue-style-loader` plugins with Webpack, you will need to pass the `shadowMode: true` option to the plugins also. This is required so the plugins know they that CSS styles should be attached under the `shadowDOM` and not in the `document.head` (which is the default behavior).\n\n**webpack.config.js example for scss stylings**\n``` js\n{\n    test: /\\.vue$/,\n    use: [\n        {\n            loader: 'vue-loader',\n            options: {\n            \tshadowMode: true\n            }\n        }\n    ]\n},\n{\n    test: /\\.scss$/, //as example I used scss\n    use: [\n        {\n            loader: 'vue-style-loader',\n            options: {\n                shadowMode: true\n            }\n        }\n    ]\n}\n```\n\n**vue.config.js for Vue CLI 3**\n``` js\nfunction enableShadowCss(config) {\n  const configs = [\n    config.module.rule('vue').use('vue-loader'),\n  ];\n  // based on common rules returned by `vue inspect`\n  const ruleSets = ['css', 'postcss', 'scss', 'sass', 'less', 'stylus'];\n  const ruleNames = ['vue-modules', 'vue', 'normal-modules', 'normal'];\n\n  ruleSets.forEach((ruleSet) =\u003e {\n    if (config.module.rules.store.has(ruleSet)) {\n      ruleNames.forEach((rName) =\u003e {\n        if (config.module.rule(ruleSet).oneOfs.store.has(rName)) {\n          if (config.module.rule(ruleSet).oneOf(rName).uses.store.has('vue-style-loader')) {\n            configs.push(config.module.rule(ruleSet).oneOf(rName).use('vue-style-loader'));\n          }\n        }\n      });\n    }\n  });\n  if (!process.env.BUILD_MODE) {\n    process.env.BUILD_MODE = config.store.get('mode');\n  }\n  configs.forEach((c) =\u003e c.tap((options) =\u003e {\n    options.shadowMode = true;\n    return options;\n  }));\n}\n\nmodule.exports = {\n  chainWebpack:\n    (config) =\u003e {\n      enableShadowCss(config);\n    },\n  css: {\n    sourceMap: true,\n    extract: false,\n  },\n};\n```\n\n_Note: for stylings to work, CSS must be bundled in JS and injected at runtime. Otherwise you will need to manually link the CSS under the shadowDOM inside your component (which is obviously an anti-pattern)._\n\nFor additional ShadowDom examples see: https://github.com/bryanvaz/vue-custom-element-shadow-examples\n\n## Browser support\n\n| [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/firefox.png\" alt=\"Firefox\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eFirefox | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome.png\" alt=\"Chrome\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eChrome | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari.png\" alt=\"Safari\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eSafari | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/opera.png\" alt=\"Opera\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eOpera | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari-ios.png\" alt=\"iOS Safari\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eiOS | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome-android.png\" alt=\"Chrome for Android\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eAndroid |\n|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|\n| 63+ | 54+ | 10.1+ | 42+ | 10.3+ | 55+\n\n[Custom Elements v1 support](http://caniuse.com/#feat=custom-elementsv1)\n\n#### With optional polyfills\n\n| [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/edge.png\" alt=\"IE / Edge\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eIE / Edge | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/firefox.png\" alt=\"Firefox\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eFirefox | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome.png\" alt=\"Chrome\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eChrome | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari.png\" alt=\"Safari\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eSafari | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/opera.png\" alt=\"Opera\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eOpera | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/safari-ios.png\" alt=\"iOS Safari\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eiOS | [\u003cimg src=\"https://raw.githubusercontent.com/godban/browsers-support-badges/master/src/images/chrome-android.png\" alt=\"Chrome for Android\" width=\"16px\" height=\"16px\" /\u003e](http://godban.github.io/browsers-support-badges/)\u003c/br\u003eAndroid |\n|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|:---------:|\n| IE9+, Edge| \u0026check;| \u0026check; | \u0026check; | \u0026check; | \u0026check; | \u0026check;\n\n## Options\nAdditional, optional, third parameter to `Vue.customElement()` is options object. You can pass following methods.\n\n'This' in callbacks points to Custom Element's DOM Node.\n\n```javascript\n{\n  // 'constructorCallback' can be triggered multiple times when e.g. vue-router is used\n  constructorCallback() {\n      console.info('constructorCallback', this);\n  },\n\n  // element is mounted/inserted into document\n  connectedCallback() {\n    console.info('connectedCallback', this);\n  },\n\n  // element is removed from document\n  disconnectedCallback() {\n    console.warn('disconnectedCallback', this);\n  },\n\n  // one of element's attributes (Vue instance props) is changed \n  attributeChangedCallback(name, oldValue, value) {\n    console.info('attributeChangedCallback', name, oldValue, value);\n  },\n  \n  // Root component's definition is passed to this hook just before Vue instance creation - so you can modify it\n  beforeCreateVueInstance(RootComponentDefinition) {\n    console.info('beforeCreateVueInstance', RootComponentDefinition);\n    return RootComponentDefinition;\n  },\n  \n  // Vue instance is created\n  vueInstanceCreatedCallback() {\n    console.info('vueInstanceCreatedCallback');\n  },\n  \n  // in case of using vue-custom-element with modals, we destroy  it after defined timeout. Use \"null\" value if you want to manually \"$destroy\" it.\n  destroyTimeout: 3000,\n  \n  // only needed when using lazy-loading - 'props' are not accessible on Custom Element registration so we have to provide them\n  props: [],\n\n  // you can set shadow root for element. Only works if native implementation is available.\n  shadow: false,\n  \n  // you can set CSS that will be available in Shadow DOM.\n  shadowCss: ''\n}\n```\n\nExample options usage:\n\n```javascript\nimport MyElement from './MyElement.vue';\n\nVue.customElement('my-element', MyElement, {\n  shadow: true,\n  shadowCss: `\n  .card {\n     background-color: blue;\n  }`\n});\n```\n\nCallbacks are executed before the lifecycle hooks from Vue component passed to Vue-custom-element. It's a better idea to just use Vue component lifecycle hooks (e.g. `created`, `mounted`, `beforeDestroy`).\n\n## How does it work?\n![Vue-custom-element](demo/assets/images/vue-custom-element-schema.png)\n\nInside HTML tags of the defined custom element, Vue-custom-element will create:\n\n* Proxy component for seamless Hot Module Replacement, using render function for performance (Vue 2.x+) \n* Vue component is passed to Vue-custom-element\n\nCustom Element HTML tag will expose API to interact with underlying Vue component - you can change HTML attributes or props, using JavaScript. \n\n## Testing\n\nFor advanced access, when exposed API is not enough, defined custom element can expose Vue instance via `getVueInstance()` method.\n\n```javascript\nconsole.info(document.querySelector('widget-vue').getVueInstance())\n```\n## Caveats\n\n* custom elements **must** contain a hyphen in its tag name. For example, `my-element` is valid, but `myelement` is not\n* in dev mode Vue will display console warning about element not being registered. It's desirable behaviour as we want to use browser's Custom Elements registration. You can use https://vuejs.org/v2/api/#ignoredElements to get rid of this warnings.\n\n## Contribute\n\n#### Development\n```\nnpm install\nnpm run dev:demo\n```\n\n#### Release\n```\nnpm run build\n```\nThis command will compile `vue-custom-element.js` and docs files.\n\nPlease take a note that `npm run build` will use `config.build.assetsPublicPath`, which is set to Github Pages path in `config/index.js`.\n\n## License\n\n[MIT](http://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarol-f%2Fvue-custom-element","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarol-f%2Fvue-custom-element","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarol-f%2Fvue-custom-element/lists"}