feat(useResizeObserver): observe a chosen box model#1695
Open
xobotyi wants to merge 2 commits into
Open
Conversation
A ResizeObserver only reports changes of the box it was asked to observe, and the shared observer always asked for the content box. Border and padding growth around an unchanged content box therefore raised no callback -- the gap that makes `borderBoxMeasurer` report stale sizes. An optional fourth argument selects the box; observers are now kept one per box model, since a single instance cannot serve two of them.
`borderBoxMeasurer` read border box sizes while the hook still observed content box changes, so a padding-only change left the returned measures stale. The third argument picks the observed box.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1695 +/- ##
==========================================
+ Coverage 83.19% 83.25% +0.05%
==========================================
Files 62 62
Lines 839 842 +3
Branches 151 151
==========================================
+ Hits 698 701 +3
Misses 16 16
Partials 125 125 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Closes the correctness gap flagged when #1603 landed:
borderBoxMeasurerreads border box sizes, but the hook underneath only ever observed the content box.Problem
A
ResizeObservernotifies on changes to the box it was asked to observe. The shared singleton calledobserver.observe(target)— content box, the spec default. So:useMeasure(true, borderBoxMeasurer)keeps returning the previous sizeChange
useResizeObserver(target, callback, enabled, box?)anduseMeasure(enabled, measurer, box?)— both optional, both defaulting to'content-box', so existing calls behave exactly as before.Observers are now kept one per box model:
One instance cannot serve two box models, and elements may be observed by several observers, so this stays within the same "one observer, many subscribers" design — just keyed.
Usage for the border-box case the measurer was written for:
The pairing is documented on both hooks' JSDoc, including the failure mode when the two disagree.
Tests
useResizeObserverforwards{box}toobserveand allocates a separate observer per box modeluseMeasurethreads itsboxargument through — mutation-checked: dropping the argument fails withexpected "vi.fn()" to be called with arguments: [ <div></div>, { box: 'border-box' } ]{box: 'content-box'}callVerification
vp fmt,tsc --noEmit,vp lint,vp test --run(117 files / 532 tests), and the build all pass. Emitted signatures confirm the additions are optional: