Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

scrollmirror

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

scrollmirror - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

LICENSE

2

dist/index.modern.js

@@ -1,2 +0,2 @@

function e(){return e=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var l in o)Object.prototype.hasOwnProperty.call(o,l)&&(e[l]=o[l])}return e},e.apply(this,arguments)}const t=({clientWidth:e,clientHeight:t,scrollWidth:o,scrollHeight:l})=>l>t||o>e,o=e=>{const t=window.getComputedStyle(e).overflow;return t.includes("auto")||t.includes("scroll")};class l{constructor(t,o={}){var l=this;this.elements=void 0,this.defaults={proportional:!0,vertical:!0,horizontal:!0},this.options=void 0,this.paused=!1,this.prefix="[scroll-mirror]",this.handleScroll=async function(e){if(l.paused)return;const t=l.getScrollContainer(e.target);await new Promise(e=>{requestAnimationFrame(()=>e())}),l.elements.forEach(e=>{t!==e&&(l.removeHandler(e),l.mirrorScrollPosition(t,e),window.requestAnimationFrame(()=>{l.addHandler(e)}))})},this.elements=[...t].map(e=>this.getScrollContainer(e)),this.options=e({},this.defaults,o),this.validateElements()&&this.elements.forEach(e=>this.addHandler(e))}pause(){this.paused=!0}resume(){this.paused=!1}destroy(){this.elements.forEach(e=>this.removeHandler(e))}validateElements(){const e=[...this.elements];if(e.length<2)return console.error(`${this.prefix} Please provide at least two elements`),!1;for(const l of e){if(!l)return console.warn(`${this.prefix} element is not defined:`,l),!1;l instanceof HTMLElement&&!t(l)&&console.warn(`${this.prefix} element doesn't have overflow:`,l),l instanceof HTMLElement&&!o(l)&&console.warn(`${this.prefix} no "overflow: auto;" or "overflow: scroll;" set on element:`,l)}return!0}addHandler(e){e.addEventListener("scroll",this.handleScroll)}removeHandler(e){e.removeEventListener("scroll",this.handleScroll)}getScrollContainer(e){return e instanceof HTMLElement&&e.matches("body *")?e:window}mirrorScrollPosition(e,t){const{scrollTop:o,scrollHeight:l,clientHeight:r,scrollLeft:i,scrollWidth:s,clientWidth:n}=this.getProps(e),{scrollHeight:c,scrollWidth:a}=this.getProps(t),h=l-r,d=s-n,{proportional:f,vertical:m,horizontal:p}=this.options,u=a-n;m&&h>0&&this.setScrollTop(t,f?(c-r)*o/h:o),p&&d>0&&this.setScrollLeft(t,f?u*i/d:i)}setScrollTop(e,t){e instanceof Window?e.scrollTo(e.scrollX,t):e.scrollTop=t}setScrollLeft(e,t){e instanceof Window?e.scrollTo(t,e.scrollY):e.scrollLeft=t}getProps(e){return e instanceof Window?{scrollTop:window.scrollY,scrollHeight:document.documentElement.scrollHeight,clientHeight:window.innerHeight,scrollLeft:window.scrollX,scrollWidth:document.documentElement.scrollWidth,clientWidth:window.innerWidth}:{scrollTop:e.scrollTop,scrollHeight:e.scrollHeight,clientHeight:e.clientHeight,scrollLeft:e.scrollLeft,scrollWidth:e.scrollWidth,clientWidth:e.clientWidth}}}export{l as default};
function e(){return e=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var l in o)Object.prototype.hasOwnProperty.call(o,l)&&(e[l]=o[l])}return e},e.apply(this,arguments)}const t=({clientWidth:e,clientHeight:t,scrollWidth:o,scrollHeight:l})=>l>t||o>e,o=e=>{const t=window.getComputedStyle(e).overflow;return t.includes("auto")||t.includes("scroll")};class l{constructor(t,o={}){var l=this;this.elements=void 0,this.defaults={proportional:!0,vertical:!0,horizontal:!0},this.options=void 0,this.paused=!1,this.prefix="[scroll-mirror]",this.handleScroll=async function(e){if(l.paused)return;const t=l.getScrollContainer(e.currentTarget);await new Promise(e=>{requestAnimationFrame(()=>e())}),l.elements.forEach(e=>{t!==e&&(l.removeHandler(e),l.mirrorScrollPosition(t,e),window.requestAnimationFrame(()=>{l.addHandler(e)}))})},this.elements=[...t].filter(Boolean).map(e=>this.getScrollContainer(e)),this.options=e({},this.defaults,o),this.validateElements()&&this.elements.forEach(e=>this.addHandler(e))}pause(){this.paused=!0}resume(){this.paused=!1}destroy(){this.elements.forEach(e=>this.removeHandler(e))}validateElements(){const e=[...this.elements];if(e.length<2)return console.error(`${this.prefix} Please provide at least two elements`),!1;for(const l of e){if(!l)return console.warn(`${this.prefix} element is not defined:`,l),!1;l instanceof HTMLElement&&!t(l)&&console.warn(`${this.prefix} element doesn't have overflow:`,l),l instanceof HTMLElement&&!o(l)&&console.warn(`${this.prefix} no "overflow: auto;" or "overflow: scroll;" set on element:`,l)}return!0}addHandler(e){e.addEventListener("scroll",this.handleScroll)}removeHandler(e){e.removeEventListener("scroll",this.handleScroll)}getScrollContainer(e){return e instanceof HTMLElement&&e.matches("body *")?e:window}mirrorScrollPosition(e,t){const{scrollTop:o,scrollHeight:l,clientHeight:r,scrollLeft:i,scrollWidth:n,clientWidth:s}=this.getProps(e),{scrollHeight:c,scrollWidth:a}=this.getProps(t),h=l-r,d=n-s,{proportional:m,vertical:f,horizontal:p}=this.options,u=a-s;f&&h>0&&t.scrollTo({top:m?(c-r)*o/h:o,behavior:"instant"}),p&&d>0&&t.scrollTo({left:m?u*i/d:i,behavior:"instant"})}getProps(e){return e instanceof Window?{scrollTop:window.scrollY,scrollHeight:document.documentElement.scrollHeight,clientHeight:window.innerHeight,scrollLeft:window.scrollX,scrollWidth:document.documentElement.scrollWidth,clientWidth:window.innerWidth}:{scrollTop:e.scrollTop,scrollHeight:e.scrollHeight,clientHeight:e.clientHeight,scrollLeft:e.scrollLeft,scrollWidth:e.scrollWidth,clientWidth:e.clientWidth}}}export{l as default};
//# sourceMappingURL=index.modern.js.map

