@marcwiest/midday.js 0.2.1 → 0.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.
- package/README.md +2 -0
- package/dist/core.mjs +57 -56
- package/dist/midday.umd.js +1 -1
- package/dist/svelte.mjs +5 -5
- package/dist/vue.mjs +16 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -181,6 +181,8 @@ useEffect(() => {
|
|
|
181
181
|
}, [navItems]);
|
|
182
182
|
```
|
|
183
183
|
|
|
184
|
+
**Section changes are not auto-detected.** The library uses `ResizeObserver` to track section and element *sizes*, but it does not watch for new or removed `[data-midday-section]` elements. In SPAs or pages with dynamic sections, call `refresh()` after route changes or any DOM mutation that adds, removes, or reorders sections.
|
|
185
|
+
|
|
184
186
|
**Headless mode** doesn't manage element DOM — you own the variant elements. The engine reads element and variant sizes live on every scroll frame via `getBoundingClientRect()`, so size changes to your variant elements are picked up automatically. You only need `refresh()` when sections change, since section bounds are cached.
|
|
185
187
|
|
|
186
188
|
### `onChange` callback
|
package/dist/core.mjs
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
function z(t) {
|
|
2
|
-
const
|
|
2
|
+
const o = t.getBoundingClientRect();
|
|
3
3
|
return {
|
|
4
|
-
top:
|
|
5
|
-
height:
|
|
4
|
+
top: o.top + window.scrollY,
|
|
5
|
+
height: o.height
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
8
|
function G(t) {
|
|
9
|
-
const
|
|
9
|
+
const o = t.getBoundingClientRect();
|
|
10
10
|
return {
|
|
11
|
-
top:
|
|
12
|
-
height:
|
|
11
|
+
top: o.top,
|
|
12
|
+
height: o.height
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
15
|
function S(t) {
|
|
16
|
-
for (const
|
|
17
|
-
const
|
|
18
|
-
|
|
16
|
+
for (const o of t) {
|
|
17
|
+
const p = z(o.el);
|
|
18
|
+
o.top = p.top, o.height = p.height;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
const K = "[data-midday-section]", J = "data-midday-section", Q = "data-midday-target";
|
|
22
22
|
function U(t) {
|
|
23
|
-
const
|
|
24
|
-
for (const s of
|
|
23
|
+
const o = document.querySelectorAll(K), p = [];
|
|
24
|
+
for (const s of o) {
|
|
25
25
|
const w = s.getAttribute(J);
|
|
26
26
|
if (!w) continue;
|
|
27
27
|
const a = s.getAttribute(Q);
|
|
28
|
-
a && (!t || !a.split(" ").includes(t)) ||
|
|
28
|
+
a && (!t || !a.split(" ").includes(t)) || p.push({ el: s, variant: w, top: 0, height: 0 });
|
|
29
29
|
}
|
|
30
|
-
return S(
|
|
30
|
+
return S(p), p;
|
|
31
31
|
}
|
|
32
32
|
function X(t) {
|
|
33
|
-
let { element:
|
|
33
|
+
let { element: o, variants: p, sections: s } = t;
|
|
34
34
|
const { defaultName: w, onChange: a } = t;
|
|
35
|
-
let h = null, B = !1, x = "",
|
|
35
|
+
let h = null, B = !1, x = "", l = null;
|
|
36
36
|
function R() {
|
|
37
37
|
g();
|
|
38
38
|
}
|
|
@@ -46,27 +46,27 @@ function X(t) {
|
|
|
46
46
|
B = !1, v();
|
|
47
47
|
}
|
|
48
48
|
function v() {
|
|
49
|
-
const r = G(
|
|
50
|
-
if (
|
|
51
|
-
const k = window.scrollY, _ = r.top, D = _ +
|
|
52
|
-
let L =
|
|
49
|
+
const r = G(o), i = r.height;
|
|
50
|
+
if (i <= 0) return;
|
|
51
|
+
const k = window.scrollY, _ = r.top, D = _ + i, I = [], q = /* @__PURE__ */ new Map();
|
|
52
|
+
let L = i, j = 0, F = 0;
|
|
53
53
|
const H = /* @__PURE__ */ new Map();
|
|
54
54
|
for (const e of s) {
|
|
55
55
|
const y = e.top - k, m = y + e.height, C = Math.max(_, y), u = Math.min(D, m), M = Math.max(0, u - C);
|
|
56
56
|
if (M > 0) {
|
|
57
57
|
F += M;
|
|
58
|
-
const
|
|
59
|
-
L = Math.min(L,
|
|
58
|
+
const b = C - _, E = D - u;
|
|
59
|
+
L = Math.min(L, b), j = Math.max(j, i - E);
|
|
60
60
|
const P = q.get(e.variant);
|
|
61
|
-
P ? (P.topInset = Math.min(P.topInset,
|
|
61
|
+
P ? (P.topInset = Math.min(P.topInset, b), P.bottomInset = Math.min(P.bottomInset, E)) : q.set(e.variant, { topInset: b, bottomInset: E });
|
|
62
62
|
}
|
|
63
63
|
let f = H.get(e.variant);
|
|
64
64
|
f || (f = [], H.set(e.variant, f)), f.push({ viewTop: y, viewBottom: m });
|
|
65
65
|
}
|
|
66
|
-
let
|
|
67
|
-
for (const e of
|
|
66
|
+
let T = null;
|
|
67
|
+
for (const e of p) {
|
|
68
68
|
if (e.name === w) {
|
|
69
|
-
|
|
69
|
+
T = e.wrapper;
|
|
70
70
|
continue;
|
|
71
71
|
}
|
|
72
72
|
const y = H.get(e.name);
|
|
@@ -74,41 +74,41 @@ function X(t) {
|
|
|
74
74
|
e.wrapper.style.clipPath = "inset(0 0 100% 0)";
|
|
75
75
|
continue;
|
|
76
76
|
}
|
|
77
|
-
const m = e.wrapper.getBoundingClientRect(), C = m.height ||
|
|
77
|
+
const m = e.wrapper.getBoundingClientRect(), C = m.height || i;
|
|
78
78
|
let u = C, M = C;
|
|
79
79
|
for (const f of y) {
|
|
80
|
-
const
|
|
81
|
-
E <=
|
|
80
|
+
const b = Math.max(m.top, f.viewTop), E = Math.min(m.bottom, f.viewBottom);
|
|
81
|
+
E <= b || (u = Math.min(u, b - m.top), M = Math.min(M, m.bottom - E));
|
|
82
82
|
}
|
|
83
83
|
if (u + M < C) {
|
|
84
84
|
e.wrapper.style.clipPath = `inset(${u}px 0 ${M}px 0)`;
|
|
85
|
-
const f = q.get(e.name),
|
|
86
|
-
I.push({ name: e.name, progress:
|
|
85
|
+
const f = q.get(e.name), b = f ? (i - f.topInset - f.bottomInset) / i : 0;
|
|
86
|
+
I.push({ name: e.name, progress: b });
|
|
87
87
|
} else
|
|
88
88
|
e.wrapper.style.clipPath = "inset(0 0 100% 0)";
|
|
89
89
|
}
|
|
90
|
-
if (
|
|
91
|
-
const e =
|
|
92
|
-
if (F >=
|
|
93
|
-
|
|
90
|
+
if (T) {
|
|
91
|
+
const e = T.getBoundingClientRect().height || i;
|
|
92
|
+
if (F >= i)
|
|
93
|
+
T.style.clipPath = "inset(0 0 100% 0)";
|
|
94
94
|
else if (F <= 0)
|
|
95
|
-
|
|
95
|
+
T.style.clipPath = "inset(0)", I.unshift({
|
|
96
96
|
name: w,
|
|
97
97
|
progress: 1
|
|
98
98
|
});
|
|
99
99
|
else {
|
|
100
100
|
const y = L;
|
|
101
|
-
if (
|
|
102
|
-
const u = e * (j /
|
|
103
|
-
|
|
101
|
+
if (i - j >= y) {
|
|
102
|
+
const u = e * (j / i);
|
|
103
|
+
T.style.clipPath = `inset(${u}px 0 0 0)`;
|
|
104
104
|
} else {
|
|
105
|
-
const u = e * ((
|
|
106
|
-
|
|
105
|
+
const u = e * ((i - L) / i);
|
|
106
|
+
T.style.clipPath = `inset(0 0 ${u}px 0)`;
|
|
107
107
|
}
|
|
108
|
-
const C =
|
|
108
|
+
const C = i - F;
|
|
109
109
|
I.unshift({
|
|
110
110
|
name: w,
|
|
111
|
-
progress: C /
|
|
111
|
+
progress: C / i
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
114
|
}
|
|
@@ -116,29 +116,30 @@ function X(t) {
|
|
|
116
116
|
O !== x && (x = O, a == null || a(I));
|
|
117
117
|
}
|
|
118
118
|
function d() {
|
|
119
|
-
|
|
119
|
+
l == null || l.disconnect(), l = new ResizeObserver(() => {
|
|
120
120
|
S(s), g();
|
|
121
121
|
});
|
|
122
122
|
for (const r of s)
|
|
123
|
-
|
|
123
|
+
l.observe(r.el);
|
|
124
|
+
l.observe(o);
|
|
124
125
|
}
|
|
125
126
|
function n() {
|
|
126
127
|
window.addEventListener("scroll", R, { passive: !0 }), window.addEventListener("resize", V, { passive: !0 }), d(), g();
|
|
127
128
|
}
|
|
128
|
-
function
|
|
129
|
+
function A() {
|
|
129
130
|
S(s), g();
|
|
130
131
|
}
|
|
131
|
-
function N(r,
|
|
132
|
-
|
|
132
|
+
function N(r, i) {
|
|
133
|
+
p = r, s = i, S(s), d(), g();
|
|
133
134
|
}
|
|
134
135
|
function $() {
|
|
135
|
-
h !== null && cancelAnimationFrame(h), window.removeEventListener("scroll", R), window.removeEventListener("resize", V),
|
|
136
|
+
h !== null && cancelAnimationFrame(h), window.removeEventListener("scroll", R), window.removeEventListener("resize", V), l == null || l.disconnect(), l = null;
|
|
136
137
|
}
|
|
137
|
-
return n(), { recalculate:
|
|
138
|
+
return n(), { recalculate: A, update: N, destroy: $ };
|
|
138
139
|
}
|
|
139
140
|
const W = "data-midday-variant", Y = "default";
|
|
140
|
-
function Z(t,
|
|
141
|
-
const { onChange:
|
|
141
|
+
function Z(t, o = {}) {
|
|
142
|
+
const { onChange: p } = o, s = o.name ?? (t.getAttribute("data-midday-element") || void 0), w = t.style.overflow;
|
|
142
143
|
let a = null, h = [];
|
|
143
144
|
function B() {
|
|
144
145
|
const c = U(s), v = /* @__PURE__ */ new Set();
|
|
@@ -151,8 +152,8 @@ function Z(t, i = {}) {
|
|
|
151
152
|
const n = [];
|
|
152
153
|
for (const r of v)
|
|
153
154
|
n.push(x(r, d, !0));
|
|
154
|
-
const
|
|
155
|
-
|
|
155
|
+
const A = document.createElement("div");
|
|
156
|
+
A.style.visibility = "hidden", A.style.pointerEvents = "none", A.setAttribute("aria-hidden", "true"), A.appendChild(d.cloneNode(!0)), t.appendChild(A);
|
|
156
157
|
const N = x(Y, d, !1);
|
|
157
158
|
t.appendChild(N.wrapper);
|
|
158
159
|
const $ = [N];
|
|
@@ -164,14 +165,14 @@ function Z(t, i = {}) {
|
|
|
164
165
|
const n = document.createElement("div");
|
|
165
166
|
return n.setAttribute(W, c), n.style.position = "absolute", n.style.top = "0", n.style.left = "0", n.style.right = "0", n.style.bottom = "0", n.style.willChange = "clip-path", n.style.clipPath = "inset(0 0 100% 0)", d ? (n.setAttribute("aria-hidden", "true"), n.setAttribute("inert", ""), n.style.pointerEvents = "none", n.appendChild(v.cloneNode(!0))) : n.appendChild(v), { wrapper: n, name: c };
|
|
166
167
|
}
|
|
167
|
-
function
|
|
168
|
+
function l() {
|
|
168
169
|
const c = U(s);
|
|
169
170
|
h = B(), a = X({
|
|
170
171
|
element: t,
|
|
171
172
|
variants: h,
|
|
172
173
|
defaultName: Y,
|
|
173
174
|
sections: c,
|
|
174
|
-
onChange:
|
|
175
|
+
onChange: p
|
|
175
176
|
});
|
|
176
177
|
}
|
|
177
178
|
function R() {
|
|
@@ -194,7 +195,7 @@ function Z(t, i = {}) {
|
|
|
194
195
|
function g() {
|
|
195
196
|
a == null || a.destroy(), a = null, R(), h = [], t.style.overflow = w;
|
|
196
197
|
}
|
|
197
|
-
return
|
|
198
|
+
return l(), { refresh: V, destroy: g };
|
|
198
199
|
}
|
|
199
200
|
export {
|
|
200
201
|
Z as a,
|
package/dist/midday.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(M,I){typeof exports=="object"&&typeof module<"u"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(M=typeof globalThis<"u"?globalThis:M||self,I(M.midday={}))})(this,(function(M){"use strict";function I(t){const n=t.getBoundingClientRect();return{top:n.top+window.scrollY,height:n.height}}function K(t){const n=t.getBoundingClientRect();return{top:n.top,height:n.height}}function V(t){for(const n of t){const
|
|
1
|
+
(function(M,I){typeof exports=="object"&&typeof module<"u"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(M=typeof globalThis<"u"?globalThis:M||self,I(M.midday={}))})(this,(function(M){"use strict";function I(t){const n=t.getBoundingClientRect();return{top:n.top+window.scrollY,height:n.height}}function K(t){const n=t.getBoundingClientRect();return{top:n.top,height:n.height}}function V(t){for(const n of t){const d=I(n.el);n.top=d.top,n.height=d.height}}const J="[data-midday-section]",Q="data-midday-section",X="data-midday-target";function N(t){const n=document.querySelectorAll(J),d=[];for(const s of n){const p=s.getAttribute(Q);if(!p)continue;const a=s.getAttribute(X);a&&(!t||!a.split(" ").includes(t))||d.push({el:s,variant:p,top:0,height:0})}return V(d),d}function k(t){let{element:n,variants:d,sections:s}=t;const{defaultName:p,onChange:a}=t;let i=null,y=!1,B="",r=null;function E(){w()}function v(){V(s),w()}function w(){y||(y=!0,i=requestAnimationFrame(l))}function l(){y=!1,g()}function g(){const u=K(n),c=u.height;if(c<=0)return;const ot=window.scrollY,W=u.top,z=W+c,$=[],Y=new Map;let F=c,_=0,q=0;const D=new Map;for(const e of s){const b=e.top-ot,C=b+e.height,T=Math.max(W,b),h=Math.min(z,C),R=Math.max(0,h-T);if(R>0){q+=R;const A=T-W,P=z-h;F=Math.min(F,A),_=Math.max(_,c-P);const j=Y.get(e.variant);j?(j.topInset=Math.min(j.topInset,A),j.bottomInset=Math.min(j.bottomInset,P)):Y.set(e.variant,{topInset:A,bottomInset:P})}let m=D.get(e.variant);m||(m=[],D.set(e.variant,m)),m.push({viewTop:b,viewBottom:C})}let S=null;for(const e of d){if(e.name===p){S=e.wrapper;continue}const b=D.get(e.name);if(!b){e.wrapper.style.clipPath="inset(0 0 100% 0)";continue}const C=e.wrapper.getBoundingClientRect(),T=C.height||c;let h=T,R=T;for(const m of b){const A=Math.max(C.top,m.viewTop),P=Math.min(C.bottom,m.viewBottom);P<=A||(h=Math.min(h,A-C.top),R=Math.min(R,C.bottom-P))}if(h+R<T){e.wrapper.style.clipPath=`inset(${h}px 0 ${R}px 0)`;const m=Y.get(e.name),A=m?(c-m.topInset-m.bottomInset)/c:0;$.push({name:e.name,progress:A})}else e.wrapper.style.clipPath="inset(0 0 100% 0)"}if(S){const e=S.getBoundingClientRect().height||c;if(q>=c)S.style.clipPath="inset(0 0 100% 0)";else if(q<=0)S.style.clipPath="inset(0)",$.unshift({name:p,progress:1});else{const b=F;if(c-_>=b){const h=e*(_/c);S.style.clipPath=`inset(${h}px 0 0 0)`}else{const h=e*((c-F)/c);S.style.clipPath=`inset(0 0 ${h}px 0)`}const T=c-q;$.unshift({name:p,progress:T/c})}}const G=$.map(e=>`${e.name}:${e.progress.toFixed(3)}`).join("|");G!==B&&(B=G,a==null||a($))}function f(){r==null||r.disconnect(),r=new ResizeObserver(()=>{V(s),w()});for(const u of s)r.observe(u.el);r.observe(n)}function o(){window.addEventListener("scroll",E,{passive:!0}),window.addEventListener("resize",v,{passive:!0}),f(),w()}function x(){V(s),w()}function L(u,c){d=u,s=c,V(s),f(),w()}function H(){i!==null&&cancelAnimationFrame(i),window.removeEventListener("scroll",E),window.removeEventListener("resize",v),r==null||r.disconnect(),r=null}return o(),{recalculate:x,update:L,destroy:H}}const O="data-midday-variant",U="default";function Z(t,n={}){const{onChange:d}=n,s=n.name??(t.getAttribute("data-midday-element")||void 0),p=t.style.overflow;let a=null,i=[];function y(){const l=N(s),g=new Set;for(const u of l)g.add(u.variant);t.style.overflow="visible";const f=document.createDocumentFragment();for(;t.firstChild;)f.appendChild(t.firstChild);const o=[];for(const u of g)o.push(B(u,f,!0));const x=document.createElement("div");x.style.visibility="hidden",x.style.pointerEvents="none",x.setAttribute("aria-hidden","true"),x.appendChild(f.cloneNode(!0)),t.appendChild(x);const L=B(U,f,!1);t.appendChild(L.wrapper);const H=[L];for(const u of o)t.appendChild(u.wrapper),H.push(u);return H}function B(l,g,f){const o=document.createElement("div");return o.setAttribute(O,l),o.style.position="absolute",o.style.top="0",o.style.left="0",o.style.right="0",o.style.bottom="0",o.style.willChange="clip-path",o.style.clipPath="inset(0 0 100% 0)",f?(o.setAttribute("aria-hidden","true"),o.setAttribute("inert",""),o.style.pointerEvents="none",o.appendChild(g.cloneNode(!0))):o.appendChild(g),{wrapper:o,name:l}}function r(){const l=N(s);i=y(),a=k({element:t,variants:i,defaultName:U,sections:l,onChange:d})}function E(){const l=t.querySelector(`[${O}="${U}"]`),g=t.querySelectorAll(`[${O}]`);for(const f of g)f.remove();for(;t.firstChild;)t.removeChild(t.firstChild);if(l)for(;l.firstChild;)t.appendChild(l.firstChild)}function v(){E();const l=N(s);i=y(),a==null||a.update(i,l)}function w(){a==null||a.destroy(),a=null,E(),i=[],t.style.overflow=p}return r(),{refresh:v,destroy:w}}function tt(t){const{element:n,variants:d,defaultVariant:s="default",name:p,onChange:a}=t;let i=null;function y(){return Object.entries(d).map(([v,w])=>({name:v,wrapper:w}))}function B(){const v=N(p);i=k({element:n,variants:y(),defaultName:s,sections:v,onChange:a})}function r(){const v=N(p);i==null||i.update(y(),v)}function E(){i==null||i.destroy(),i=null}return B(),{refresh:r,destroy:E}}function et(t,n){return Z(t,n)}function nt(t){return tt(t)}M.midday=et,M.middayHeadless=nt,Object.defineProperty(M,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/svelte.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
function
|
|
3
|
-
let r =
|
|
1
|
+
import { a as o } from "./core.mjs";
|
|
2
|
+
function l(d, y) {
|
|
3
|
+
let r = o(d, y);
|
|
4
4
|
return {
|
|
5
5
|
update(e) {
|
|
6
|
-
r == null || r.destroy(), r =
|
|
6
|
+
e !== void 0 ? (r == null || r.destroy(), r = o(d, e)) : r == null || r.refresh();
|
|
7
7
|
},
|
|
8
8
|
destroy() {
|
|
9
9
|
r == null || r.destroy(), r = null;
|
|
@@ -11,5 +11,5 @@ function u(d, o) {
|
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
export {
|
|
14
|
-
|
|
14
|
+
l as midday
|
|
15
15
|
};
|
package/dist/vue.mjs
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
import { shallowRef as o, onMounted as u, onUnmounted as s } from "vue";
|
|
2
|
-
import { a as
|
|
3
|
-
function c(n,
|
|
4
|
-
const
|
|
2
|
+
import { a as t } from "./core.mjs";
|
|
3
|
+
function c(n, e) {
|
|
4
|
+
const a = o(null);
|
|
5
5
|
return u(() => {
|
|
6
|
-
n.value && (
|
|
6
|
+
n.value && (a.value = t(n.value, e));
|
|
7
7
|
}), s(() => {
|
|
8
|
-
var
|
|
9
|
-
(
|
|
10
|
-
}),
|
|
8
|
+
var d;
|
|
9
|
+
(d = a.value) == null || d.destroy(), a.value = null;
|
|
10
|
+
}), a;
|
|
11
11
|
}
|
|
12
12
|
const l = {
|
|
13
|
-
mounted(n,
|
|
14
|
-
const
|
|
15
|
-
n.__middayInstance =
|
|
13
|
+
mounted(n, e) {
|
|
14
|
+
const a = t(n, e.value);
|
|
15
|
+
n.__middayInstance = a;
|
|
16
|
+
},
|
|
17
|
+
updated(n) {
|
|
18
|
+
var e;
|
|
19
|
+
(e = n.__middayInstance) == null || e.refresh();
|
|
16
20
|
},
|
|
17
21
|
unmounted(n) {
|
|
18
|
-
var
|
|
19
|
-
(
|
|
22
|
+
var e;
|
|
23
|
+
(e = n.__middayInstance) == null || e.destroy(), delete n.__middayInstance;
|
|
20
24
|
}
|
|
21
25
|
};
|
|
22
26
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marcwiest/midday.js",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "A modern vanilla JS plugin for fixed headers that change style as you scroll through sections. Zero dependencies. The spiritual successor to midnight.js.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|