@overlastic/react 0.8.0 → 0.8.3

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 CHANGED
@@ -1,17 +1,16 @@
1
- // src/composable/define.ts
2
- import { delay, noop as noop2 } from "@overlastic/core";
3
- import { useContext, useEffect } from "react";
1
+ // src/components/Provider.tsx
2
+ import { useState } from "react";
4
3
 
5
4
  // src/internal/contexts.tsx
6
5
  import { noop } from "@overlastic/core";
7
6
  import { createContext } from "react";
8
7
  var ScriptsContext = createContext({
9
- reject: noop,
10
- resolve: noop,
11
- setVisible: noop,
8
+ cancel: noop,
9
+ confirm: noop,
10
+ change: noop,
12
11
  vanish: noop,
13
12
  visible: false,
14
- in_dec: true
13
+ __is_declarative: true
15
14
  });
16
15
  var InstancesContext = createContext({});
17
16
  InstancesContext.displayName = "OverlayInstancesContext";
@@ -22,43 +21,67 @@ function defineAnonymousComponent(component) {
22
21
  return { "--": component }["--"];
23
22
  }
24
23
 
24
+ // src/components/Provider.tsx
25
+ import { jsx, jsxs } from "react/jsx-runtime";
26
+ function OverlaysProvider(props) {
27
+ const [instances, setInstances] = useState([]);
28
+ function render(Instance, props2) {
29
+ setInstances((instances2) => [...instances2, { Instance, props: props2 }]);
30
+ }
31
+ function vanish(instance) {
32
+ setInstances((instances2) => [...instances2.filter(({ Instance }) => instance !== Instance)]);
33
+ }
34
+ return /* @__PURE__ */ jsxs(InstancesContext.Provider, { value: { render, vanish }, children: [
35
+ instances.map(({ Instance, props: props2 }, index) => /* @__PURE__ */ jsx(Instance, { ...props2 }, index)),
36
+ props.children
37
+ ] });
38
+ }
39
+
25
40
  // src/composable/define.ts
