luna-plus 0.0.37 → 0.0.39

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.
@@ -9,11 +9,10 @@
9
9
  import { tick } from "svelte";
10
10
 
11
11
  interface Props {
12
- zIndex?: number;
13
12
  showClose?: boolean;
14
13
  }
15
14
 
16
- let { zIndex = 3000, showClose = true }: Props = $props();
15
+ let { showClose = true }: Props = $props();
17
16
 
18
17
  let visible = $state(false);
19
18
  let options = $state<MessageBoxOptions | null>(null);
@@ -91,7 +90,6 @@
91
90
  {@const opts = options}
92
91
  <div
93
92
  class="lm-messagebox__wrapper"
94
- style="--lm-messagebox-z-index: {zIndex}"
95
93
  use:portal
96
94
  >
97
95
  <Dialog
@@ -1,5 +1,4 @@
1
1
  interface Props {
2
- zIndex?: number;
3
2
  showClose?: boolean;
4
3
  }
5
4
  declare const MessageBox: import("svelte").Component<Props, {}, "">;
@@ -1,29 +1,48 @@
1
- import { mount, unmount } from 'svelte';
1
+ import { mount } from 'svelte';
2
2
  import MessageBox from './MessageBox.svelte';
3
- let containerEl = null;
4
- let messageBoxInstance = null;
3
+ let mounted = false;
4
+ let mountingPromise = null;
5
+ const nextFrame = () => new Promise((resolve) => requestAnimationFrame(() => resolve()));
5
6
  /** 确保 MessageBox 容器已挂载 */
6
- const ensureContainer = () => {
7
+ const ensureMounted = async () => {
7
8
  if (typeof window === 'undefined')
8
9
  return;
9
- if (containerEl)
10
+ if (mounted)
10
11
  return;
11
- containerEl = document.createElement('div');
12
- containerEl.className = 'lm-messagebox-container';
13
- document.body.appendChild(containerEl);
14
- messageBoxInstance = mount(MessageBox, {
15
- target: containerEl,
16
- props: {}
17
- });
12
+ if (mountingPromise)
13
+ return mountingPromise;
14
+ mountingPromise = (async () => {
15
+ let container = document.querySelector('.lm-messagebox-container');
16
+ if (!container) {
17
+ container = document.createElement('div');
18
+ container.className = 'lm-messagebox-container';
19
+ document.body.appendChild(container);
20
+ }
21
+ // 容器已有内容(或标记)说明已挂载过 MessageBox。
22
+ // 避免重复 mount 导致同一事件触发两个弹窗。
23
+ if (container.childElementCount > 0 || container.dataset.lmMessageboxMounted === 'true') {
24
+ await nextFrame();
25
+ mounted = true;
26
+ return;
27
+ }
28
+ mount(MessageBox, {
29
+ target: container,
30
+ props: {}
31
+ });
32
+ container.dataset.lmMessageboxMounted = 'true';
33
+ // MessageBox 内部用 $effect 注册 window 事件监听。
34
+ // 首次 mount 后立刻 dispatch 可能会丢事件,所以等到下一帧再认为已就绪。
35
+ await nextFrame();
36
+ mounted = true;
37
+ })();
38
+ return mountingPromise;
18
39
  };
19
40
  /** 显示消息框 */
20
- export const showMessageBox = (options) => {
41
+ export const showMessageBox = async (options) => {
42
+ if (typeof window === 'undefined')
43
+ return false;
44
+ await ensureMounted();
21
45
  return new Promise((resolve) => {
22
- if (typeof window === 'undefined') {
23
- resolve(false);
24
- return;
25
- }
26
- ensureContainer();
27
46
  const wrappedOptions = {
28
47
  ...options,
29
48
  onOk: () => {
@@ -35,11 +54,9 @@ export const showMessageBox = (options) => {
35
54
  resolve(false);
36
55
  }
37
56
  };
38
- // 派发自定义事件触发 MessageBox 组件
39
- const event = new CustomEvent('lumen-messagebox', {
57
+ window.dispatchEvent(new CustomEvent('lumen-messagebox', {
40
58
  detail: wrappedOptions
41
- });
42
- window.dispatchEvent(event);
59
+ }));
43
60
  });
44
61
  };
45
62
  /** 便捷方法 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "luna-plus",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "description": "A modern Svelte 5 component library with 60+ components",
5
5
  "type": "module",
6
6
  "svelte": "./dist/index.js",