Skip to content

Instantly share code, notes, and snippets.

@jaalorsa517
Last active March 2, 2024 16:35
Show Gist options
  • Save jaalorsa517/e2e167bd39f2b7c36cfb12ed895b28b9 to your computer and use it in GitHub Desktop.
Save jaalorsa517/e2e167bd39f2b7c36cfb12ed895b28b9 to your computer and use it in GitHub Desktop.
Script base para incorporar dentro del proyecto Vue, para hacer un Custom Element
import { defineCustomElement, h, createApp, getCurrentInstance } from "vue";
export const vueDefineCustomElement = (component, { plugins = [] } = {}) =>
defineCustomElement({
...component,
setup(props, { emit }) {
const app = createApp(component);
// install plugins
plugins.forEach(app.use);
const inst = getCurrentInstance();
Object.assign(inst.appContext, app._context);
Object.assign(inst.provides, app._context.provides);
//translate events
const toCamelCase = (str) => str.slice(0, 1).toUpperCase() + str.slice(1);
let events = {};
if (inst.emitsOptions) {
events = Object.keys(inst.emitsOptions).reduce((acc, key) => {
acc[`on${toCamelCase(key)}`] = (...args) => emit(key, ...args);
return acc;
}, {});
}
return () =>
h(component, {
...props,
...events,
});
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment