Skip to content

fix(modal): use resizeobserver#2969

Open
lubber-de wants to merge 18 commits into
fomantic:developfrom
lubber-de:resizeObserver
Open

fix(modal): use resizeobserver#2969
lubber-de wants to merge 18 commits into
fomantic:developfrom
lubber-de:resizeObserver

Conversation

@lubber-de

@lubber-de lubber-de commented Dec 20, 2023

Copy link
Copy Markdown
Member

Description

This PR changes the refresh behavior of modals to recalculate their dimensions. The previous logic was checking for DOM-Nodes only, but the only need was to know when the modal gets resized.

This now works in Firefox as well.

The dimmers scrollbar will now also not trigger content movement (#679) anymore when used in modern browsers. However this does not nicely work when detachable: false is used (tried a lot dealing with complicated tricks using the html tag as the scrollbar-gutter property does not work for the body element out of the box but it always left a white scrollbar even if the modal fitted into the viewport, so i decided to live with that limitation for now)
As this feature will always occupy a possible scrollbar width (even if a scrollbar won't appear), only modals which are supposed to be scrollable (by interaction or application logic while the modal is shown) should be given a new css class scrollable to gain the fix. Otherwise every modal wouldnt be centered anymore.

I also adjusted the dimmer positioning and close icon appearance when detachable:false in a custom context is used

Testcase

Resize the textarea, a scrollbar should appear to the modals dimmer
https://jsfiddle.net/lubber/drb6fpog/4/

Closes

#679
#2476
#2920

Summary by CodeRabbit

Summary by CodeRabbit

  • Bug Fixes
    • Improved modal resizing/position updates by detecting element size changes and syncing with DOM mutations.
    • Refined modal refresh, fit, and centering logic across body and non-body contexts (including scroll offsets).
    • Corrected dimmer scrollbar handling, height calculations, and cleanup when showing/hiding modals.
  • Styles
    • Updated dimmer/modal desktop behavior, added conditional scrollbar-gutter support, and improved scrolling modal overflow/spacer handling for better browser compatibility.

@lubber-de lubber-de added type/bug Any issue which is a bug or PR which fixes a bug lang/css Anything involving CSS state/awaiting-reviews Pull requests which are waiting for reviews javascript labels Dec 20, 2023
@lubber-de lubber-de added this to the 2.9.4 milestone Dec 20, 2023
Comment thread src/definitions/modules/modal.js Outdated
Comment thread src/definitions/modules/modal.less Outdated
overscroll-behavior: @overscrollBehavior;
}
.modals.dimmer {
scrollbar-gutter: stable;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed, will it imply scrollbars always shown? In the PR description there is mentioned something with detachable: false, is this related?

There is mentioned "The dimmers scrollbar will now also not trigger content movement anymore". I tried the demo (in Firefox) and when the textarea is resized, the content is unexpectedly always scrolled to the top when resized.

@lubber-de lubber-de Dec 21, 2023

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed, will it imply scrollbars always shown? In the PR description there is mentioned something with detachable: false, is this related?

No, the scollbars are not always shown. This will occupy a possible shown scrollbar margin, but will not show an empty scrollbar in case the element is not body and no overflow happens
See example at #679

The problem with "detachable:false" in case context is body, is that body does not support this feature (scrollbar-gutter) (firefox+safari don't care at all, because those have their own implemented "overlay scrollbar feature")
Because of this the parent element would need to get scrollbar-gutter:stable instead. This basically works, but as the html element does not have any dimmer color set, it would display a white empty scrollbar in case the modal fits the screen. It also left a second scrollbar when closing the modal...very ugly

If you want to see/try my hack:

The jsfiddle does only barely show the issue as jsfiddle has a black parent background which the html element inside the iframe inherits (so the permanent occupied right scrollbar area is black, but still not the "dimmer black"
https://jsfiddle.net/lubber/cyjnta1d/3/

I also tried messing around with the dimmer animation to also make the html element inherits that, but the transition was not in sync and the code bloated away as we would need to support every possible dimmer variation just for that.

We would also need to support non body context elements... lots of code and cases for, IMHO, little benefit

@supports (scrollbar-gutter: stable) and (selector(:has(.f))) {
    html:has(body.undetached.dimmable) {
      overflow: auto;
    }
    html:has(body.undetached.dimmable.dimmed) {
      overflow: auto;
      scrollbar-gutter: stable;
    }
    body.undetached.dimmable.dimmed.dimmed {
      overflow: hidden;
    }
}

There is mentioned "The dimmers scrollbar will now also not trigger content movement anymore".

What was meant by this is #679 (comment)

I tried the demo (in Firefox) and when the textarea is resized, the content is unexpectedly always scrolled to the top when resized.

mmh, thats the in between discussion where i was not able to reproduce this.

The modal is only put to the top when the it detects the current modals size would not fit (thus adds the scrolling class). Whenever i am able to reproduce this, i take a look again and prepare a separate PR as some browser (firefox) measurements seem to provide questionable values inside module.can.fit() in some situations

@mvorisek mvorisek Dec 21, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your detailed explanation and your work on this topic.

I tried the demo (in Firefox) and when the textarea is resized, the content is unexpectedly always scrolled to the top when resized.

mmh, thats the in between discussion where i was not able to reproduce this.

I meant the demo of/from this PR - https://jsfiddle.net/lubber/drb6fpog/1/

Steps to reproduce:

  1. open the https://jsfiddle.net/lubber/drb6fpog/1/ in Firefox
  2. resize the textarea vertically by dragging the right bottom corner to make the modal larger than the viewport
  3. scroll down the modal
  4. now try to resize the dropdown by dragging the right bottom corner
  5. the modal is unexpectedly scrolled to the top immediately - I would expect no scroll/jump
  6. also notice when you click the textarea resize corner and have F12 "Inspector" tab open, div.dimmer div.modal div in the inspector tab flashes yellow meaning something has changed, but I did not noticed/see any real css/class changes, meaning some same value is set from a mutation observer or even possibly worse, some value is set and immediatelly changed back

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i take a look into this

@lubber-de lubber-de added the state/awaiting-docs Pull requests which need doc changes/additions label May 13, 2024
# Conflicts:
#	src/definitions/modules/modal.less
@lubber-de lubber-de modified the milestones: 2.9.4, 2.10.x Feb 19, 2025
# Conflicts:
#	src/definitions/modules/modal.less
@lubber-de lubber-de added lang/javascript Anything involving JavaScript and removed javascript labels Mar 15, 2025
ko2in
ko2in previously approved these changes Mar 22, 2025

@ko2in ko2in left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread src/definitions/modules/modal.js Outdated
if (getScrollingMargins) {
if (module.can.useFlex()) {
$module.addClass(className.scrolling);
scrollingTop = parseInt($module.css('top').replace(/[^\d.]/g, ''), 10) || 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the top css set by us - if yes, we should know the format and something like .replace(/px$/, '') would be much more cleaner.

@mvorisek

Copy link
Copy Markdown
Contributor

I have tested the JS and the CSS from the jsfiddle and it does indeed fix the "resize textarea issue in Firefox".

@lubber-de might you update jsfiddle (https://jsfiddle.net/lubber/drb6fpog/1/ ATM) to represent the latest compiled CSS?

I am unable to review this PR at the code level, but I would be happy to test all of my reported issues.

ko2in
ko2in previously approved these changes Nov 13, 2025

@ko2in ko2in left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 19ebf81b-ff6f-4a53-bee4-39b148fad9cc

📥 Commits

Reviewing files that changed from the base of the PR and between 9a4913d and 9c5b181.

📒 Files selected for processing (1)
  • src/definitions/modules/modal.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/definitions/modules/modal.js

📝 Walkthrough

Walkthrough

Modal layout handling now observes element resizing, avoids refreshes for hidden modals, incorporates scrolling margins into fit calculations, and refines dimmer sizing, cleanup, positioning, and scrolling styles.

Changes

Modal layout behavior

Layer / File(s) Summary
Resize observation and refresh gating
src/definitions/modules/modal.js
Adds resize-based refresh handling, disconnects the observer during destruction, coordinates mutation refreshes, and skips layout recalculation while hidden.
Scrolling measurements and modal positioning
src/definitions/modules/modal.js
Caches optional scrolling margins, includes them in fit calculations, and selects the appropriate scroll source for modal centering.
Dimmer sizing and scrolling styles
src/definitions/modules/modal.js, src/definitions/modules/modal.less
Refines dimmer height and style cleanup, undetached modal styling, scrollbar gutters, pseudo-element spacing, and dimmer overflow behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ModalElement
  participant ResizeObserver
  participant ModalModule
  participant Dimmer
  ModalElement->>ResizeObserver: report resize
  ResizeObserver->>ModalModule: call refresh()
  ModalModule->>ModalElement: recalculate cached layout
  ModalModule->>Dimmer: update height and positioning
Loading

Suggested reviewers: colinfrick, prudho, y0hami

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely highlights the main modal change: using ResizeObserver.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lang/css Anything involving CSS lang/javascript Anything involving JavaScript state/awaiting-docs Pull requests which need doc changes/additions state/awaiting-reviews Pull requests which are waiting for reviews type/bug Any issue which is a bug or PR which fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants