atharv-mandlavdiya 1.2.1 → 1.2.2

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.
@@ -19,10 +19,8 @@ const progressCardStyles = `
19
19
 
20
20
  .progress-wrapper {
21
21
  position: relative;
22
- min-height: 1024px;
23
22
  width: 100%;
24
23
  box-sizing: border-box;
25
- overflow: visible;
26
24
  }
27
25
 
28
26
  progress-card {
@@ -31,18 +29,18 @@ progress-card {
31
29
  top: 30vh;
32
30
  height: 40vh;
33
31
  width: 100%;
34
- max-width: 100%;
35
32
  margin-bottom: 30vh;
33
+ overflow: hidden;
36
34
  scale: calc(0.5 + var(--progress) * 0.5);
37
35
  clip-path: rect(
38
- calc(20% - var(--progress) * 20%)
39
- calc(90% + var(--progress) * 10%)
40
- calc(80% + var(--progress) * 20%)
36
+ calc(20% - var(--progress) * 20%)
37
+ calc(90% + var(--progress) * 10%)
38
+ calc(80% + var(--progress) * 20%)
41
39
  calc(10% - var(--progress) * 10%)
42
40
  round calc(2.441rem - var(--progress) * 2.441rem)
43
41
  );
44
42
  transform: translateZ(0);
45
- overflow: hidden;
43
+ will-change: clip-path, scale;
46
44
  }
47
45
 
48
46
  @media (min-width: 1024px) {
@@ -54,10 +52,10 @@ progress-card {
54
52
  margin-right: auto;
55
53
  margin-bottom: 15vh;
56
54
  clip-path: rect(
57
- calc(20% - var(--progress) * 20%)
58
- calc(70% + var(--progress) * 30%)
55
+ calc(20% - var(--progress) * 20%)
56
+ calc(70% + var(--progress) * 30%)
59
57
  calc(80% + var(--progress) * 20%)
60
- calc(30% - var(--progress) * 30%)
58
+ calc(30% - var(--progress) * 30%)
61
59
  round calc(2.441rem - var(--progress) * 2.441rem)
62
60
  );
63
61
  }
@@ -74,7 +72,8 @@ progress-card > div {
74
72
  `;
75
73
 
