@overlastic/vanilla 0.4.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/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-PRESENT Hairyf<https://github.com/hairyf>
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
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
package/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # @overlastic/vanilla
2
+
3
+ create imperative overlays in the native application, support custom-element!
4
+
5
+ ## Install
6
+
7
+ With pnpm:
8
+ ```sh
9
+ pnpm add @overlastic/vanilla
10
+ ```
11
+
12
+ With yarn:
13
+ ```sh
14
+ yarn add @overlastic/vanilla
15
+ ```
16
+
17
+ ## Usage
18
+
19
+
20
+ ### Step 1: Define Component
21
+
22
+ Create custom elements in the form of functions and return them.
23
+
24
+ ```js
25
+ // overlay.ts
26
+ function Component(props) {
27
+ const element = document.createElement('div')
28
+ element.innerHTML = props.title
29
+
30
+ const { resolve, reject, deferred } = usePrograms({
31
+ // Duration of overlays duration to avoid premature destruction of the component
32
+ duration: 1000,
33
+ })
34
+
35
+ // Add events that cause the overlays to end
36
+ element.onclick = function () {
37
+ resolve('ok')
38
+ }
39
+
40
+ // Use setTimeout to wait for the element to be appended, then add a class name with animation
41
+ setTimeout(() => element.classList.add('show'))
42
+ // When the deferred is triggered, remove the displayed class name
43
+ deferred.finally(() => element.classList.remove('show'))
44
+
45
+ return element
46
+ }
47
+
48
+ export default Component
49
+ ```
50
+
51
+ ### Step 2: Create Overlay
52
+
53
+ You can use the `defineOverlay` method to convert the component into a modal dialog in Javascript / Typescript, which allows you to call it.
54
+
55
+ ```ts
56
+ import { defineOverlay } from '@overlastic/vanilla'
57
+ import Component from './overlay'
58
+
59
+ // Convert to imperative callback
60
+ const callback = defineOverlay(Component)
61
+ // Call the component and get the value of the resolve callback
62
+ const value = await callback({ title: 'callbackOverlay' })
63
+ // value === "callbackOverlay:confirmed"
64
+ ```
65
+
66
+ You can also use `renderOverlay` to directly call the component and skip the `defineOverlay` method.
67
+
68
+ ```ts
69
+ import { defineOverlay } from '@overlastic/vanilla'
70
+ import Component from './overlay'
71
+
72
+ const value = await renderOverlay(Component, {
73
+ title: 'usePrograms'
74
+ })
75
+ // value === "usePrograms:confirmed"
76
+ ```
77
+
78
+ ## Custom Element
79
+
80
+ After mounting a custom element, you can pass in the corresponding custom element or name through 'defineOverlay' to use the custom element.
81
+
82
+ > You can use [lit](https://lit.dev/) Quickly build custom elements.
83
+
84
+ ```ts
85
+ const callback1 = defineOverlay('my-custom-element')
86
+
87
+ callback1({/* props(attrs) */})
88
+
89
+ const CustomComponent = (props) => {
90
+ const customElement = document.createElement('my-custom-element')
91
+
92
+ const { resolve, reject } = usePrograms({
93
+ duration: 1000,
94
+ })
95
+
96
+ // ...
97
+ return customElement
98
+ }
99
+
100
+ const callback2 = defineOverlay(CustomComponent)
101
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ defineOverlay: () => defineOverlay,
24
+ renderOverlay: () => renderOverlay,
25
+ usePrograms: () => usePrograms
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+
29
+ // src/define/constructor.ts
30
+ var import_core = require("@overlastic/core");
31
+
32
+ // src/internal/index.ts
33
+ var context = {
34
+ trigger: void 0,
35
+ options: void 0
36
+ };
37
+ function setupTrigger(c) {
38
+ context.trigger = c;
39
+ }
40
+ function clearTrigger() {
41
+ context.trigger = void 0;
42
+ }
43
+ function setupOptions(o = {}) {
44
+ context.options = o;
45
+ }
46
+ function clearOptions() {
47
+ context.options = void 0;
48
+ }
49
+ function useOptions() {
50
+ return context.options || {};
51
+ }
52
+
53
+ // src/define/constructor.ts
54
+ var constructor = (0, import_core.createConstructor)((Inst, props, options) => {
55
+ const { container, deferred } = options;
56
+ function vanish() {
57
+ container.remove();
58
+ }
59
+ setupTrigger({
60
+ resolve: deferred.resolve,
61
+ reject: deferred.reject,
62
+ deferred,
63
+ vanish
64
+ });
65
+ const inst = parseElement(Inst, props);
66
+ const { duration = 0 } = useOptions();
67
+ clearTrigger();
68
+ clearOptions();
69
+ deferred.finally(() => (0, import_core.delay)(duration).then(vanish));
70
+ container.append(inst);
71
+ });
72
+ function parseElement(component, props) {
73
+ if (typeof component === "function")
74
+ return component(props);
75
+ const element = typeof component === "string" ? document.createElement(component) : component;
76
+ for (const [key, value] of Object.entries(props))
77
+ element.setAttribute(key, value);
78
+ return element;
79
+ }
80
+
81
+ // src/define/index.ts
82
+ var defineOverlay = constructor.define;
83
+ var renderOverlay = constructor.render;
84
+
85
+ // src/composable/usePrograms.ts
86
+ function usePrograms(options) {
87
+ const trigger = context.trigger;
88
+ if (!trigger)
89
+ throw new Error("Please execute in the overlays constructor");
90
+ setupOptions(options);
91
+ return trigger;
92
+ }
93
+ // Annotate the CommonJS export names for ESM import in node:
94
+ 0 && (module.exports = {
95
+ defineOverlay,
96
+ renderOverlay,
97
+ usePrograms
98
+ });
@@ -0,0 +1,25 @@
1
+ import * as _overlastic_core from '@overlastic/core';
2
+ import { Deferred } from '@overlastic/core';
3
+
4
+ interface EFComponent<T = any> {
5
+ (props: T): HTMLElement;
6
+ }
7
+ type ElementComponent = HTMLElement | EFComponent | string;
8
+
9
+ declare const defineOverlay: <Props, Resolved = void>(instance: ElementComponent, options?: _overlastic_core.GlobalMountOptions | undefined) => _overlastic_core.ImperativeOverlay<Props, Resolved, {}>;
10
+ declare const renderOverlay: <Props, Resolved = void>(instance: ElementComponent, props?: Props | undefined, options?: _overlastic_core.GlobalMountOptions | undefined) => Promise<Resolved>;
11
+
12
+ interface Context<T = void> {
13
+ resolve: Deferred<T>['resolve'];
14
+ reject: Deferred<T>['reject'];
15
+ deferred: Deferred<T>;
16
+ vanish: Function;
17
+ }
18
+
19
+ interface ProgramsOptions {
20
+ /** animation duration to avoid premature destruction of components */
21
+ duration?: number;
22
+ }
23
+ declare function usePrograms(options?: ProgramsOptions): Context<void>;
24
+
25
+ export { type Context, type ProgramsOptions, defineOverlay, renderOverlay, usePrograms };
@@ -0,0 +1,25 @@
1
+ import * as _overlastic_core from '@overlastic/core';
2
+ import { Deferred } from '@overlastic/core';
3
+
4
+ interface EFComponent<T = any> {
5
+ (props: T): HTMLElement;
6
+ }
7
+ type ElementComponent = HTMLElement | EFComponent | string;
8
+
9
+ declare const defineOverlay: <Props, Resolved = void>(instance: ElementComponent, options?: _overlastic_core.GlobalMountOptions | undefined) => _overlastic_core.ImperativeOverlay<Props, Resolved, {}>;
10
+ declare const renderOverlay: <Props, Resolved = void>(instance: ElementComponent, props?: Props | undefined, options?: _overlastic_core.GlobalMountOptions | undefined) => Promise<Resolved>;
11
+
12
+ interface Context<T = void> {
13
+ resolve: Deferred<T>['resolve'];
14
+ reject: Deferred<T>['reject'];
15
+ deferred: Deferred<T>;
16
+ vanish: Function;
17
+ }
18
+
19
+ interface ProgramsOptions {
20
+ /** animation duration to avoid premature destruction of components */
21
+ duration?: number;
22
+ }
23
+ declare function usePrograms(options?: ProgramsOptions): Context<void>;
24
+
25
+ export { type Context, type ProgramsOptions, defineOverlay, renderOverlay, usePrograms };
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+ var OverlaysVanilla = (() => {
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var src_exports = {};
23
+ __export(src_exports, {
24
+ defineOverlay: () => defineOverlay,
25
+ renderOverlay: () => renderOverlay,
26
+ usePrograms: () => usePrograms
27
+ });
28
+
29
+ // ../@core/src/define/global.ts
30
+ var UnifiedOverlayProviderID = "unified-overlay";
31
+ function defineGlobalElement(id = "", root = document.body) {
32
+ const el = document.createElement("div");
33
+ if (id)
34
+ el.id = id;
35
+ if (root !== false)
36
+ root.appendChild(el);
37
+ return el;
38
+ }
39
+
40
+ // ../@core/src/internal/index.ts
41
+ var context = {
42
+ position: null,
43
+ spaces: {}
44
+ };
45
+
46
+ // ../@core/src/define/name-index.ts
47
+ function defineName(id = UnifiedOverlayProviderID, auto = true) {
48
+ if (!context.spaces[id])
49
+ context.spaces[id] = 0;
50
+ if (auto)
51
+ return `${id}--${++context.spaces[id]}`;
52
+ return id;
53
+ }
54
+ function getIndex(id = UnifiedOverlayProviderID) {
55
+ return context.spaces[id] || 0;
56
+ }
57
+
58
+ // ../@core/src/deferred.ts
59
+ function createDeferred() {
60
+ let resolve, reject;
61
+ const promise = new Promise((_resolve, _reject) => {
62
+ resolve = _resolve;
63
+ reject = _reject;
64
+ });
65
+ promise.resolve = (v) => {
66
+ resolve(v);
67
+ return promise;
68
+ };
69
+ promise.reject = reject;
70
+ return promise;
71
+ }
72
+
73
+ // ../@core/src/events/index.ts
74
+ function watchClickPosition() {
75
+ if (typeof window === "undefined" && typeof document === "undefined")
76
+ return;
77
+ document.addEventListener("click", (event) => {
78
+ if (event.target instanceof Element) {
79
+ const { left, top, width, height } = event.target.getBoundingClientRect();
80
+ if (left > 0 || top > 0) {
81
+ context.position = { x: left + width / 2, y: top + height / 2 };
82
+ } else {
83
+ context.position = null;
84
+ }
85
+ } else {
86
+ context.position = null;
87
+ }
88
+ setTimeout(() => context.position = null, 64);
89
+ });
90
+ }
91
+
92
+ // ../@core/src/constructor.ts
93
+ function createConstructor(mount) {
94
+ function define(instance, options) {
95
+ function executor(props, options2) {
96
+ const deferred = createDeferred();
97
+ const name = defineName(options2.id, options2.autoIncrement);
98
+ const index = getIndex(options2.id);
99
+ const container = defineGlobalElement(name, options2.root);
100
+ mount(instance, props, Object.assign(options2, {
101
+ position: context.position,
102
+ id: name,
103
+ deferred,
104
+ index,
105
+ container
106
+ }));
107
+ return deferred;
108
+ }
109
+ let inst;
110
+ function only(props, options2) {
111
+ if (!inst) {
112
+ inst = executor(props, options2);
113
+ inst.finally(() => inst = void 0);
114
+ }
115
+ return inst;
116
+ }
117
+ function caller(props, overrides) {
118
+ const opts = { ...options, ...overrides };
119
+ return opts.only ? only(props, opts) : executor(props, opts);
120
+ }
121
+ return caller;
122
+ }
123
+ function render(instance, props, options) {
124
+ return define(instance, options)(props);
125
+ }
126
+ return { define, render };
127
+ }
128
+ watchClickPosition();
129
+
130
+ // ../@core/src/utils/util.ts
131
+ function delay(milliseconds) {
132
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
133
+ }
134
+
135
+ // src/internal/index.ts
136
+ var context2 = {
137
+ trigger: void 0,
138
+ options: void 0
139
+ };
140
+ function setupTrigger(c) {
141
+ context2.trigger = c;
142
+ }
143
+ function clearTrigger() {
144
+ context2.trigger = void 0;
145
+ }
146
+ function setupOptions(o = {}) {
147
+ context2.options = o;
148
+ }
149
+ function clearOptions() {
150
+ context2.options = void 0;
151
+ }
152
+ function useOptions() {
153
+ return context2.options || {};
154
+ }
155
+
156
+ // src/define/constructor.ts
157
+ var constructor = createConstructor((Inst, props, options) => {
158
+ const { container, deferred } = options;
159
+ function vanish() {
160
+ container.remove();
161
+ }
162
+ setupTrigger({
163
+ resolve: deferred.resolve,
164
+ reject: deferred.reject,
165
+ deferred,
166
+ vanish
167
+ });
168
+ const inst = parseElement(Inst, props);
169
+ const { duration = 0 } = useOptions();
170
+ clearTrigger();
171
+ clearOptions();
172
+ deferred.finally(() => delay(duration).then(vanish));
173
+ container.append(inst);
174
+ });
175
+ function parseElement(component, props) {
176
+ if (typeof component === "function")
177
+ return component(props);
178
+ const element = typeof component === "string" ? document.createElement(component) : component;
179
+ for (const [key, value] of Object.entries(props))
180
+ element.setAttribute(key, value);
181
+ return element;
182
+ }
183
+
184
+ // src/define/index.ts
185
+ var defineOverlay = constructor.define;
186
+ var renderOverlay = constructor.render;
187
+
188
+ // src/composable/usePrograms.ts
189
+ function usePrograms(options) {
190
+ const trigger = context2.trigger;
191
+ if (!trigger)
192
+ throw new Error("Please execute in the overlays constructor");
193
+ setupOptions(options);
194
+ return trigger;
195
+ }
196
+ return __toCommonJS(src_exports);
197
+ })();
package/dist/index.js ADDED
@@ -0,0 +1,69 @@
1
+ // src/define/constructor.ts
2
+ import { createConstructor, delay } from "@overlastic/core";
3
+
4
+ // src/internal/index.ts
5
+ var context = {
6
+ trigger: void 0,
7
+ options: void 0
8
+ };
9
+ function setupTrigger(c) {
10
+ context.trigger = c;
11
+ }
12
+ function clearTrigger() {
13
+ context.trigger = void 0;
14
+ }
15
+ function setupOptions(o = {}) {
16
+ context.options = o;
17
+ }
18
+ function clearOptions() {
19
+ context.options = void 0;
20
+ }
21
+ function useOptions() {
22
+ return context.options || {};
23
+ }
24
+
25
+ // src/define/constructor.ts
26
+ var constructor = createConstructor((Inst, props, options) => {
27
+ const { container, deferred } = options;
28
+ function vanish() {
29
+ container.remove();
30
+ }
31
+ setupTrigger({
32
+ resolve: deferred.resolve,
33
+ reject: deferred.reject,
34
+ deferred,
35
+ vanish
36
+ });
37
+ const inst = parseElement(Inst, props);
38
+ const { duration = 0 } = useOptions();
39
+ clearTrigger();
40
+ clearOptions();
41
+ deferred.finally(() => delay(duration).then(vanish));
42
+ container.append(inst);
43
+ });
44
+ function parseElement(component, props) {
45
+ if (typeof component === "function")
46
+ return component(props);
47
+ const element = typeof component === "string" ? document.createElement(component) : component;
48
+ for (const [key, value] of Object.entries(props))
49
+ element.setAttribute(key, value);
50
+ return element;
51
+ }
52
+
53
+ // src/define/index.ts
54
+ var defineOverlay = constructor.define;
55
+ var renderOverlay = constructor.render;
56
+
57
+ // src/composable/usePrograms.ts
58
+ function usePrograms(options) {
59
+ const trigger = context.trigger;
60
+ if (!trigger)
61
+ throw new Error("Please execute in the overlays constructor");
62
+ setupOptions(options);
63
+ return trigger;
64
+ }
65
+ export {
66
+ defineOverlay,
67
+ renderOverlay,
68
+ usePrograms
69
+ };
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@overlastic/vanilla",
3
+ "type": "module",
4
+ "version": "0.4.3",
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
+ "svelte"
18
+ ],
19
+ "main": "./dist/index.cjs",
20
+ "publishConfig": {
21
+ "jsdelivr": "./dist/index.global.js",
22
+ "linkDirectory": false
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "dependencies": {
28
+ "@overlastic/core": "^0.4.3"
29
+ },
30
+ "scripts": {
31
+ "build": "tsup src/index.ts",
32
+ "lint": "eslint ."
33
+ },
34
+ "exports": {
35
+ ".": {
36
+ "import": "./dist/index.js",
37
+ "require": "./dist/index.cjs"
38
+ }
39
+ },
40
+ "types": "./dist/index.d.ts"
41
+ }