scroll-lock-react 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/dist/bundle.css +25 -0
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/package.json +35 -0
- package/rollup.config.js +26 -0
- package/src/index.css +25 -0
- package/src/index.tsx +90 -0
- package/tsconfig.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nedralia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
File without changes
|
package/dist/bundle.css
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
.index_lock-scroll__pjkCC {
|
|
2
|
+
width: 100%;
|
|
3
|
+
z-index: 9999;
|
|
4
|
+
|
|
5
|
+
@media (max-width: 746px) {
|
|
6
|
+
display: none;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.index_counter-view__ZNIa7 {
|
|
11
|
+
display: flex;
|
|
12
|
+
justify-content: center;
|
|
13
|
+
align-items: center;
|
|
14
|
+
width: 100%;
|
|
15
|
+
height: 100%;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.index_sticky-element__bB-ax {
|
|
19
|
+
position: sticky;
|
|
20
|
+
top: 0;
|
|
21
|
+
left: 0;
|
|
22
|
+
width: 100%;
|
|
23
|
+
height:100vh;
|
|
24
|
+
overflow: hidden;
|
|
25
|
+
}
|
package/dist/bundle.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).ScrollLock=t(e.React)}(this,function(e){"use strict";function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=t(e);return({id:t,children:o,duration:r,maxCount:c})=>{const[l,i]=e.useState(0),u=e.useRef(null),s=e.useCallback(()=>{const e=-(u.current?.getBoundingClientRect().top??0),t=u.current?.offsetHeight??0,n=window.innerHeight-80;if(e<0)return 0;const o=e/(t-n);if(o>1)return c;const r=o*c;return Math.min(Math.max(r,0),c)},[u.current?.offsetHeight,c]);e.useEffect(()=>{const e=()=>{const e=s();i(e)};return window.addEventListener("scroll",e),()=>{window.removeEventListener("scroll",e)}},[]);const a=e.useCallback(e=>{e.preventDefault();const t=s();i(t)},[s]);e.useEffect(()=>(window.addEventListener("touchmove",a),()=>{window.removeEventListener("touchmove",a)}),[a]);const d=10*r+"px";return n.default.createElement("div",{className:"lock-scroll",ref:u,style:{height:d}},n.default.createElement("div",{className:"sticky-element"},n.default.createElement("div",{className:"counter-view"},n.default.Children.map(o,e=>n.default.isValidElement(e)?n.default.cloneElement(e,{counter:l}):e))))}});
|
|
2
|
+
//# sourceMappingURL=bundle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundle.js","sources":["../src/index.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useRef, useState } from 'react'\n\nimport './index.css'\n\ninterface ScrollLockProps {\n id: string\n maxCount: number\n duration: number\n children: React.ReactNode\n}\n\ninterface ScrollLockChildProps {\n counter: number\n}\n\nconst ScrollLock = ({ id, children, duration, maxCount }: ScrollLockProps): React.ReactElement => {\n const [count, setCount] = useState(0)\n\n const ref = useRef<HTMLDivElement>(null)\n\n const calculateCount = useCallback((): number => {\n const refYPosition = -(ref.current?.getBoundingClientRect().top ?? 0)\n const elementHeight = ref.current?.offsetHeight ?? 0\n const navbarHeight = 80\n const screenHeight = window.innerHeight - navbarHeight\n\n if (refYPosition < 0) {\n return 0\n }\n\n const relativePosition = refYPosition / (elementHeight - screenHeight)\n\n if (relativePosition > 1) {\n return maxCount\n }\n\n const count = relativePosition * maxCount\n return Math.min(Math.max(count, 0), maxCount)\n },\n [\n ref.current?.offsetHeight,\n maxCount\n ])\n\n // Handle scroll\n useEffect(() => {\n const handleScroll = (): void => {\n const count = calculateCount()\n setCount(count)\n }\n\n window.addEventListener('scroll', handleScroll)\n return () => { window.removeEventListener('scroll', handleScroll) }\n }, [])\n\n const handleTouchMove = useCallback((e: TouchEvent) => {\n e.preventDefault()\n const count = calculateCount()\n setCount(count)\n }, [calculateCount])\n\n // Handle touch events\n useEffect(() => {\n window.addEventListener('touchmove', handleTouchMove)\n return () => {\n window.removeEventListener('touchmove', handleTouchMove)\n }\n }, [handleTouchMove])\n\n // TODO: Add optional progress bar to show how far the user has scrolled in the section\n\n const height = `${duration * 10}px` // Set this constant and the page y should be the same for every view port\n\n return (\n <div className=\"lock-scroll\" ref={ref} style={{ height }}>\n <div className='sticky-element'>\n <div className=\"counter-view\">\n {React.Children.map(children, (child) => {\n if (React.isValidElement<ScrollLockChildProps>(child)) {\n return React.cloneElement(child, { counter: count })\n }\n return child;\n })}\n </div>\n </div>\n </div>\n )\n}\n\nexport default ScrollLock\n"],"names":["id","children","duration","maxCount","count","setCount","useState","ref","useRef","calculateCount","useCallback","refYPosition","current","getBoundingClientRect","top","elementHeight","offsetHeight","screenHeight","window","innerHeight","relativePosition","Math","min","max","useEffect","handleScroll","addEventListener","removeEventListener","handleTouchMove","e","preventDefault","height","React","createElement","className","style","Children","map","child","isValidElement","cloneElement","counter"],"mappings":"qWAemB,EAAGA,KAAIC,WAAUC,WAAUC,eAC5C,MAAOC,EAAOC,GAAYC,EAAQA,SAAC,GAE7BC,EAAMC,SAAuB,MAE7BC,EAAiBC,EAAAA,YAAY,KACjC,MAAMC,IAAiBJ,EAAIK,SAASC,wBAAwBC,KAAO,GAC7DC,EAAgBR,EAAIK,SAASI,cAAgB,EAE7CC,EAAeC,OAAOC,YADP,GAGrB,GAAIR,EAAe,EACjB,OAAO,EAGT,MAAMS,EAAmBT,GAAgBI,EAAgBE,GAEzD,GAAIG,EAAmB,EACrB,OAAOjB,EAGT,MAAMC,EAAQgB,EAAmBjB,EACjC,OAAOkB,KAAKC,IAAID,KAAKE,IAAInB,EAAO,GAAID,IAEtC,CACEI,EAAIK,SAASI,aACbb,IAIFqB,EAAAA,UAAU,KACR,MAAMC,EAAe,KACnB,MAAMrB,EAAQK,IACdJ,EAASD,IAIX,OADAc,OAAOQ,iBAAiB,SAAUD,GAC3B,KAAQP,OAAOS,oBAAoB,SAAUF,KACnD,IAEH,MAAMG,EAAkBlB,cAAamB,IACnCA,EAAEC,iBACF,MAAM1B,EAAQK,IACdJ,EAASD,IACR,CAACK,IAGJe,EAAAA,UAAU,KACRN,OAAOQ,iBAAiB,YAAaE,GAC9B,KACLV,OAAOS,oBAAoB,YAAaC,KAEzC,CAACA,IAIJ,MAAMG,EAAuB,GAAX7B,EAAH,KAEf,OACE8B,UAAAC,cAAA,MAAA,CAAKC,UAAU,cAAc3B,IAAKA,EAAK4B,MAAO,CAAEJ,WAC9CC,EAAAA,QAAKC,cAAA,MAAA,CAAAC,UAAU,kBACbF,EAAAA,QAAAC,cAAA,MAAA,CAAKC,UAAU,gBACZF,EAAK,QAACI,SAASC,IAAIpC,EAAWqC,GACzBN,EAAK,QAACO,eAAqCD,GACtCN,EAAAA,QAAMQ,aAAaF,EAAO,CAAEG,QAASrC,IAEvCkC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import './index.css';
|
|
3
|
+
interface ScrollLockProps {
|
|
4
|
+
id: string;
|
|
5
|
+
maxCount: number;
|
|
6
|
+
duration: number;
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}
|
|
9
|
+
declare const ScrollLock: ({ id, children, duration, maxCount }: ScrollLockProps) => React.ReactElement;
|
|
10
|
+
export default ScrollLock;
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "scroll-lock-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A React component to lock scrolling on the webpage.",
|
|
5
|
+
"main": "dist/bundle.js",
|
|
6
|
+
"module": "dist/bundle.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "rollup -c",
|
|
9
|
+
"start": "rollup -c -w"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"react",
|
|
13
|
+
"scroll",
|
|
14
|
+
"lock"
|
|
15
|
+
],
|
|
16
|
+
"author": "Nedralia",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"react": "^18.2.0",
|
|
20
|
+
"react-dom": "^18.2.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
24
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
25
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
26
|
+
"@types/node": "^25.0.9",
|
|
27
|
+
"@types/react": "^18.2.0",
|
|
28
|
+
"rollup": "^2.56.3",
|
|
29
|
+
"rollup-plugin-postcss": "^4.0.0",
|
|
30
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
31
|
+
"rollup-plugin-typescript2": "^0.30.0",
|
|
32
|
+
"typescript": "^4.4.3"
|
|
33
|
+
},
|
|
34
|
+
"types": "dist/index.d.ts"
|
|
35
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
2
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
3
|
+
import typescript from '@rollup/plugin-typescript';
|
|
4
|
+
import postcss from 'rollup-plugin-postcss';
|
|
5
|
+
import { terser } from 'rollup-plugin-terser';
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
input: 'src/index.tsx',
|
|
9
|
+
output: {
|
|
10
|
+
file: 'dist/bundle.js',
|
|
11
|
+
format: 'umd',
|
|
12
|
+
name: 'ScrollLock',
|
|
13
|
+
sourcemap: true,
|
|
14
|
+
},
|
|
15
|
+
plugins: [
|
|
16
|
+
resolve(),
|
|
17
|
+
commonjs(),
|
|
18
|
+
typescript(),
|
|
19
|
+
postcss({
|
|
20
|
+
modules: true,
|
|
21
|
+
extract: true,
|
|
22
|
+
}),
|
|
23
|
+
terser(),
|
|
24
|
+
],
|
|
25
|
+
external: ['react', 'react-dom'],
|
|
26
|
+
};
|
package/src/index.css
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
.lock-scroll {
|
|
2
|
+
width: 100%;
|
|
3
|
+
z-index: 9999;
|
|
4
|
+
|
|
5
|
+
@media (max-width: 746px) {
|
|
6
|
+
display: none;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
.counter-view {
|
|
11
|
+
display: flex;
|
|
12
|
+
justify-content: center;
|
|
13
|
+
align-items: center;
|
|
14
|
+
width: 100%;
|
|
15
|
+
height: 100%;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.sticky-element {
|
|
19
|
+
position: sticky;
|
|
20
|
+
top: 0;
|
|
21
|
+
left: 0;
|
|
22
|
+
width: 100%;
|
|
23
|
+
height:100vh;
|
|
24
|
+
overflow: hidden;
|
|
25
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
import './index.css'
|
|
4
|
+
|
|
5
|
+
interface ScrollLockProps {
|
|
6
|
+
id: string
|
|
7
|
+
maxCount: number
|
|
8
|
+
duration: number
|
|
9
|
+
children: React.ReactNode
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface ScrollLockChildProps {
|
|
13
|
+
counter: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const ScrollLock = ({ id, children, duration, maxCount }: ScrollLockProps): React.ReactElement => {
|
|
17
|
+
const [count, setCount] = useState(0)
|
|
18
|
+
|
|
19
|
+
const ref = useRef<HTMLDivElement>(null)
|
|
20
|
+
|
|
21
|
+
const calculateCount = useCallback((): number => {
|
|
22
|
+
const refYPosition = -(ref.current?.getBoundingClientRect().top ?? 0)
|
|
23
|
+
const elementHeight = ref.current?.offsetHeight ?? 0
|
|
24
|
+
const navbarHeight = 80
|
|
25
|
+
const screenHeight = window.innerHeight - navbarHeight
|
|
26
|
+
|
|
27
|
+
if (refYPosition < 0) {
|
|
28
|
+
return 0
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const relativePosition = refYPosition / (elementHeight - screenHeight)
|
|
32
|
+
|
|
33
|
+
if (relativePosition > 1) {
|
|
34
|
+
return maxCount
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const count = relativePosition * maxCount
|
|
38
|
+
return Math.min(Math.max(count, 0), maxCount)
|
|
39
|
+
},
|
|
40
|
+
[
|
|
41
|
+
ref.current?.offsetHeight,
|
|
42
|
+
maxCount
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
// Handle scroll
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
const handleScroll = (): void => {
|
|
48
|
+
const count = calculateCount()
|
|
49
|
+
setCount(count)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
window.addEventListener('scroll', handleScroll)
|
|
53
|
+
return () => { window.removeEventListener('scroll', handleScroll) }
|
|
54
|
+
}, [])
|
|
55
|
+
|
|
56
|
+
const handleTouchMove = useCallback((e: TouchEvent) => {
|
|
57
|
+
e.preventDefault()
|
|
58
|
+
const count = calculateCount()
|
|
59
|
+
setCount(count)
|
|
60
|
+
}, [calculateCount])
|
|
61
|
+
|
|
62
|
+
// Handle touch events
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
window.addEventListener('touchmove', handleTouchMove)
|
|
65
|
+
return () => {
|
|
66
|
+
window.removeEventListener('touchmove', handleTouchMove)
|
|
67
|
+
}
|
|
68
|
+
}, [handleTouchMove])
|
|
69
|
+
|
|
70
|
+
// TODO: Add optional progress bar to show how far the user has scrolled in the section
|
|
71
|
+
|
|
72
|
+
const height = `${duration * 10}px` // Set this constant and the page y should be the same for every view port
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<div className="lock-scroll" ref={ref} style={{ height }}>
|
|
76
|
+
<div className='sticky-element'>
|
|
77
|
+
<div className="counter-view">
|
|
78
|
+
{React.Children.map(children, (child) => {
|
|
79
|
+
if (React.isValidElement<ScrollLockChildProps>(child)) {
|
|
80
|
+
return React.cloneElement(child, { counter: count })
|
|
81
|
+
}
|
|
82
|
+
return child;
|
|
83
|
+
})}
|
|
84
|
+
</div>
|
|
85
|
+
</div>
|
|
86
|
+
</div>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export default ScrollLock
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "esnext",
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"jsx": "react",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
|
10
|
+
"moduleResolution": "node",
|
|
11
|
+
"baseUrl": "./src",
|
|
12
|
+
"paths": {
|
|
13
|
+
"@/*": ["*"]
|
|
14
|
+
},
|
|
15
|
+
"declaration": true,
|
|
16
|
+
"outDir": "dist"
|
|
17
|
+
},
|
|
18
|
+
"include": ["src/**/*"],
|
|
19
|
+
"exclude": ["node_modules", "dist"]
|
|
20
|
+
}
|