Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ramstack/pagelock",
"version": "1.0.1",
"version": "1.1.0",
"description": "A simple utility for managing page scroll locking. No external dependencies.",
"type": "module",
"author": "rameel <rameel-b@hotmail.com>",
Expand Down
18 changes: 16 additions & 2 deletions rollup.config.mjs → rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from "node:path";
import resolve from "@rollup/plugin-node-resolve";
import size from "rollup-plugin-bundle-size";
import strip_comments from "strip-comments";
Expand Down Expand Up @@ -25,7 +26,8 @@ const plugins = [
resolve(),
typescript(),
size(),
remove_comments()
remove_comments(),
trim_ws()
];

export default [{
Expand Down Expand Up @@ -54,11 +56,23 @@ export default [{

function remove_comments() {
return {
name: "strip",
name: "remove_comments",
transform(source) {
return {
code: strip_comments(source)
};
}
};
}

function trim_ws() {
return {
name: "trim_ws",
generateBundle(options, bundle) {
if (options.file.match(/\.js$/)) {
const key = path.basename(options.file);
bundle[key].code = bundle[key].code.trim();
}
}
};
}
49 changes: 27 additions & 22 deletions src/pagelock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
let locks: Array<number> = [];
let sequence: number = 0;

let width: string;
interface ExtendedDocument extends Document {
_r_pagelock?: {
locks: number[];
sequence: number;
width?: string | null;
props?: {
overflow: string;
paddingRight: string
} | null;
};
}

let props: {
overflow: string,
paddingRight: string
} | null | undefined;
const storage = (document as ExtendedDocument)._r_pagelock ??= { locks: [], sequence: 0 };

/**
* Locks the page scroll. Subsequent calls to the function add to the queue of lock holders.
Expand All @@ -18,30 +22,31 @@ let props: {
* without affecting the others in the queue.
*/
export function pagelock(): () => void {
if (!props) {
if (!storage.props) {
const {
offsetHeight,
clientHeight
} = document.documentElement;

const style = document.body.style;
props = {

storage.props = {
overflow: style.overflow,
paddingRight: style.paddingRight
};

if (offsetHeight > clientHeight) {
style.paddingRight = scrollbarWidth();
style.paddingRight = scrollbar_width();
}

style.overflow = "hidden";
}

const seq = ++sequence;
locks.push(seq);
const seq = ++storage.sequence;
storage.locks.push(seq);

return () => {
locks = locks.filter(v => v !== seq);
storage.locks = storage.locks.filter(v => v !== seq);
restore();
};
}
Expand All @@ -54,26 +59,26 @@ export function pagelock(): () => void {
* Otherwise, only the most recent lock is released. Defaults to `false`.
*/
export function pageunlock(force?: boolean): void {
force ? locks = [] : locks.pop();
force ? storage.locks = [] : storage.locks.pop();
restore();
}

function restore(): void {
if (props && !locks.length) {
Object.assign(document.body.style, props);
props = null;
if (storage.props && !storage.locks.length) {
Object.assign(document.body.style, storage.props);
storage.props = null;
}
}

function scrollbarWidth(): string {
if (!width) {
function scrollbar_width(): string {
if (!storage.width) {
const outer = document.createElement("div");
outer.innerHTML = "<div style='width:80px;height:80px;position:absolute;left:-90px;top:-90px;overflow:auto'><div style='height:99px'></div></div>";
const inner = outer.firstChild as HTMLDivElement;
document.body.appendChild(outer);
width = (inner.offsetWidth - inner.clientWidth) + "px";
storage.width = (inner.offsetWidth - inner.clientWidth) + "px";
document.body.removeChild(outer);
}

return width;
return storage.width;
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"target": "es2022",
"module": "es2022",
"strict": true,
"declaration": true,
"outDir": "./dist",
Expand Down