76
74
  function injectProgressCardStyles(root) {
77
- if (!root.document || root.document.getElementById('atharv-progress-card-styles')) return;
75
+ if (!root.document) return;
76
+ if (root.document.getElementById('atharv-progress-card-styles')) return;
78
77
 
79
78
  const style = root.document.createElement('style');
80
79
  style.id = 'atharv-progress-card-styles';
@@ -82,11 +81,14 @@ function injectProgressCardStyles(root) {
82
81
  (root.document.head || root.document.documentElement).appendChild(style);
83
82
  }
84
83
 
84
+ function clamp(value, min, max) {
85
+ return Math.min(max, Math.max(min, value));
86
+ }
87
+
85
88
  function isScrollable(element, root) {
86
89
  const style = root.getComputedStyle(element);
87
90
  const overflow = `${style.overflow} ${style.overflowY}`;
88
- return /(auto|scroll|overlay)/.test(overflow) &&
89
- (element.scrollHeight > element.clientHeight || /(auto|scroll|overlay)/.test(overflow));
91
+ return /(auto|scroll|overlay)/.test(overflow);
90
92
  }
91
93
 
92
94
  function findScrollContainer(element, root) {
@@ -99,84 +101,138 @@ function findScrollContainer(element, root) {
99
101
  }
100
102
 
101
103
  function getScrollPosition(container, root) {
102
- return container === root ? (root.pageYOffset || root.scrollY || 0) : container.scrollTop;
104
+ if (container !== root) return container.scrollTop;
105
+ return root.scrollY || root.pageYOffset || 0;
103
106
  }
104
107
 
105
108
  function getViewportHeight(container, root) {
106
- return container === root ? root.innerHeight : container.clientHeight;
109
+ if (container !== root) return container.clientHeight;
110
+ return root.innerHeight;
107
111
  }
108
112
 
109
- function getWrapperPosition(wrapper, container, root) {
110
- const wrapperRect = wrapper.getBoundingClientRect();
111
- if (container === root) return wrapperRect.top + getScrollPosition(container, root);
113
+ const cards = new Set();
114
+ let frameQueued = false;
112
115
 
113
- const containerRect = container.getBoundingClientRect();
114
- return wrapperRect.top - containerRect.top + getScrollPosition(container, root);
116
+ function queueFrame() {
117
+ if (frameQueued) return;
118
+ frameQueued = true;
119
+ requestAnimationFrame(runFrame);
115
120
  }
116
121
 
117
- function clamp(value, min, max) {
118
- return Math.min(max, Math.max(min, value));
122
+ function runFrame() {
123
+ frameQueued = false;
124
+ for (const card of cards) {
125
+ if (card.dirty) card.measure();
126
+ }
127
+ for (const card of cards) card.render();
128
+ }
129
+
130
+ function markAllDirty() {
131
+ for (const card of cards) card.dirty = true;
132
+ queueFrame();
133
+ }
134
+
135
+ function releaseContainer(container) {
136
+ for (const card of cards) {
137
+ if (card.container === container) return;
138
+ }
139
+ container.removeEventListener('scroll', queueFrame);
119
140
  }
120
141
 
121
142
  function defineProgressCard(root) {
122
- if (!root.document || !root.customElements || root.customElements.get('progress-card')) return;
143
+ if (!root.document || !root.customElements) return;
144
+ if (root.customElements.get('progress-card')) return;
145
+
146
+ root.addEventListener('resize', markAllDirty, { passive: true });
123
147
 
124
148
  root.customElements.define('progress-card', class extends root.HTMLElement {
125
149
  constructor() {
126
150
  super();
127
- this._wrapper = null;
128
- this._scrollContainer = null;
129
- this._onScroll = () => this._updateProgress();
130
- this._onResize = () => this._updateProgress();
151
+ this.wrapper = null;
152
+ this.container = null;
153
+ this.observer = null;
154
+ this.mounting = false;
155
+ this.dirty = true;
156
+ this.start = 0;
157
+ this.range = 1;
158
+ this.last = -1;
131
159
  }
132
160
 
133
161
  connectedCallback() {
134
- if (!this._wrapper) {
135
- const scrollLength = Number.parseFloat(this.dataset.scrollLength) || 300;
136
- const wrapper = root.document.createElement('div');
137
- wrapper.className = 'progress-wrapper';
138
- wrapper.style.minHeight = `${scrollLength}vh`;
139
- this._wrapper = wrapper;
140
- this.parentNode.insertBefore(wrapper, this);
141
- wrapper.appendChild(this);
162
+ if (this.mounting) return;
163
+ if (!this.wrapper) this.mount();
164
+
165
+ this.container = findScrollContainer(this, root);
166
+ this.container.addEventListener('scroll', queueFrame, { passive: true });
167
+
168
+ if (root.ResizeObserver && !this.observer) {
169
+ this.observer = new root.ResizeObserver(markAllDirty);
170
+ this.observer.observe(this.wrapper);
142
171
  }
143
172
 
144
- this._scrollContainer = findScrollContainer(this, root);
145
- this._scrollContainer.addEventListener('scroll', this._onScroll, { passive: true });
146
- root.addEventListener('resize', this._onResize, { passive: true });
147
- this._updateProgress();
173
+ cards.add(this);
174
+ this.dirty = true;
175
+ queueFrame();
148
176
  }
149
177
 
150
178
  disconnectedCallback() {
151
- if (this._scrollContainer) {
152
- this._scrollContainer.removeEventListener('scroll', this._onScroll);
153
- this._scrollContainer = null;
154
- }
155
- root.removeEventListener('resize', this._onResize);
156
- if (this._wrapper && this._wrapper.parentNode && !this._wrapper.contains(this)) {
157
- this._wrapper.parentNode.removeChild(this._wrapper);
179
+ if (this.mounting) return;
180
+ cards.delete(this);
181
+
182
+ if (this.observer) this.observer.disconnect();
183
+ this.observer = null;
184
+
185
+ if (this.container) releaseContainer(this.container);
186
+ this.container = null;
187
+
188
+ const wrapper = this.wrapper;
189
+ this.wrapper = null;
190
+ if (!wrapper || !wrapper.parentNode) return;
191
+ if (wrapper.contains(this)) return;
192
+ wrapper.parentNode.removeChild(wrapper);
193
+ }
194
+
195
+ mount() {
196
+ const length = Number.parseFloat(this.dataset.scrollLength) || 300;
197
+ const wrapper = root.document.createElement('div');
198
+ wrapper.className = 'progress-wrapper';
199
+ wrapper.style.minHeight = `${length}vh`;
200
+
201
+ this.wrapper = wrapper;
202
+ this.mounting = true;
203
+ this.parentNode.insertBefore(wrapper, this);
204
+ wrapper.appendChild(this);
205
+ this.mounting = false;
206
+ }
207
+
208
+ measure() {
209
+ const container = this.container;
210
+ if (!container || !this.wrapper) return;
211
+
212
+ const viewport = getViewportHeight(container, root);
213
+ const rect = this.wrapper.getBoundingClientRect();
214
+ const scroll = getScrollPosition(container, root);
215
+
216
+ let top = rect.top + scroll;
217
+ if (container !== root) {
218
+ top = rect.top - container.getBoundingClientRect().top + scroll;
158
219
  }
159
- this._wrapper = null;
220
+
221
+ this.start = top;
222
+ this.range = Math.max(1, rect.height - viewport);
223
+ this.dirty = false;
160
224
  }
161
225
 
162
- _updateProgress() {
163
- if (!this._wrapper || !this._scrollContainer) return;
164
- const viewportHeight = getViewportHeight(this._scrollContainer, root);
165
- const wrapperStart = getWrapperPosition(this._wrapper, this._scrollContainer, root);
166
- const wrapperHeight = this._wrapper.offsetHeight ||
167
- this._wrapper.getBoundingClientRect().height;
168
- const sectionStart = wrapperStart - viewportHeight;
169
- const sectionEnd = wrapperStart + wrapperHeight - viewportHeight;
170
- const scrollPosition = getScrollPosition(this._scrollContainer, root);
171
- const progress = sectionEnd <= sectionStart
172
- ? (scrollPosition >= sectionEnd ? 1 : 0)
173
- : clamp(
174
- (scrollPosition - sectionStart) / (sectionEnd - sectionStart),
175
- 0,
176
- 1
177
- );
178
-
179
- this.style.setProperty('--progress', String(progress));
226
+ render() {
227
+ if (!this.container) return;
228
+
229
+ const scroll = getScrollPosition(this.container, root);
230
+ const raw = (scroll - this.start) / this.range;
231
+ const value = Math.round(clamp(raw, 0, 1) * 1e4) / 1e4;
232
+
233
+ if (value === this.last) return;
234
+ this.last = value;
235
+ this.style.setProperty('--progress', String(value));
180
236
  }
181
237
  });
182
238
  }
@@ -190,6 +246,63 @@ export function progressCard() {
190
246
  defineProgressCard(globalThis);
191
247
  }
192
248
 
249
+ export function smoothScroll(options = {}) {
250
+ const root = globalThis;
251
+ if (!root.document || !root.matchMedia) return;
252
+ if (root.matchMedia('(pointer: coarse)').matches) return;
253
+
254
+ const speed = options.speed ?? 0.09;
255
+ const accel = options.accel ?? 0.42;
256
+ const multiplier = options.multiplier ?? 1.1;
257
+ const maxDelta = options.maxDelta ?? 2400;
258
+ const stop = options.stop ?? 0.08;
259
+
260
+ let delta = 0;
261
+ let target = 0;
262
+ let current = 0;
263
+ let running = false;
264
+
265
+ function limit() {
266
+ const doc = root.document.documentElement;
267
+ return Math.max(0, doc.scrollHeight - root.innerHeight);
268
+ }
269
+
270
+ function step() {
271
+ if (delta === 0) {
272
+ running = false;
273
+ return;
274
+ }
275
+
276
+ delta = clamp(delta, -maxDelta, maxDelta);
277
+ target = clamp(target + delta * accel, 0, limit());
278
+
279
+ const lerped = (target - current) * speed;
280
+ current += lerped;
281
+ delta *= 1 - accel;
282
+
283
+ if (Math.abs(lerped) < stop) {
284
+ current = Math.round(target);
285
+ delta = 0;
286
+ }
287
+
288
+ root.scrollTo(0, current);
289
+ requestAnimationFrame(step);
290
+ }
291
+
292
+ root.addEventListener('wheel', (event) => {
293
+ event.preventDefault();
294
+
295
+ if (!running) {
296
+ target = getScrollPosition(root, root);
297
+ current = target;
298
+ running = true;
299
+ requestAnimationFrame(step);
300
+ }
301
+
302
+ delta += event.deltaY * multiplier;
303
+ }, { passive: false });
304
+ }
305
+
193
306
  const _svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 650 200" width="650" height="200">
194
307
  <defs>
195
308
  <linearGradient id="pathGradient" x1="0%" y1="0%" x2="100%" y2="0%">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "atharv-mandlavdiya",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Made With Love By Atharv Mandlavdiya",
5
5
  "type": "module",
6
6
  "main": "atharvmandlavdiya.js",
@@ -1,213 +0,0 @@
1
- import test, { afterEach } from 'node:test';
2
- import assert from 'node:assert/strict';
3
-
4
- class FakeStyle {
5
- setProperty(name, value) {
6
- this[name] = value;
7
- }
8
-
9
- getPropertyValue(name) {
10
- return this[name] || '';
11
- }
12
- }
13
-
14
- class FakeElement {
15
- constructor() {
16
- this.children = [];
17
- this.attributes = new Map();
18
- this.style = new FakeStyle();
19
- this.listeners = new Map();
20
- this.parentElement = null;
21
- this._rect = { top: 0, height: 0 };
22
- this._height = 0;
23
- this.clientHeight = 0;
24
- this.scrollHeight = 0;
25
- this.scrollTop = 0;
26
- }
27
-
28
- get parentNode() {
29
- return this.parentElement;
30
- }
31
-
32
- get dataset() {
33
- return Object.fromEntries([...this.attributes]
34
- .filter(([name]) => name.startsWith('data-'))
35
- .map(([name, value]) => [
36
- name.slice(5).replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()),
37
- value
38
- ]));
39
- }
40
-
41
- get offsetHeight() {
42
- return this._height;
43
- }
44
-
45
- set className(value) {
46
- this.attributes.set('class', value);
47
- }
48
-
49
- get className() {
50
- return this.attributes.get('class') || '';
51
- }
52
-
53
- setAttribute(name, value) {
54
- this.attributes.set(name, String(value));
55
- }
56
-
57
- hasAttribute(name) {
58
- return this.attributes.has(name);
59
- }
60
-
61
- appendChild(child) {
62
- child.parentElement = this;
63
- this.children.push(child);
64
- return child;
65
- }
66
-
67
- insertBefore(child, reference) {
68
- child.parentElement = this;
69
- const index = this.children.indexOf(reference);
70
- this.children.splice(index < 0 ? this.children.length : index, 0, child);
71
- return child;
72
- }
73
-
74
- removeChild(child) {
75
- this.children.splice(this.children.indexOf(child), 1);
76
- child.parentElement = null;
77
- return child;
78
- }
79
-
80
- contains(child) {
81
- return this.children.includes(child) || this.children.some((item) => item.contains(child));
82
- }
83
-
84
- getBoundingClientRect() {
85
- return this._rect;
86
- }
87
-
88
- addEventListener(type, listener) {
89
- if (!this.listeners.has(type)) this.listeners.set(type, new Set());
90
- this.listeners.get(type).add(listener);
91
- }
92
-
93
- removeEventListener(type, listener) {
94
- this.listeners.get(type)?.delete(listener);
95
- }
96
- }
97
-
98
- class FakeHTMLElement extends FakeElement {}
99
-
100
- const body = new FakeElement();
101
- const head = new FakeElement();
102
- const documentElement = new FakeElement();
103
- globalThis.HTMLElement = FakeHTMLElement;
104
- globalThis.document = {
105
- body,
106
- head,
107
- documentElement,
108
- createElement: () => new FakeElement(),
109
- getElementById(id) {
110
- return [head, ...head.children].find((element) => element.attributes.get('id') === id) || null;
111
- }
112
- };
113
- globalThis.customElements = {
114
- registry: new Map(),
115
- define(name, constructor) {
116
- this.registry.set(name, constructor);
117
- },
118
- get(name) {
119
- return this.registry.get(name);
120
- }
121
- };
122
- globalThis.innerHeight = 1000;
123
- globalThis.pageYOffset = 0;
124
- globalThis.scrollY = 0;
125
- globalThis.listeners = new Map();
126
- globalThis.addEventListener = (type, listener) => {
127
- if (!globalThis.listeners.has(type)) globalThis.listeners.set(type, new Set());
128
- globalThis.listeners.get(type).add(listener);
129
- };
130
- globalThis.removeEventListener = (type, listener) => globalThis.listeners.get(type)?.delete(listener);
131
- globalThis.getComputedStyle = (element) => ({
132
- overflow: element._overflow || 'visible',
133
- overflowY: element._overflowY || 'visible'
134
- });
135
-
136
- await import('../atharvmandlavdiya.js');
137
-
138
- const ProgressCard = customElements.get('progress-card');
139
- const createdCards = [];
140
-
141
- afterEach(() => {
142
- for (const card of createdCards.splice(0)) {
143
- card.parentElement?.removeChild(card);
144
- card.disconnectedCallback();
145
- }
146
- globalThis.pageYOffset = 0;
147
- });
148
-
149
- function createCard(parent = body, scrollLength = '300') {
150
- const card = new ProgressCard();
151
- card.setAttribute('data-scroll-length', scrollLength);
152
- parent.appendChild(card);
153
- card.connectedCallback();
154
- createdCards.push(card);
155
- const wrapper = card.parentElement;
156
- wrapper._height = 3000;
157
- wrapper._rect = { top: 0, height: 3000 };
158
- card._onScroll();
159
- return { card, wrapper };
160
- }
161
-
162
- test('registers automatically, injects CSS, and needs no StringTune', () => {
163
- assert.equal(typeof ProgressCard, 'function');
164
- assert.match(head.children[0].textContent || head.children[0].style?.textContent || '', /progress-wrapper/);
165
- assert.equal(globalThis.StringTune, undefined);
166
- });
167
-
168
- test('uses the window scroll range and clamps at both ends', () => {
169
- const { card } = createCard();
170
- assert.equal(card.style.getPropertyValue('--progress'), '0');
171
- globalThis.pageYOffset = 1000;
172
- card.parentElement._rect = { top: -1000, height: 3000 };
173
- card._onScroll();
174
- assert.equal(card.style.getPropertyValue('--progress'), '0.5');
175
- globalThis.pageYOffset = 2000;
176
- card.parentElement._rect = { top: -2000, height: 3000 };
177
- card._onScroll();
178
- assert.equal(card.style.getPropertyValue('--progress'), '1');
179
- globalThis.pageYOffset = 2500;
180
- card.parentElement._rect = { top: -2500, height: 3000 };
181
- card._onScroll();
182
- assert.equal(card.style.getPropertyValue('--progress'), '1');
183
- });
184
-
185
- test('uses the nearest nested scroll container', () => {
186
- globalThis.pageYOffset = 0;
187
- const brand = new FakeElement();
188
- brand._overflowY = 'auto';
189
- brand.clientHeight = 500;
190
- brand.scrollHeight = 2000;
191
- brand._rect = { top: 10, height: 500 };
192
- body.appendChild(brand);
193
- const { card, wrapper } = createCard(brand);
194
- wrapper._height = 1500;
195
- wrapper._rect = { top: 10, height: 1500 };
196
- brand.scrollTop = 500;
197
- wrapper._rect = { top: -490, height: 1500 };
198
- card._onScroll();
199
- assert.equal(card.style.getPropertyValue('--progress'), '0.5');
200
- });
201
-
202
- test('keeps cards independent and removes listeners on disconnect', () => {
203
- const first = createCard();
204
- const second = createCard();
205
- assert.notEqual(first.card, second.card);
206
- assert.equal(globalThis.listeners.get('resize').size >= 2, true);
207
- first.card.parentElement.removeChild(first.card);
208
- first.card.disconnectedCallback();
209
- assert.equal(globalThis.listeners.get('resize').size >= 1, true);
210
- second.card.parentElement.removeChild(second.card);
211
- second.card.disconnectedCallback();
212
- assert.equal(globalThis.listeners.get('resize').size, 0);
213
- });