| 6 | const instanceToContextMap = new WeakMap<UI5Element, ReturnType<typeof createContext<UI5Element>>>(); |
| 7 | |
| 8 | const jsxRenderer: Renderer = (instance: UI5Element, container: HTMLElement | DocumentFragment) => { |
| 9 | let ctx = instanceToContextMap.get(instance); |
| 10 | if (!ctx) { |
| 11 | // create a context holding the element that is being rendered |
| 12 | // this contect will be used when adding event listeners - they all will be bound to this element |
| 13 | ctx = createContext(instance); |
| 14 | instanceToContextMap.set(instance, ctx); |
| 15 | } |
| 16 | |
| 17 | const templateResult = instance.render(); |
| 18 | const vnode = jsx(ctx.Provider, { value: instance, children: templateResult }); |
| 19 | |
| 20 | if (instance.__shouldHydrate) { |
| 21 | // Remove all server-generated style elements for component styles |
| 22 | // to avoid conflicts or duplication with styles added through adopted style sheets. |
| 23 | instance.shadowRoot?.querySelectorAll("style").forEach(style => style.remove()); |
| 24 | hydrate(vnode, container); |
| 25 | instance.__shouldHydrate = false; |
| 26 | } else { |
| 27 | render(vnode, container); |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | export default jsxRenderer; |