client.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { createElement, startTransition } from 'react';
  2. import { createRoot, hydrateRoot } from 'react-dom/client';
  3. import StaticHtml from './static-html.js';
  4. function isAlreadyHydrated(element) {
  5. for (const key in element) {
  6. if (key.startsWith('__reactContainer')) {
  7. return key;
  8. }
  9. }
  10. }
  11. function createReactElementFromDOMElement(element) {
  12. let attrs = {};
  13. for (const attr of element.attributes) {
  14. attrs[attr.name] = attr.value;
  15. }
  16. // If the element has no children, we can create a simple React element
  17. if (element.firstChild === null) {
  18. return createElement(element.localName, attrs);
  19. }
  20. return createElement(
  21. element.localName,
  22. attrs,
  23. Array.from(element.childNodes)
  24. .map((c) => {
  25. if (c.nodeType === Node.TEXT_NODE) {
  26. return c.data;
  27. } else if (c.nodeType === Node.ELEMENT_NODE) {
  28. return createReactElementFromDOMElement(c);
  29. } else {
  30. return undefined;
  31. }
  32. })
  33. .filter((a) => !!a)
  34. );
  35. }
  36. function getChildren(childString, experimentalReactChildren) {
  37. if (experimentalReactChildren && childString) {
  38. let children = [];
  39. let template = document.createElement('template');
  40. template.innerHTML = childString;
  41. for (let child of template.content.children) {
  42. children.push(createReactElementFromDOMElement(child));
  43. }
  44. return children;
  45. } else if (childString) {
  46. return createElement(StaticHtml, { value: childString });
  47. } else {
  48. return undefined;
  49. }
  50. }
  51. export default (element) =>
  52. (Component, props, { default: children, ...slotted }, { client }) => {
  53. if (!element.hasAttribute('ssr')) return;
  54. const renderOptions = {
  55. identifierPrefix: element.getAttribute('prefix'),
  56. };
  57. for (const [key, value] of Object.entries(slotted)) {
  58. props[key] = createElement(StaticHtml, { value, name: key });
  59. }
  60. const componentEl = createElement(
  61. Component,
  62. props,
  63. getChildren(children, element.hasAttribute('data-react-children'))
  64. );
  65. const rootKey = isAlreadyHydrated(element);
  66. // HACK: delete internal react marker for nested components to suppress aggressive warnings
  67. if (rootKey) {
  68. delete element[rootKey];
  69. }
  70. if (client === 'only') {
  71. return startTransition(() => {
  72. const root = createRoot(element);
  73. root.render(componentEl);
  74. element.addEventListener('astro:unmount', () => root.unmount(), { once: true });
  75. });
  76. }
  77. startTransition(() => {
  78. const root = hydrateRoot(element, componentEl, renderOptions);
  79. root.render(componentEl);
  80. element.addEventListener('astro:unmount', () => root.unmount(), { once: true });
  81. });
  82. };