@@ -1,2 +0,2 @@

const e=({clientWidth:e,clientHeight:t,scrollWidth:o,scrollHeight:l})=>l>t||o>e,t=e=>{const t=window.getComputedStyle(e).overflow;return t.includes("auto")||t.includes("scroll")};class o{constructor(e,t={}){const o=this;this.elements=void 0,this.defaults={proportional:!0,vertical:!0,horizontal:!0},this.options=void 0,this.paused=!1,this.prefix="[scroll-mirror]",this.handleScroll=function(e){try{if(o.paused)return Promise.resolve();const t=o.getScrollContainer(e.target);return Promise.resolve(new Promise(e=>{requestAnimationFrame(()=>e())})).then(function(){o.elements.forEach(e=>{t!==e&&(o.removeHandler(e),o.mirrorScrollPosition(t,e),window.requestAnimationFrame(()=>{o.addHandler(e)}))})})}catch(e){return Promise.reject(e)}},this.elements=[...e].map(e=>this.getScrollContainer(e)),this.options={...this.defaults,...t},this.validateElements()&&this.elements.forEach(e=>this.addHandler(e))}pause(){this.paused=!0}resume(){this.paused=!1}destroy(){this.elements.forEach(e=>this.removeHandler(e))}validateElements(){const o=[...this.elements];if(o.length<2)return console.error(`${this.prefix} Please provide at least two elements`),!1;for(const l of o){if(!l)return console.warn(`${this.prefix} element is not defined:`,l),!1;l instanceof HTMLElement&&!e(l)&&console.warn(`${this.prefix} element doesn't have overflow:`,l),l instanceof HTMLElement&&!t(l)&&console.warn(`${this.prefix} no "overflow: auto;" or "overflow: scroll;" set on element:`,l)}return!0}addHandler(e){e.addEventListener("scroll",this.handleScroll)}removeHandler(e){e.removeEventListener("scroll",this.handleScroll)}getScrollContainer(e){return e instanceof HTMLElement&&e.matches("body *")?e:window}mirrorScrollPosition(e,t){const{scrollTop:o,scrollHeight:l,clientHeight:r,scrollLeft:s,scrollWidth:i,clientWidth:n}=this.getProps(e),{scrollHeight:c,scrollWidth:h}=this.getProps(t),d=l-r,a=i-n,{proportional:m,vertical:f,horizontal:u}=this.options,p=h-n;f&&d>0&&this.setScrollTop(t,m?(c-r)*o/d:o),u&&a>0&&this.setScrollLeft(t,m?p*s/a:s)}setScrollTop(e,t){e instanceof Window?e.scrollTo(e.scrollX,t):e.scrollTop=t}setScrollLeft(e,t){e instanceof Window?e.scrollTo(t,e.scrollY):e.scrollLeft=t}getProps(e){return e instanceof Window?{scrollTop:window.scrollY,scrollHeight:document.documentElement.scrollHeight,clientHeight:window.innerHeight,scrollLeft:window.scrollX,scrollWidth:document.documentElement.scrollWidth,clientWidth:window.innerWidth}:{scrollTop:e.scrollTop,scrollHeight:e.scrollHeight,clientHeight:e.clientHeight,scrollLeft:e.scrollLeft,scrollWidth:e.scrollWidth,clientWidth:e.clientWidth}}}export{o as default};
const e=({clientWidth:e,clientHeight:t,scrollWidth:o,scrollHeight:l})=>l>t||o>e,t=e=>{const t=window.getComputedStyle(e).overflow;return t.includes("auto")||t.includes("scroll")};class o{constructor(e,t={}){const o=this;this.elements=void 0,this.defaults={proportional:!0,vertical:!0,horizontal:!0},this.options=void 0,this.paused=!1,this.prefix="[scroll-mirror]",this.handleScroll=function(e){try{if(o.paused)return Promise.resolve();const t=o.getScrollContainer(e.currentTarget);return Promise.resolve(new Promise(e=>{requestAnimationFrame(()=>e())})).then(function(){o.elements.forEach(e=>{t!==e&&(o.removeHandler(e),o.mirrorScrollPosition(t,e),window.requestAnimationFrame(()=>{o.addHandler(e)}))})})}catch(e){return Promise.reject(e)}},this.elements=[...e].filter(Boolean).map(e=>this.getScrollContainer(e)),this.options={...this.defaults,...t},this.validateElements()&&this.elements.forEach(e=>this.addHandler(e))}pause(){this.paused=!0}resume(){this.paused=!1}destroy(){this.elements.forEach(e=>this.removeHandler(e))}validateElements(){const o=[...this.elements];if(o.length<2)return console.error(`${this.prefix} Please provide at least two elements`),!1;for(const l of o){if(!l)return console.warn(`${this.prefix} element is not defined:`,l),!1;l instanceof HTMLElement&&!e(l)&&console.warn(`${this.prefix} element doesn't have overflow:`,l),l instanceof HTMLElement&&!t(l)&&console.warn(`${this.prefix} no "overflow: auto;" or "overflow: scroll;" set on element:`,l)}return!0}addHandler(e){e.addEventListener("scroll",this.handleScroll)}removeHandler(e){e.removeEventListener("scroll",this.handleScroll)}getScrollContainer(e){return e instanceof HTMLElement&&e.matches("body *")?e:window}mirrorScrollPosition(e,t){const{scrollTop:o,scrollHeight:l,clientHeight:r,scrollLeft:i,scrollWidth:n,clientWidth:s}=this.getProps(e),{scrollHeight:c,scrollWidth:h}=this.getProps(t),a=l-r,d=n-s,{proportional:m,vertical:f,horizontal:u}=this.options,p=h-s;f&&a>0&&t.scrollTo({top:m?(c-r)*o/a:o,behavior:"instant"}),u&&d>0&&t.scrollTo({left:m?p*i/d:i,behavior:"instant"})}getProps(e){return e instanceof Window?{scrollTop:window.scrollY,scrollHeight:document.documentElement.scrollHeight,clientHeight:window.innerHeight,scrollLeft:window.scrollX,scrollWidth:document.documentElement.scrollWidth,clientWidth:window.innerWidth}:{scrollTop:e.scrollTop,scrollHeight:e.scrollHeight,clientHeight:e.clientHeight,scrollLeft:e.scrollLeft,scrollWidth:e.scrollWidth,clientWidth:e.clientWidth}}}export{o as default};
//# sourceMappingURL=index.module.js.map