26
- function useExtendOverlay(options = {}) {
27
- const { immediate = true, duration = 0, automatic = true } = options;
41
+ import { delay, noop as noop2 } from "@overlastic/core";
42
+ import { useContext, useEffect } from "react";
43
+ function useDisclosure(options = {}) {
44
+ const { immediate = true, duration = 300, automatic = true } = options;
28
45
  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;
46
+ const isDeclarative = Reflect.get(context, "__is_declarative");
47
+ const overlay = isDeclarative ? useDeclarative(options) : context;
32
48
  async function destroy() {
33
- setVisible(false);
49
+ var _a;
50
+ overlay.change(false);
34
51
  await delay(duration);
35
- vanish == null ? void 0 : vanish();
52
+ (_a = overlay.vanish) == null ? void 0 : _a.call(overlay);
36
53
  return Promise.resolve();
37
54
  }
38
- useMount(() => immediate && setVisible(true));
55
+ useMount(() => immediate && overlay.change(true));
39
56
  useMount(() => {
40
- if (!dec && automatic)
41
- deferred == null ? void 0 : deferred.then(destroy).catch(destroy);
57
+ var _a;
58
+ if (!isDeclarative && automatic)
59
+ (_a = overlay.deferred) == null ? void 0 : _a.then(destroy).catch(destroy);
42
60
  });
43
61
  return overlay;
44
62
  }
45
63
  function useDeclarative(options = {}) {
46
64
  const { props = {}, model = "visible", events = {} } = options;
47
- const { reject = "onReject", resolve = "onResolve" } = events;
48
- const _reject = (value) => {
65
+ const { cancel = "onCancel", confirm = "onConfirm", close = "onClose" } = events;
66
+ const _cancel = (value) => {
67
+ var _a;
68
+ return (_a = props[cancel]) == null ? void 0 : _a.call(props, value);
69
+ };
70
+ const _confirm = (value) => {
49
71
  var _a;
50
- (_a = props[reject]) == null ? void 0 : _a.call(props, value);
72
+ return (_a = props[confirm]) == null ? void 0 : _a.call(props, value);
51
73
  };
52
- const _resolve = (value) => {
74
+ const _close = (value) => {
53
75
  var _a;
54
- (_a = props[resolve]) == null ? void 0 : _a.call(props, value);
76
+ return (_a = props[close]) == null ? void 0 : _a.call(props, value);
55
77
  };
56
78
  return {
57
- reject: _reject,
58
- resolve: _resolve,
79
+ cancel: _cancel,
80
+ confirm: _confirm,
81
+ close: _close,
59
82
  visible: props[model],
60
83
  vanish: noop2,
61
- setVisible: noop2,
84
+ change: noop2,
62
85
  deferred: void 0
63
86
  };
64
87
  }
@@ -66,73 +89,46 @@ function useMount(callback = noop2) {
66
89
  useEffect(() => callback(), []);
67
90
  }
68
91
 
69
- // src/composable/scripts.ts
70
- import { useState } from "react";
71
- function useScripts(options) {
72
- const { reject: _reject } = options.deferred || {};
92
+ // src/composable/utils/index.ts
93
+ import { createDeferred } from "@overlastic/core";
94
+ import { useRef, useState as useState2 } from "react";
95
+ function createScripts(options) {
96
+ const { cancel: _cancel } = options.deferred || {};
73
97
  const { vanish: _vanish } = options;
74
- const [visible, setVisible] = useState(false);
98
+ const [visible, change] = useState2(false);
75
99
  function vanish() {
76
100
  _vanish == null ? void 0 : _vanish();
77
- _reject == null ? void 0 : _reject();
101
+ _cancel == null ? void 0 : _cancel();
78
102
  }
79
103
  return {
80
- resolve: options.deferred.resolve,
81
- reject: options.deferred.reject,
104
+ confirm: options.deferred.confirm,
105
+ cancel: options.deferred.cancel,
106
+ close: () => options.deferred.confirm(),
82
107
  deferred: options.deferred,
83
- setVisible,
108
+ change,
84
109
  visible,
85
110
  vanish
86
111
  };
87
112
  }
88
-
89
- // src/composable/holder.tsx
90
- import { createDeferred, defineName } from "@overlastic/core";
91
- import { pascalCase } from "pascal-case";
92
- import { useRef, useState as useState2 } from "react";
93
- import { createPortal } from "react-dom";
94
- import { jsx, jsxs } from "react/jsx-runtime";
95
- function useOverlayHolder(component, options = {}) {
96
- const { callback, scripts, props, refresh } = useRefreshMetadata();
97
- const name = defineName(options.id, options.autoIncrement);
98
- function render() {
99
- const root = options.root || (typeof document !== "undefined" ? document.body : void 0);
100
- const Comp = component;
101
- const content = /* @__PURE__ */ jsxs("div", { id: name, children: [
102
- " ",
103
- /* @__PURE__ */ jsx(Comp, { ...props }),
104
- " "
105
- ] });
106
- return options.root !== false && root ? createPortal(content, root) : content;
107
- }
108
- const holder = /* @__PURE__ */ jsx(
109
- ScriptsContext.Provider,
110
- {
111
- value: scripts,
112
- ...{ id: pascalCase(name) },
113
- children: refresh ? render() : null
114
- }
115
- );
116
- return [holder, callback];
117
- }
118
- function useRefreshMetadata() {
113
+ function createRefreshMetadata() {
119
114
  const [props, setProps] = useState2();
120
115
  const [refresh, setRefresh] = useState2(false);
121
- const [visible, setVisible] = useState2(false);
116
+ const [visible, change] = useState2(false);
122
117
  const deferred = useRef({});
123
118
  const scripts = {
124
119
  deferred: deferred.current,
125
- resolve: deferred.current.resolve,
126
- reject: deferred.current.reject,
120
+ confirm: deferred.current.confirm,
121
+ cancel: deferred.current.cancel,
122
+ close: () => deferred.current.confirm(),
127
123
  vanish,
128
124
  visible,
129
- setVisible
125
+ change
130
126
  };
131
127
  function vanish() {
132
128
  var _a;
133
129
  setRefresh(false);
134
130
  setProps({});
135
- (_a = scripts.reject) == null ? void 0 : _a.call(scripts);
131
+ (_a = scripts.cancel) == null ? void 0 : _a.call(scripts);
136
132
  }
137
133
  async function callback(props2) {
138
134
  deferred.current = createDeferred();
@@ -143,38 +139,64 @@ function useRefreshMetadata() {
143
139
  return { callback, scripts, props, refresh };
144
140
  }
145
141
 
146
- // src/composable/inject.tsx
147
- import { createConstructor } from "@overlastic/core";
142
+ // src/composable/overlay.tsx
143
+ import { createConstructor, defineName } from "@overlastic/core";
144
+ import { pascalCase } from "pascal-case";
148
145
  import { useContext as useContext2 } from "react";
149
- import { pascalCase as pascalCase2 } from "pascal-case";
150
- import { jsx as jsx2 } from "react/jsx-runtime";
151
- var { define } = createConstructor((Instance, props, options) => {
152
- const { id, deferred, render, vanish: _vanish } = options;
153
- const InstanceWithProvider = defineAnonymousComponent(() => {
154
- const content = /* @__PURE__ */ jsx2(
155
- ScriptsContext.Provider,
156
- {
157
- value: useScripts({ deferred, vanish }),
158
- ...{ id: pascalCase2(id) },
159
- children: /* @__PURE__ */ jsx2(Instance, { ...props })
160
- }
161
- );
162
- return content;
163
- });
164
- function vanish() {
165
- _vanish(InstanceWithProvider);
146
+ import { createPortal } from "react-dom";
147
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
148
+ var { define: defineInject } = createConstructor(
149
+ (Instance, props, options) => {
150
+ const { id, deferred, render, vanish: _vanish } = options;
151
+ const InstanceWithProvider = defineAnonymousComponent(() => {
152
+ const content = /* @__PURE__ */ jsx2(
153
+ ScriptsContext.Provider,
154
+ {
155
+ value: createScripts({ deferred, vanish }),
156
+ ...{ id: pascalCase(id) },
157
+ children: /* @__PURE__ */ jsx2(Instance, { ...props })
158
+ }
159
+ );
160
+ return content;
161
+ });
162
+ function vanish() {
163
+ _vanish(InstanceWithProvider);
164
+ }
165
+ render(InstanceWithProvider, props);
166
+ },
167
+ { container: false }
168
+ );
169
+ function defineHolder(component, options = {}) {
170
+ const { callback, scripts, props, refresh } = createRefreshMetadata();
171
+ const name = defineName(options.id, options.autoIncrement);
172
+ function render() {
173
+ const root = options.root || (typeof document !== "undefined" ? document.body : void 0);
174
+ const Comp = component;
175
+ const content = /* @__PURE__ */ jsxs2("div", { id: name, children: [
176
+ " ",
177
+ /* @__PURE__ */ jsx2(Comp, { ...props }),
178
+ " "
179
+ ] });
180
+ return options.root !== false && root ? createPortal(content, root) : content;
181
+ }
182
+ const holder = /* @__PURE__ */ jsx2(ScriptsContext.Provider, { value: scripts, ...{ id: pascalCase(name) }, children: refresh ? render() : null });
183
+ return [holder, callback];
184
+ }
185
+ function useOverlay(Instance, options = {}) {
186
+ const { type = "inject" } = options != null ? options : {};
187
+ if (type === "inject") {
188
+ const { render, vanish } = useContext2(InstancesContext);
189
+ return defineInject(Instance, { render, vanish });
190
+ }
191
+ if (type === "holder") {
192
+ return defineHolder(Instance, options);
166
193
  }
167
- render(InstanceWithProvider, props);
168
- }, { container: false });
169
- function useOverlayInject(Instance) {
170
- const { render, vanish } = useContext2(InstancesContext);
171
- return define(Instance, { render, vanish });
172
194
  }
173
195
 
174
196
  // src/define/constructor.tsx
175
197
  import { createConstructor as createConstructor2 } from "@overlastic/core";
198
+ import { pascalCase as pascalCase2 } from "pascal-case";
176
199
  import { createRoot } from "react-dom/client";
177
- import { pascalCase as pascalCase3 } from "pascal-case";
178
200
  import { jsx as jsx3 } from "react/jsx-runtime";
179
201
  var constructor = createConstructor2((Instance, props, options) => {
180
202
  const { container, id, deferred } = options;
@@ -183,8 +205,8 @@ var constructor = createConstructor2((Instance, props, options) => {
183
205
  return /* @__PURE__ */ jsx3(
184
206
  ScriptsContext.Provider,
185
207
  {
186
- value: useScripts({ deferred, vanish }),
187
- ...{ id: pascalCase3(id) },
208
+ value: createScripts({ deferred, vanish }),
209
+ ...{ id: pascalCase2(id) },
188
210
  children: /* @__PURE__ */ jsx3(Instance, { ...props })
189
211
  }
190
212
  );
@@ -202,28 +224,10 @@ var constructor = createConstructor2((Instance, props, options) => {
202
224
  // src/define/index.ts
203
225
  var defineOverlay = constructor.define;
204
226
  var renderOverlay = constructor.render;
205
-
206
- // src/components/Provider.tsx
207
- import { useState as useState3 } from "react";
208
- import { jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
209
- function OverlaysProvider(props) {
210
- const [instances, setInstances] = useState3([]);
211
- function render(Instance, props2) {
212
- setInstances((instances2) => [...instances2, { Instance, props: props2 }]);
213
- }
214
- function vanish(instance) {
215
- setInstances((instances2) => [...instances2.filter(({ Instance }) => instance !== Instance)]);
216
- }
217
- return /* @__PURE__ */ jsxs2(InstancesContext.Provider, { value: { render, vanish }, children: [
218
- instances.map(({ Instance, props: props2 }, index) => /* @__PURE__ */ jsx4(Instance, { ...props2 }, index)),
219
- props.children
220
- ] });
221
- }
222
227
  export {
223
228
  OverlaysProvider,
224
229
  defineOverlay,
225
230
  renderOverlay,
226
- useExtendOverlay,
227
- useOverlayHolder,
228
- useOverlayInject
231
+ useDisclosure,
232
+ useOverlay
229
233
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@overlastic/react",
3
3
  "type": "module",
4
- "version": "0.8.0",
4
+ "version": "0.8.3",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/hairyf/overlastic#readme",
7
7
  "repository": {
@@ -32,11 +32,11 @@
32
32
  "pascal-case": "3.1.2",
33
33
  "react": "^18.3.1",
34
34
  "react-dom": "^18.3.1",
35
- "@overlastic/core": "^0.8.0"
35
+ "@overlastic/core": "^0.8.3"
36
36
  },
37
37
  "devDependencies": {
38
- "@types/react": "^18.3.2",
39
- "@types/react-dom": "^18.3.0",
38
+ "@types/react": "^18.3.20",
39
+ "@types/react-dom": "^18.3.6",
40
40
  "react": "^18.3.1",
41
41
  "react-dom": "^18.3.1"
42
42
  },