@overlastic/svelte 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 +23 -0
- package/README.md +102 -0
- package/dist/index.cjs +230 -0
- package/dist/index.d.cts +40 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.global.js +766 -0
- package/dist/index.js +216 -0
- package/package.json +42 -0
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,102 @@
|
|
|
1
|
+
# Getting Started
|
|
2
|
+
|
|
3
|
+
create imperative overlays in the svelte application, supporting context inheritance!
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
With pnpm:
|
|
8
|
+
```sh
|
|
9
|
+
pnpm add @overlastic/svelte
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
With yarn:
|
|
13
|
+
```sh
|
|
14
|
+
yarn add @overlastic/svelte
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Step 1: Define Component
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
```svelte
|
|
24
|
+
<script lang="ts">
|
|
25
|
+
import { usePrograms, Overlay } from "@overlastic/svelte";
|
|
26
|
+
import { fly } from "svelte/transition";
|
|
27
|
+
|
|
28
|
+
export let title: number
|
|
29
|
+
export let duration = 200
|
|
30
|
+
|
|
31
|
+
// duration of overlay duration, helps prevent premature component destroy
|
|
32
|
+
const { resolve, reject } = usePrograms({ duration })
|
|
33
|
+
|
|
34
|
+
function onClick() {
|
|
35
|
+
resolve(`${title}:confirmed`)
|
|
36
|
+
}
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
<Overlay>
|
|
40
|
+
<div transition:fly={{ opacity: 0, duration }} on:click={onClick}>
|
|
41
|
+
<slot name="title">
|
|
42
|
+
{ title }
|
|
43
|
+
</slot>
|
|
44
|
+
</div>
|
|
45
|
+
</Overlay>
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Step 2: Create Overlay
|
|
49
|
+
|
|
50
|
+
You can use the `defineOverlay` method to convert the component into a modal dialog in Javascript / Typescript, which allows you to call it.
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { defineOverlay } from '@overlastic/svelte'
|
|
54
|
+
import OverlayComponent from './overlay.svelte'
|
|
55
|
+
|
|
56
|
+
// Convert to imperative callback
|
|
57
|
+
const callback = defineOverlay(OverlayComponent)
|
|
58
|
+
// Call the component and get the value of the resolve callback
|
|
59
|
+
const value = await callback({ title: 'callbackOverlay' })
|
|
60
|
+
// value === "callbackOverlay:confirmed"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
You can also use `renderOverlay` to directly call the component and skip the `defineOverlay` method.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { renderOverlay } from '@overlastic/svelte'
|
|
67
|
+
import OverlayComponent from './overlay.svelte'
|
|
68
|
+
|
|
69
|
+
const value = await renderOverlay(OverlayComponent, {
|
|
70
|
+
title: 'usePrograms'
|
|
71
|
+
})
|
|
72
|
+
// value === "usePrograms:confirmed"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Controlled manner
|
|
76
|
+
|
|
77
|
+
By default, you do not need to control the display and hiding of the `visible` variable. The value is controlled by the component `Overlay`, and you can pass in `visible` to control the display
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
```svelte
|
|
81
|
+
<script lang="ts">
|
|
82
|
+
import { usePrograms, Overlay } from "@overlastic/svelte";
|
|
83
|
+
|
|
84
|
+
let visible = false
|
|
85
|
+
|
|
86
|
+
const { resolve, reject, deferred, vanish } = usePrograms({
|
|
87
|
+
// close the transition duration, at this point you need to manually destroy it
|
|
88
|
+
duration: false,
|
|
89
|
+
// cancel setting visible to true immediately
|
|
90
|
+
immediate: false
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
// Manually set vanish (when promise ends)
|
|
94
|
+
deferred.finally(() => vanish())
|
|
95
|
+
</script>
|
|
96
|
+
|
|
97
|
+
<Overlay bind:visible={visible}>
|
|
98
|
+
<div on:click={() => resolve(`${title}:confirmed`)}>
|
|
99
|
+
...
|
|
100
|
+
</div>
|
|
101
|
+
</Overlay>
|
|
102
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
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
|
+
Overlay: () => Overlay,
|
|
24
|
+
defineOverlay: () => defineOverlay,
|
|
25
|
+
renderOverlay: () => renderOverlay,
|
|
26
|
+
usePrograms: () => usePrograms
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
|
|
30
|
+
// src/define/constructor.ts
|
|
31
|
+
var import_core = require("@overlastic/core");
|
|
32
|
+
|
|
33
|
+
// src/internal/index.ts
|
|
34
|
+
var injectOverlayKey = Symbol("overlays:svelte");
|
|
35
|
+
var injectOptionsKey = Symbol("overlays:options");
|
|
36
|
+
|
|
37
|
+
// src/define/constructor.ts
|
|
38
|
+
var constructor = (0, import_core.createConstructor)((Inst, props, options) => {
|
|
39
|
+
const { container, deferred, context: _context = /* @__PURE__ */ new Map() } = options;
|
|
40
|
+
function vanish() {
|
|
41
|
+
childApp.$destroy();
|
|
42
|
+
container.remove();
|
|
43
|
+
}
|
|
44
|
+
const context = new Map([..._context.entries()]);
|
|
45
|
+
context.set(injectOverlayKey, {
|
|
46
|
+
resolve: deferred.resolve,
|
|
47
|
+
reject: deferred.reject,
|
|
48
|
+
deferred,
|
|
49
|
+
visible: false,
|
|
50
|
+
vanish
|
|
51
|
+
});
|
|
52
|
+
const childApp = new Inst({
|
|
53
|
+
target: container,
|
|
54
|
+
props,
|
|
55
|
+
context
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// src/define/index.ts
|
|
60
|
+
var defineOverlay = constructor.define;
|
|
61
|
+
var renderOverlay = constructor.render;
|
|
62
|
+
|
|
63
|
+
// src/composable/usePrograms.ts
|
|
64
|
+
var import_svelte = require("svelte");
|
|
65
|
+
function usePrograms(options = {}) {
|
|
66
|
+
(0, import_svelte.setContext)(injectOptionsKey, options);
|
|
67
|
+
return (0, import_svelte.getContext)(injectOverlayKey);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/components/index.ts
|
|
71
|
+
var import_svelte2 = require("svelte");
|
|
72
|
+
var import_core2 = require("@overlastic/core");
|
|
73
|
+
var import_internal3 = require("svelte/internal");
|
|
74
|
+
function create_if_block(ctx) {
|
|
75
|
+
let current;
|
|
76
|
+
const default_slot_template = (
|
|
77
|
+
/* #slots */
|
|
78
|
+
ctx[2].default
|
|
79
|
+
);
|
|
80
|
+
const default_slot = (0, import_internal3.create_slot)(
|
|
81
|
+
default_slot_template,
|
|
82
|
+
ctx,
|
|
83
|
+
/* $$scope */
|
|
84
|
+
ctx[1],
|
|
85
|
+
null
|
|
86
|
+
);
|
|
87
|
+
return {
|
|
88
|
+
c() {
|
|
89
|
+
if (default_slot)
|
|
90
|
+
default_slot.c();
|
|
91
|
+
},
|
|
92
|
+
m(target, anchor) {
|
|
93
|
+
if (default_slot)
|
|
94
|
+
default_slot.m(target, anchor);
|
|
95
|
+
current = true;
|
|
96
|
+
},
|
|
97
|
+
p(ctx2, dirty) {
|
|
98
|
+
if (default_slot) {
|
|
99
|
+
if (default_slot.p && (!current || dirty & 2)) {
|
|
100
|
+
(0, import_internal3.update_slot_base)(
|
|
101
|
+
default_slot,
|
|
102
|
+
default_slot_template,
|
|
103
|
+
ctx2,
|
|
104
|
+
/* $$scope */
|
|
105
|
+
ctx2[1],
|
|
106
|
+
!current ? (0, import_internal3.get_all_dirty_from_scope)(
|
|
107
|
+
/* $$scope */
|
|
108
|
+
ctx2[1]
|
|
109
|
+
) : (0, import_internal3.get_slot_changes)(
|
|
110
|
+
default_slot_template,
|
|
111
|
+
/* $$scope */
|
|
112
|
+
ctx2[1],
|
|
113
|
+
dirty,
|
|
114
|
+
null
|
|
115
|
+
),
|
|
116
|
+
null
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
i(local) {
|
|
122
|
+
if (current)
|
|
123
|
+
return;
|
|
124
|
+
(0, import_internal3.transition_in)(default_slot, local);
|
|
125
|
+
current = true;
|
|
126
|
+
},
|
|
127
|
+
o(local) {
|
|
128
|
+
(0, import_internal3.transition_out)(default_slot, local);
|
|
129
|
+
current = false;
|
|
130
|
+
},
|
|
131
|
+
d(detaching) {
|
|
132
|
+
if (default_slot)
|
|
133
|
+
default_slot.d(detaching);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function create_fragment(ctx) {
|
|
138
|
+
let if_block_anchor;
|
|
139
|
+
let current;
|
|
140
|
+
let if_block = (
|
|
141
|
+
/* visible */
|
|
142
|
+
ctx[0] && create_if_block(ctx)
|
|
143
|
+
);
|
|
144
|
+
return {
|
|
145
|
+
c() {
|
|
146
|
+
if (if_block)
|
|
147
|
+
if_block.c();
|
|
148
|
+
if_block_anchor = (0, import_internal3.empty)();
|
|
149
|
+
},
|
|
150
|
+
m(target, anchor) {
|
|
151
|
+
if (if_block)
|
|
152
|
+
if_block.m(target, anchor);
|
|
153
|
+
(0, import_internal3.insert)(target, if_block_anchor, anchor);
|
|
154
|
+
current = true;
|
|
155
|
+
},
|
|
156
|
+
p(ctx2, [dirty]) {
|
|
157
|
+
if (
|
|
158
|
+
/* visible */
|
|
159
|
+
ctx2[0]
|
|
160
|
+
) {
|
|
161
|
+
if (if_block) {
|
|
162
|
+
if_block.p(ctx2, dirty);
|
|
163
|
+
if (dirty & 1)
|
|
164
|
+
(0, import_internal3.transition_in)(if_block, 1);
|
|
165
|
+
} else {
|
|
166
|
+
if_block = create_if_block(ctx2);
|
|
167
|
+
if_block.c();
|
|
168
|
+
(0, import_internal3.transition_in)(if_block, 1);
|
|
169
|
+
if_block.m(if_block_anchor.parentNode, if_block_anchor);
|
|
170
|
+
}
|
|
171
|
+
} else if (if_block) {
|
|
172
|
+
(0, import_internal3.group_outros)();
|
|
173
|
+
(0, import_internal3.transition_out)(if_block, 1, 1, () => {
|
|
174
|
+
if_block = null;
|
|
175
|
+
});
|
|
176
|
+
(0, import_internal3.check_outros)();
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
i() {
|
|
180
|
+
if (current)
|
|
181
|
+
return;
|
|
182
|
+
(0, import_internal3.transition_in)(if_block);
|
|
183
|
+
current = true;
|
|
184
|
+
},
|
|
185
|
+
o() {
|
|
186
|
+
(0, import_internal3.transition_out)(if_block);
|
|
187
|
+
current = false;
|
|
188
|
+
},
|
|
189
|
+
d(detaching) {
|
|
190
|
+
if (if_block)
|
|
191
|
+
if_block.d(detaching);
|
|
192
|
+
if (detaching)
|
|
193
|
+
(0, import_internal3.detach)(if_block_anchor);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function instance($$self, $$props, $$invalidate) {
|
|
198
|
+
let { $$slots: slots = {}, $$scope } = $$props;
|
|
199
|
+
let { visible = false } = $$props;
|
|
200
|
+
const { duration = 0, immediate = true } = (0, import_svelte2.getContext)(injectOptionsKey) || {};
|
|
201
|
+
const { deferred, vanish } = (0, import_svelte2.getContext)(injectOverlayKey);
|
|
202
|
+
async function destroy() {
|
|
203
|
+
$$invalidate(0, visible = false);
|
|
204
|
+
await (0, import_core2.delay)(duration);
|
|
205
|
+
vanish == null ? void 0 : vanish();
|
|
206
|
+
return Promise.resolve();
|
|
207
|
+
}
|
|
208
|
+
(0, import_svelte2.onMount)(() => immediate && $$invalidate(0, visible = true));
|
|
209
|
+
deferred == null ? void 0 : deferred.then(destroy).catch(destroy);
|
|
210
|
+
$$self.$$set = ($$props2) => {
|
|
211
|
+
if ("visible" in $$props2)
|
|
212
|
+
$$invalidate(0, visible = $$props2.visible);
|
|
213
|
+
if ("$$scope" in $$props2)
|
|
214
|
+
$$invalidate(1, $$scope = $$props2.$$scope);
|
|
215
|
+
};
|
|
216
|
+
return [visible, $$scope, slots];
|
|
217
|
+
}
|
|
218
|
+
var Overlay = class extends import_internal3.SvelteComponentTyped {
|
|
219
|
+
constructor(options) {
|
|
220
|
+
super(options);
|
|
221
|
+
(0, import_internal3.init)(this, options, instance, create_fragment, import_internal3.safe_not_equal, { visible: 0 }, import_internal3.noop);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
225
|
+
0 && (module.exports = {
|
|
226
|
+
Overlay,
|
|
227
|
+
defineOverlay,
|
|
228
|
+
renderOverlay,
|
|
229
|
+
usePrograms
|
|
230
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as _overlastic_core from '@overlastic/core';
|
|
2
|
+
import { Deferred } from '@overlastic/core';
|
|
3
|
+
import { ComponentConstructorOptions } from 'svelte';
|
|
4
|
+
import { SvelteComponentTyped } from 'svelte/internal';
|
|
5
|
+
|
|
6
|
+
interface SMountOptions {
|
|
7
|
+
/** current app context */
|
|
8
|
+
context?: Map<any, any>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare const defineOverlay: <Props, Resolved = void>(instance: any, options?: _overlastic_core.MountOptions<SMountOptions> | undefined) => _overlastic_core.ImperativeOverlay<Props, Resolved, SMountOptions>;
|
|
12
|
+
declare const renderOverlay: <Props, Resolved = void>(instance: any, props?: Props | undefined, options?: _overlastic_core.MountOptions<SMountOptions> | undefined) => Promise<Resolved>;
|
|
13
|
+
|
|
14
|
+
interface ProgramsReturn {
|
|
15
|
+
/** the notification reject, modify visible, and destroy it after the duration ends */
|
|
16
|
+
reject: Function;
|
|
17
|
+
/** the notification resolve, modify visible, and destroy it after the duration ends */
|
|
18
|
+
resolve: Function;
|
|
19
|
+
/** destroy the current instance (immediately) */
|
|
20
|
+
vanish: Function;
|
|
21
|
+
/** visible control popup display and hide */
|
|
22
|
+
/** current deferred */
|
|
23
|
+
deferred?: Deferred<any>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ProgramsOptions {
|
|
27
|
+
/** animation duration to avoid premature destruction of components */
|
|
28
|
+
duration?: number;
|
|
29
|
+
/** whether to set visible to true immediately */
|
|
30
|
+
immediate?: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare function usePrograms(options?: ProgramsOptions): ProgramsReturn;
|
|
33
|
+
|
|
34
|
+
declare class Overlay extends SvelteComponentTyped<{
|
|
35
|
+
visible?: boolean;
|
|
36
|
+
}> {
|
|
37
|
+
constructor(options: ComponentConstructorOptions);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { Overlay, type ProgramsOptions, type ProgramsReturn, defineOverlay, renderOverlay, usePrograms };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as _overlastic_core from '@overlastic/core';
|
|
2
|
+
import { Deferred } from '@overlastic/core';
|
|
3
|
+
import { ComponentConstructorOptions } from 'svelte';
|
|
4
|
+
import { SvelteComponentTyped } from 'svelte/internal';
|
|
5
|
+
|
|
6
|
+
interface SMountOptions {
|
|
7
|
+
/** current app context */
|
|
8
|
+
context?: Map<any, any>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare const defineOverlay: <Props, Resolved = void>(instance: any, options?: _overlastic_core.MountOptions<SMountOptions> | undefined) => _overlastic_core.ImperativeOverlay<Props, Resolved, SMountOptions>;
|
|
12
|
+
declare const renderOverlay: <Props, Resolved = void>(instance: any, props?: Props | undefined, options?: _overlastic_core.MountOptions<SMountOptions> | undefined) => Promise<Resolved>;
|
|
13
|
+
|
|
14
|
+
interface ProgramsReturn {
|
|
15
|
+
/** the notification reject, modify visible, and destroy it after the duration ends */
|
|
16
|
+
reject: Function;
|
|
17
|
+
/** the notification resolve, modify visible, and destroy it after the duration ends */
|
|
18
|
+
resolve: Function;
|
|
19
|
+
/** destroy the current instance (immediately) */
|
|
20
|
+
vanish: Function;
|
|
21
|
+
/** visible control popup display and hide */
|
|
22
|
+
/** current deferred */
|
|
23
|
+
deferred?: Deferred<any>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ProgramsOptions {
|
|
27
|
+
/** animation duration to avoid premature destruction of components */
|
|
28
|
+
duration?: number;
|
|
29
|
+
/** whether to set visible to true immediately */
|
|
30
|
+
immediate?: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare function usePrograms(options?: ProgramsOptions): ProgramsReturn;
|
|
33
|
+
|
|
34
|
+
declare class Overlay extends SvelteComponentTyped<{
|
|
35
|
+
visible?: boolean;
|
|
36
|
+
}> {
|
|
37
|
+
constructor(options: ComponentConstructorOptions);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { Overlay, type ProgramsOptions, type ProgramsReturn, defineOverlay, renderOverlay, usePrograms };
|