# scrollbar-compensate

> A hack to compensate for scrollbar size for modals

Latest version **0.2.2** (published 2017-12-23) · ISC license · 0 weekly downloads

## Install

```sh
npm install scrollbar-compensate
pnpm add scrollbar-compensate
yarn add scrollbar-compensate
bun add scrollbar-compensate
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2017-12-23 |
| First published | 2016-08-29 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Kier Borromeo |
| Maintainers | srph |

## Links

- npm: https://www.npmjs.com/package/scrollbar-compensate
- Repository: https://github.com/srph/scrollbar-compensate
- Homepage: https://github.com/srph/scrollbar-compensate#readme
- Issues: https://github.com/srph/scrollbar-compensate/issues
- npm.io page: https://npm.io/package/scrollbar-compensate

## Dependencies (1)

- [scrollbar-size](https://npm.io/package/scrollbar-size.md) ^2.0.0

## Recent versions

- 0.2.2 (latest) — 2017-12-23
- 0.2.1 — 2017-08-17
- 0.2.0 — 2017-01-17
- 0.1.2 — 2016-08-30
- 0.1.1 — 2016-08-30
- 0.1.0 — 2016-08-29
- 1.0.0 — 2016-08-29

## README

# scrollbar-compensate [![npm version](http://img.shields.io/npm/v/scrollbar-compensate.svg?style=flat)](https://npmjs.org/package/scrollbar-compensate)
```
npm i scrollbar-compensate
```
A hack to compensate for scrollbar size for modals

## Problem
Usually, you would set `overflow: hidden` to body when the modal mounts. However, this causes a weird adjustment to the layout (because the scrollbar disappears). This mostly affects Windows/Linux users, and some OSX users that use a mouse (because it forces the scrollbar to appear).

As an example, compare [Semantic UI's Modal](http://semantic-ui.com/modules/modal.html) and [TWBS' Modal](http://getbootstrap.com/javascript/#modals). TWBS solved this by adding a `padding-right` to the `body` when the modal mounts.

## What It Does
It adds an inline-style that adds a `padding-right` to the provided selectors. It appends the following to `head`:

```html
<style>.my-selector, .my-selector-2 .my-nested-selector { padding-right: 15px; }</style>
```

Take note that the `15px` value here may vary depending on your scrollbar width. Typically, OS X has this set to `15px`.

## Usage
`compensate(selectors)` &mdash; This module only exposes a single function that accepts an array of elements as *an* argument. Usually, you pass class names here that are added to the body when a modal mounts.

```js
var compensate = require('scrollbar-compensate');

document.addEventListener('DOMContentLoaded', function() {
  compensate(['.modal-open', '.overlay-enabled .global-nav']);
});
```

**Note** — If you're wondering why it's necessary to run `compensate` when the document loads: We're using the [`scrollbar-size`](https://www.npmjs.com/package/scrollbar-size) library internally which needs to be called after the document is ready.

## Examples
[jquery-modal](https://github.com/srph/jquery-modal) is using this library.

## Alternatives
[no-scroll](https://github.com/davidtheclark/no-scroll) also solves this problem, however covers more than scrollbar compensation.

Use this library only if you want the scrollbar compensation library. Otherwise, check the alternatives.

## Note
This library isn't available in UMD because I don't really need it at the moment. Please send a PR or submit an issue if you'd like it to be implemented. Moreover, no tests yet.

---
_Source: https://npm.io/package/scrollbar-compensate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
