Vanilla Resize Box

A lightweight, zero-dependency, resizable container web component with a visible bottom-right drag handle. Built with vanilla JavaScript — no frameworks needed.

Features

Installation

Direct File Include

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

The component automatically injects its CSS (vanilla-resize-box.css) into <head>. No manual <link> tag is required.

Usage

New to the resize box? Getting started builds one step by step, with a live example at each step.

Basic Usage

<vn-resize-box width="600" height="400">
    <p>Any content goes here</p>
</vn-resize-box>

With Constraints

<vn-resize-box
    width="800"
    height="500"
    min-width="300"
    min-height="200"
    max-width="1200"
    max-height="800">
    <div id="myContent">Resizable content</div>
</vn-resize-box>

Wrapping a Grid Component

<vn-resize-box min-width="360" min-height="220">
    <vn-grid id="usersGrid" theme="default">
        <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>

No Initial Size (Fills Parent)

When width and height are omitted, the box fills its parent container:

<div style="width: 100%; height: 80vh;">
    <vn-resize-box>
        <p>Fills parent, then can be resized</p>
    </vn-resize-box>
</div>

Attributes

All attributes are optional.

Attribute Type Default Description
width CSS length — Initial width. A bare number is px; any CSS length works (600, 600px, 100%, 60vw). If omitted, the container fills its parent's width
height CSS length — Initial height. Same syntax as width. If omitted, the container fills its parent's height
min-width px 100 Minimum width. Bare number or explicit px only — a relative value is rejected and the default applies
min-height px 100 Minimum height. Same rule as min-width
max-width CSS length — Maximum width. Same syntax as width. If omitted, limited by parent bounds
max-height CSS length — Maximum height. Same syntax as width. If omitted, limited by parent bounds
theme string — Handle theme name. If omitted, the box follows the theme of the first <vn-grid> inside it, else default. See Theming

Removing any of the sizing attributes clears the inline style it wrote, so the dimension falls back to whatever CSS says.

Properties

All are read-only except theme; attributes are the configuration channel and resize() is the write path for size.

