@marcwiest/midday.js 0.2.0 → 0.2.1
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/dist/core.mjs +134 -127
- package/dist/midday.umd.js +1 -1
- package/package.json +1 -1
package/dist/core.mjs
CHANGED
|
@@ -1,196 +1,203 @@
|
|
|
1
|
-
function
|
|
1
|
+
function z(t) {
|
|
2
2
|
const i = t.getBoundingClientRect();
|
|
3
3
|
return {
|
|
4
4
|
top: i.top + window.scrollY,
|
|
5
5
|
height: i.height
|
|
6
6
|
};
|
|
7
7
|
}
|
|
8
|
-
function
|
|
8
|
+
function G(t) {
|
|
9
9
|
const i = t.getBoundingClientRect();
|
|
10
10
|
return {
|
|
11
11
|
top: i.top,
|
|
12
12
|
height: i.height
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
function
|
|
15
|
+
function S(t) {
|
|
16
16
|
for (const i of t) {
|
|
17
|
-
const
|
|
18
|
-
i.top =
|
|
17
|
+
const l = z(i.el);
|
|
18
|
+
i.top = l.top, i.height = l.height;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
const
|
|
22
|
-
function
|
|
23
|
-
const i = document.querySelectorAll(
|
|
21
|
+
const K = "[data-midday-section]", J = "data-midday-section", Q = "data-midday-target";
|
|
22
|
+
function U(t) {
|
|
23
|
+
const i = document.querySelectorAll(K), l = [];
|
|
24
24
|
for (const s of i) {
|
|
25
|
-
const h = s.getAttribute(K);
|
|
26
|
-
if (!h) continue;
|
|
27
25
|
const w = s.getAttribute(J);
|
|
28
|
-
|
|
26
|
+
if (!w) continue;
|
|
27
|
+
const a = s.getAttribute(Q);
|
|
28
|
+
a && (!t || !a.split(" ").includes(t)) || l.push({ el: s, variant: w, top: 0, height: 0 });
|
|
29
29
|
}
|
|
30
|
-
return
|
|
30
|
+
return S(l), l;
|
|
31
31
|
}
|
|
32
|
-
function
|
|
33
|
-
let { element: i, variants:
|
|
34
|
-
const { defaultName:
|
|
35
|
-
let
|
|
36
|
-
function
|
|
37
|
-
|
|
32
|
+
function X(t) {
|
|
33
|
+
let { element: i, variants: l, sections: s } = t;
|
|
34
|
+
const { defaultName: w, onChange: a } = t;
|
|
35
|
+
let h = null, B = !1, x = "", p = null;
|
|
36
|
+
function R() {
|
|
37
|
+
g();
|
|
38
38
|
}
|
|
39
|
-
function
|
|
40
|
-
|
|
39
|
+
function V() {
|
|
40
|
+
S(s), g();
|
|
41
41
|
}
|
|
42
|
-
function
|
|
43
|
-
|
|
42
|
+
function g() {
|
|
43
|
+
B || (B = !0, h = requestAnimationFrame(c));
|
|
44
44
|
}
|
|
45
|
-
function
|
|
46
|
-
|
|
45
|
+
function c() {
|
|
46
|
+
B = !1, v();
|
|
47
47
|
}
|
|
48
48
|
function v() {
|
|
49
|
-
const
|
|
49
|
+
const r = G(i), o = r.height;
|
|
50
50
|
if (o <= 0) return;
|
|
51
|
-
const
|
|
52
|
-
let
|
|
53
|
-
const
|
|
51
|
+
const k = window.scrollY, _ = r.top, D = _ + o, I = [], q = /* @__PURE__ */ new Map();
|
|
52
|
+
let L = o, j = 0, F = 0;
|
|
53
|
+
const H = /* @__PURE__ */ new Map();
|
|
54
54
|
for (const e of s) {
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
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
|
+
if (M > 0) {
|
|
57
|
+
F += M;
|
|
58
|
+
const A = C - _, E = D - u;
|
|
59
|
+
L = Math.min(L, A), j = Math.max(j, o - E);
|
|
60
|
+
const P = q.get(e.variant);
|
|
61
|
+
P ? (P.topInset = Math.min(P.topInset, A), P.bottomInset = Math.min(P.bottomInset, E)) : q.set(e.variant, { topInset: A, bottomInset: E });
|
|
62
62
|
}
|
|
63
|
-
let f =
|
|
64
|
-
f || (f = [],
|
|
63
|
+
let f = H.get(e.variant);
|
|
64
|
+
f || (f = [], H.set(e.variant, f)), f.push({ viewTop: y, viewBottom: m });
|
|
65
65
|
}
|
|
66
|
-
let
|
|
67
|
-
for (const e of
|
|
68
|
-
if (e.name ===
|
|
69
|
-
|
|
66
|
+
let b = null;
|
|
67
|
+
for (const e of l) {
|
|
68
|
+
if (e.name === w) {
|
|
69
|
+
b = e.wrapper;
|
|
70
70
|
continue;
|
|
71
71
|
}
|
|
72
|
-
const
|
|
73
|
-
if (!
|
|
72
|
+
const y = H.get(e.name);
|
|
73
|
+
if (!y) {
|
|
74
74
|
e.wrapper.style.clipPath = "inset(0 0 100% 0)";
|
|
75
75
|
continue;
|
|
76
76
|
}
|
|
77
|
-
const
|
|
78
|
-
let u =
|
|
79
|
-
for (const f of
|
|
80
|
-
const
|
|
81
|
-
E <=
|
|
77
|
+
const m = e.wrapper.getBoundingClientRect(), C = m.height || o;
|
|
78
|
+
let u = C, M = C;
|
|
79
|
+
for (const f of y) {
|
|
80
|
+
const A = Math.max(m.top, f.viewTop), E = Math.min(m.bottom, f.viewBottom);
|
|
81
|
+
E <= A || (u = Math.min(u, A - m.top), M = Math.min(M, m.bottom - E));
|
|
82
82
|
}
|
|
83
|
-
if (u +
|
|
84
|
-
e.wrapper.style.clipPath = `inset(${u}px 0 ${
|
|
85
|
-
const f =
|
|
86
|
-
|
|
83
|
+
if (u + M < C) {
|
|
84
|
+
e.wrapper.style.clipPath = `inset(${u}px 0 ${M}px 0)`;
|
|
85
|
+
const f = q.get(e.name), A = f ? (o - f.topInset - f.bottomInset) / o : 0;
|
|
86
|
+
I.push({ name: e.name, progress: A });
|
|
87
87
|
} else
|
|
88
88
|
e.wrapper.style.clipPath = "inset(0 0 100% 0)";
|
|
89
89
|
}
|
|
90
|
-
if (
|
|
91
|
-
const e =
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
else if (
|
|
95
|
-
|
|
96
|
-
name:
|
|
90
|
+
if (b) {
|
|
91
|
+
const e = b.getBoundingClientRect().height || o;
|
|
92
|
+
if (F >= o)
|
|
93
|
+
b.style.clipPath = "inset(0 0 100% 0)";
|
|
94
|
+
else if (F <= 0)
|
|
95
|
+
b.style.clipPath = "inset(0)", I.unshift({
|
|
96
|
+
name: w,
|
|
97
97
|
progress: 1
|
|
98
98
|
});
|
|
99
99
|
else {
|
|
100
|
-
const
|
|
101
|
-
if (o -
|
|
102
|
-
const u = e * (
|
|
103
|
-
|
|
100
|
+
const y = L;
|
|
101
|
+
if (o - j >= y) {
|
|
102
|
+
const u = e * (j / o);
|
|
103
|
+
b.style.clipPath = `inset(${u}px 0 0 0)`;
|
|
104
104
|
} else {
|
|
105
|
-
const u = e * ((o -
|
|
106
|
-
|
|
105
|
+
const u = e * ((o - L) / o);
|
|
106
|
+
b.style.clipPath = `inset(0 0 ${u}px 0)`;
|
|
107
107
|
}
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
name:
|
|
111
|
-
progress:
|
|
108
|
+
const C = o - F;
|
|
109
|
+
I.unshift({
|
|
110
|
+
name: w,
|
|
111
|
+
progress: C / o
|
|
112
112
|
});
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
|
-
const
|
|
116
|
-
|
|
115
|
+
const O = I.map((e) => `${e.name}:${e.progress.toFixed(3)}`).join("|");
|
|
116
|
+
O !== x && (x = O, a == null || a(I));
|
|
117
117
|
}
|
|
118
|
-
function
|
|
119
|
-
|
|
120
|
-
|
|
118
|
+
function d() {
|
|
119
|
+
p == null || p.disconnect(), p = new ResizeObserver(() => {
|
|
120
|
+
S(s), g();
|
|
121
121
|
});
|
|
122
|
-
for (const
|
|
123
|
-
|
|
122
|
+
for (const r of s)
|
|
123
|
+
p.observe(r.el);
|
|
124
124
|
}
|
|
125
125
|
function n() {
|
|
126
|
-
window.addEventListener("scroll",
|
|
126
|
+
window.addEventListener("scroll", R, { passive: !0 }), window.addEventListener("resize", V, { passive: !0 }), d(), g();
|
|
127
127
|
}
|
|
128
|
-
function
|
|
129
|
-
|
|
128
|
+
function T() {
|
|
129
|
+
S(s), g();
|
|
130
130
|
}
|
|
131
|
-
function
|
|
132
|
-
|
|
131
|
+
function N(r, o) {
|
|
132
|
+
l = r, s = o, S(s), d(), g();
|
|
133
133
|
}
|
|
134
|
-
function
|
|
135
|
-
|
|
134
|
+
function $() {
|
|
135
|
+
h !== null && cancelAnimationFrame(h), window.removeEventListener("scroll", R), window.removeEventListener("resize", V), p == null || p.disconnect(), p = null;
|
|
136
136
|
}
|
|
137
|
-
return n(), { recalculate:
|
|
137
|
+
return n(), { recalculate: T, update: N, destroy: $ };
|
|
138
138
|
}
|
|
139
|
-
const
|
|
139
|
+
const W = "data-midday-variant", Y = "default";
|
|
140
140
|
function Z(t, i = {}) {
|
|
141
|
-
const { onChange:
|
|
142
|
-
let
|
|
143
|
-
function
|
|
144
|
-
const
|
|
145
|
-
for (const
|
|
146
|
-
v.add(
|
|
141
|
+
const { onChange: l } = i, s = i.name ?? (t.getAttribute("data-midday-element") || void 0), w = t.style.overflow;
|
|
142
|
+
let a = null, h = [];
|
|
143
|
+
function B() {
|
|
144
|
+
const c = U(s), v = /* @__PURE__ */ new Set();
|
|
145
|
+
for (const r of c)
|
|
146
|
+
v.add(r.variant);
|
|
147
147
|
t.style.overflow = "visible";
|
|
148
|
-
const
|
|
148
|
+
const d = document.createDocumentFragment();
|
|
149
149
|
for (; t.firstChild; )
|
|
150
|
-
|
|
150
|
+
d.appendChild(t.firstChild);
|
|
151
151
|
const n = [];
|
|
152
|
-
for (const
|
|
153
|
-
n.push(r
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
const
|
|
157
|
-
t.appendChild(
|
|
158
|
-
const
|
|
159
|
-
for (const
|
|
160
|
-
t.appendChild(
|
|
161
|
-
return
|
|
162
|
-
}
|
|
163
|
-
function
|
|
152
|
+
for (const r of v)
|
|
153
|
+
n.push(x(r, d, !0));
|
|
154
|
+
const T = document.createElement("div");
|
|
155
|
+
T.style.visibility = "hidden", T.style.pointerEvents = "none", T.setAttribute("aria-hidden", "true"), T.appendChild(d.cloneNode(!0)), t.appendChild(T);
|
|
156
|
+
const N = x(Y, d, !1);
|
|
157
|
+
t.appendChild(N.wrapper);
|
|
158
|
+
const $ = [N];
|
|
159
|
+
for (const r of n)
|
|
160
|
+
t.appendChild(r.wrapper), $.push(r);
|
|
161
|
+
return $;
|
|
162
|
+
}
|
|
163
|
+
function x(c, v, d) {
|
|
164
164
|
const n = document.createElement("div");
|
|
165
|
-
return n.setAttribute(
|
|
165
|
+
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
166
|
}
|
|
167
|
-
function
|
|
168
|
-
const
|
|
169
|
-
|
|
167
|
+
function p() {
|
|
168
|
+
const c = U(s);
|
|
169
|
+
h = B(), a = X({
|
|
170
170
|
element: t,
|
|
171
|
-
variants:
|
|
172
|
-
defaultName:
|
|
173
|
-
sections:
|
|
174
|
-
onChange:
|
|
171
|
+
variants: h,
|
|
172
|
+
defaultName: Y,
|
|
173
|
+
sections: c,
|
|
174
|
+
onChange: l
|
|
175
175
|
});
|
|
176
176
|
}
|
|
177
|
-
function
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const
|
|
182
|
-
|
|
177
|
+
function R() {
|
|
178
|
+
const c = t.querySelector(
|
|
179
|
+
`[${W}="${Y}"]`
|
|
180
|
+
), v = t.querySelectorAll(`[${W}]`);
|
|
181
|
+
for (const d of v)
|
|
182
|
+
d.remove();
|
|
183
|
+
for (; t.firstChild; )
|
|
184
|
+
t.removeChild(t.firstChild);
|
|
185
|
+
if (c)
|
|
186
|
+
for (; c.firstChild; )
|
|
187
|
+
t.appendChild(c.firstChild);
|
|
188
|
+
}
|
|
189
|
+
function V() {
|
|
190
|
+
R();
|
|
191
|
+
const c = U(s);
|
|
192
|
+
h = B(), a == null || a.update(h, c);
|
|
183
193
|
}
|
|
184
|
-
function
|
|
185
|
-
|
|
186
|
-
for (const l of d)
|
|
187
|
-
l.wrapper.remove();
|
|
188
|
-
d = [], t.innerHTML = h, t.style.overflow = w;
|
|
194
|
+
function g() {
|
|
195
|
+
a == null || a.destroy(), a = null, R(), h = [], t.style.overflow = w;
|
|
189
196
|
}
|
|
190
|
-
return
|
|
197
|
+
return p(), { refresh: V, destroy: g };
|
|
191
198
|
}
|
|
192
199
|
export {
|
|
193
200
|
Z as a,
|
|
194
|
-
|
|
195
|
-
|
|
201
|
+
X as c,
|
|
202
|
+
U as s
|
|
196
203
|
};
|
package/dist/midday.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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 l=I(n.el);n.top=l.top,n.height=l.height}}const J="[data-midday-section]",Q="data-midday-section",X="data-midday-target";function N(t){const n=document.querySelectorAll(J),l=[];for(const s of n){const p=s.getAttribute(Q);if(!p)continue;const a=s.getAttribute(X);a&&(!t||!a.split(" ").includes(t))||l.push({el:s,variant:p,top:0,height:0})}return V(l),l}function k(t){let{element:n,variants:l,sections:s}=t;const{defaultName:p,onChange:a}=t;let i=null,y=!1,B="",d=null;function E(){w()}function v(){V(s),w()}function w(){y||(y=!0,i=requestAnimationFrame(r))}function r(){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 l){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(){d==null||d.disconnect(),d=new ResizeObserver(()=>{V(s),w()});for(const u of s)d.observe(u.el)}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){l=u,s=c,V(s),f(),w()}function H(){i!==null&&cancelAnimationFrame(i),window.removeEventListener("scroll",E),window.removeEventListener("resize",v),d==null||d.disconnect(),d=null}return o(),{recalculate:x,update:L,destroy:H}}const O="data-midday-variant",U="default";function Z(t,n={}){const{onChange:l}=n,s=n.name??(t.getAttribute("data-midday-element")||void 0),p=t.style.overflow;let a=null,i=[];function y(){const r=N(s),g=new Set;for(const u of r)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(r,g,f){const o=document.createElement("div");return o.setAttribute(O,r),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:r}}function d(){const r=N(s);i=y(),a=k({element:t,variants:i,defaultName:U,sections:r,onChange:l})}function E(){const r=t.querySelector(`[${O}="${U}"]`),g=t.querySelectorAll(`[${O}]`);for(const f of g)f.remove();for(;t.firstChild;)t.removeChild(t.firstChild);if(r)for(;r.firstChild;)t.appendChild(r.firstChild)}function v(){E();const r=N(s);i=y(),a==null||a.update(i,r)}function w(){a==null||a.destroy(),a=null,E(),i=[],t.style.overflow=p}return d(),{refresh:v,destroy:w}}function tt(t){const{element:n,variants:l,defaultVariant:s="default",name:p,onChange:a}=t;let i=null;function y(){return Object.entries(l).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 d(){const v=N(p);i==null||i.update(y(),v)}function E(){i==null||i.destroy(),i=null}return B(),{refresh:d,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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marcwiest/midday.js",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
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"
|