Skip to content

Commit c80f452

Browse files
committed
perf: reduce bundle size by introducing style setter helpers
- Add setPositionAndTop helper in top.ts to consolidate position and top style assignments - Add setPositionAndBottom helper in bottom.ts to consolidate position and bottom style assignments - Reduces minified bundle size by ~0.1kb for both top and bottom scripts
1 parent b950b52 commit c80f452

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/bottom.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export function naturalStickyBottom(
6363
// Determine move positioning mode based on reserveSpace setting
6464
const movePosition = reserveSpace ? STATE_RELATIVE : 'absolute';
6565

66+
const setPositionAndBottom = (position: string, bottom: string) => {
67+
element.style.position = position;
68+
element.style.bottom = bottom;
69+
};
70+
6671
// Function to update state and dispatch event if changed
6772
const setState = (newState: StickyState) => {
6873
if (currentState !== newState) {
@@ -99,8 +104,7 @@ export function naturalStickyBottom(
99104
currentScrollY + viewportHeight >= documentHeight - 1 ||
100105
(isElementHidden && currentState === STATE_RELATIVE)
101106
) {
102-
element.style.position = movePosition;
103-
element.style.bottom = '0';
107+
setPositionAndBottom(movePosition, '0');
104108
setState(STATE_HOME);
105109
}
106110
// Priority 2: Handle scrolling DOWN logic.
@@ -110,23 +114,21 @@ export function naturalStickyBottom(
110114
elementBottom - snapEagerness * scrollStep <= viewportHeight &&
111115
currentState === STATE_RELATIVE
112116
) {
113-
element.style.position = reserveSpace ? STATE_STICKY : 'fixed';
114-
element.style.bottom = '0';
117+
setPositionAndBottom(reserveSpace ? STATE_STICKY : 'fixed', '0');
115118
setState(STATE_STICKY);
116119
}
117120
// If not becoming sticky, check if we need to release it below the viewport.
118121
else if (scrollStep >= scrollThreshold && isElementHidden) {
119-
element.style.position = movePosition;
120-
element.style.bottom = `${
121-
viewportBottomOffset - (elementBottom - elementTop)
122-
}px`;
122+
setPositionAndBottom(
123+
movePosition,
124+
`${viewportBottomOffset - (elementBottom - elementTop)}px`
125+
);
123126
setState(STATE_RELATIVE);
124127
}
125128
}
126129
// Priority 3: Handle scrolling UP logic.
127130
else if (scrollStep < 0 && currentState === STATE_STICKY) {
128-
element.style.position = movePosition;
129-
element.style.bottom = `${viewportBottomOffset}px`;
131+
setPositionAndBottom(movePosition, `${viewportBottomOffset}px`);
130132
setState(STATE_RELATIVE);
131133
}
132134

src/top.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ export function naturalStickyTop(
6464
// Determine move positioning mode based on reserveSpace setting
6565
const movePosition = reserveSpace ? STATE_RELATIVE : 'absolute';
6666

67+
const setPositionAndTop = (position: string, top: string) => {
68+
element.style.position = position;
69+
element.style.top = top;
70+
};
71+
6772
// Function to update state and dispatch event if changed
6873
const setState = (newState: StickyState) => {
6974
if (currentState !== newState) {
@@ -91,8 +96,7 @@ export function naturalStickyTop(
9196
currentScrollY <= 0 ||
9297
(isElementHidden && currentState === STATE_RELATIVE)
9398
) {
94-
element.style.position = movePosition;
95-
element.style.top = '0';
99+
setPositionAndTop(movePosition, '0');
96100
setState(STATE_HOME);
97101
}
98102
// Priority 2: Handle scrolling UP logic.
@@ -102,22 +106,21 @@ export function naturalStickyTop(
102106
elementTop - snapEagerness * scrollStep >= 0 &&
103107
currentState === STATE_RELATIVE
104108
) {
105-
element.style.position = reserveSpace ? STATE_STICKY : 'fixed';
106-
element.style.top = '0';
109+
setPositionAndTop(reserveSpace ? STATE_STICKY : 'fixed', '0');
107110
setState(STATE_STICKY);
108111
}
109112
// If not becoming sticky, check if we need to release it above the viewport.
110113
else if (-scrollStep >= scrollThreshold && isElementHidden) {
111-
element.style.position = movePosition;
112-
element.style.top = `${currentScrollY - elementBottom + elementTop}px`;
114+
setPositionAndTop(
115+
movePosition,
116+
`${currentScrollY - elementBottom + elementTop}px`
117+
);
113118
setState(STATE_RELATIVE);
114119
}
115120
}
116121
// Priority 3: Handle scrolling DOWN logic.
117122
else if (scrollStep > 0 && currentState === STATE_STICKY) {
118-
element.style.position = movePosition;
119-
// Position at current scroll position so element moves naturally with content
120-
element.style.top = `${currentScrollY}px`;
123+
setPositionAndTop(movePosition, `${currentScrollY}px`);
121124
setState(STATE_RELATIVE);
122125
}
123126

0 commit comments

Comments
 (0)