@@ -1,2 +0,2 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e||self).scrollmirror=t()}(this,function(){const e=e=>{let{clientWidth:t,clientHeight:o,scrollWidth:l,scrollHeight:r}=e;return r>o||l>t},t=e=>{const t=window.getComputedStyle(e).overflow;return t.includes("auto")||t.includes("scroll")};return class{constructor(e,t){const o=this;void 0===t&&(t={}),this.elements=void 0,this.defaults={proportional:!0,vertical:!0,horizontal:!0},this.options=void 0,this.paused=!1,this.prefix="[scroll-mirror]",this.handleScroll=function(e){try{if(o.paused)return Promise.resolve();const t=o.getScrollContainer(e.target);return Promise.resolve(new Promise(e=>{requestAnimationFrame(()=>e())})).then(function(){o.elements.forEach(e=>{t!==e&&(o.removeHandler(e),o.mirrorScrollPosition(t,e),window.requestAnimationFrame(()=>{o.addHandler(e)}))})})}catch(e){return Promise.reject(e)}},this.elements=[...e].map(e=>this.getScrollContainer(e)),this.options={...this.defaults,...t},this.validateElements()&&this.elements.forEach(e=>this.addHandler(e))}pause(){this.paused=!0}resume(){this.paused=!1}destroy(){this.elements.forEach(e=>this.removeHandler(e))}validateElements(){const o=[...this.elements];if(o.length<2)return console.error(`${this.prefix} Please provide at least two elements`),!1;for(const l of o){if(!l)return console.warn(`${this.prefix} element is not defined:`,l),!1;l instanceof HTMLElement&&!e(l)&&console.warn(`${this.prefix} element doesn't have overflow:`,l),l instanceof HTMLElement&&!t(l)&&console.warn(`${this.prefix} no "overflow: auto;" or "overflow: scroll;" set on element:`,l)}return!0}addHandler(e){e.addEventListener("scroll",this.handleScroll)}removeHandler(e){e.removeEventListener("scroll",this.handleScroll)}getScrollContainer(e){return e instanceof HTMLElement&&e.matches("body *")?e:window}mirrorScrollPosition(e,t){const{scrollTop:o,scrollHeight:l,clientHeight:r,scrollLeft:i,scrollWidth:n,clientWidth:s}=this.getProps(e),{scrollHeight:c,scrollWidth:d}=this.getProps(t),h=l-r,a=n-s,{proportional:f,vertical:m,horizontal:u}=this.options,p=d-s;m&&h>0&&this.setScrollTop(t,f?(c-r)*o/h:o),u&&a>0&&this.setScrollLeft(t,f?p*i/a:i)}setScrollTop(e,t){e instanceof Window?e.scrollTo(e.scrollX,t):e.scrollTop=t}setScrollLeft(e,t){e instanceof Window?e.scrollTo(t,e.scrollY):e.scrollLeft=t}getProps(e){return e instanceof Window?{scrollTop:window.scrollY,scrollHeight:document.documentElement.scrollHeight,clientHeight:window.innerHeight,scrollLeft:window.scrollX,scrollWidth:document.documentElement.scrollWidth,clientWidth:window.innerWidth}:{scrollTop:e.scrollTop,scrollHeight:e.scrollHeight,clientHeight:e.clientHeight,scrollLeft:e.scrollLeft,scrollWidth:e.scrollWidth,clientWidth:e.clientWidth}}}});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e||self).scrollmirror=t()}(this,function(){const e=e=>{let{clientWidth:t,clientHeight:o,scrollWidth:l,scrollHeight:r}=e;return r>o||l>t},t=e=>{const t=window.getComputedStyle(e).overflow;return t.includes("auto")||t.includes("scroll")};return class{constructor(e,t){const o=this;void 0===t&&(t={}),this.elements=void 0,this.defaults={proportional:!0,vertical:!0,horizontal:!0},this.options=void 0,this.paused=!1,this.prefix="[scroll-mirror]",this.handleScroll=function(e){try{if(o.paused)return Promise.resolve();const t=o.getScrollContainer(e.currentTarget);return Promise.resolve(new Promise(e=>{requestAnimationFrame(()=>e())})).then(function(){o.elements.forEach(e=>{t!==e&&(o.removeHandler(e),o.mirrorScrollPosition(t,e),window.requestAnimationFrame(()=>{o.addHandler(e)}))})})}catch(e){return Promise.reject(e)}},this.elements=[...e].filter(Boolean).map(e=>this.getScrollContainer(e)),this.options={...this.defaults,...t},this.validateElements()&&this.elements.forEach(e=>this.addHandler(e))}pause(){this.paused=!0}resume(){this.paused=!1}destroy(){this.elements.forEach(e=>this.removeHandler(e))}validateElements(){const o=[...this.elements];if(o.length<2)return console.error(`${this.prefix} Please provide at least two elements`),!1;for(const l of o){if(!l)return console.warn(`${this.prefix} element is not defined:`,l),!1;l instanceof HTMLElement&&!e(l)&&console.warn(`${this.prefix} element doesn't have overflow:`,l),l instanceof HTMLElement&&!t(l)&&console.warn(`${this.prefix} no "overflow: auto;" or "overflow: scroll;" set on element:`,l)}return!0}addHandler(e){e.addEventListener("scroll",this.handleScroll)}removeHandler(e){e.removeEventListener("scroll",this.handleScroll)}getScrollContainer(e){return e instanceof HTMLElement&&e.matches("body *")?e:window}mirrorScrollPosition(e,t){const{scrollTop:o,scrollHeight:l,clientHeight:r,scrollLeft:i,scrollWidth:n,clientWidth:s}=this.getProps(e),{scrollHeight:c,scrollWidth:d}=this.getProps(t),h=l-r,a=n-s,{proportional:f,vertical:m,horizontal:u}=this.options,p=d-s;m&&h>0&&t.scrollTo({top:f?(c-r)*o/h:o,behavior:"instant"}),u&&a>0&&t.scrollTo({left:f?p*i/a:i,behavior:"instant"})}getProps(e){return e instanceof Window?{scrollTop:window.scrollY,scrollHeight:document.documentElement.scrollHeight,clientHeight:window.innerHeight,scrollLeft:window.scrollX,scrollWidth:document.documentElement.scrollWidth,clientWidth:window.innerWidth}:{scrollTop:e.scrollTop,scrollHeight:e.scrollHeight,clientHeight:e.clientHeight,scrollLeft:e.scrollLeft,scrollWidth:e.scrollWidth,clientWidth:e.clientWidth}}}});
//# sourceMappingURL=index.umd.js.map

