Initial commit

This commit is contained in:
maojindao55
2025-02-12 17:18:18 +08:00
commit 394e5640e9
8002 changed files with 1687177 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import * as React from 'react';
import { GapMode } from './utils';
export interface BodyScroll {
noRelative?: boolean;
noImportant?: boolean;
gapMode?: GapMode;
}
export declare const lockAttribute = "data-scroll-locked";
export declare const useLockAttribute: () => void;
/**
* Removes page scrollbar and blocks page scroll when mounted
*/
export declare const RemoveScrollBar: React.FC<BodyScroll>;

View File

@@ -0,0 +1,85 @@
import * as React from 'react';
import { styleSingleton } from 'react-style-singleton';
import { fullWidthClassName, zeroRightClassName, noScrollbarsClassName, removedBarSizeVariable } from './constants';
import { getGapWidth } from './utils';
const Style = styleSingleton();
export const lockAttribute = 'data-scroll-locked';
// important tip - once we measure scrollBar width and remove them
// we could not repeat this operation
// thus we are using style-singleton - only the first "yet correct" style will be applied.
const getStyles = ({ left, top, right, gap }, allowRelative, gapMode = 'margin', important) => `
.${noScrollbarsClassName} {
overflow: hidden ${important};
padding-right: ${gap}px ${important};
}
body[${lockAttribute}] {
overflow: hidden ${important};
overscroll-behavior: contain;
${[
allowRelative && `position: relative ${important};`,
gapMode === 'margin' &&
`
padding-left: ${left}px;
padding-top: ${top}px;
padding-right: ${right}px;
margin-left:0;
margin-top:0;
margin-right: ${gap}px ${important};
`,
gapMode === 'padding' && `padding-right: ${gap}px ${important};`,
]
.filter(Boolean)
.join('')}
}
.${zeroRightClassName} {
right: ${gap}px ${important};
}
.${fullWidthClassName} {
margin-right: ${gap}px ${important};
}
.${zeroRightClassName} .${zeroRightClassName} {
right: 0 ${important};
}
.${fullWidthClassName} .${fullWidthClassName} {
margin-right: 0 ${important};
}
body[${lockAttribute}] {
${removedBarSizeVariable}: ${gap}px;
}
`;
const getCurrentUseCounter = () => {
const counter = parseInt(document.body.getAttribute(lockAttribute) || '0', 10);
return isFinite(counter) ? counter : 0;
};
export const useLockAttribute = () => {
React.useEffect(() => {
document.body.setAttribute(lockAttribute, (getCurrentUseCounter() + 1).toString());
return () => {
const newCounter = getCurrentUseCounter() - 1;
if (newCounter <= 0) {
document.body.removeAttribute(lockAttribute);
}
else {
document.body.setAttribute(lockAttribute, newCounter.toString());
}
};
}, []);
};
/**
* Removes page scrollbar and blocks page scroll when mounted
*/
export const RemoveScrollBar = ({ noRelative, noImportant, gapMode = 'margin' }) => {
useLockAttribute();
/*
gap will be measured on every component mount
however it will be used only by the "first" invocation
due to singleton nature of <Style
*/
const gap = React.useMemo(() => getGapWidth(gapMode), [gapMode]);
return React.createElement(Style, { styles: getStyles(gap, !noRelative, gapMode, !noImportant ? '!important' : '') });
};

View File

@@ -0,0 +1,8 @@
export declare const zeroRightClassName = "right-scroll-bar-position";
export declare const fullWidthClassName = "width-before-scroll-bar";
export declare const noScrollbarsClassName = "with-scroll-bars-hidden";
/**
* Name of a CSS variable containing the amount of "hidden" scrollbar
* ! might be undefined ! use will fallback!
*/
export declare const removedBarSizeVariable = "--removed-body-scroll-bar-size";

View File

@@ -0,0 +1,8 @@
export const zeroRightClassName = 'right-scroll-bar-position';
export const fullWidthClassName = 'width-before-scroll-bar';
export const noScrollbarsClassName = 'with-scroll-bars-hidden';
/**
* Name of a CSS variable containing the amount of "hidden" scrollbar
* ! might be undefined ! use will fallback!
*/
export const removedBarSizeVariable = '--removed-body-scroll-bar-size';

View File

@@ -0,0 +1,4 @@
import { RemoveScrollBar } from './component';
import { zeroRightClassName, fullWidthClassName, noScrollbarsClassName, removedBarSizeVariable } from './constants';
import { getGapWidth } from './utils';
export { RemoveScrollBar, zeroRightClassName, fullWidthClassName, noScrollbarsClassName, removedBarSizeVariable, getGapWidth, };

View File

@@ -0,0 +1,4 @@
import { RemoveScrollBar } from './component';
import { zeroRightClassName, fullWidthClassName, noScrollbarsClassName, removedBarSizeVariable } from './constants';
import { getGapWidth } from './utils';
export { RemoveScrollBar, zeroRightClassName, fullWidthClassName, noScrollbarsClassName, removedBarSizeVariable, getGapWidth, };

View File

@@ -0,0 +1,14 @@
export declare type GapMode = 'padding' | 'margin';
export interface GapOffset {
left: number;
top: number;
right: number;
gap: number;
}
export declare const zeroGap: {
left: number;
top: number;
right: number;
gap: number;
};
export declare const getGapWidth: (gapMode?: GapMode) => GapOffset;

View File

@@ -0,0 +1,28 @@
export const zeroGap = {
left: 0,
top: 0,
right: 0,
gap: 0,
};
const parse = (x) => parseInt(x || '', 10) || 0;
const getOffset = (gapMode) => {
const cs = window.getComputedStyle(document.body);
const left = cs[gapMode === 'padding' ? 'paddingLeft' : 'marginLeft'];
const top = cs[gapMode === 'padding' ? 'paddingTop' : 'marginTop'];
const right = cs[gapMode === 'padding' ? 'paddingRight' : 'marginRight'];
return [parse(left), parse(top), parse(right)];
};
export const getGapWidth = (gapMode = 'margin') => {
if (typeof window === 'undefined') {
return zeroGap;
}
const offsets = getOffset(gapMode);
const documentWidth = document.documentElement.clientWidth;
const windowWidth = window.innerWidth;
return {
left: offsets[0],
top: offsets[1],
right: offsets[2],
gap: Math.max(0, windowWidth - documentWidth + offsets[2] - offsets[0]),
};
};