@svadmin/ui 0.108.3 → 0.108.4
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/components/AboutSettings.svelte +1 -1
- package/dist/components/AdminApp.svelte +20 -1
- package/dist/components/AdminApp.svelte.d.ts +6 -0
- package/dist/components/ConfigProvider.svelte +78 -1
- package/dist/components/ConfigProvider.svelte.d.ts +36 -0
- package/dist/components/DevTools.svelte +198 -0
- package/dist/components/EditableTable.svelte +59 -6
- package/dist/components/EditableTable.svelte.d.ts +13 -0
- package/dist/components/EmptyState.svelte +19 -6
- package/dist/components/ListPagination.svelte +59 -11
- package/dist/components/NotificationHost.svelte +5 -4
- package/dist/components/RouteAccessGuard.svelte +42 -0
- package/dist/components/RouteAccessGuard.svelte.d.ts +11 -0
- package/dist/components/component-messages.d.ts +3 -3
- package/dist/components/config-provider-defaults.reader.svelte +8 -0
- package/dist/components/config-provider-defaults.reader.svelte.d.ts +3 -0
- package/dist/components/custom-route-access.d.ts +28 -0
- package/dist/components/custom-route-access.js +57 -0
- package/dist/components/notification-scope-probe.svelte +9 -0
- package/dist/components/notification-scope-probe.svelte.d.ts +6 -0
- package/dist/components/popup-portal-host.svelte +41 -0
- package/dist/components/popup-portal-host.svelte.d.ts +7 -0
- package/dist/components/ui/alert-dialog/alert-dialog-portal.svelte +3 -1
- package/dist/components/ui/button/button.svelte +15 -2
- package/dist/components/ui/dialog/dialog-portal.svelte +2 -1
- package/dist/components/ui/dropdown-menu/dropdown-menu-content.svelte +5 -2
- package/dist/components/ui/dropdown-menu/dropdown-menu-content.svelte.d.ts +1 -1
- package/dist/components/ui/popover/popover-content.svelte +6 -2
- package/dist/components/ui/popup-anchor.d.ts +6 -0
- package/dist/components/ui/popup-anchor.js +65 -0
- package/dist/components/ui/popup-portal.svelte +42 -0
- package/dist/components/ui/popup-portal.svelte.d.ts +11 -0
- package/dist/components/ui/sheet/sheet.svelte +3 -3
- package/dist/components/ui/tooltip/tooltip-content.svelte +5 -1
- package/dist/components/ui/tooltip/tooltip-portal.svelte +6 -2
- package/dist/components/ui/tooltip/tooltip-portal.svelte.d.ts +4 -1
- package/dist/devtools-inspector.d.ts +37 -0
- package/dist/devtools-inspector.js +56 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.js +3 -2
- package/dist/notifications.svelte.d.ts +23 -12
- package/dist/notifications.svelte.js +118 -39
- package/package.json +4 -4
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
import type { Snippet } from 'svelte';
|
|
3
3
|
import { PackageOpen } from '@lucide/svelte';
|
|
4
4
|
import { useTranslation } from '@svadmin/core';
|
|
5
|
+
import { useComponentConfig } from './ConfigProvider.svelte';
|
|
5
6
|
|
|
6
7
|
const i18n = useTranslation();
|
|
8
|
+
const config = useComponentConfig();
|
|
7
9
|
|
|
8
10
|
let {
|
|
9
|
-
title
|
|
10
|
-
description
|
|
11
|
+
title,
|
|
12
|
+
description,
|
|
11
13
|
image,
|
|
12
14
|
imageAlt,
|
|
13
15
|
icon,
|
|
@@ -22,6 +24,17 @@
|
|
|
22
24
|
icon?: Snippet;
|
|
23
25
|
children?: Snippet;
|
|
24
26
|
}>();
|
|
27
|
+
|
|
28
|
+
function textDefault(value: unknown): string | undefined {
|
|
29
|
+
return typeof value === 'string' ? value : undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const emptyDefaults = $derived(config.empty);
|
|
33
|
+
const componentEmpty = $derived(config.componentDefaults.Empty ?? {});
|
|
34
|
+
const resolvedTitle = $derived(title ?? emptyDefaults.title ?? textDefault(componentEmpty.title) ?? i18n.t('empty.title'));
|
|
35
|
+
const resolvedDescription = $derived(description ?? emptyDefaults.description ?? textDefault(componentEmpty.description) ?? i18n.t('empty.description'));
|
|
36
|
+
const resolvedImage = $derived(image ?? emptyDefaults.image ?? textDefault(componentEmpty.image));
|
|
37
|
+
const resolvedImageAlt = $derived(imageAlt ?? emptyDefaults.imageAlt ?? textDefault(componentEmpty.imageAlt) ?? '');
|
|
25
38
|
</script>
|
|
26
39
|
|
|
27
40
|
<div
|
|
@@ -30,8 +43,8 @@
|
|
|
30
43
|
aria-live="polite"
|
|
31
44
|
>
|
|
32
45
|
<span class="empty-state__icon" aria-hidden="true">
|
|
33
|
-
{#if
|
|
34
|
-
<img class="empty-state__image" src={
|
|
46
|
+
{#if resolvedImage}
|
|
47
|
+
<img class="empty-state__image" src={resolvedImage} alt={resolvedImageAlt} loading="lazy" decoding="async" />
|
|
35
48
|
{:else if icon}
|
|
36
49
|
{@render icon()}
|
|
37
50
|
{:else}
|
|
@@ -39,8 +52,8 @@
|
|
|
39
52
|
{/if}
|
|
40
53
|
</span>
|
|
41
54
|
<div class="empty-state__body">
|
|
42
|
-
<h3>{
|
|
43
|
-
{#if
|
|
55
|
+
<h3>{resolvedTitle}</h3>
|
|
56
|
+
{#if resolvedDescription}<p>{resolvedDescription}</p>{/if}
|
|
44
57
|
{#if children}
|
|
45
58
|
<div class="empty-state__action">
|
|
46
59
|
{@render children()}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import type { HTMLAttributes } from 'svelte/elements';
|
|
3
3
|
import { useTranslation } from '@svadmin/core/i18n';
|
|
4
|
+
import { useComponentConfig, DEFAULT_PAGE_SIZE_OPTIONS } from './ConfigProvider.svelte';
|
|
4
5
|
import { listText } from './list-i18n.js';
|
|
5
6
|
import { listPaginationItems } from './list-pagination.js';
|
|
6
7
|
|
|
@@ -30,11 +31,11 @@
|
|
|
30
31
|
rangeStart = 0,
|
|
31
32
|
rangeEnd = 0,
|
|
32
33
|
pageSize,
|
|
33
|
-
pageSizeOptions
|
|
34
|
+
pageSizeOptions,
|
|
34
35
|
pageSizeLabel,
|
|
35
|
-
showSummary
|
|
36
|
-
simple
|
|
37
|
-
showJumper
|
|
36
|
+
showSummary,
|
|
37
|
+
simple,
|
|
38
|
+
showJumper,
|
|
38
39
|
onPageChange,
|
|
39
40
|
onPageSizeChange,
|
|
40
41
|
class: className = '',
|
|
@@ -42,6 +43,53 @@
|
|
|
42
43
|
}: Props = $props();
|
|
43
44
|
|
|
44
45
|
const i18n = useTranslation();
|
|
46
|
+
const config = useComponentConfig();
|
|
47
|
+
|
|
48
|
+
function booleanDefault(value: unknown): boolean | undefined {
|
|
49
|
+
return typeof value === 'boolean' ? value : undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function numberList(value: unknown): readonly number[] | undefined {
|
|
53
|
+
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
54
|
+
if (value.some((item) => typeof item !== 'number' || !Number.isFinite(item))) return undefined;
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function numberDefault(value: unknown): number | undefined {
|
|
59
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const paginationDefaults = $derived(config.pagination);
|
|
63
|
+
const paginationComponentDefaults = $derived(config.componentDefaults.Pagination ?? {});
|
|
64
|
+
const resolvedPageSizeOptions = $derived(
|
|
65
|
+
pageSizeOptions
|
|
66
|
+
?? paginationDefaults.pageSizeOptions
|
|
67
|
+
?? numberList(paginationComponentDefaults.pageSizeOptions)
|
|
68
|
+
?? DEFAULT_PAGE_SIZE_OPTIONS,
|
|
69
|
+
);
|
|
70
|
+
const resolvedShowSummary = $derived(
|
|
71
|
+
showSummary
|
|
72
|
+
?? paginationDefaults.showSummary
|
|
73
|
+
?? booleanDefault(paginationComponentDefaults.showSummary)
|
|
74
|
+
?? true,
|
|
75
|
+
);
|
|
76
|
+
const resolvedSimple = $derived(
|
|
77
|
+
simple
|
|
78
|
+
?? paginationDefaults.simple
|
|
79
|
+
?? booleanDefault(paginationComponentDefaults.simple)
|
|
80
|
+
?? false,
|
|
81
|
+
);
|
|
82
|
+
const resolvedShowJumper = $derived(
|
|
83
|
+
showJumper
|
|
84
|
+
?? paginationDefaults.showJumper
|
|
85
|
+
?? booleanDefault(paginationComponentDefaults.showJumper)
|
|
86
|
+
?? false,
|
|
87
|
+
);
|
|
88
|
+
const resolvedPageSize = $derived(
|
|
89
|
+
pageSize
|
|
90
|
+
?? paginationDefaults.pageSize
|
|
91
|
+
?? numberDefault(paginationComponentDefaults.pageSize),
|
|
92
|
+
);
|
|
45
93
|
|
|
46
94
|
const items = $derived(listPaginationItems(page, totalPages));
|
|
47
95
|
const paginated = $derived(totalPages > 1);
|
|
@@ -58,17 +106,17 @@
|
|
|
58
106
|
|
|
59
107
|
{#if paginated}
|
|
60
108
|
<div {...restProps} class={`svadmin-list-pagination ${className}`.trim()} role="navigation" aria-label={listText(i18n, 'listPagination.navigation', { label })}>
|
|
61
|
-
{#if
|
|
109
|
+
{#if resolvedShowSummary}
|
|
62
110
|
<div class="svadmin-list-pagination__info">
|
|
63
111
|
<span>{listText(i18n, 'listPagination.range', { start: rangeStart, end: rangeEnd, total })}</span>
|
|
64
|
-
{#if onPageSizeChange &&
|
|
112
|
+
{#if onPageSizeChange && resolvedPageSize !== undefined}
|
|
65
113
|
<span class="svadmin-list-pagination__size">{pageSizeLabel ?? listText(i18n, 'listPagination.pageSizeLabel')}
|
|
66
114
|
<select
|
|
67
115
|
aria-label={listText(i18n, 'listPagination.pageSizeAria', { label })}
|
|
68
|
-
value={
|
|
116
|
+
value={resolvedPageSize}
|
|
69
117
|
onchange={(event) => onPageSizeChange?.(Number((event.currentTarget as HTMLSelectElement).value))}
|
|
70
118
|
>
|
|
71
|
-
{#each
|
|
119
|
+
{#each resolvedPageSizeOptions as size (size)}<option value={size}>{listText(i18n, 'listPagination.pageSizeOption', { size })}</option>{/each}
|
|
72
120
|
</select>
|
|
73
121
|
</span>
|
|
74
122
|
{/if}
|
|
@@ -78,7 +126,7 @@
|
|
|
78
126
|
<button type="button" class="svadmin-list-pagination__control" disabled={page <= 1}
|
|
79
127
|
title={listText(i18n, page <= 1 ? 'listPagination.firstPage' : 'listPagination.previous')}
|
|
80
128
|
aria-label={listText(i18n, 'listPagination.previousAria', { label })} onclick={() => onPageChange(page - 1)}>{listText(i18n, 'listPagination.previous')}</button>
|
|
81
|
-
{#if
|
|
129
|
+
{#if resolvedSimple}
|
|
82
130
|
<span class="svadmin-list-pagination__simple">{listText(i18n, 'listPagination.simplePage', { page, total: totalPages })}</span>
|
|
83
131
|
{:else}
|
|
84
132
|
<div class="svadmin-list-pagination__pages" role="group" aria-label={listText(i18n, 'listPagination.pageList', { label })}>
|
|
@@ -95,7 +143,7 @@
|
|
|
95
143
|
{/if}
|
|
96
144
|
{/each}
|
|
97
145
|
</div>
|
|
98
|
-
{#if
|
|
146
|
+
{#if resolvedShowJumper}
|
|
99
147
|
<label class="svadmin-list-pagination__jumper">
|
|
100
148
|
<span>{listText(i18n, 'listPagination.jumpTo')}</span>
|
|
101
149
|
<input type="number" min="1" max={totalPages} value={page}
|
|
@@ -109,7 +157,7 @@
|
|
|
109
157
|
aria-label={listText(i18n, 'listPagination.nextAria', { label })} onclick={() => onPageChange(page + 1)}>{listText(i18n, 'listPagination.next')}</button>
|
|
110
158
|
</div>
|
|
111
159
|
</div>
|
|
112
|
-
{:else if
|
|
160
|
+
{:else if resolvedShowSummary}
|
|
113
161
|
<p {...restProps} class={`svadmin-list-pagination__summary ${className}`.trim()}>{listText(i18n, 'common.total', { total })}</p>
|
|
114
162
|
{/if}
|
|
115
163
|
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { X } from '@lucide/svelte';
|
|
3
|
-
import {
|
|
3
|
+
import { useNotificationController, type NotificationPlacement } from '../notifications.svelte.js';
|
|
4
4
|
|
|
5
5
|
const placements: NotificationPlacement[] = ['top-right', 'top-left', 'bottom-right', 'bottom-left'];
|
|
6
|
+
const controller = useNotificationController();
|
|
6
7
|
const groups = $derived(placements
|
|
7
8
|
.map((placement) => ({
|
|
8
9
|
placement,
|
|
9
|
-
items:
|
|
10
|
+
items: controller.items.filter((item) => item.placement === placement),
|
|
10
11
|
}))
|
|
11
12
|
.filter((group) => group.items.length > 0));
|
|
12
13
|
</script>
|
|
@@ -24,10 +25,10 @@
|
|
|
24
25
|
<p class="svadmin-notification-host__title">{item.title}</p>
|
|
25
26
|
{#if item.description}<p class="svadmin-notification-host__description">{item.description}</p>{/if}
|
|
26
27
|
{#if item.actionLabel && item.onAction}
|
|
27
|
-
<button type="button" class="svadmin-notification-host__action" onclick={() => { item.onAction?.();
|
|
28
|
+
<button type="button" class="svadmin-notification-host__action" onclick={() => { item.onAction?.(); controller.close(item.id); }}>{item.actionLabel}</button>
|
|
28
29
|
{/if}
|
|
29
30
|
</div>
|
|
30
|
-
<button type="button" class="svadmin-notification-host__close" aria-label="Close notification" onclick={() =>
|
|
31
|
+
<button type="button" class="svadmin-notification-host__close" aria-label="Close notification" onclick={() => controller.close(item.id)}>
|
|
31
32
|
<X size={14} aria-hidden="true" />
|
|
32
33
|
</button>
|
|
33
34
|
</section>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { definedOptions } from '@svadmin/core/options';
|
|
3
|
+
import { captureAdminContext, useCan, type Action } from '@svadmin/core';
|
|
4
|
+
import { useTranslation } from '@svadmin/core/i18n';
|
|
5
|
+
import type { Snippet } from 'svelte';
|
|
6
|
+
import DataState from './content/DataState.svelte';
|
|
7
|
+
|
|
8
|
+
interface Props {
|
|
9
|
+
resource: string;
|
|
10
|
+
action?: Action;
|
|
11
|
+
id?: string | number;
|
|
12
|
+
children: Snippet;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let {
|
|
16
|
+
resource,
|
|
17
|
+
action = 'list',
|
|
18
|
+
id,
|
|
19
|
+
children,
|
|
20
|
+
}: Props = $props();
|
|
21
|
+
|
|
22
|
+
const adminContext = captureAdminContext();
|
|
23
|
+
const i18n = useTranslation();
|
|
24
|
+
const accessControlEnabled = $derived(Boolean(adminContext.accessControlProvider));
|
|
25
|
+
const permission = useCan(() => (definedOptions({
|
|
26
|
+
resource,
|
|
27
|
+
action,
|
|
28
|
+
id,
|
|
29
|
+
queryOptions: { enabled: accessControlEnabled },
|
|
30
|
+
})));
|
|
31
|
+
const denialDescription = $derived(
|
|
32
|
+
permission.isError ? i18n.t('common.permissionUnavailable') : i18n.t('common.permissionDenied'),
|
|
33
|
+
);
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
{#if accessControlEnabled && permission.isLoading}
|
|
37
|
+
<DataState state="loading" />
|
|
38
|
+
{:else if !accessControlEnabled || permission.allowed}
|
|
39
|
+
{@render children()}
|
|
40
|
+
{:else}
|
|
41
|
+
<DataState state={permission.isError ? 'error' : 'forbidden'} description={denialDescription} />
|
|
42
|
+
{/if}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Action } from '@svadmin/core';
|
|
2
|
+
import type { Snippet } from 'svelte';
|
|
3
|
+
interface Props {
|
|
4
|
+
resource: string;
|
|
5
|
+
action?: Action;
|
|
6
|
+
id?: string | number;
|
|
7
|
+
children: Snippet;
|
|
8
|
+
}
|
|
9
|
+
declare const RouteAccessGuard: import("svelte").Component<Props, {}, "">;
|
|
10
|
+
type RouteAccessGuard = ReturnType<typeof RouteAccessGuard>;
|
|
11
|
+
export default RouteAccessGuard;
|
|
@@ -48,9 +48,9 @@ export interface ComponentMessageSource {
|
|
|
48
48
|
t: (key: string) => string;
|
|
49
49
|
}
|
|
50
50
|
export declare function resolveComponentLocale(input: {
|
|
51
|
-
explicit?: string | null;
|
|
52
|
-
configLocale?: string | null;
|
|
53
|
-
i18nLocale?: string | null;
|
|
51
|
+
explicit?: string | null | undefined;
|
|
52
|
+
configLocale?: string | null | undefined;
|
|
53
|
+
i18nLocale?: string | null | undefined;
|
|
54
54
|
}): string;
|
|
55
55
|
/** 宿主翻译优先;缺失时按已解析 locale 选择内置中文或英文。 */
|
|
56
56
|
export declare function componentText(i18n: ComponentMessageSource, locale: string, key: ComponentMessageKey): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { MenuItem } from '@svadmin/core';
|
|
2
|
+
/** 自定义页面的直接访问权限。与菜单 `meta.resource` / `meta.action` 使用同一套检查。 */
|
|
3
|
+
export interface CustomRouteAccessConfig {
|
|
4
|
+
readonly resource: string;
|
|
5
|
+
/** 缺省为 `list`,与侧栏菜单一致。 */
|
|
6
|
+
readonly action?: string;
|
|
7
|
+
readonly id?: string | number;
|
|
8
|
+
/** 从匹配到的路由参数读取记录 id,用于带参数的直接 URL。 */
|
|
9
|
+
readonly idParam?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface CustomRouteAccess {
|
|
12
|
+
readonly resource: string;
|
|
13
|
+
readonly action: string;
|
|
14
|
+
readonly id?: string | number;
|
|
15
|
+
}
|
|
16
|
+
export interface CustomRouteAccessSource {
|
|
17
|
+
/**
|
|
18
|
+
* 显式权限覆盖菜单继承。`false` 表示该路由保持公开,即使同路径菜单受保护。
|
|
19
|
+
*/
|
|
20
|
+
readonly access?: CustomRouteAccessConfig | false;
|
|
21
|
+
}
|
|
22
|
+
export declare function normalizeNavPath(value: string | undefined): string;
|
|
23
|
+
/**
|
|
24
|
+
* 自定义路由的直接 URL 权限。
|
|
25
|
+
* 显式 `access` 优先;否则继承当前 URL 上最具体的菜单项。
|
|
26
|
+
* 更具体的菜单项没有 `meta.resource` 时视为公开,不回退到父级。
|
|
27
|
+
*/
|
|
28
|
+
export declare function resolveCustomRouteAccess(route: CustomRouteAccessSource, pathname: string, menu: readonly MenuItem[] | undefined, params?: Record<string, string | undefined>): CustomRouteAccess | undefined;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export function normalizeNavPath(value) {
|
|
2
|
+
if (!value)
|
|
3
|
+
return '';
|
|
4
|
+
const path = value.replace(/^#/, '').split(/[?#]/)[0] || '/';
|
|
5
|
+
if (path === '/')
|
|
6
|
+
return '/';
|
|
7
|
+
const withSlash = path.startsWith('/') ? path : `/${path}`;
|
|
8
|
+
return withSlash.replace(/\/+$/, '');
|
|
9
|
+
}
|
|
10
|
+
function consider(item, current, best) {
|
|
11
|
+
const href = normalizeNavPath(item.href);
|
|
12
|
+
if (href) {
|
|
13
|
+
const exact = href === current;
|
|
14
|
+
const prefix = href !== '/' && current.startsWith(`${href}/`);
|
|
15
|
+
if (exact || prefix) {
|
|
16
|
+
const score = (exact ? 1_000_000 : 0) + href.length;
|
|
17
|
+
if (!best || score > best.score)
|
|
18
|
+
best = { score, item };
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
for (const child of item.children ?? [])
|
|
22
|
+
best = consider(child, current, best);
|
|
23
|
+
return best;
|
|
24
|
+
}
|
|
25
|
+
function inheritedMenuAccess(menu, pathname) {
|
|
26
|
+
const current = normalizeNavPath(pathname);
|
|
27
|
+
if (!current)
|
|
28
|
+
return undefined;
|
|
29
|
+
let best;
|
|
30
|
+
for (const item of menu ?? [])
|
|
31
|
+
best = consider(item, current, best);
|
|
32
|
+
const resource = best?.item.meta?.resource;
|
|
33
|
+
if (!resource)
|
|
34
|
+
return undefined;
|
|
35
|
+
const action = best?.item.meta?.action;
|
|
36
|
+
return { resource, action: action && action.length > 0 ? action : 'list' };
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 自定义路由的直接 URL 权限。
|
|
40
|
+
* 显式 `access` 优先;否则继承当前 URL 上最具体的菜单项。
|
|
41
|
+
* 更具体的菜单项没有 `meta.resource` 时视为公开,不回退到父级。
|
|
42
|
+
*/
|
|
43
|
+
export function resolveCustomRouteAccess(route, pathname, menu, params = {}) {
|
|
44
|
+
if (route.access === false)
|
|
45
|
+
return undefined;
|
|
46
|
+
if (route.access) {
|
|
47
|
+
const action = route.access.action && route.access.action.length > 0 ? route.access.action : 'list';
|
|
48
|
+
const paramId = route.access.idParam ? params[route.access.idParam] : undefined;
|
|
49
|
+
const id = route.access.id !== undefined ? route.access.id : paramId;
|
|
50
|
+
return {
|
|
51
|
+
resource: route.access.resource,
|
|
52
|
+
action,
|
|
53
|
+
...(id !== undefined && id !== '' ? { id } : {}),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return inheritedMenuAccess(menu, pathname);
|
|
57
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { notification, useNotificationController } from '../notifications.svelte.js';
|
|
3
|
+
|
|
4
|
+
let { label }: { label: string } = $props();
|
|
5
|
+
const center = useNotificationController();
|
|
6
|
+
</script>
|
|
7
|
+
|
|
8
|
+
<button type="button" onclick={() => center.success(`${label} scoped`, 'from context', { duration: 0 })}>Scoped {label}</button>
|
|
9
|
+
<button type="button" onclick={() => notification.success(`${label} module`, 'from module', { duration: 0 })}>Module {label}</button>
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import ConfigProvider, { type GetPopupContainer } from './ConfigProvider.svelte';
|
|
3
|
+
import * as Dialog from './ui/dialog/index.js';
|
|
4
|
+
import * as Popover from './ui/popover/index.js';
|
|
5
|
+
import * as Tooltip from './ui/tooltip/index.js';
|
|
6
|
+
import { Root as Sheet } from './ui/sheet/index.js';
|
|
7
|
+
|
|
8
|
+
let {
|
|
9
|
+
calls = [],
|
|
10
|
+
useCustomContainer = true,
|
|
11
|
+
}: {
|
|
12
|
+
calls?: Array<HTMLElement | null | undefined>;
|
|
13
|
+
useCustomContainer?: boolean;
|
|
14
|
+
} = $props();
|
|
15
|
+
|
|
16
|
+
const getPopupContainer: GetPopupContainer = (node) => {
|
|
17
|
+
calls.push(node);
|
|
18
|
+
if (typeof document === 'undefined') return undefined;
|
|
19
|
+
return node?.closest('.workspace') ?? undefined;
|
|
20
|
+
};
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<ConfigProvider getPopupContainer={useCustomContainer ? getPopupContainer : undefined}>
|
|
24
|
+
<div class="workspace" data-testid="workspace-a">
|
|
25
|
+
<Dialog.Root open>
|
|
26
|
+
<Dialog.Trigger>Open dialog</Dialog.Trigger>
|
|
27
|
+
<Dialog.Content>Dialog body</Dialog.Content>
|
|
28
|
+
</Dialog.Root>
|
|
29
|
+
<Tooltip.Root open>
|
|
30
|
+
<Tooltip.Trigger>Open tooltip</Tooltip.Trigger>
|
|
31
|
+
<Tooltip.Content>Tooltip body</Tooltip.Content>
|
|
32
|
+
</Tooltip.Root>
|
|
33
|
+
</div>
|
|
34
|
+
<div class="workspace" data-testid="workspace-b">
|
|
35
|
+
<Popover.Root open>
|
|
36
|
+
<Popover.Trigger>Open popover</Popover.Trigger>
|
|
37
|
+
<Popover.Content>Popover body</Popover.Content>
|
|
38
|
+
</Popover.Root>
|
|
39
|
+
<Sheet open>Sheet body</Sheet>
|
|
40
|
+
</div>
|
|
41
|
+
</ConfigProvider>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
type $$ComponentProps = {
|
|
2
|
+
calls?: Array<HTMLElement | null | undefined>;
|
|
3
|
+
useCustomContainer?: boolean;
|
|
4
|
+
};
|
|
5
|
+
declare const PopupPortalHost: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
6
|
+
type PopupPortalHost = ReturnType<typeof PopupPortalHost>;
|
|
7
|
+
export default PopupPortalHost;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { AlertDialog as AlertDialogPrimitive } from "bits-ui";
|
|
3
|
+
import PopupPortal from "../popup-portal.svelte";
|
|
4
|
+
|
|
3
5
|
let { ...restProps }: AlertDialogPrimitive.PortalProps = $props();
|
|
4
6
|
</script>
|
|
5
7
|
|
|
6
|
-
<
|
|
8
|
+
<PopupPortal {...restProps} kind="alert-dialog" />
|
|
@@ -26,10 +26,21 @@
|
|
|
26
26
|
import * as Tooltip from "../tooltip/index.js";
|
|
27
27
|
import { useComponentConfig } from "../../ConfigProvider.svelte";
|
|
28
28
|
|
|
29
|
+
const BUTTON_VARIANTS = new Set(["default", "outline", "secondary", "ghost", "destructive", "link", "dashed", "text"]);
|
|
30
|
+
const BUTTON_SIZES = new Set(["default", "xs", "sm", "lg", "icon", "icon-xs", "icon-sm", "icon-lg"]);
|
|
31
|
+
|
|
32
|
+
function readButtonVariant(value: unknown): ButtonVariantValue | undefined {
|
|
33
|
+
return typeof value === "string" && BUTTON_VARIANTS.has(value) ? value as ButtonVariantValue : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function readButtonSize(value: unknown): ButtonSizeValue | undefined {
|
|
37
|
+
return typeof value === "string" && BUTTON_SIZES.has(value) ? value as ButtonSizeValue : undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
29
40
|
const uid = $props.id();
|
|
30
41
|
let {
|
|
31
42
|
class: className,
|
|
32
|
-
variant
|
|
43
|
+
variant: variantProp,
|
|
33
44
|
size: sizeProp,
|
|
34
45
|
ref = $bindable(null),
|
|
35
46
|
href = undefined,
|
|
@@ -45,7 +56,9 @@
|
|
|
45
56
|
}: ButtonProps = $props();
|
|
46
57
|
|
|
47
58
|
const config = useComponentConfig();
|
|
48
|
-
const
|
|
59
|
+
const buttonDefaults = $derived(config.componentDefaults['Button'] ?? {});
|
|
60
|
+
const variant = $derived(variantProp ?? readButtonVariant(buttonDefaults['variant']) ?? "default");
|
|
61
|
+
const size = $derived(sizeProp ?? readButtonSize(buttonDefaults['size']) ?? (config.density === "compact" || config.componentSize === "sm" ? "sm" : config.componentSize === "lg" ? "lg" : "default"));
|
|
49
62
|
|
|
50
63
|
const restriction = $derived((disabledReason ?? "").trim());
|
|
51
64
|
const isDisabled = $derived(Boolean(disabled || restriction));
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { Dialog as DialogPrimitive } from "bits-ui";
|
|
3
|
+
import PopupPortal from "../popup-portal.svelte";
|
|
3
4
|
|
|
4
5
|
let { ...restProps }: DialogPrimitive.PortalProps = $props();
|
|
5
6
|
</script>
|
|
6
7
|
|
|
7
|
-
<
|
|
8
|
+
<PopupPortal {...restProps} kind="dialog" />
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
import { cn, type WithElementRef } from "../../../utils.js";
|
|
3
3
|
import type { HTMLAttributes } from "svelte/elements";
|
|
4
4
|
import { getContext } from "svelte";
|
|
5
|
+
import { resolvePopupContainer, useComponentConfig } from "../../ConfigProvider.svelte";
|
|
5
6
|
|
|
6
7
|
type Props = WithElementRef<HTMLAttributes<HTMLDivElement>> & {
|
|
7
8
|
align?: "start" | "center" | "end";
|
|
8
|
-
/** 挂到 document.body,避免表格等 overflow 容器裁切菜单。 */
|
|
9
|
+
/** 挂到 getPopupContainer,默认 document.body,避免表格等 overflow 容器裁切菜单。 */
|
|
9
10
|
portal?: boolean;
|
|
10
11
|
};
|
|
11
12
|
|
|
@@ -18,6 +19,7 @@
|
|
|
18
19
|
...restProps
|
|
19
20
|
}: Props = $props();
|
|
20
21
|
|
|
22
|
+
const config = useComponentConfig();
|
|
21
23
|
const getOpen = getContext<() => boolean>("svadmin-dropdown-open");
|
|
22
24
|
const getRoot = getContext<(() => HTMLElement | null) | undefined>("svadmin-dropdown-root");
|
|
23
25
|
const contentBridge = getContext<{ node: HTMLElement | null } | undefined>("svadmin-dropdown-content");
|
|
@@ -55,7 +57,8 @@
|
|
|
55
57
|
|
|
56
58
|
function positionLayer(node: HTMLElement, enabled: boolean) {
|
|
57
59
|
if (!enabled || typeof document === "undefined") return;
|
|
58
|
-
document.body
|
|
60
|
+
const target = resolvePopupContainer(config.getPopupContainer, node) ?? document.body;
|
|
61
|
+
target.appendChild(node);
|
|
59
62
|
if (contentBridge) contentBridge.node = node;
|
|
60
63
|
const update = () => place(node);
|
|
61
64
|
update();
|
|
@@ -2,7 +2,7 @@ import { type WithElementRef } from "../../../utils.js";
|
|
|
2
2
|
import type { HTMLAttributes } from "svelte/elements";
|
|
3
3
|
type Props = WithElementRef<HTMLAttributes<HTMLDivElement>> & {
|
|
4
4
|
align?: "start" | "center" | "end";
|
|
5
|
-
/** 挂到 document.body,避免表格等 overflow 容器裁切菜单。 */
|
|
5
|
+
/** 挂到 getPopupContainer,默认 document.body,避免表格等 overflow 容器裁切菜单。 */
|
|
6
6
|
portal?: boolean;
|
|
7
7
|
};
|
|
8
8
|
declare const DropdownMenuContent: import("svelte").Component<Props, {}, "ref">;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { Popover as PopoverPrimitive } from "bits-ui";
|
|
3
3
|
import { cn } from "../../../utils.js";
|
|
4
|
+
import PopupPortal from "../popup-portal.svelte";
|
|
4
5
|
|
|
5
6
|
let {
|
|
6
7
|
ref = $bindable(null),
|
|
@@ -9,9 +10,12 @@
|
|
|
9
10
|
sideOffset = 4,
|
|
10
11
|
...restProps
|
|
11
12
|
}: PopoverPrimitive.ContentProps & { class?: string } = $props();
|
|
13
|
+
|
|
14
|
+
const customAnchor = $derived(restProps.customAnchor);
|
|
15
|
+
const anchor = $derived(typeof HTMLElement !== 'undefined' && customAnchor instanceof HTMLElement ? customAnchor : null);
|
|
12
16
|
</script>
|
|
13
17
|
|
|
14
|
-
<
|
|
18
|
+
<PopupPortal kind="popover" {anchor}>
|
|
15
19
|
<PopoverPrimitive.Content
|
|
16
20
|
bind:ref
|
|
17
21
|
{align}
|
|
@@ -19,4 +23,4 @@
|
|
|
19
23
|
class={cn("svadmin-popover-content", className)}
|
|
20
24
|
{...restProps}
|
|
21
25
|
/>
|
|
22
|
-
</
|
|
26
|
+
</PopupPortal>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type PopupAnchorKind = 'popover' | 'dialog' | 'alert-dialog' | 'tooltip' | 'sheet';
|
|
2
|
+
/**
|
|
3
|
+
* getPopupContainer 收到的节点:显式锚点优先,否则是同类型触发器,最后才是源码树里的锚点标记。
|
|
4
|
+
* Sheet 没有触发器,使用声明位置上的标记,这样 node?.closest() 仍能看到所在容器。
|
|
5
|
+
*/
|
|
6
|
+
export declare function resolvePopupAnchor(explicit: HTMLElement | null | undefined, marker: HTMLElement | null | undefined, kind?: PopupAnchorKind): HTMLElement | undefined;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const TRIGGER_SELECTOR = {
|
|
2
|
+
popover: '[data-popover-trigger]',
|
|
3
|
+
dialog: '[data-dialog-trigger], [data-slot="dialog-trigger"]',
|
|
4
|
+
'alert-dialog': '[data-alert-dialog-trigger]',
|
|
5
|
+
tooltip: '[data-tooltip-trigger], [data-slot="tooltip-trigger"]',
|
|
6
|
+
};
|
|
7
|
+
function isHtmlElement(value) {
|
|
8
|
+
return typeof HTMLElement !== 'undefined' && value instanceof HTMLElement;
|
|
9
|
+
}
|
|
10
|
+
function isOpenTrigger(element) {
|
|
11
|
+
const state = element.getAttribute('data-state');
|
|
12
|
+
if (state && state !== 'closed')
|
|
13
|
+
return true;
|
|
14
|
+
return element.getAttribute('aria-expanded') === 'true';
|
|
15
|
+
}
|
|
16
|
+
function nextMarkerAfter(trigger, markers, NodeCtor) {
|
|
17
|
+
let candidate;
|
|
18
|
+
for (const marker of markers) {
|
|
19
|
+
const position = trigger.compareDocumentPosition(marker);
|
|
20
|
+
if ((position & NodeCtor.DOCUMENT_POSITION_FOLLOWING) === 0)
|
|
21
|
+
continue;
|
|
22
|
+
if (!candidate || (marker.compareDocumentPosition(candidate) & NodeCtor.DOCUMENT_POSITION_FOLLOWING) !== 0) {
|
|
23
|
+
candidate = marker;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return candidate;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 把打开的触发器配到它后面最近的同类型锚点。
|
|
30
|
+
* 后出现的触发器会覆盖更早的配对,因此紧挨着浮层的那个触发器胜出。
|
|
31
|
+
*/
|
|
32
|
+
function triggerForMarker(marker, kind) {
|
|
33
|
+
if (!kind || kind === 'sheet')
|
|
34
|
+
return undefined;
|
|
35
|
+
const doc = marker.ownerDocument;
|
|
36
|
+
const NodeCtor = doc?.defaultView?.Node;
|
|
37
|
+
const selector = TRIGGER_SELECTOR[kind];
|
|
38
|
+
if (!doc || !NodeCtor || !selector)
|
|
39
|
+
return undefined;
|
|
40
|
+
const markers = [...doc.querySelectorAll(`[data-svadmin-popup-anchor="${kind}"]`)];
|
|
41
|
+
const triggers = [...doc.querySelectorAll(selector)];
|
|
42
|
+
const openOwner = ownersFor(triggers.filter(isOpenTrigger), markers, NodeCtor).get(marker);
|
|
43
|
+
// 受控 open 时触发器的 data-state 可能仍是 closed。没有打开中的触发器时,退回同类型里紧邻的那个。
|
|
44
|
+
return openOwner ?? ownersFor(triggers, markers, NodeCtor).get(marker);
|
|
45
|
+
}
|
|
46
|
+
function ownersFor(triggers, markers, NodeCtor) {
|
|
47
|
+
const owners = new Map();
|
|
48
|
+
for (const trigger of triggers) {
|
|
49
|
+
const owner = nextMarkerAfter(trigger, markers, NodeCtor);
|
|
50
|
+
if (owner)
|
|
51
|
+
owners.set(owner, trigger);
|
|
52
|
+
}
|
|
53
|
+
return owners;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* getPopupContainer 收到的节点:显式锚点优先,否则是同类型触发器,最后才是源码树里的锚点标记。
|
|
57
|
+
* Sheet 没有触发器,使用声明位置上的标记,这样 node?.closest() 仍能看到所在容器。
|
|
58
|
+
*/
|
|
59
|
+
export function resolvePopupAnchor(explicit, marker, kind) {
|
|
60
|
+
if (isHtmlElement(explicit))
|
|
61
|
+
return explicit;
|
|
62
|
+
if (!isHtmlElement(marker))
|
|
63
|
+
return undefined;
|
|
64
|
+
return triggerForMarker(marker, kind) ?? marker;
|
|
65
|
+
}
|