Property Type Description
width string | null The width attribute, verbatim — width="600" reads back "600", not "600px"
height string | null The height attribute, verbatim
maxWidth string | null The max-width attribute, verbatim
maxHeight string | null The max-height attribute, verbatim
minWidth number The minimum width actually enforced, in px. Resolved, not raw: a rejected min-width reads back as the default (100)
minHeight number The minimum height actually enforced, in px
currentWidth number Current rendered width in pixels
currentHeight number Current rendered height in pixels
theme string Read/write. Reads the resolved theme name (own theme attribute, else the first descendant grid's theme, else "default"). Setting writes the theme attribute; null or '' removes it

Methods

Method Signature Description
resize resize(width?, height?) Programmatically resize the box. Numbers are clamped to the min-*/max-* attributes; CSS strings are applied as-is
const box = document.querySelector('vn-resize-box');
box.resize(500, 300);

resize() deliberately differs from a drag in three ways:

Events

Event Detail Description
vn-resize-start { width, height } Fired when the user begins dragging the handle, carrying the size at press time
vn-resize-move { width, height } Fired on every frame during drag
vn-resize-end { width, height } Fired when the user releases the handle, by resize(), and after a double-click on the handle (maximize or restore)
document.querySelector('vn-resize-box').addEventListener('vn-resize-end', (e) => {
    console.log('New size:', e.detail.width, 'x', e.detail.height);
});

Accessibility

Resizing is pointer-only. The drag handle carries role="separator" and an accessible name so assistive technology announces the resizable boundary, but it is deliberately not focusable and answers to no key — it adds no tab stop to the page, in particular none between the page and the content the box wraps. A host that needs a keyboard-operable size drives resize() from its own control. The double-click maximize and restore is pointer-only too.

Labelling. Put aria-label on the <vn-resize-box> and the handle takes it with a suffix:

<vn-resize-box aria-label="Resizable orders grid">…</vn-resize-box>
<!-- handle: aria-label="Resizable orders grid — resize handle" -->

With no host label the handle falls back to "Resize". The suffix is hard-coded English (the component ships no i18n mechanism) and the name is derived once, when the internal markup is built — set the handle's aria-label directly if you need another language or a runtime change.

The handle carries no aria-valuenow / aria-orientation: it drives two axes at once, so neither could be stated honestly.

Right-to-Left

The component follows the inline direction. Under dir="rtl" — set on <html>, on the box, or on any ancestor — 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. There is no attribute to switch this on.

The mirrored cursor, corner radius and handle glyph are [dir="rtl"] CSS rules, so set the dir attribute rather than only the CSS direction property: with direction alone the box still resizes correctly, but the handle keeps its LTR visual.

Vertical writing modes are not supported — only the inline axis mirrors.

Theming

The drag handle has a look per theme. The component resolves a theme name and reflects it as data-theme on the <vn-resize-box>:

  1. the box's own theme attribute, when set and non-empty;
  2. otherwise the theme attribute of the first <vn-grid> inside the box, followed live: the grid's bubbling vn-grid-attribute-changed event re-resolves it on every theme switch, and a grid rendered after the box connects (the Angular order) is picked up too;
  3. otherwise default.

A box that wraps a grid therefore needs no attribute and no code to match it. A grid themed only through theme-css-path has no theme name, so its box shows default. In nested boxes, an outer box without theme follows the first grid inside it, which can be an inner box's grid.

Built-in themes

All rules live in vanilla-resize-box.css, keyed by vn-resize-box[data-theme="<name>"], so two boxes on one page can wear different themes. Idle colors reach at least 3:1 against the theme's grid surface (WCAG 1.4.11), except default, which keeps its original subtle look.

Theme Icon Idle color Hover color
default Triangle rgba(128,128,128,.16) rgba(80,120,220,.28)
default-dark Triangle rgba(165,169,201,.55) rgba(163,177,255,.7)
material / material-dark Two diagonal strokes, round caps #5f6368 / #9aa0a6 #1a73e8 / #8ab4f8
fluent / fluent-dark Staircase of outlined round dots #616161 / #adadad #0f6cbd / #479ef5
carbon / carbon-dark Staircase of filled squares #525252 / #a8a8a8 #0f62fe / #78a9ff
fiori / fiori-dark Staircase of outlined squares #6a6d70 / #d3d7d9 #0a6ed1 / #91c8f6
glow / glow-dark Two nested corner brackets #6b7280 / #9ca3af #6366f1 / #818cf8
apple / apple-dark Two diagonal strokes, round caps rgba(60,60,67,.6) / rgba(235,235,245,.6) #007aff / #0a84ff
ledger Dog-eared page corner #6b6458 #8c2f39

An unknown name is kept as-is in data-theme; no rule matches it, so the handle shows the default look until you add one.

Custom properties

Property Default Meaning
--vn-resize-box-handle-icon SVG of the bottom-right triangle Glyph, drawn as a CSS mask. Any url(); draw it for the bottom-right corner (RTL mirrors it)
--vn-resize-box-handle-color rgba(128, 128, 128, 0.16) Glyph color, idle
--vn-resize-box-handle-hover-color rgba(80, 120, 220, 0.28) Glyph color on hover and while pressed
--vn-resize-box-handle-size 24px Side of the square hit area; the glyph scales with it

The defaults are var() fallbacks inside the handle rules, not declarations on the host, so a value set on :root or any container reaches every unthemed box.

A custom theme

A host theme needs one CSS rule, no JS and no registration:

vn-resize-box[data-theme="corporate"] {
    --vn-resize-box-handle-icon: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='2' stroke-linecap='round'><path d='M20 8 8 20'/><path d='M20 14 14 20'/></svg>");
    --vn-resize-box-handle-color: #5b6470;
    --vn-resize-box-handle-hover-color: #0b5cad;
}

In Windows high-contrast (forced-colors) mode the glyph is painted in CanvasText, and in Highlight on hover, whatever the theme.

CSS Customization

The component uses light DOM, so you can override styles with normal CSS selectors. Change the handle through the custom properties above: a background override on .vn-resize-handle::before is still cut by the icon mask.

/* Change handle appearance for every unthemed box */
:root {
    --vn-resize-box-handle-color: rgba(0, 120, 215, 0.3);
    --vn-resize-box-handle-size: 28px;
}

/* Add a border to the container */
.vn-resize-box {
    border: 1px solid #ccc;
    border-radius: 8px;
    padding: 6px;
}

CSS Classes

Class Element Description
.vn-resize-box Container div The outer resizable container
.vn-resize-handle Handle div The bottom-right drag handle
.vn-resize-box--dragging Container div Added during active drag

Architecture

The component uses light DOM (no Shadow DOM) so that existing app and theme CSS continues to apply to slotted content. It auto-injects a single shared <link> stylesheet into <head> on first use.

Children may be added at any time: content present at connect is wrapped into the internal .vn-resize-box structural div, and children appended after connect are adopted into it automatically (frameworks like Angular attach the element to the document before rendering its children).

File Structure

vanilla-resize-box/
├── vanilla-resize-box.js    # Web component definition
├── vanilla-resize-box.css   # Base structural styles and built-in handle themes
├── vanilla-resize-box.d.ts  # Hand-authored TypeScript declarations
└── README.md                # This file

Browser Support

Works in all modern browsers that support Custom Elements v1 (Chrome, Firefox, Safari, Edge).

Changelog

Version 1.6.1

Version 1.6.0

Version 1.5.2

Version 1.5.1

Version 1.5.0

Version 1.4.1

Version 1.4.0

Keyboard resizing removed — the handle is pointer-only.

Breaking:

Version 1.3.1

Version 1.3.0

Pointer Events, keyboard accessibility and RTL, plus a batch of correctness fixes.

New:

Fixed:

Changed (resize()):

Docs:

Version 1.2.3

Version 1.2.2

Version 1.2.1

Version 1.2.0

Version 1.1.2

Version 1.1.1

Version 1.1.0

Version 1.0.0

License

MIT