A codesnippet which you can use to demonstrate APIs, patterns, bugs, or other techniques. (Note that the code is run in an iframe and will have reduced permissions, including only readonly access to the Hyperdrive API.)

Source

<!doctype html5>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/.ui/ui.css">
  </head>
  <body>
    <main>
      <section>
        <header>index.js</header>
        <div class="editor" id="js"></div>
      </section>
      <section>
        <header>index.html</header>
        <div class="editor" id="html"></div>
      </section>
      <section>
        <header>index.css</header>
        <div class="editor" id="css"></div>
      </section>
      <section>
        <header>result</header>
        <div id="result"></div>
      </section>
    </main>
    <script src="/.ui/ace/ace.js"></script>
    <script type="module" src="/.ui/ui.js"></script>
  </body>
</html>
var editors = {
  js: ace.edit('js'),
  css: ace.edit('css'),
  html: ace.edit('html')
}
window.editors = editors
editors.js.session.setMode('ace/mode/javascript')
editors.css.session.setMode('ace/mode/css')
editors.html.session.setMode('ace/mode/html')
for (let k in editors) {
  editors[k].session.setUseWorker(false)
  editors[k].session.setTabSize(2)
  editors[k].session.setUseSoftTabs(true)
}

var result = document.getElementById('result')
var iframe = document.createElement('iframe')
iframe.setAttribute('sandbox', 'allow-scripts')

setup()

async function setup () {
  var info = await beaker.hyperdrive.getInfo()

  editors.js.setValue(await beaker.hyperdrive.readFile('/index.js').catch(e => ''))
  editors.css.setValue(await beaker.hyperdrive.readFile('/index.css').catch(e => ''))
  editors.html.setValue(await beaker.hyperdrive.readFile('/index.html').catch(e => ''))
  for (let k in editors) {
    editors[k].selection.moveTo(0,0)
  }
  result.append(iframe)

  for (let k in editors) {
    editors[k].session.on('change', debounce(e => onChangeEditor(k), 1e3))
  }

  runSnippet()
}

function runSnippet () {
  iframe.src = `data:text/html;base64,${btoa(`
    <style>
      ${editors.css.getValue()}
    </style>
    ${editors.html.getValue()}
    <script>
      window.snippetUrl = "${window.location.toString()}";
      ${editors.js.getValue()}
    </script>
  `)}`
}

function debounce (fn, timeout) {
  var timer
  return () => {
    if (timer) clearTimeout(timer)
    timer = setTimeout(fn, timeout)
  }
}

async function onChangeEditor (id) {
  var editor = editors[id]
  var filename = `/index.${id}`
  var current = await beaker.hyperdrive.readFile(filename).catch(e => '')
  if (current !== editor.getValue()) {
    runSnippet()
    beaker.hyperdrive.writeFile(filename, editor.getValue()).catch(e => undefined)
  }
}
body {
  --border-color: #aab;
  --header-background: #f3f3f8;
  --header-height: 28px;

  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

main {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  width: 100vw;
  height: 100vh;
}

main section {
  position: relative;
  min-width: 0;
  min-height: 0;
}

main section:nth-child(1) {
  border-right: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
}

main section:nth-child(2) {
  border-bottom: 1px solid var(--border-color);
}

main section:nth-child(3) {
  border-right: 1px solid var(--border-color);
}

main section header {
  background: var(--header-background);
  border-bottom: 1px solid #dde;
  padding: 0 10px;
  height: var(--header-height);
  line-height: var(--header-height);
  box-sizing: border-box;
  font-weight: bold;
  letter-spacing: 0.5px;
  font-size: 13px;
}

.editor {
  position: absolute;
  top: var(--header-height);
  left: 0;
  bottom: 0;
  right: 0;
}

iframe {
  width: 100%;
  height: calc(100% - var(--header-height));
  border: 0;
  overflow: auto;
}
MIT License

Copyright (c) 2020 Blue Link Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.