@clubmed/trident-ui
Version:
Shared ClubMed React UI components
85 lines (84 loc) • 2.36 kB
JavaScript
"use client";
import { jsx as l } from "react/jsx-runtime";
import { createContext as c, useReducer as v, useMemo as u, useCallback as x } from "react";
const C = (t) => {
const e = Math.max(Math.min(t.min, t.max), 0), r = Math.max(Math.min(t.max, 1 / 0), e);
return {
min: e,
max: r,
clamp: (n) => Math.max(Math.min(n, r), e)
};
}, g = (t) => {
const { clamp: e } = C(t);
return e(t.value);
}, P = (t) => {
const { min: e, max: r, clamp: n } = C(t);
return (a, o) => {
switch (o.type) {
case "update":
return n(o.payload);
case "next":
return a + 1 <= r ? a + 1 : e;
case "previous":
return a - 1 >= e ? a - 1 : r;
case "start":
return e;
case "end":
return r;
}
};
}, R = (t, e) => {
switch (e.type) {
case "register":
return new Map(t.set(e.payload.value, e.payload.ref));
case "unregister":
return t.delete(e.payload.value), new Map(t);
}
}, h = c({ value: 0 });
h.displayName = "ActiveTabContext";
const y = c({
refs: /* @__PURE__ */ new Map()
});
y.displayName = "TabRefsContext";
const b = c({
dispatch: () => {
console.warn("Tab Controller Context dispatch used outside of Provider");
},
register: () => {
console.warn("Tab Controller Context register used outside of Provider");
},
unregister: () => {
console.warn("Tab Controller Context unregister used outside of Provider");
}
});
b.displayName = "TabControlContext";
const A = ({
min: t = 0,
max: e,
selected: r = t,
children: n
}) => {
const [a, o] = v(
P({ min: t, max: e }),
{ value: r, min: t, max: e },
g
), [i, d] = v(R, /* @__PURE__ */ new Map()), M = u(() => ({ value: a }), [a]), f = u(() => ({ refs: i }), [i]), p = x(
(s, w) => {
d({ type: "register", payload: { value: s, ref: w } });
},
[]
), m = x(
(s) => {
d({ type: "unregister", payload: { value: s } });
},
[]
), T = u(() => ({ dispatch: o, register: p, unregister: m }), [p, m]);
return /* @__PURE__ */ l(h.Provider, { value: M, children: /* @__PURE__ */ l(y.Provider, { value: f, children: /* @__PURE__ */ l(b.Provider, { value: T, children: n }) }) });
};
export {
h as ActiveTabContext,
b as TabControlContext,
y as TabRefsContext,
A as TabsProvider
};
//# sourceMappingURL=TabControl.js.map