Skip to content

fix(COffcanvas): restore body scroll when unmounted while visible - #562

Merged
mrholek merged 1 commit into
mainfrom
fix/offcanvas-restore-scroll-on-unmount
Sep 14, 2026
Merged

mrholek merged 1 commit into
mainfrom
fix/offcanvas-restore-scroll-on-unmount

Conversation

@mrholek

@mrholek mrholek commented Sep 14, 2026

Copy link
Copy Markdown
Member

React half of the same defect fixed for vanilla in coreui#1192. The Vue half is in coreui-vue.

What was wrong

The effect that locks body scroll had no cleanup, so unmounting an open offcanvas left overflow: hidden and the padding compensation on the body with nothing to undo them. The page stayed unscrollable. A route change that tears the tree down while the offcanvas is open is the ordinary way to reach this.

CModal next door already handles it — its effect ends in a cleanup that removes the same properties. This brings COffcanvas to the same shape.

Why it could not just be appended

The effect had an early return in the visible branch:

if (_visible && !scroll) {
  document.body.style.overflow = 'hidden'
  document.body.style.paddingRight = '0px'
  return            // ← returns undefined, so no cleanup is registered
}

React takes the effect's return value as the cleanup, so that branch registered none — in exactly the state the cleanup exists for. The lock and release are one if/else now, with the release repeated in the returned cleanup.

Test

An offcanvas rendered visible and then unmounted leaves no overflow or padding-right on the body. Verified red beforehand. Full suite green — 130 files, 402 tests.

Scope

coreui-react-pro carries the same file and is currently 0 commits behind, so it gets this through the next free→pro sync. v6 solved it differently: both frameworks moved to a shared useDialog hook whose unmount effect closes the dialog and clears the lock.

The effect that locks body scroll had no cleanup, so unmounting an open
offcanvas left `overflow: hidden` and the padding compensation on the body with
nothing to undo them — the page stayed unscrollable. An SPA route change that
tears the tree down while the offcanvas is open is the ordinary way to hit it.

CModal next door already does this correctly; the effect now has the same shape,
with the lock and release in one if/else and the release repeated in a cleanup.
The early `return` in the visible branch is what made a cleanup impossible to
add without restructuring: it skipped the return value in exactly the case the
cleanup exists for.

Test: an offcanvas rendered visible and then unmounted leaves no overflow or
padding-right on the body. Verified red beforehand.
@mrholek
mrholek merged commit ca3afc7 into main Sep 14, 2026
5 checks passed
@mrholek
mrholek deleted the fix/offcanvas-restore-scroll-on-unmount branch September 14, 2026 00:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant