UNPKG

@muban/muban

Version:

Writing components for server-rendered HTML

27 lines (26 loc) 1.11 kB
import { getComponentForElement } from './global'; export function getDirectChildComponents(container) { return Array.from(container.querySelectorAll(`[data-component]`)).filter((element) => { var _a; return ((_a = element.parentElement) === null || _a === void 0 ? void 0 : _a.closest(`[data-component]`)) === container.closest(`[data-component]`); }); } /** * Finds the parent data-component element that is not the element itself. * @param element */ export function getParentComponentElement(element) { var _a, _b; return (_b = (_a = element.parentElement) === null || _a === void 0 ? void 0 : _a.closest(`[data-component]`)) !== null && _b !== void 0 ? _b : null; } /** * Finds the parent component instance that "owns" this element * @param element */ export function findParentComponent(element) { let instance; let parent = element; do { parent = getParentComponentElement(parent) || undefined; instance = parent && getComponentForElement(parent); // console.log('while', instance, parent); } while (!instance && parent); return instance; }