fix(joint-core): emit 'resize' when host element CSS size changes#3317
Merged
Geliogabalus merged 4 commits intoMay 19, 2026
Conversation
`dia.Paper` now attaches a ResizeObserver to its host `<div>` and re-emits
the existing `'resize'` event when the host's computed size changes. This
makes downstream subscribers — `GridLayerView`, `PaperScroller`, rulers,
the React host — track CSS-relative sizing (`width: null`/`'100%'`/flex/
container queries) without manual `setDimensions()` calls. The observer
self-skips when both `options.width` and `options.height` are numeric,
preserving the explicit-size contract. New opt-out: `autoResizePaper: false`.
Auto-emitted events carry `{ source: 'observer' }` as `data` so listeners
can distinguish them from explicit `setDimensions()` emissions.
https://claude.ai/code/session_013FyjBbiuXq4gr3ZRkT4Uvz
There was a problem hiding this comment.
Pull request overview
This PR enhances dia.Paper to automatically re-emit the existing 'resize' event when the host element’s CSS-driven size changes by attaching a ResizeObserver (while preserving the explicit numeric width/height behavior).
Changes:
- Add
ResizeObserverlifecycle management todia.Paperand emit'resize'with{ source: 'observer' }when the host element’s computed size changes. - Ensure observer behavior is coordinated with
setDimensions()(dedup/avoid double resize emissions). - Add QUnit coverage for observer attach/detach, opt-out, and toggle scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/joint-core/src/dia/Paper.mjs | Adds ResizeObserver-based resize re-emission and lifecycle hooks (init, onRemove, setDimensions). |
| packages/joint-core/test/jointjs/paper.js | Adds QUnit tests validating observer-driven resize behavior and teardown/toggle paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
packages/joint-core/test/jointjs/paper.js:142
- This test doesn’t actually verify that the observer is skipped in numeric-dimensions mode: even if a
ResizeObserverwere attached, changing$containersize wouldn’t change the paper element’s px size, so no callback/event would fire. To make the test assert the intended behavior, also check thatpaper._elementResizeObserveris not created (or spy onResizeObserver/observe()calls).
QUnit.test('numeric dimensions skip the observer', function(assert) {
var done = assert.async();
var paper = this.paper;
paper.setDimensions(200, 100);
var spy = sinon.spy();
paper.on('resize', spy);
$container.css({ width: '500px', height: '500px' });
afterResizeObserverDelivery(function() {
assert.ok(spy.notCalled, 'host CSS resize ignored in explicit-size mode');
done();
});
'resize' when host element CSS size changes'resize' when host element CSS size changes
Geliogabalus
approved these changes
May 19, 2026
This was referenced Jul 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dia.Papernow attaches aResizeObserverto its host<div>and re-emits the existing'resize'event when the host's computed size changes. Downstream subscribers —GridLayerView,PaperScroller, rulers, the React host — automatically follow CSS-relative sizing (width: null/'100%'/ flex / container queries) without manualsetDimensions()calls.The observer self-skips when both
options.widthandoptions.heightare numeric, so the existing explicit-size contract is preserved. Auto-emitted events carry{ source: 'observer' }as thedataargument so listeners doing expensive work can short-circuit on it.Motivation
Paper#getComputedSize()already falls back toel.clientWidth/el.clientHeightwhenoptions.width/heightare non-numeric, so CSS-relative sizing works — but JointJS never learns when the host actually changes size. The'resize'event only fired from explicitsetDimensions()calls, leavingGridLayerViewand other size-aware consumers stale on flex resizes, window resizes, sidebar toggles, container queries, etc.@joint/reactmakes this especially visible: it already forceswidth: undefined/height: undefinedon every Paper, so every React paper is in CSS-relative mode. This change automatically lights up grid / scroller / rulers across the entire React surface with no other code changes required.Flow
setDimensions()keeps its own directtrigger('resize', …)for the explicit-API path; both code paths feed_lastObservedSize, so observer + setDimensions never double-fire.Changes
packages/joint-core/src/dia/Paper.mjs_startObservingElementSize/_stopObservingElementSizeand dedup state_lastObservedSize.init(),onRemove(), andsetDimensions()(re-evaluates observer attachment when toggling between explicit and CSS-relative modes).packages/joint-core/test/jointjs/paper.js— five new QUnit tests covering fire / skip / teardown / toggle paths.Test plan
yarn workspace @joint/core test— full QUnit + Mocha suite green.yarn workspace @joint/core run test-ts— TS pass.@joint/reactstorybook → Examples / Dynamic Interactivity), adddrawGrid={{ name: 'dot' }}and confirm the grid follows window resize and sidebar toggles.width/height. Confirm no extra'resize'events fire on window resize.ResizeObserver#disconnectis called exactly once per unmount.