Skip to content

Commit ea283c1

Browse files
committed
Preserve document scrollbar when showing modal
If the browser is showing a scrollbar that takes up space before kpop opens then opening the modal will cause the scrollbar to disappear and could reflow the page. Set overflowY to "scroll" if the document is bigger than the window to ensure the scrollbar remains.
1 parent 7f2f7c8 commit ea283c1

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
katalyst-kpop (3.2.6)
4+
katalyst-kpop (3.2.7)
55
katalyst-html-attributes
66
turbo-rails
77
view_component

app/javascript/kpop/controllers/scrim_controller.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,14 @@ export default class ScrimController extends Controller {
130130
this.zIndexValue = zIndex;
131131
this.scrollY = top;
132132

133-
this.previousPosition = document.body.style.position;
134-
this.previousTop = document.body.style.top;
135-
136133
this.element.style.zIndex = this.zIndexValue;
137134
document.body.style.top = `-${top}px`;
138135
document.body.style.position = "fixed";
136+
document.body.style.paddingRight = `-${this.scrollPadding}px`;
137+
138+
if (document.body.scrollHeight > window.innerHeight) {
139+
document.body.style.overflowY = "scroll";
140+
}
139141
}
140142

141143
/**
@@ -145,22 +147,13 @@ export default class ScrimController extends Controller {
145147
this.captiveValue = this.defaultCaptiveValue;
146148
this.zIndexValue = this.defaultZIndexValue;
147149

148-
resetStyle(this.element, "z-index", null);
149-
resetStyle(document.body, "position", null);
150-
resetStyle(document.body, "top", null);
150+
this.element.style.removeProperty("z-index");
151+
document.body.style.removeProperty("position");
152+
document.body.style.removeProperty("top");
153+
document.body.style.removeProperty("overflow-y");
151154

152155
window.scrollTo({ left: 0, top: this.scrollY, behavior: "instant" });
153156

154157
delete this.scrollY;
155-
delete this.previousPosition;
156-
delete this.previousTop;
157-
}
158-
}
159-
160-
function resetStyle(element, property, previousValue) {
161-
if (previousValue) {
162-
element.style.setProperty(property, previousValue);
163-
} else {
164-
element.style.removeProperty(property);
165158
}
166159
}

katalyst-kpop.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Gem::Specification.new do |spec|
44
spec.name = "katalyst-kpop"
5-
spec.version = "3.2.6"
5+
spec.version = "3.2.7"
66
spec.authors = ["Katalyst Interactive"]
77
spec.email = ["[email protected]"]
88

0 commit comments

Comments
 (0)