client-v5.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { mount } from 'svelte';
  2. import { add_snippet_symbol } from 'svelte/internal';
  3. // Allow a slot to be rendered as a snippet (dev validation only)
  4. const tagSlotAsSnippet = import.meta.env.DEV ? add_snippet_symbol : (s) => s;
  5. export default (element) => {
  6. return async (Component, props, slotted) => {
  7. if (!element.hasAttribute('ssr')) return;
  8. let children = undefined;
  9. let $$slots = undefined;
  10. for (const [key, value] of Object.entries(slotted)) {
  11. if (key === 'default') {
  12. children = createSlotDefinition(key, value);
  13. } else {
  14. $$slots ??= {};
  15. $$slots[key] = createSlotDefinition(key, value);
  16. }
  17. }
  18. const [, destroy] = mount(Component, {
  19. target: element,
  20. props: {
  21. ...props,
  22. children,
  23. $$slots,
  24. },
  25. });
  26. element.addEventListener('astro:unmount', () => destroy(), { once: true });
  27. };
  28. };
  29. function createSlotDefinition(key, children) {
  30. /**
  31. * @param {Comment} $$anchor A comment node for slots in Svelte 5
  32. */
  33. const fn = ($$anchor, _$$slotProps) => {
  34. const parent = $$anchor.parentNode;
  35. const el = document.createElement('div');
  36. el.innerHTML = `<astro-slot${
  37. key === 'default' ? '' : ` name="${key}"`
  38. }>${children}</astro-slot>`;
  39. parent.insertBefore(el.children[0], $$anchor);
  40. };
  41. return tagSlotAsSnippet(fn);
  42. }