@mateosuarezdev/react-ui 1.0.0

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/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Mateo Suárez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # React UI
2
+
3
+ # WIP
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("react/jsx-runtime"),o=require("react"),h=require("react-dom"),a=o.createContext(null),P=({children:e,value:r})=>f.jsx(a.Provider,{value:r,children:e}),K=()=>{const e=o.useContext(a);if(!e)throw new Error("useAliveScope must be wrapped within AliveScopeProvider");return e},S=({children:e})=>{const[r,l]=o.useState({}),n=o.useRef({}),c=o.useRef({}),p=(t,u,s)=>{if(!r[t]){const i=document.createElement("div");return t&&(i.id=t),s&&(i.className=s),l(v=>({...v,[t]:{children:u,element:i}})),n.current[t]=[],c.current[t]=[],n.current&&n.current[t]&&n.current[t].forEach(v=>v(t)),i}return n.current&&n.current[t]&&n.current[t].forEach(i=>i(t)),r[t].element},m=t=>{l(u=>{const s={...u};return delete s[t],s}),c.current&&c.current[t]&&c.current[t].forEach(u=>u(t))};return f.jsxs(P,{value:{getPortalElement:p,dropPortalElement:m},children:[e,Object.entries(r).map(([t,{children:u,element:s}])=>f.jsx(j,{id:t,onMountCallbacks:n,onDropCallbacks:c,children:u,element:s},t))]})},A=o.createContext(null),x=({children:e,value:r})=>f.jsx(A.Provider,{value:r,children:e}),j=o.memo(({id:e,onMountCallbacks:r,onDropCallbacks:l,children:n,element:c})=>{const p=(t,u={runOnFirstMount:!1})=>{o.useEffect(()=>{const s=()=>{if(document.getElementById(e))t(e);else{const v=new MutationObserver((b,E)=>{document.getElementById(e)&&(t(e),E.disconnect())});return v.observe(document.body,{childList:!0,subtree:!0}),()=>v.disconnect()}};return u?.runOnFirstMount&&s(),r.current[e].push(s),()=>{r.current[e]=r.current[e].filter(i=>i!==s)}},[])},m=t=>{o.useEffect(()=>(l.current[e].push(t),()=>{l.current[e]=l.current[e].filter(u=>u!==t)}),[])};return f.jsx(x,{value:{keepAliveId:e,onKeepAliveMount:p,onKeepAliveDrop:m},children:h.createPortal(n,c,e)})}),w=()=>{const e=o.useContext(A);if(!e)throw new Error("useKeepAlive must be used within KeepAliveProvider");return e},C=({aliveKey:e,children:r,enabled:l=!0,...n})=>{const c=o.useRef(null),{getPortalElement:p}=K();return o.useEffect(()=>{const m=p(e,r,n.className);return l&&c.current&&c.current.replaceWith(m),()=>{}},[e,r,l,n]),f.jsx("div",{...n,ref:c})};exports.AliveScope=S;exports.KeepAlive=C;exports.KeepAliveContext=A;exports.KeepAliveProvider=x;exports.useKeepAlive=w;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/components/keep-alive.tsx"],"sourcesContent":["import {\n createContext,\n memo,\n useContext,\n useEffect,\n useRef,\n useState,\n} from \"react\";\nimport { createPortal } from \"react-dom\";\n\n//============================\n// #region AliveScope\n//============================\ntype AliveScopeContextType = {\n getPortalElement: (\n id: string,\n children: React.ReactNode,\n className?: string\n ) => HTMLElement;\n dropPortalElement: (id: string) => void;\n};\n\nconst AliveScopeContext = createContext<AliveScopeContextType | null>(null);\n\nconst AliveScopeProvider = ({\n children,\n value,\n}: {\n children: React.ReactNode;\n value: AliveScopeContextType;\n}) => {\n return (\n <AliveScopeContext.Provider value={value}>\n {children}\n </AliveScopeContext.Provider>\n );\n};\n\nconst useAliveScope = () => {\n const context = useContext(AliveScopeContext);\n\n if (!context)\n throw new Error(\"useAliveScope must be wrapped within AliveScopeProvider\");\n\n return context;\n};\n\ntype AliveScopeProps = {\n children?: React.ReactNode;\n};\n\nconst AliveScope: React.FC<AliveScopeProps> = ({ children }) => {\n const [nodes, setNodes] = useState<{\n [id: string]: { children: React.ReactNode; element: HTMLElement };\n }>({});\n\n const onMountCallbacks = useRef<{ [id: string]: ((id: string) => void)[] }>(\n {}\n );\n const onDropCallbacks = useRef<{ [id: string]: ((id: string) => void)[] }>(\n {}\n );\n\n const getPortalElement = (\n id: string,\n children: React.ReactNode,\n className?: string\n ) => {\n if (!nodes[id]) {\n const element = document.createElement(\"div\");\n if (id) {\n element.id = id;\n }\n if (className) {\n element.className = className;\n }\n setNodes((prevNodes) => ({\n ...prevNodes,\n [id]: { children, element },\n }));\n onMountCallbacks.current[id] = [];\n onDropCallbacks.current[id] = [];\n if (onMountCallbacks.current && onMountCallbacks.current[id]) {\n onMountCallbacks.current[id].forEach((callback) => callback(id));\n }\n return element;\n }\n if (onMountCallbacks.current && onMountCallbacks.current[id]) {\n onMountCallbacks.current[id].forEach((callback) => callback(id));\n }\n return nodes[id].element;\n };\n\n const dropPortalElement = (id: string) => {\n setNodes((prevNodes) => {\n const copy = { ...prevNodes };\n delete copy[id];\n return copy;\n });\n //console.log(\"dropped\", id);\n if (onDropCallbacks.current && onDropCallbacks.current[id]) {\n onDropCallbacks.current[id].forEach((callback) => callback(id));\n }\n };\n\n return (\n <AliveScopeProvider\n value={{\n getPortalElement,\n dropPortalElement,\n }}\n >\n {children}\n {Object.entries(nodes).map(([id, { children, element }]) => (\n <Component\n key={id}\n id={id}\n onMountCallbacks={onMountCallbacks}\n onDropCallbacks={onDropCallbacks}\n children={children}\n element={element}\n />\n ))}\n </AliveScopeProvider>\n );\n};\n//============================\n// #endregion AliveScope\n//============================\n\n//============================\n// #region KeepAlive\n//============================\ntype KeepAliveContextType = {\n keepAliveId: string;\n onKeepAliveMount: (\n callback: (id: string) => void,\n options?: { runOnFirstMount?: boolean }\n ) => void;\n onKeepAliveDrop: (callback: (id: string) => void) => void;\n};\n\nexport const KeepAliveContext = createContext<KeepAliveContextType | null>(\n null\n);\n\nexport const KeepAliveProvider = ({\n children,\n value,\n}: {\n children: React.ReactNode;\n value: KeepAliveContextType;\n}) => {\n return (\n <KeepAliveContext.Provider value={value}>\n {children}\n </KeepAliveContext.Provider>\n );\n};\n\nconst Component = memo<{\n id: string;\n onMountCallbacks: React.MutableRefObject<{\n [id: string]: ((id: string) => void)[];\n }>;\n onDropCallbacks: React.MutableRefObject<{\n [id: string]: ((id: string) => void)[];\n }>;\n children: React.ReactNode;\n element: HTMLElement;\n}>(({ id, onMountCallbacks, onDropCallbacks, children, element }) => {\n const onKeepAliveMount = (\n callback: (id: string) => void,\n options: { runOnFirstMount?: boolean } = { runOnFirstMount: false }\n ) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useEffect(() => {\n const runCallback = () => {\n const el = document.getElementById(id);\n if (el) {\n callback(id);\n } else {\n // If element is not found, use MutationObserver to wait for it\n const observer = new MutationObserver((_mutations, obs) => {\n const el = document.getElementById(id);\n if (el) {\n callback(id);\n obs.disconnect(); // Stop observing once element is found\n }\n });\n\n observer.observe(document.body, {\n childList: true,\n subtree: true,\n });\n\n // Cleanup function to disconnect the observer if component unmounts\n return () => observer.disconnect();\n }\n };\n\n if (options?.runOnFirstMount) {\n runCallback();\n }\n\n onMountCallbacks.current[id].push(runCallback);\n return () => {\n onMountCallbacks.current[id] = onMountCallbacks.current[id].filter(\n (cb) => cb !== runCallback\n );\n };\n }, []);\n };\n\n const onKeepAliveDrop = (callback: (id: string) => void) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useEffect(() => {\n onDropCallbacks.current[id].push(callback);\n return () => {\n onDropCallbacks.current[id] = onDropCallbacks.current[id].filter(\n (cb) => cb !== callback\n );\n };\n }, []);\n };\n\n return (\n <KeepAliveProvider\n value={{ keepAliveId: id, onKeepAliveMount, onKeepAliveDrop }}\n >\n {createPortal(children, element, id)}\n </KeepAliveProvider>\n );\n});\n\nconst useKeepAlive = () => {\n const context = useContext(KeepAliveContext);\n\n if (!context)\n throw new Error(\"useKeepAlive must be used within KeepAliveProvider\");\n\n return context;\n};\n\ntype KeepAliveProps = React.HTMLAttributes<HTMLDivElement> & {\n aliveKey: string;\n children: React.ReactNode;\n enabled?: boolean;\n};\n\nconst KeepAlive: React.FC<KeepAliveProps> = ({\n aliveKey,\n children,\n enabled = true,\n ...others\n}) => {\n const keepAliveRef = useRef<HTMLDivElement | null>(null);\n const { getPortalElement } = useAliveScope();\n\n useEffect(() => {\n const element = getPortalElement(aliveKey, children, others.className);\n\n if (enabled) {\n if (keepAliveRef.current) {\n keepAliveRef.current.replaceWith(element);\n }\n }\n\n return () => {\n //TODO\n //callUnmountCallbacks(aliveKey)\n };\n }, [aliveKey, children, enabled, others]);\n\n return <div {...others} ref={keepAliveRef} />;\n};\n//============================\n// #endregion KeepAlive\n//============================\n\nexport { AliveScope, KeepAlive, useKeepAlive };\n"],"names":["AliveScopeContext","createContext","AliveScopeProvider","children","value","jsx","useAliveScope","context","useContext","AliveScope","nodes","setNodes","useState","onMountCallbacks","useRef","onDropCallbacks","getPortalElement","id","className","element","prevNodes","callback","dropPortalElement","copy","jsxs","Component","KeepAliveContext","KeepAliveProvider","memo","onKeepAliveMount","options","useEffect","runCallback","observer","_mutations","obs","cb","onKeepAliveDrop","createPortal","useKeepAlive","KeepAlive","aliveKey","enabled","others","keepAliveRef"],"mappings":"+JAsBMA,EAAoBC,EAAAA,cAA4C,IAAI,EAEpEC,EAAqB,CAAC,CAC1B,SAAAC,EACA,MAAAC,CACF,IAKIC,EAAAA,IAACL,EAAkB,SAAlB,CAA2B,MAAAI,EACzB,SAAAD,CAAA,CACH,EAIEG,EAAgB,IAAM,CAC1B,MAAMC,EAAUC,EAAAA,WAAWR,CAAiB,EAE5C,GAAI,CAACO,EACH,MAAM,IAAI,MAAM,yDAAyD,EAE3E,OAAOA,CACT,EAMME,EAAwC,CAAC,CAAE,SAAAN,KAAe,CAC9D,KAAM,CAACO,EAAOC,CAAQ,EAAIC,EAAAA,SAEvB,CAAA,CAAE,EAECC,EAAmBC,EAAAA,OACvB,CAAA,CAAC,EAEGC,EAAkBD,EAAAA,OACtB,CAAA,CAAC,EAGGE,EAAmB,CACvBC,EACAd,EACAe,IACG,CACH,GAAI,CAACR,EAAMO,CAAE,EAAG,CACd,MAAME,EAAU,SAAS,cAAc,KAAK,EAC5C,OAAIF,IACFE,EAAQ,GAAKF,GAEXC,IACFC,EAAQ,UAAYD,GAEtBP,EAAUS,IAAe,CACvB,GAAGA,EACH,CAACH,CAAE,EAAG,CAAE,SAAAd,EAAU,QAAAgB,CAAA,CAAQ,EAC1B,EACFN,EAAiB,QAAQI,CAAE,EAAI,CAAA,EAC/BF,EAAgB,QAAQE,CAAE,EAAI,CAAA,EAC1BJ,EAAiB,SAAWA,EAAiB,QAAQI,CAAE,GACzDJ,EAAiB,QAAQI,CAAE,EAAE,QAASI,GAAaA,EAASJ,CAAE,CAAC,EAE1DE,CACT,CACA,OAAIN,EAAiB,SAAWA,EAAiB,QAAQI,CAAE,GACzDJ,EAAiB,QAAQI,CAAE,EAAE,QAASI,GAAa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,EAEjCtC,EAAAA,IAAC,MAAA,CAAK,GAAGsC,EAAQ,IAAKC,EAAc,CAC7C"}
@@ -0,0 +1,35 @@
1
+ import { Context } from 'react';
2
+ import { JSX } from 'react/jsx-runtime';
3
+
4
+ export declare const AliveScope: React.FC<AliveScopeProps>;
5
+
6
+ declare type AliveScopeProps = {
7
+ children?: React.ReactNode;
8
+ };
9
+
10
+ export declare const KeepAlive: React.FC<KeepAliveProps>;
11
+
12
+ export declare const KeepAliveContext: Context<KeepAliveContextType | null>;
13
+
14
+ declare type KeepAliveContextType = {
15
+ keepAliveId: string;
16
+ onKeepAliveMount: (callback: (id: string) => void, options?: {
17
+ runOnFirstMount?: boolean;
18
+ }) => void;
19
+ onKeepAliveDrop: (callback: (id: string) => void) => void;
20
+ };
21
+
22
+ declare type KeepAliveProps = React.HTMLAttributes<HTMLDivElement> & {
23
+ aliveKey: string;
24
+ children: React.ReactNode;
25
+ enabled?: boolean;
26
+ };
27
+
28
+ export declare const KeepAliveProvider: ({ children, value, }: {
29
+ children: React.ReactNode;
30
+ value: KeepAliveContextType;
31
+ }) => JSX.Element;
32
+
33
+ export declare const useKeepAlive: () => KeepAliveContextType;
34
+
35
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,118 @@
1
+ import { jsxs as K, jsx as p } from "react/jsx-runtime";
2
+ import { createContext as a, useState as w, useRef as f, memo as S, useContext as E, useEffect as A } from "react";
3
+ import { createPortal as g } from "react-dom";
4
+ const h = a(null), b = ({
5
+ children: e,
6
+ value: r
7
+ }) => /* @__PURE__ */ p(h.Provider, { value: r, children: e }), C = () => {
8
+ const e = E(h);
9
+ if (!e)
10
+ throw new Error("useAliveScope must be wrapped within AliveScopeProvider");
11
+ return e;
12
+ }, F = ({ children: e }) => {
13
+ const [r, s] = w({}), n = f(
14
+ {}
15
+ ), o = f(
16
+ {}
17
+ );
18
+ return /* @__PURE__ */ K(
19
+ b,
20
+ {
21
+ value: {
22
+ getPortalElement: (t, c, u) => {
23
+ if (!r[t]) {
24
+ const l = document.createElement("div");
25
+ return t && (l.id = t), u && (l.className = u), s((i) => ({
26
+ ...i,
27
+ [t]: { children: c, element: l }
28
+ })), n.current[t] = [], o.current[t] = [], n.current && n.current[t] && n.current[t].forEach((i) => i(t)), l;
29
+ }
30
+ return n.current && n.current[t] && n.current[t].forEach((l) => l(t)), r[t].element;
31
+ },
32
+ dropPortalElement: (t) => {
33
+ s((c) => {
34
+ const u = { ...c };
35
+ return delete u[t], u;
36
+ }), o.current && o.current[t] && o.current[t].forEach((c) => c(t));
37
+ }
38
+ },
39
+ children: [
40
+ e,
41
+ Object.entries(r).map(([t, { children: c, element: u }]) => /* @__PURE__ */ p(
42
+ O,
43
+ {
44
+ id: t,
45
+ onMountCallbacks: n,
46
+ onDropCallbacks: o,
47
+ children: c,
48
+ element: u
49
+ },
50
+ t
51
+ ))
52
+ ]
53
+ }
54
+ );
55
+ }, P = a(
56
+ null
57
+ ), M = ({
58
+ children: e,
59
+ value: r
60
+ }) => /* @__PURE__ */ p(P.Provider, { value: r, children: e }), O = S(({ id: e, onMountCallbacks: r, onDropCallbacks: s, children: n, element: o }) => /* @__PURE__ */ p(
61
+ M,
62
+ {
63
+ value: { keepAliveId: e, onKeepAliveMount: (t, c = { runOnFirstMount: !1 }) => {
64
+ A(() => {
65
+ const u = () => {
66
+ if (document.getElementById(e))
67
+ t(e);
68
+ else {
69
+ const i = new MutationObserver((d, x) => {
70
+ document.getElementById(e) && (t(e), x.disconnect());
71
+ });
72
+ return i.observe(document.body, {
73
+ childList: !0,
74
+ subtree: !0
75
+ }), () => i.disconnect();
76
+ }
77
+ };
78
+ return c?.runOnFirstMount && u(), r.current[e].push(u), () => {
79
+ r.current[e] = r.current[e].filter(
80
+ (l) => l !== u
81
+ );
82
+ };
83
+ }, []);
84
+ }, onKeepAliveDrop: (t) => {
85
+ A(() => (s.current[e].push(t), () => {
86
+ s.current[e] = s.current[e].filter(
87
+ (c) => c !== t
88
+ );
89
+ }), []);
90
+ } },
91
+ children: g(n, o, e)
92
+ }
93
+ )), N = () => {
94
+ const e = E(P);
95
+ if (!e)
96
+ throw new Error("useKeepAlive must be used within KeepAliveProvider");
97
+ return e;
98
+ }, R = ({
99
+ aliveKey: e,
100
+ children: r,
101
+ enabled: s = !0,
102
+ ...n
103
+ }) => {
104
+ const o = f(null), { getPortalElement: v } = C();
105
+ return A(() => {
106
+ const m = v(e, r, n.className);
107
+ return s && o.current && o.current.replaceWith(m), () => {
108
+ };
109
+ }, [e, r, s, n]), /* @__PURE__ */ p("div", { ...n, ref: o });
110
+ };
111
+ export {
112
+ F as AliveScope,
113
+ R as KeepAlive,
114
+ P as KeepAliveContext,
115
+ M as KeepAliveProvider,
116
+ N as useKeepAlive
117
+ };
118
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/components/keep-alive.tsx"],"sourcesContent":["import {\n createContext,\n memo,\n useContext,\n useEffect,\n useRef,\n useState,\n} from \"react\";\nimport { createPortal } from \"react-dom\";\n\n//============================\n// #region AliveScope\n//============================\ntype AliveScopeContextType = {\n getPortalElement: (\n id: string,\n children: React.ReactNode,\n className?: string\n ) => HTMLElement;\n dropPortalElement: (id: string) => void;\n};\n\nconst AliveScopeContext = createContext<AliveScopeContextType | null>(null);\n\nconst AliveScopeProvider = ({\n children,\n value,\n}: {\n children: React.ReactNode;\n value: AliveScopeContextType;\n}) => {\n return (\n <AliveScopeContext.Provider value={value}>\n {children}\n </AliveScopeContext.Provider>\n );\n};\n\nconst useAliveScope = () => {\n const context = useContext(AliveScopeContext);\n\n if (!context)\n throw new Error(\"useAliveScope must be wrapped within AliveScopeProvider\");\n\n return context;\n};\n\ntype AliveScopeProps = {\n children?: React.ReactNode;\n};\n\nconst AliveScope: React.FC<AliveScopeProps> = ({ children }) => {\n const [nodes, setNodes] = useState<{\n [id: string]: { children: React.ReactNode; element: HTMLElement };\n }>({});\n\n const onMountCallbacks = useRef<{ [id: string]: ((id: string) => void)[] }>(\n {}\n );\n const onDropCallbacks = useRef<{ [id: string]: ((id: string) => void)[] }>(\n {}\n );\n\n const getPortalElement = (\n id: string,\n children: React.ReactNode,\n className?: string\n ) => {\n if (!nodes[id]) {\n const element = document.createElement(\"div\");\n if (id) {\n element.id = id;\n }\n if (className) {\n element.className = className;\n }\n setNodes((prevNodes) => ({\n ...prevNodes,\n [id]: { children, element },\n }));\n onMountCallbacks.current[id] = [];\n onDropCallbacks.current[id] = [];\n if (onMountCallbacks.current && onMountCallbacks.current[id]) {\n onMountCallbacks.current[id].forEach((callback) => callback(id));\n }\n return element;\n }\n if (onMountCallbacks.current && onMountCallbacks.current[id]) {\n onMountCallbacks.current[id].forEach((callback) => callback(id));\n }\n return nodes[id].element;\n };\n\n const dropPortalElement = (id: string) => {\n setNodes((prevNodes) => {\n const copy = { ...prevNodes };\n delete copy[id];\n return copy;\n });\n //console.log(\"dropped\", id);\n if (onDropCallbacks.current && onDropCallbacks.current[id]) {\n onDropCallbacks.current[id].forEach((callback) => callback(id));\n }\n };\n\n return (\n <AliveScopeProvider\n value={{\n getPortalElement,\n dropPortalElement,\n }}\n >\n {children}\n {Object.entries(nodes).map(([id, { children, element }]) => (\n <Component\n key={id}\n id={id}\n onMountCallbacks={onMountCallbacks}\n onDropCallbacks={onDropCallbacks}\n children={children}\n element={element}\n />\n ))}\n </AliveScopeProvider>\n );\n};\n//============================\n// #endregion AliveScope\n//============================\n\n//============================\n// #region KeepAlive\n//============================\ntype KeepAliveContextType = {\n keepAliveId: string;\n onKeepAliveMount: (\n callback: (id: string) => void,\n options?: { runOnFirstMount?: boolean }\n ) => void;\n onKeepAliveDrop: (callback: (id: string) => void) => void;\n};\n\nexport const KeepAliveContext = createContext<KeepAliveContextType | null>(\n null\n);\n\nexport const KeepAliveProvider = ({\n children,\n value,\n}: {\n children: React.ReactNode;\n value: KeepAliveContextType;\n}) => {\n return (\n <KeepAliveContext.Provider value={value}>\n {children}\n </KeepAliveContext.Provider>\n );\n};\n\nconst Component = memo<{\n id: string;\n onMountCallbacks: React.MutableRefObject<{\n [id: string]: ((id: string) => void)[];\n }>;\n onDropCallbacks: React.MutableRefObject<{\n [id: string]: ((id: string) => void)[];\n }>;\n children: React.ReactNode;\n element: HTMLElement;\n}>(({ id, onMountCallbacks, onDropCallbacks, children, element }) => {\n const onKeepAliveMount = (\n callback: (id: string) => void,\n options: { runOnFirstMount?: boolean } = { runOnFirstMount: false }\n ) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useEffect(() => {\n const runCallback = () => {\n const el = document.getElementById(id);\n if (el) {\n callback(id);\n } else {\n // If element is not found, use MutationObserver to wait for it\n const observer = new MutationObserver((_mutations, obs) => {\n const el = document.getElementById(id);\n if (el) {\n callback(id);\n obs.disconnect(); // Stop observing once element is found\n }\n });\n\n observer.observe(document.body, {\n childList: true,\n subtree: true,\n });\n\n // Cleanup function to disconnect the observer if component unmounts\n return () => observer.disconnect();\n }\n };\n\n if (options?.runOnFirstMount) {\n runCallback();\n }\n\n onMountCallbacks.current[id].push(runCallback);\n return () => {\n onMountCallbacks.current[id] = onMountCallbacks.current[id].filter(\n (cb) => cb !== runCallback\n );\n };\n }, []);\n };\n\n const onKeepAliveDrop = (callback: (id: string) => void) => {\n // eslint-disable-next-line react-hooks/rules-of-hooks\n useEffect(() => {\n onDropCallbacks.current[id].push(callback);\n return () => {\n onDropCallbacks.current[id] = onDropCallbacks.current[id].filter(\n (cb) => cb !== callback\n );\n };\n }, []);\n };\n\n return (\n <KeepAliveProvider\n value={{ keepAliveId: id, onKeepAliveMount, onKeepAliveDrop }}\n >\n {createPortal(children, element, id)}\n </KeepAliveProvider>\n );\n});\n\nconst useKeepAlive = () => {\n const context = useContext(KeepAliveContext);\n\n if (!context)\n throw new Error(\"useKeepAlive must be used within KeepAliveProvider\");\n\n return context;\n};\n\ntype KeepAliveProps = React.HTMLAttributes<HTMLDivElement> & {\n aliveKey: string;\n children: React.ReactNode;\n enabled?: boolean;\n};\n\nconst KeepAlive: React.FC<KeepAliveProps> = ({\n aliveKey,\n children,\n enabled = true,\n ...others\n}) => {\n const keepAliveRef = useRef<HTMLDivElement | null>(null);\n const { getPortalElement } = useAliveScope();\n\n useEffect(() => {\n const element = getPortalElement(aliveKey, children, others.className);\n\n if (enabled) {\n if (keepAliveRef.current) {\n keepAliveRef.current.replaceWith(element);\n }\n }\n\n return () => {\n //TODO\n //callUnmountCallbacks(aliveKey)\n };\n }, [aliveKey, children, enabled, others]);\n\n return <div {...others} ref={keepAliveRef} />;\n};\n//============================\n// #endregion KeepAlive\n//============================\n\nexport { AliveScope, KeepAlive, useKeepAlive };\n"],"names":["AliveScopeContext","createContext","AliveScopeProvider","children","value","jsx","useAliveScope","context","useContext","AliveScope","nodes","setNodes","useState","onMountCallbacks","useRef","onDropCallbacks","jsxs","id","className","element","prevNodes","callback","copy","Component","KeepAliveContext","KeepAliveProvider","memo","options","useEffect","runCallback","observer","_mutations","obs","cb","createPortal","useKeepAlive","KeepAlive","aliveKey","enabled","others","keepAliveRef","getPortalElement"],"mappings":";;;AAsBA,MAAMA,IAAoBC,EAA4C,IAAI,GAEpEC,IAAqB,CAAC;AAAA,EAC1B,UAAAC;AAAA,EACA,OAAAC;AACF,MAKI,gBAAAC,EAACL,EAAkB,UAAlB,EAA2B,OAAAI,GACzB,UAAAD,EAAA,CACH,GAIEG,IAAgB,MAAM;AAC1B,QAAMC,IAAUC,EAAWR,CAAiB;AAE5C,MAAI,CAACO;AACH,UAAM,IAAI,MAAM,yDAAyD;AAE3E,SAAOA;AACT,GAMME,IAAwC,CAAC,EAAE,UAAAN,QAAe;AAC9D,QAAM,CAACO,GAAOC,CAAQ,IAAIC,EAEvB,CAAA,CAAE,GAECC,IAAmBC;AAAA,IACvB,CAAA;AAAA,EAAC,GAEGC,IAAkBD;AAAA,IACtB,CAAA;AAAA,EAAC;AA6CH,SACE,gBAAAE;AAAA,IAACd;AAAA,IAAA;AAAA,MACC,OAAO;AAAA,QACL,kBA7CmB,CACvBe,GACAd,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AQ,GAClC,MAAM;AACX,QAAAN,EAAgB,QAAQE,CAAE,IAAIF,EAAgB,QAAQE,CAAE,EAAE;AAAA,UACxD,CAACgB,MAAOA,MAAOZ;AAAA,QAAA;AAAA,MAEnB,IACC,CAAA,CAAE;AAAA,IACP,EAIgD;AAAA,IAE3C,UAAAa,EAAa/B,GAAUgB,GAASF,CAAE;AAAA,EAAA;AAAA,CAGxC,GAEKkB,IAAe,MAAM;AACzB,QAAM5B,IAAUC,EAAWgB,CAAgB;AAE3C,MAAI,CAACjB;AACH,UAAM,IAAI,MAAM,oDAAoD;AAEtE,SAAOA;AACT,GAQM6B,IAAsC,CAAC;AAAA,EAC3C,UAAAC;AAAA,EACA,UAAAlC;AAAA,EACA,SAAAmC,IAAU;AAAA,EACV,GAAGC;AACL,MAAM;AACJ,QAAMC,IAAe1B,EAA8B,IAAI,GACjD,EAAE,kBAAA2B,EAAA,IAAqBnC,EAAA;AAE7B,SAAAsB,EAAU,MAAM;AACd,UAAMT,IAAUsB,EAAiBJ,GAAUlC,GAAUoC,EAAO,SAAS;AAErE,WAAID,KACEE,EAAa,WACfA,EAAa,QAAQ,YAAYrB,CAAO,GAIrC,MAAM;AAAA,IAGb;AAAA,EACF,GAAG,CAACkB,GAAUlC,GAAUmC,GAASC,CAAM,CAAC,GAEjC,gBAAAlC,EAAC,OAAA,EAAK,GAAGkC,GAAQ,KAAKC,GAAc;AAC7C;"}
package/package.json ADDED
@@ -0,0 +1,96 @@
1
+ {
2
+ "name": "@mateosuarezdev/react-ui",
3
+ "version": "1.0.0",
4
+ "description": "Dynamic React UI library designed to work alongside @mateosuarezdev/react-router",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist/**/*.js",
18
+ "dist/**/*.cjs",
19
+ "dist/**/*.d.ts",
20
+ "dist/**/*.map"
21
+ ],
22
+ "sideEffects": [
23
+ "./dist/init.js",
24
+ "./dist/init.cjs"
25
+ ],
26
+ "scripts": {
27
+ "build": "vite build",
28
+ "build:visualize": "vite-bundle-visualizer",
29
+ "typecheck": "tsc --noEmit",
30
+ "clean": "rimraf dist",
31
+ "prepublish": "yarn clean && yarn build",
32
+ "rel": "yarn npm publish --access public",
33
+ "rel:patch": "yarn version patch && yarn npm publish --access public",
34
+ "rel:minor": "yarn version minor && yarn npm publish --access public",
35
+ "rel:major": "yarn version major && yarn npm publish --access public"
36
+ },
37
+ "packageManager": "yarn@4.10.3",
38
+ "keywords": [
39
+ "react",
40
+ "router",
41
+ "react-router",
42
+ "navigation",
43
+ "routing",
44
+ "spa",
45
+ "url",
46
+ "history"
47
+ ],
48
+ "author": "Mateo Suarez <suarezlifestyleuy@gmail.com>",
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://github.com/mateosuarezdev/brpc-space.git",
52
+ "directory": "packages/react-ui"
53
+ },
54
+ "bugs": {
55
+ "url": "https://github.com/mateosuarezdev/brpc-space/issues"
56
+ },
57
+ "homepage": "https://github.com/mateosuarezdev/brpc-space/tree/main/packages/react-ui#readme",
58
+ "license": "MIT",
59
+ "peerDependencies": {
60
+ "framer-motion": "^12.0.0",
61
+ "motion": "^12.0.0",
62
+ "react": "^18.0.0",
63
+ "react-dom": "^18.0.0"
64
+ },
65
+ "peerDependenciesMeta": {
66
+ "framer-motion": {
67
+ "optional": true
68
+ },
69
+ "motion": {
70
+ "optional": true
71
+ },
72
+ "react": {
73
+ "optional": false
74
+ },
75
+ "react-dom": {
76
+ "optional": false
77
+ }
78
+ },
79
+ "dependencies": {
80
+ "@mateosuarezdev/url-change-event": "^1.0.12"
81
+ },
82
+ "devDependencies": {
83
+ "@types/node": "^24.7.0",
84
+ "@types/react": "^19.2.2",
85
+ "@types/react-dom": "^19.2.1",
86
+ "@vitejs/plugin-react": "^5.0.4",
87
+ "motion": "^12.23.22",
88
+ "react": "^19.2.0",
89
+ "react-dom": "^19.2.0",
90
+ "rimraf": "^6.0.1",
91
+ "typescript": "^5.9.3",
92
+ "vite": "^7.1.9",
93
+ "vite-bundle-visualizer": "^1.2.1",
94
+ "vite-plugin-dts": "^4.5.4"
95
+ }
96
+ }