@@ -39,3 +39,3 @@ type ScrollContainer = Window | HTMLElement;

* Get the scroll container, based on element provided:
* - return the element if an HTMLElement and a child of <body>
* - return the element if it's a child of <body>
* - otherwise, return the window

@@ -42,0 +42,0 @@ */

{
"name": "scrollmirror",
"version": "0.0.1",
"description": "Sync the scoll position of multiple elements on your page",
"version": "0.0.2",
"description": "Sync the scroll position of multiple elements on your page",
"author": {

@@ -39,11 +39,11 @@ "name": "Rasso Hilber",

"watch": "BROWSERSLIST_ENV=development microbundle src/index.ts --watch --format modern",
"docs:dev": "cd docs && astro dev",
"docs:build": "cd docs && astro check && astro build",
"docs:dev": "astro dev --root docs",
"docs:build": "astro build --root docs",
"prepublishOnly": "npm run build",
"test": "npm run test:e2e",
"test:e2e": "npx playwright test --config ./tests/playwright/config.ts",
"test:e2e:dev": "npm run test:e2e -- --ui"
"test:e2e": "npx playwright test --config ./tests/e2e/config.playwright.ts",
"test:e2e:dev": "npm run test:e2e -- --ui",
"test:e2e:serve": "npm run docs:build && npx serve -n -S -L -p 8274 --config ./tests/e2e/config.serve.json"
},
"devDependencies": {
"@astrojs/check": "^0.3.1",
"@astrojs/mdx": "^1.1.5",

@@ -53,6 +53,8 @@ "@playwright/test": "^1.40.1",

"astro-expressive-code": "^0.29.2",
"astro-feather": "^1.0.0",
"microbundle": "^0.15.1",
"prettier": "^3.1.0",
"serve": "^14.2.1",
"typescript": "^5.3.2"
}
}

