@overlastic/react 0.4.8

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/index.js ADDED
@@ -0,0 +1,212 @@
1
+ // src/composable/usePrograms.ts
2
+ import { delay, noop as noop2 } from "@overlastic/core";
3
+ import { useContext, useEffect } from "react";
4
+
5
+ // src/internal/contexts.tsx
6
+ import { noop } from "@overlastic/core";
7
+ import { createContext } from "react";
8
+ var ScriptsContext = createContext({
9
+ reject: noop,
10
+ resolve: noop,
11
+ setVisible: noop,
12
+ vanish: noop,
13
+ visible: false,
14
+ in_dec: true
15
+ });
16
+ var InstancesContext = createContext({});
17
+ InstancesContext.displayName = "OverlayInstancesContext";
18
+ ScriptsContext.displayName = "OverlayScriptsContext";
19
+
20
+ // src/internal/utils.ts
21
+ function defineAnonymousComponent(component) {
22
+ return { "--": component }["--"];
23
+ }
24
+
25
+ // src/composable/usePrograms.ts
26
+ function usePrograms(options = {}) {
27
+ const { immediate = true, duration = 0, automatic = true } = options;
28
+ const context = useContext(ScriptsContext);
29
+ const dec = Reflect.get(context, "in_dec");
30
+ const overlay = dec ? useDeclarative(options) : context;
31
+ const { setVisible, vanish, deferred } = overlay;
32
+ async function destroy() {
33
+ setVisible(false);
34
+ await delay(duration);
35
+ vanish == null ? void 0 : vanish();
36
+ return Promise.resolve();
37
+ }
38
+ useMount(() => immediate && setVisible(true));
39
+ useMount(() => {
40
+ if (!dec && automatic)
41
+ deferred == null ? void 0 : deferred.then(destroy).catch(destroy);
42
+ });
43
+ return overlay;
44
+ }
45
+ function useDeclarative(options = {}) {
46
+ const { props = {}, model = "visible", events = {} } = options;
47
+ const { reject = "onReject", resolve = "onResolve" } = events;
48
+ const _reject = (value) => {
49
+ var _a;
50
+ (_a = props[reject]) == null ? void 0 : _a.call(props, value);
51
+ };
52
+ const _resolve = (value) => {
53
+ var _a;
54
+ (_a = props[resolve]) == null ? void 0 : _a.call(props, value);
55
+ };
56
+ return {
57
+ reject: _reject,
58
+ resolve: _resolve,
59
+ visible: props[model],
60
+ vanish: noop2,
61
+ setVisible: noop2,
62
+ deferred: void 0
63
+ };
64
+ }
65
+ function useMount(callback = noop2) {
66
+ useEffect(() => callback(), []);
67
+ }
68
+
69
+ // src/composable/useScripts.ts
70
+ import { useState } from "react";
71
+ function useScripts(options) {
72
+ const { reject: _reject } = options.deferred || {};
73
+ const { vanish: _vanish } = options;
74
+ const [visible, setVisible] = useState(false);
75
+ function vanish() {
76
+ _vanish == null ? void 0 : _vanish();
77
+ _reject == null ? void 0 : _reject();
78
+ }
79
+ return {
80
+ resolve: options.deferred.resolve,
81
+ reject: options.deferred.reject,
82
+ deferred: options.deferred,
83
+ setVisible,
84
+ visible,
85
+ vanish
86
+ };
87
+ }
88
+
89
+ // src/composable/useOverlay.tsx
90
+ import { createConstructor } from "@overlastic/core";
91
+ import { useContext as useContext2 } from "react";
92
+ import { pascalCase } from "pascal-case";
93
+ import { createPortal } from "react-dom";
94
+ import { jsx } from "react/jsx-runtime";
95
+ var { define } = createConstructor((Instance, props, options) => {
96
+ const { container, id, deferred, render, vanish: _vanish } = options;
97
+ const InstanceWithProvider = defineAnonymousComponent(() => {
98
+ const content = /* @__PURE__ */ jsx(
99
+ ScriptsContext.Provider,
100
+ {
101
+ value: useScripts({ deferred, vanish }),
102
+ ...{ id: pascalCase(id) },
103
+ children: /* @__PURE__ */ jsx(Instance, { ...props })
104
+ }
105
+ );
106
+ return createPortal(content, container);
107
+ });
108
+ function vanish() {
109
+ _vanish(InstanceWithProvider);
110
+ container.remove();
111
+ }
112
+ render(InstanceWithProvider, props);
113
+ });
114
+ function useOverlay(Instance) {
115
+ const { render, vanish } = useContext2(InstancesContext);
116
+ return define(Instance, { render, vanish });
117
+ }
118
+
119
+ // src/composable/useOverlayHolder.tsx
120
+ import { createDeferred, defineName } from "@overlastic/core";
121
+ import { pascalCase as pascalCase2 } from "pascal-case";
122
+ import { useRef, useState as useState2 } from "react";
123
+ import { createPortal as createPortal2 } from "react-dom";
124
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
125
+ function useOverlayHolder(Component, options = {}) {
126
+ const { callback, scripts, props, refresh } = useRefreshMetadata();
127
+ const name = defineName(options.id, options.autoIncrement);
128
+ function render() {
129
+ const root = options.root || (typeof document !== "undefined" ? document.body : void 0);
130
+ const content = /* @__PURE__ */ jsxs("div", { id: name, children: [
131
+ " ",
132
+ /* @__PURE__ */ jsx2(Component, { ...props }),
133
+ " "
134
+ ] });
135
+ return options.root !== false && root ? createPortal2(content, root) : content;
136
+ }
137
+ const holder = /* @__PURE__ */ jsx2(
138
+ ScriptsContext.Provider,
139
+ {
140
+ value: scripts,
141
+ ...{ id: pascalCase2(name) },
142
+ children: refresh ? render() : null
143
+ }
144
+ );
145
+ return [holder, callback];
146
+ }
147
+ function useRefreshMetadata() {
148
+ const [props, setProps] = useState2();
149
+ const [refresh, setRefresh] = useState2(false);
150
+ const [visible, setVisible] = useState2(false);
151
+ const deferred = useRef({});
152
+ const scripts = {
153
+ deferred: deferred.current,
154
+ resolve: deferred.current.resolve,
155
+ reject: deferred.current.reject,
156
+ vanish,
157
+ visible,
158
+ setVisible
159
+ };
160
+ function vanish() {
161
+ var _a;
162
+ setRefresh(false);
163
+ setProps({});
164
+ (_a = scripts.reject) == null ? void 0 : _a.call(scripts);
165
+ }
166
+ async function callback(props2) {
167
+ deferred.current = createDeferred();
168
+ setProps(props2);
169
+ setRefresh(true);
170
+ return deferred.current;
171
+ }
172
+ return { callback, scripts, props, refresh };
173
+ }
174
+
175
+ // src/define/constructor.tsx
176
+ import { createConstructor as createConstructor2 } from "@overlastic/core";
177
+ import { createRoot } from "react-dom/client";
178
+ import { pascalCase as pascalCase3 } from "pascal-case";
179
+ import { jsx as jsx3 } from "react/jsx-runtime";
180
+ var constructor = createConstructor2((Instance, props, options) => {
181
+ const { container, id, deferred } = options;
182
+ const root = createRoot(container);
183
+ const Provider = defineAnonymousComponent(() => {
184
+ return /* @__PURE__ */ jsx3(
185
+ ScriptsContext.Provider,
186
+ {
187
+ value: useScripts({ deferred, vanish }),
188
+ ...{ id: pascalCase3(id) },
189
+ children: /* @__PURE__ */ jsx3(Instance, { ...props })
190
+ }
191
+ );
192
+ });
193
+ function vanish() {
194
+ const handle = requestAnimationFrame(() => {
195
+ root.unmount();
196
+ container.remove();
197
+ cancelAnimationFrame(handle);
198
+ });
199
+ }
200
+ root.render(/* @__PURE__ */ jsx3(Provider, {}));
201
+ });
202
+
203
+ // src/define/index.ts
204
+ var defineOverlay = constructor.define;
205
+ var renderOverlay = constructor.render;
206
+ export {
207
+ defineOverlay,
208
+ renderOverlay,
209
+ useOverlay,
210
+ useOverlayHolder,
211
+ usePrograms
212
+ };
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@overlastic/react",
3
+ "type": "module",
4
+ "version": "0.4.8",
5
+ "license": "MIT",
6
+ "homepage": "https://github.com/hairyf/overlastic#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/hairyf/overlastic.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/hairyf/overlastic/issues"
13
+ },
14
+ "keywords": [
15
+ "unified",
16
+ "overlay",
17
+ "react"
18
+ ],
19
+ "main": "./dist/index.cjs",
20
+ "publishConfig": {
21
+ "jsdelivr": "./dist/index.global.js",
22
+ "linkDirectory": false
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "peerDependencies": {
28
+ "react": ">=18",
29
+ "react-dom": ">=18"
30
+ },
31
+ "dependencies": {
32
+ "pascal-case": "3.1.2",
33
+ "react": "^18.3.1",
34
+ "react-dom": "^18.3.1",
35
+ "@overlastic/core": "^0.4.3"
36
+ },
37
+ "devDependencies": {
38
+ "@types/react": "^18.3.2",
39
+ "@types/react-dom": "^18.3.0",
40
+ "react": "^18.3.1",
41
+ "react-dom": "^18.3.1"
42
+ },
43
+ "scripts": {
44
+ "build": "tsup src/index.ts",
45
+ "lint": "eslint ."
46
+ },
47
+ "exports": {
48
+ ".": {
49
+ "import": "./dist/index.js",
50
+ "require": "./dist/index.cjs"
51
+ }
52
+ },
53
+ "types": "./dist/index.d.ts"
54
+ }