@linear_non/stellar-libs 1.0.25 → 1.0.26

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Sticky/index.js +14 -36
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linear_non/stellar-libs",
3
- "version": "1.0.25",
3
+ "version": "1.0.26",
4
4
  "description": "Reusable JavaScript libraries for Non-Linear Studio projects.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -5,11 +5,13 @@ import { gsap, ScrollTrigger } from "@linear_non/stellar-kit/gsap"
5
5
  export default class Sticky {
6
6
  constructor(obj) {
7
7
  const { el, sticky, isSmooth, onUpdateCallback } = obj
8
+
8
9
  this.el = el
9
10
  this.sticky = sticky
10
11
  this.isSticky = false
11
12
  this.isSmooth = isSmooth
12
13
  this.progress = 0
14
+ this.total = 0
13
15
  this.positionX = 0
14
16
  this.positionY = 0
15
17
  this.scrollTrigger = null
@@ -22,26 +24,23 @@ export default class Sticky {
22
24
  totalHeight: 0,
23
25
  totalItems: 0,
24
26
  }
25
-
26
27
  if (!isSmooth) sticky.style.pointerEvents = "none"
27
28
  this.init()
28
29
  }
29
30
 
30
31
  observer() {
31
- if (this.scrollTrigger) this.scrollTrigger.kill()
32
+ if (this.scrollTrigger) this.scrollTrigger.kill() // Prevent duplicate listeners
32
33
 
33
34
  this.scrollTrigger = ScrollTrigger.create({
34
35
  trigger: this.el,
35
36
  start: "top top",
36
37
  end: "bottom bottom",
37
38
  scrub: true,
38
- invalidateOnRefresh: true,
39
39
  onEnter: () => this.toggleSticky(true),
40
40
  onEnterBack: () => this.toggleSticky(true),
41
41
  onLeave: () => this.toggleSticky(false, true),
42
42
  onLeaveBack: () => this.toggleSticky(false),
43
43
  onUpdate: self => this.onUpdate(self),
44
- onRefresh: self => this.onRefresh(self),
45
44
  })
46
45
  }
47
46
 
@@ -68,13 +67,17 @@ export default class Sticky {
68
67
  const newY = newProgress * totalHeight
69
68
  const newX = -newProgress * totalWidth
70
69
 
70
+ // Only update if values change to prevent unnecessary calls
71
71
  if (this.progress !== newProgress || this.positionY !== newY || this.positionX !== newX) {
72
72
  this.progress = newProgress
73
73
  this.positionY = newY
74
74
  this.positionX = newX
75
75
 
76
- if (this.isSmooth) gsap.set(this.sticky, { y: this.positionY })
76
+ if (this.isSmooth) {
77
+ gsap.set(this.sticky, { y: this.positionY })
78
+ }
77
79
 
80
+ // Call the callback function if provided
78
81
  if (this.onUpdateCallback) {
79
82
  this.onUpdateCallback({
80
83
  progress: this.progress,
@@ -85,43 +88,18 @@ export default class Sticky {
85
88
  }
86
89
  }
87
90
 
88
- onRefresh(self) {
89
- this.computeSizes()
90
-
91
- const { totalWidth, totalHeight } = this.config
92
- this.progress = self?.progress || 0
93
- this.positionY = this.progress * totalHeight
94
- this.positionX = -this.progress * totalWidth
95
-
96
- if (this.isSmooth) {
97
- gsap.set(this.sticky, { y: this.positionY })
98
- } else {
99
- if (this.isSticky) gsap.set(this.sticky, { top: 0 })
100
- else gsap.set(this.sticky, { top: `${this.positionY}px` })
101
- }
102
- }
103
-
104
- computeSizes() {
91
+ resize() {
105
92
  if (!this.sticky) return
93
+
94
+ // Get the viewport height dynamically instead of using store
106
95
  const vh = window.innerHeight
107
- const stickyRect = bounds(this.el)
108
- const totalHeight = Math.max(stickyRect.height - vh, 0)
96
+ let stickyRect = bounds(this.el)
109
97
 
110
98
  Object.assign(this.config, {
111
- totalHeight,
112
- totalWidth: this.config.totalWidth || 0,
99
+ totalHeight: stickyRect.height - vh,
113
100
  })
114
101
  }
115
102
 
116
- resize() {
117
- this.computeSizes()
118
- }
119
-
120
- refresh() {
121
- this.computeSizes()
122
- if (this.scrollTrigger) this.scrollTrigger.refresh()
123
- }
124
-
125
103
  destroy() {
126
104
  if (this.scrollTrigger) {
127
105
  this.scrollTrigger.kill()
@@ -132,7 +110,7 @@ export default class Sticky {
132
110
  }
133
111
 
134
112
  init() {
135
- this.computeSizes()
113
+ this.resize()
136
114
  this.observer()
137
115
  }
138
116
  }