@@ -1,6 +0,2 @@

import {
hasCSSOverflow,
hasOverflow,
nextTick,
} from "./support/utils.js";
import { hasCSSOverflow, hasOverflow, nextTick } from "./support/utils.js";

@@ -50,3 +46,5 @@ type ScrollContainer = Window | HTMLElement;

) {
this.elements = [...elements].map((el) => this.getScrollContainer(el));
this.elements = [...elements]
.filter(Boolean)
.map((el) => this.getScrollContainer(el));

@@ -112,3 +110,3 @@ this.options = { ...this.defaults, ...options };

* Get the scroll container, based on element provided:
* - return the element if an HTMLElement and a child of <body>
* - return the element if it's a child of <body>
* - otherwise, return the window

@@ -127,3 +125,3 @@ */

const scrolledElement = this.getScrollContainer(event.target);
const scrolledElement = this.getScrollContainer(event.currentTarget);

@@ -176,37 +174,13 @@ await nextTick();

/* Adjust the scroll position of it accordingly */
/* Adjust the scroll position accordingly */
if (vertical && scrollTopOffset > 0) {
this.setScrollTop(
element,
proportional ? (elementHeight * scrollTop) / scrollTopOffset : scrollTop
);
const top = proportional ? (elementHeight * scrollTop) / scrollTopOffset : scrollTop // prettier-ignore
element.scrollTo({ top, behavior: "instant" });
}
if (horizontal && scrollLeftOffset > 0) {
this.setScrollLeft(
element,
proportional
? (elementWidth * scrollLeft) / scrollLeftOffset
: scrollLeft
);
const left = proportional ? (elementWidth * scrollLeft) / scrollLeftOffset : scrollLeft // prettier-ignore
element.scrollTo({ left, behavior: "instant" });
}
}
/** set the scrollTop position on a scroll container @internal */
setScrollTop(element: ScrollContainer, y: number): void {
if (element instanceof Window) {
element.scrollTo(element.scrollX, y);
return;
}
element.scrollTop = y;
}
/** set the scrollLeft position on a scroll container @internal */
setScrollLeft(element: ScrollContainer, x: number): void {
if (element instanceof Window) {
element.scrollTo(x, element.scrollY);
return;
}
element.scrollLeft = x;
}
/** Get required properties from either the window or an HTMLElement */

@@ -213,0 +187,0 @@ getProps(element: ScrollContainer): ScrollContainerProps {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc