client-v17.js 758 B

12345678910111213141516171819202122
  1. import { createElement } from 'react';
  2. import { render, hydrate, unmountComponentAtNode } from 'react-dom';
  3. import StaticHtml from './static-html.js';
  4. export default (element) =>
  5. (Component, props, { default: children, ...slotted }, { client }) => {
  6. for (const [key, value] of Object.entries(slotted)) {
  7. props[key] = createElement(StaticHtml, { value, name: key });
  8. }
  9. const componentEl = createElement(
  10. Component,
  11. props,
  12. children != null ? createElement(StaticHtml, { value: children }) : children
  13. );
  14. const isHydrate = client !== 'only';
  15. const bootstrap = isHydrate ? hydrate : render;
  16. bootstrap(componentEl, element);
  17. element.addEventListener('astro:unmount', () => unmountComponentAtNode(element), {
  18. once: true,
  19. });
  20. };