demo.html 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. {{ $uniq := .Inner | htmlEscape | base64Encode | truncate 15 "" }}
  2. <div class="demo-container">
  3. {{ if .Get "caption" }}
  4. <figure role="group" aria-labelledby="caption-{{ $uniq }}">
  5. {{ end }}
  6. <div class="demo" id="demo-{{ $uniq }}"></div>
  7. {{ if .Get "caption" }}
  8. <figcaption id="caption-{{ $uniq }}">{{ .Get "caption" }}</figcaption>
  9. {{ end }}
  10. {{ if .Get "caption" }}
  11. </figure>
  12. {{ end }}
  13. <template id="template-{{ $uniq }}">
  14. {{ .Inner }}
  15. </template>
  16. <script>
  17. (function() {
  18. var root = document.getElementById('demo-{{ $uniq }}');
  19. if (document.head.attachShadow) {
  20. root.attachShadow({mode: 'open'});
  21. var template = document.getElementById('template-{{ $uniq }}');
  22. var script = template.content.querySelector('script');
  23. if (script) {
  24. script.textContent = `(function() { var demo = document.getElementById(\'demo-{{ $uniq }}\').shadowRoot; ${script.textContent} })()`
  25. }
  26. root.shadowRoot.appendChild(document.importNode(template.content, true));
  27. } else {
  28. root.innerHTML = '<p class="site-error"><strong style="font-weight:bold">Site error:</strong> Shadow DOM is needed to display encapsulated demos. The browser does not have an issue with the demo code itself.</p>';
  29. }
  30. })();
  31. </script>
  32. </div>