Skip to content

Commit 5db1a96

Browse files
committed
Dynamically calculate scrollbar width
1 parent 4061368 commit 5db1a96

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- In de vorige versies werd bij het openen van het feature modal een standaard `padding-right` van `17px` op de `body` class toegepast (`body.gh-dim-modal-open`) om de ruimte te compenseren voor het verwijderen van de browser scrollbar. Deze waarde kan echter verschillen per scherm en device en wordt daarom nu dynamisch bepaald en wordt direct toegepast op `body.style.paddingRight`. Mogelijk heeft dit invloed op eigen styling.
2+
13
**1.10.11 (2023-11-28)**
24

35
- Nieuwe versies van ondersteunende software bibliotheken in gebruik genomen.

src/components/feature.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import React, {Component} from 'react';
1515
import {CSSTransition} from 'react-transition-group';
1616
import _ from 'lodash';
17+
import { getScrollbarWidth } from '../util/browser';
1718
import { FEATURE_TYPE_WMSFEATURE, FEATURE_TYPE_KMLFEATURE, FEATURE_TYPE_DIMFEATURE, FEATURE_TYPE_UNKNOWN } from '../constants';
1819

1920
function CloseModal({ onClick }) {
@@ -135,13 +136,15 @@ export default class FeatureComponent extends Component {
135136
componentDidUpdate(prevProps) {
136137
if(this.props.feature !== null) {
137138
document.body.classList.add('gh-dim-modal-open');
139+
document.body.style.paddingRight = getScrollbarWidth() + 'px';
138140
if(typeof this.props.cb === 'function') {
139141
this.refModal.current.focus();
140142
this.props.cb(this.props.feature);
141143
}
142144
}
143145
else {
144146
document.body.classList.remove('gh-dim-modal-open');
147+
document.body.style.paddingRight = null;
145148
}
146149

147150
// Close modal when Escape is pressed

src/scss/style.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ $shadow-color: rgba(0, 0, 0, 0.5);
427427

428428
body.gh-dim-modal-open {
429429
overflow:hidden;
430-
padding-right:17px;
431430
}
432431

433432
.gh-dim-feature-modal {

src/util/browser.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020-2023 Gemeente Heerenveen
3+
*
4+
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
5+
* You may not use this work except in compliance with the Licence.
6+
* You may obtain a copy of the Licence at:
7+
*
8+
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Licence for the specific language governing permissions and limitations under the Licence.
13+
*/
14+
15+
/**
16+
* Calculate width of scrollbar
17+
*
18+
* Source: https://stackoverflow.com/a/16771535
19+
* @returns {int} Width of scrollbar in pixels
20+
*/
21+
const _getScrollbarWidth = () => {
22+
let width = _getScrollbarWidth.width;
23+
if (width === undefined) {
24+
let div = document.createElement('div');
25+
div.innerHTML = '<div style="width:50px;height:50px;position:absolute;left:-50px;top:-50px;overflow:auto;"><div style="width:1px;height:100px;"></div></div>';
26+
div = div.firstChild;
27+
document.body.appendChild(div);
28+
width = _getScrollbarWidth.width = div.offsetWidth - div.clientWidth;
29+
document.body.removeChild(div);
30+
}
31+
return width;
32+
};
33+
34+
/**
35+
* Calculate width of scrollbar
36+
*
37+
* Source: https://stackoverflow.com/a/16771535
38+
* @returns {int} Width of scrollbar in pixels
39+
*/
40+
export function getScrollbarWidth() {
41+
return _getScrollbarWidth();
42+
}

0 commit comments

Comments
 (0)