@muban/muban
Version:
Writing components for server-rendered HTML
25 lines (24 loc) • 1 kB
JavaScript
// TODO move inside the "createApi" ?
export function mount(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
app, component, container, template, data) {
if (!container) {
// eslint-disable-next-line no-console
console.error(`The received container is null, so nothing can be rendered`);
return;
}
if (template) {
const templateResult = template(data || {});
// eslint-disable-next-line no-param-reassign
container.innerHTML = [].concat(templateResult).join('');
}
const rootElement = container.dataset['data-component'] === component.displayName
? container
: container.querySelector(`[data-component="${component.displayName}"]`);
if (!rootElement) {
// eslint-disable-next-line no-console
console.error(`No element found with "data-component" set to "${component.displayName}", unable to render the component.`);
return;
}
return component(rootElement, { app });
}