react-sticky-kit 0.1.3 → 0.1.4
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/README.md +3 -1
- package/dist/react-sticky.es.js +38 -40
- package/dist/react-sticky.umd.js +1 -1
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -40,7 +40,9 @@ pnpm add react-sticky-kit
|
|
|
40
40
|
|
|
41
41
|
```tsx
|
|
42
42
|
import { StickyContainer, StickyItem } from 'react-sticky-kit';
|
|
43
|
-
|
|
43
|
+
// !! Import styles for sticky components
|
|
44
|
+
import 'react-sticky-kit/dist/style.css';
|
|
45
|
+
// or `import 'react-sticky-kit/style';` for more clean style path(require modern bundler tools)
|
|
44
46
|
|
|
45
47
|
export default function Example() {
|
|
46
48
|
return (
|
package/dist/react-sticky.es.js
CHANGED
|
@@ -1,61 +1,60 @@
|
|
|
1
1
|
import { jsx as R } from "react/jsx-runtime";
|
|
2
2
|
import { createContext as b, useContext as Z, useRef as I, useState as N, useEffect as B, useCallback as w, useMemo as T } from "react";
|
|
3
|
-
const x = 20, $ = b(null);
|
|
4
|
-
function
|
|
3
|
+
const x = 20, _ = 200, $ = b(null);
|
|
4
|
+
function L() {
|
|
5
5
|
return Z($);
|
|
6
6
|
}
|
|
7
|
-
function
|
|
8
|
-
const e =
|
|
7
|
+
function M({ mode: s, children: o, className: y, ...h }) {
|
|
8
|
+
const e = L(), S = I(null), k = I(null), [r, n] = N(!1), t = I({
|
|
9
9
|
fixedOffsetTop: (e == null ? void 0 : e.fixedOffsetTop) || 0,
|
|
10
10
|
isSticky: r,
|
|
11
11
|
baseZIndex: (e == null ? void 0 : e.baseZIndex) || x
|
|
12
12
|
});
|
|
13
13
|
return t.current.baseZIndex = (e == null ? void 0 : e.baseZIndex) || x, t.current.fixedOffsetTop = (e == null ? void 0 : e.fixedOffsetTop) || 0, t.current.isSticky = r, B(() => {
|
|
14
|
-
const u = k.current, f = S.current,
|
|
15
|
-
if (!e || !f || !u ||
|
|
14
|
+
const u = k.current, f = S.current, d = s || (e == null ? void 0 : e.mode);
|
|
15
|
+
if (!e || !f || !u || d === "none" || !d || !e.register) {
|
|
16
16
|
n(!1);
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
|
-
const
|
|
19
|
+
const a = {
|
|
20
20
|
el: f,
|
|
21
21
|
update: (l, i, p, C, H) => {
|
|
22
22
|
if (!l)
|
|
23
23
|
return t.current.isSticky && n(!1), 0;
|
|
24
24
|
if (i <= p) {
|
|
25
|
-
const
|
|
25
|
+
const g = u.getBoundingClientRect().height;
|
|
26
26
|
let v = p;
|
|
27
|
-
if (
|
|
28
|
-
const
|
|
29
|
-
if (
|
|
27
|
+
if (d === "replace" && typeof C < "u") {
|
|
28
|
+
const m = C - (p + g);
|
|
29
|
+
if (m < 0 && (v = p + m, m + g < 0))
|
|
30
30
|
return t.current.isSticky && n(!1), 0;
|
|
31
31
|
}
|
|
32
|
-
return t.current.isSticky || n(!0), u.style.top = `${v}px`, u.style.width = `${f.offsetWidth}px`, u.style.zIndex = `${t.current.baseZIndex + (
|
|
32
|
+
return t.current.isSticky || n(!0), u.style.top = `${v}px`, u.style.width = `${f.offsetWidth}px`, u.style.zIndex = `${t.current.baseZIndex + (d === "replace" ? -H : H)}`, f.style.height = `${g}px`, d === "replace" ? 0 : g;
|
|
33
33
|
} else
|
|
34
34
|
n(!1);
|
|
35
35
|
return 0;
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
|
-
return e.register(
|
|
38
|
+
return e.register(a);
|
|
39
39
|
}, [e == null ? void 0 : e.mode, e == null ? void 0 : e.register, s]), B(() => {
|
|
40
|
-
const u = S.current, f = k.current,
|
|
41
|
-
if (!r || !u || !f || !
|
|
40
|
+
const u = S.current, f = k.current, d = e == null ? void 0 : e.updateStickyItemsHeight;
|
|
41
|
+
if (!r || !u || !f || !d)
|
|
42
42
|
return;
|
|
43
|
-
const c =
|
|
43
|
+
const c = d(f.getBoundingClientRect().height);
|
|
44
44
|
return () => {
|
|
45
45
|
O(u, ["height"]), O(f, ["top", "z-index", "width"]), c();
|
|
46
46
|
};
|
|
47
|
-
}, [r, e == null ? void 0 : e.updateStickyItemsHeight]), /* @__PURE__ */ R("div", { className: "oe-sticky-item " + (
|
|
47
|
+
}, [r, e == null ? void 0 : e.updateStickyItemsHeight]), /* @__PURE__ */ R("div", { className: "oe-sticky-item " + (y || ""), ...h, ref: S, children: /* @__PURE__ */ R("div", { className: "oe-sticky-content " + (r ? "is-sticky" : ""), ref: k, children: o }) });
|
|
48
48
|
}
|
|
49
49
|
function O(s, o) {
|
|
50
|
-
o.forEach((
|
|
51
|
-
s.style.removeProperty(
|
|
50
|
+
o.forEach((y) => {
|
|
51
|
+
s.style.removeProperty(y);
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
|
-
|
|
55
|
-
function U({
|
|
54
|
+
function P({
|
|
56
55
|
children: s,
|
|
57
56
|
offsetTop: o = 0,
|
|
58
|
-
baseZIndex:
|
|
57
|
+
baseZIndex: y,
|
|
59
58
|
onStickyItemsHeightChange: h,
|
|
60
59
|
defaultMode: e = "replace",
|
|
61
60
|
...S
|
|
@@ -71,18 +70,18 @@ function U({
|
|
|
71
70
|
const c = k.current;
|
|
72
71
|
if (!c)
|
|
73
72
|
return;
|
|
74
|
-
const
|
|
75
|
-
let H = i,
|
|
76
|
-
|
|
77
|
-
const v = r.current.map((
|
|
78
|
-
r.current.forEach((
|
|
79
|
-
H +=
|
|
73
|
+
const a = c.getBoundingClientRect(), l = n.current, i = l.fixedOffsetTop, p = l.stickyItemsHeight, C = !(a.top > i || a.bottom < i);
|
|
74
|
+
let H = i, g = a.bottom - (i + p);
|
|
75
|
+
g > 0 && (g = 0);
|
|
76
|
+
const v = r.current.map((m) => m.el.getBoundingClientRect().top);
|
|
77
|
+
r.current.forEach((m, E) => {
|
|
78
|
+
H += m.update(C, v[E], H + g, v[E + 1], E);
|
|
80
79
|
});
|
|
81
80
|
}, []), u = w((c) => {
|
|
82
|
-
const
|
|
83
|
-
return n.current.stickyItemsHeight =
|
|
81
|
+
const a = n.current.stickyItemsHeight + c;
|
|
82
|
+
return n.current.stickyItemsHeight = a, setTimeout(() => {
|
|
84
83
|
var l, i;
|
|
85
|
-
(i = (l = n.current).onStickyItemsHeightChange) == null || i.call(l,
|
|
84
|
+
(i = (l = n.current).onStickyItemsHeightChange) == null || i.call(l, a);
|
|
86
85
|
}, 0), () => {
|
|
87
86
|
const l = n.current.stickyItemsHeight - c;
|
|
88
87
|
n.current.stickyItemsHeight = l, setTimeout(() => {
|
|
@@ -95,18 +94,18 @@ function U({
|
|
|
95
94
|
n.current.fixedOffsetTop = o, n.current.defaultMode = e, t();
|
|
96
95
|
}, [o, e, t]);
|
|
97
96
|
const f = w((c) => (r.current.push(c), r.current = A(r.current), t(), () => {
|
|
98
|
-
r.current = r.current.filter((
|
|
97
|
+
r.current = r.current.filter((a) => a !== c), t();
|
|
99
98
|
}), [t]);
|
|
100
99
|
B(() => (window.addEventListener("scroll", t, { passive: !0 }), window.addEventListener("resize", t, { passive: !0 }), () => {
|
|
101
100
|
window.removeEventListener("scroll", t), window.removeEventListener("resize", t);
|
|
102
101
|
}), [t]);
|
|
103
|
-
const
|
|
102
|
+
const d = T(() => z(y), [y]);
|
|
104
103
|
return /* @__PURE__ */ R(
|
|
105
104
|
$.Provider,
|
|
106
105
|
{
|
|
107
106
|
value: {
|
|
108
107
|
register: f,
|
|
109
|
-
baseZIndex:
|
|
108
|
+
baseZIndex: d,
|
|
110
109
|
updateStickyItemsHeight: u,
|
|
111
110
|
fixedOffsetTop: o,
|
|
112
111
|
mode: e
|
|
@@ -117,7 +116,6 @@ function U({
|
|
|
117
116
|
...S,
|
|
118
117
|
ref: k,
|
|
119
118
|
className: "oe-sticky-container",
|
|
120
|
-
"data-mode": e,
|
|
121
119
|
children: s
|
|
122
120
|
}
|
|
123
121
|
)
|
|
@@ -125,15 +123,15 @@ function U({
|
|
|
125
123
|
);
|
|
126
124
|
}
|
|
127
125
|
function z(s) {
|
|
128
|
-
return typeof s > "u" ?
|
|
126
|
+
return typeof s > "u" ? _ : Math.max(Number(s) || 0, x);
|
|
129
127
|
}
|
|
130
128
|
function A(s) {
|
|
131
|
-
return s.sort((o,
|
|
132
|
-
const h = o.el.getBoundingClientRect(), e =
|
|
129
|
+
return s.sort((o, y) => {
|
|
130
|
+
const h = o.el.getBoundingClientRect(), e = y.el.getBoundingClientRect();
|
|
133
131
|
return h.top - e.top;
|
|
134
132
|
});
|
|
135
133
|
}
|
|
136
134
|
export {
|
|
137
|
-
|
|
138
|
-
|
|
135
|
+
P as StickyContainer,
|
|
136
|
+
M as StickyItem
|
|
139
137
|
};
|
package/dist/react-sticky.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(p,
|
|
1
|
+
(function(p,g){typeof exports=="object"&&typeof module<"u"?g(exports,require("react/jsx-runtime"),require("react")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","react"],g):(p=typeof globalThis<"u"?globalThis:p||self,g(p.ReactSticky={},p.jsxRuntime,p.React))})(this,function(p,g,n){"use strict";const B=n.createContext(null);function N(){return n.useContext(B)}const $="";function O({mode:c,children:o,className:m,...S}){const e=N(),R=n.useRef(null),E=n.useRef(null),[s,i]=n.useState(!1),t=n.useRef({fixedOffsetTop:(e==null?void 0:e.fixedOffsetTop)||0,isSticky:s,baseZIndex:(e==null?void 0:e.baseZIndex)||20});return t.current.baseZIndex=(e==null?void 0:e.baseZIndex)||20,t.current.fixedOffsetTop=(e==null?void 0:e.fixedOffsetTop)||0,t.current.isSticky=s,n.useEffect(()=>{const f=E.current,d=R.current,l=c||(e==null?void 0:e.mode);if(!e||!d||!f||l==="none"||!l||!e.register){i(!1);return}const y={el:d,update:(a,r,h,C,H)=>{if(!a)return t.current.isSticky&&i(!1),0;if(r<=h){const I=f.getBoundingClientRect().height;let _=h;if(l==="replace"&&typeof C<"u"){const k=C-(h+I);if(k<0&&(_=h+k,k+I<0))return t.current.isSticky&&i(!1),0}return t.current.isSticky||i(!0),f.style.top=`${_}px`,f.style.width=`${d.offsetWidth}px`,f.style.zIndex=`${t.current.baseZIndex+(l==="replace"?-H:H)}`,d.style.height=`${I}px`,l==="replace"?0:I}else i(!1);return 0}};return e.register(y)},[e==null?void 0:e.mode,e==null?void 0:e.register,c]),n.useEffect(()=>{const f=R.current,d=E.current,l=e==null?void 0:e.updateStickyItemsHeight;if(!s||!f||!d||!l)return;const u=l(d.getBoundingClientRect().height);return()=>{x(f,["height"]),x(d,["top","z-index","width"]),u()}},[s,e==null?void 0:e.updateStickyItemsHeight]),g.jsx("div",{className:"oe-sticky-item "+(m||""),...S,ref:R,children:g.jsx("div",{className:"oe-sticky-content "+(s?"is-sticky":""),ref:E,children:o})})}function x(c,o){o.forEach(m=>{c.style.removeProperty(m)})}function Z({children:c,offsetTop:o=0,baseZIndex:m,onStickyItemsHeightChange:S,defaultMode:e="replace",...R}){const E=n.useRef(null),s=n.useRef([]),i=n.useRef({fixedOffsetTop:o,defaultMode:e,stickyItemsHeight:0,onStickyItemsHeightChange:S});i.current.onStickyItemsHeightChange=S;const t=n.useCallback(()=>{const u=E.current;if(!u)return;const y=u.getBoundingClientRect(),a=i.current,r=a.fixedOffsetTop,h=a.stickyItemsHeight,C=!(y.top>r||y.bottom<r);let H=r,I=y.bottom-(r+h);I>0&&(I=0);const _=s.current.map(k=>k.el.getBoundingClientRect().top);s.current.forEach((k,v)=>{H+=k.update(C,_[v],H+I,_[v+1],v)})},[]),f=n.useCallback(u=>{const y=i.current.stickyItemsHeight+u;return i.current.stickyItemsHeight=y,setTimeout(()=>{var a,r;(r=(a=i.current).onStickyItemsHeightChange)==null||r.call(a,y)},0),()=>{const a=i.current.stickyItemsHeight-u;i.current.stickyItemsHeight=a,setTimeout(()=>{var r,h;(h=(r=i.current).onStickyItemsHeightChange)==null||h.call(r,a)},0)}},[]);n.useEffect(()=>{i.current.fixedOffsetTop=o,i.current.defaultMode=e,t()},[o,e,t]);const d=n.useCallback(u=>(s.current.push(u),s.current=w(s.current),t(),()=>{s.current=s.current.filter(y=>y!==u),t()}),[t]);n.useEffect(()=>(window.addEventListener("scroll",t,{passive:!0}),window.addEventListener("resize",t,{passive:!0}),()=>{window.removeEventListener("scroll",t),window.removeEventListener("resize",t)}),[t]);const l=n.useMemo(()=>b(m),[m]);return g.jsx(B.Provider,{value:{register:d,baseZIndex:l,updateStickyItemsHeight:f,fixedOffsetTop:o,mode:e},children:g.jsx("div",{...R,ref:E,className:"oe-sticky-container",children:c})})}function b(c){return typeof c>"u"?200:Math.max(Number(c)||0,20)}function w(c){return c.sort((o,m)=>{const S=o.el.getBoundingClientRect(),e=m.el.getBoundingClientRect();return S.top-e.top})}p.StickyContainer=Z,p.StickyItem=O,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-sticky-kit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A lightweight, flexible React sticky container and item component library supporting multiple sticky modes, advanced layouts, and edge cases.",
|
|
6
6
|
"keywords": [
|
|
@@ -39,7 +39,9 @@
|
|
|
39
39
|
"import": "./dist/react-sticky.es.js",
|
|
40
40
|
"require": "./dist/react-sticky.umd.js",
|
|
41
41
|
"types": "./dist/index.d.ts"
|
|
42
|
-
}
|
|
42
|
+
},
|
|
43
|
+
"./style": "./dist/style.css",
|
|
44
|
+
"./dist/style.css": "./dist/style.css"
|
|
43
45
|
},
|
|
44
46
|
"peerDependencies": {
|
|
45
47
|
"react": ">=17.0.0",
|