Integration Guide
This document shows how to integrate the <vn-resize-box> web component into a host application, including script loading, attribute configuration, event handling, and styling.
1. Script Loading
Include the component script before your application code:
<script src="path/to/vanilla-resize-box/vanilla-resize-box.js"></script>
The component automatically injects its stylesheet (vanilla-resize-box.css) into <head>. No manual <link> tag is needed.
Note: The script resolves CSS paths relative to its own location. Do not move the
.jsfile without also moving the.cssfile.
2. Basic HTML Integration
2.1 Wrapping Any Content
<vn-resize-box width="600" height="400" min-width="200" min-height="150">
<div id="myContent">
<p>Drag the bottom-right corner to resize this container.</p>
</div>
</vn-resize-box>
2.2 Wrapping a Vanilla-Grid
<vn-resize-box id="gridContainer" min-width="360" min-height="220">
<vn-grid id="myGrid" theme="carbon">
<vn-grid-column kind="data" column-key="Name" header="Name" type="string"></vn-grid-column>
<vn-grid-column kind="data" column-key="Email" header="Email" type="string"></vn-grid-column>
</vn-grid>
</vn-resize-box>
The <vn-grid> component uses an internal ResizeObserver to react to size changes, so it automatically relays content when the resize box dimensions change.
The box also follows the grid's theme: with no theme attribute of its own it takes the theme of the first <vn-grid> inside it, reflects it as data-theme, and restyles its drag handle to match (here, Carbon's staircase of squares). A live switch (grid.theme = 'carbon-dark') is followed through the grid's bubbling vn-grid-attribute-changed event, with no host code. A box that wraps no grid shows the default handle unless it carries theme.
2.3 Wrapping a Gantt
A <vn-gantt> resizes inside the box the same way: its task list and its timeline follow the new size through their own ResizeObservers, and the lanes stay level with their rows while the drag runs. The box follows the Gantt's theme too, with no host code: the Gantt's embedded task grid carries the Gantt's theme (Gantt Web Component §4), so it is the first <vn-grid> the box finds, and a setTheme() on the Gantt reaches the box through that grid's vn-grid-attribute-changed. The project-tasks-js sample wraps its toolbar and its Gantt this way.
3. Attribute Configuration
All attributes are optional. When not set, the component falls back to sensible defaults.
| Attribute | Default | Description |
|---|---|---|
width |
parent width | Initial width — any CSS length (600, 600px, 100%, 60vw); a bare number means px |
height |
parent height | Initial height — same syntax as width |
min-width |
100 |
Minimum width in px — bare number or explicit px only |
min-height |
100 |
Minimum height in px — bare number or explicit px only |
max-width |
parent-relative | Maximum width — same syntax as width |
max-height |
parent-relative | Maximum height — same syntax as width |
theme |
the wrapped grid's theme, else default |
Handle theme name; set it to override the grid's |
The min-* attributes are px-only because they feed the drag clamp, which is arithmetic on pixels; a relative value such as 50% is rejected and the default (100) applies. Removing any of these attributes drops the inline style it wrote, handing that dimension back to CSS.
Example with full constraints:
<vn-resize-box
width="800"
height="500"
min-width="300"
min-height="200"
max-width="1200"
max-height="800">
...
</vn-resize-box>
4. Listening to Resize Events
const box = document.getElementById('gridContainer');
box.addEventListener('vn-resize-start', () => {
console.log('Resize started');
});
box.addEventListener('vn-resize-move', (e) => {
console.log('Resizing:', e.detail.width, 'x', e.detail.height);
});
box.addEventListener('vn-resize-end', (e) => {
console.log('Final size:', e.detail.width, 'x', e.detail.height);
// e.g., save size to localStorage
});
vn-resize-end also fires after a double-click on the handle (see §5.1), so a listener that persists the size sees the maximized and the restored size too.
5. Programmatic Resize
const box = document.getElementById('gridContainer');
// Set specific dimensions (clamped to the min-*/max-* attributes)
box.resize(700, 450);
// Read current size
console.log(box.currentWidth, box.currentHeight);
// Read back the configuration, verbatim from the attributes
console.log(box.width, box.maxWidth); // e.g. "600", null
console.log(box.minWidth); // the px minimum actually enforced
resize() fires vn-resize-end (and only that one). If your vn-resize-end listener calls resize(), break the loop yourself — the component does not suppress the event for programmatic calls, because hosts that persist size on vn-resize-end would otherwise silently miss every programmatic change.
resize() also does not apply the parent-relative ceiling that a drag enforces. It is your explicit instruction, and it may run before your own layout has settled, so a resize() can produce a size larger than a drag could reach.
5.1 Double-click to maximize
A double-click (or a double-tap, where the browser turns one into a dblclick) on the handle grows the box to the largest size a drag could reach: the parent's remaining space minus a 12px margin, capped by the max-* attributes. It never shrinks an axis, and a CSS max-width / max-height of your own still caps the result. A second double-click restores the size the box had before.
The gesture is always on and needs no code. It writes through resize(), so each maximize or restore fires one vn-resize-end. A drag that changes the size, a resize() call, or a width / height attribute change in between makes the next double-click maximize again instead of restoring.
6. Adding Visual Chrome (CSS)
The component's base CSS provides only structural rules. To add borders, shadows, or backgrounds, add rules in your host stylesheet targeting .vn-resize-box:
#gridContainer .vn-resize-box {
border: 1px solid rgba(0, 0, 0, 0.08);
border-radius: 8px;
padding: 6px;
background: var(--vn-grid-bg, transparent);
}
Tip: Scope overrides by container ID to avoid affecting other
<vn-resize-box>instances on the page.
6.1 Theming the handle
The handle is themed through four custom properties, keyed per box by data-theme. A custom theme needs one rule:
vn-resize-box[data-theme="my-theme"] {
--vn-resize-box-handle-icon: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M24 0V24H0Z'/></svg>");
--vn-resize-box-handle-color: #5f6368;
--vn-resize-box-handle-hover-color: #1a73e8;
--vn-resize-box-handle-size: 24px;
}
Draw the icon for the bottom-right corner; RTL mirrors it. See 04-css-architecture.md §7 for the built-in themes and the defaults.
7. Right-to-Left Layouts
<vn-resize-box> follows the inline direction. Under dir="rtl" the handle sits in the bottom-left corner, dragging left grows the box, and the parent-relative ceiling is measured from the parent's right edge. No attribute or option turns this on — set dir="rtl" as you would anywhere else:
<html dir="rtl">
...
<vn-resize-box id="gridContainer" min-width="360" min-height="220">
<vn-grid id="myGrid"></vn-grid>
</vn-resize-box>
Two things to know:
- Set the
dirattribute, not only the CSSdirectionproperty. The drag arithmetic reads computeddirectionand works either way, but the mirrored cursor, corner radius and handle glyph are[dir="rtl"]rules. A host that setsdirection: rtlin CSS alone resizes correctly with an unmirrored handle visual. - Vertical writing modes are not supported. Only the inline axis mirrors; the block axis always grows downwards.
8. Accessibility
Resizing is pointer-driven — there is no keyboard resize path. The drag handle carries role="separator" and an accessible name, so assistive technology announces the resizable boundary, but it is deliberately not focusable: it holds no tabindex, and no key does anything to it. It therefore adds no tab stop to a page — in particular, none between the page and the content the box wraps.
Hosts that need a keyboard-operable size can drive resize() from their own control. The double-click maximize (§5.1) is pointer-only too.
8.1 Labelling
Put an aria-label on the <vn-resize-box>; the component copies it onto the handle with a suffix:
<vn-resize-box id="gridContainer" aria-label="Resizable orders grid">…</vn-resize-box>
<!-- handle gets aria-label="Resizable orders grid — resize handle" -->
With no host label the handle falls back to "Resize".
Two limits:
- The
" — resize handle"suffix is hard-coded English. The component ships no i18n mechanism. If you need a fully localized name, set the handle'saria-labelyourself (see §8.2). - The name is derived once, when the internal markup is built. Changing the host's
aria-labellater does not re-derive it.
8.2 Overriding the label or adding a tooltip
const box = document.getElementById('gridContainer');
const handle = box.querySelector('.vn-resize-handle');
if (handle) {
handle.setAttribute('aria-label', 'Ridimensiona la griglia ordini');
handle.setAttribute('title', 'Trascina l\'angolo per ridimensionare');
}
9. Migration from Inline Resize Code
If you previously had a custom resize implementation (like the one formerly in people-cities-js), the migration steps are:
- Remove the old HTML markup (
.resizable-container,.resize-visual-handle). - Remove the old JavaScript function (
initResizableContainer()). - Remove the old CSS rules (
.resizable-container,.resize-visual-handle, etc.). - Add the script tag:
<script src="../vanilla-resize-box/vanilla-resize-box.js"></script>. - Wrap your content in
<vn-resize-box>with the desired attributes. - Add app-specific CSS targeting
#yourId .vn-resize-boxfor borders, padding, etc. - Update any tooltip/i18n code to query
.vn-resize-handleinstead of.resize-visual-handle.
10. Script Loading Order
When using both Vanilla-Grid and Vanilla-Resize-Box:
<!-- Resize box (no dependencies) -->
<script src="../vanilla-resize-box/vanilla-resize-box.js"></script>
<!-- Grid engine + web component -->
<script src="../vanilla-grid/vanilla-grid.js"></script>
<script src="../vanilla-grid/vanilla-grid-element.js"></script>
<!-- App code -->
<script type="module" src="app.js"></script>
<vn-resize-box> has no dependency on <vn-grid> and can be used independently.