Getting Started with vanilla-resize-box

<vn-resize-box> is a container the user resizes by dragging its bottom-right corner. Each step shows the running example and its whole source, a complete page you can copy.

What you need. One script, vanilla-resize-box.bundle.js, from the build's dist/vanilla-components/latest/vanilla-resize-box/ folder. It adds its own stylesheet to the page.

1. A resizable box

Put the content inside the box, as one child. width and height set the starting size; a bare number is pixels. Drag the corner to resize it; double-click the corner to fill the space around it, and again to restore it.

Example: a resizable boxOpen in a new tab ↗
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>A resizable box</title>
  <style>
    html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
    body { box-sizing: border-box; padding: 16px; }
    .note { box-sizing: border-box; height: 100%; padding: 12px; border: 1px solid #c8cdd6; background: #f4f6fa; }
  </style>
</head>
<body>
  <!-- width and height: the starting size. Drag the bottom-right corner to resize. -->
  <vn-resize-box width="320" height="160">
    <div class="note">Drag the bottom-right corner to resize this box. Double-click the corner to maximize it.</div>
  </vn-resize-box>

  <script src="path/to/vanilla-resize-box/vanilla-resize-box.bundle.js"></script>
</body>
</html>

2. Size limits

min-width, min-height, max-width and max-height keep the box within a range while the user drags. The box fires vn-resize-move on every frame of a drag, with the new size in event.detail, and vn-resize-end when the user lets go.

Example: size limitsOpen in a new tab ↗
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Size limits</title>
  <style>
    html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
    body { box-sizing: border-box; padding: 16px; }
    .note { box-sizing: border-box; height: 100%; padding: 12px; border: 1px solid #c8cdd6; background: #f4f6fa; }
  </style>
</head>
<body>
  <!-- min-* and max-*: the box never gets smaller or larger than these. -->
  <vn-resize-box id="box" width="320" height="160"
                 min-width="200" min-height="100" max-width="480" max-height="240">
    <div class="note" id="size">320 × 160</div>
  </vn-resize-box>

  <script src="path/to/vanilla-resize-box/vanilla-resize-box.bundle.js"></script>
  <script type="module">
    // vn-resize-move fires on every frame of a drag, with the new size.
    document.getElementById('box').addEventListener('vn-resize-move', (e) => {
      document.getElementById('size').textContent =
        Math.round(e.detail.width) + ' × ' + Math.round(e.detail.height);
    });
  </script>
</body>
</html>

3. A resizable grid

A <vn-grid> inside the box fills it and follows its size. The corner handle takes the grid's theme, with no attribute of its own.

Example: a resizable gridOpen in a new tab ↗
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>A resizable grid</title>
  <style>
    html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
    body { box-sizing: border-box; padding: 16px; }
    vn-grid { display: block; height: 100%; }
  </style>
</head>
<body>
  <!-- The box's handle follows the grid's theme; the grid fills the box. -->
  <vn-resize-box width="440" height="220" min-width="260" min-height="140">
    <vn-grid id="people">
      <vn-grid-column kind="data" column-key="id" header="ID" type="number" width="60"></vn-grid-column>
      <vn-grid-column kind="data" column-key="name" header="Name" type="string" width="160"></vn-grid-column>
      <vn-grid-column kind="data" column-key="city" header="City" type="string" width="130"></vn-grid-column>
    </vn-grid>
  </vn-resize-box>

  <script src="path/to/vanilla-grid/vanilla-grid.bundle.js"></script>
  <script src="path/to/vanilla-resize-box/vanilla-resize-box.bundle.js"></script>
  <script type="module">
    await window.VanillaGridReady;

    const rows = [
      { id: 1, name: 'Ada Lovelace', city: 'London' },
      { id: 2, name: 'Alan Turing', city: 'Manchester' },
      { id: 3, name: 'Grace Hopper', city: 'New York' },
      { id: 4, name: 'Edsger Dijkstra', city: 'Austin' },
      { id: 5, name: 'Barbara Liskov', city: 'Boston' },
      { id: 6, name: 'Donald Knuth', city: 'Stanford' },
      { id: 7, name: 'Margaret Hamilton', city: 'Boston' },
      { id: 8, name: 'Tim Berners-Lee', city: 'Geneva' },
    ];

    const grid = document.getElementById('people');
    grid.setDataManager(new StaticDataManager({ rows }));
    grid.initializeGrid();
    await grid.loadRowsAsync();
  </script>
</body>
</html>

Next steps