@svadmin/ui 0.108.2 → 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.
Files changed (47) hide show
  1. package/dist/components/AboutSettings.svelte +1 -1
  2. package/dist/components/AdminApp.svelte +20 -1
  3. package/dist/components/AdminApp.svelte.d.ts +6 -0
  4. package/dist/components/Calendar.svelte +28 -8
  5. package/dist/components/ConfigProvider.svelte +101 -10
  6. package/dist/components/ConfigProvider.svelte.d.ts +39 -0
  7. package/dist/components/DatePicker.svelte +32 -9
  8. package/dist/components/DatePicker.svelte.d.ts +3 -0
  9. package/dist/components/DevTools.svelte +198 -0
  10. package/dist/components/EditableTable.svelte +59 -6
  11. package/dist/components/EditableTable.svelte.d.ts +13 -0
  12. package/dist/components/EmptyState.svelte +19 -6
  13. package/dist/components/ListPagination.svelte +59 -11
  14. package/dist/components/NotificationHost.svelte +5 -4
  15. package/dist/components/RouteAccessGuard.svelte +42 -0
  16. package/dist/components/RouteAccessGuard.svelte.d.ts +11 -0
  17. package/dist/components/component-messages.d.ts +59 -0
  18. package/dist/components/component-messages.js +47 -0
  19. package/dist/components/config-provider-defaults.reader.svelte +8 -0
  20. package/dist/components/config-provider-defaults.reader.svelte.d.ts +3 -0
  21. package/dist/components/custom-route-access.d.ts +28 -0
  22. package/dist/components/custom-route-access.js +57 -0
  23. package/dist/components/notification-scope-probe.svelte +9 -0
  24. package/dist/components/notification-scope-probe.svelte.d.ts +6 -0
  25. package/dist/components/popup-portal-host.svelte +41 -0
  26. package/dist/components/popup-portal-host.svelte.d.ts +7 -0
  27. package/dist/components/ui/alert-dialog/alert-dialog-portal.svelte +3 -1
  28. package/dist/components/ui/button/button.svelte +15 -2
  29. package/dist/components/ui/dialog/dialog-portal.svelte +2 -1
  30. package/dist/components/ui/dropdown-menu/dropdown-menu-content.svelte +5 -2
  31. package/dist/components/ui/dropdown-menu/dropdown-menu-content.svelte.d.ts +1 -1
  32. package/dist/components/ui/popover/popover-content.svelte +6 -2
  33. package/dist/components/ui/popup-anchor.d.ts +6 -0
  34. package/dist/components/ui/popup-anchor.js +65 -0
  35. package/dist/components/ui/popup-portal.svelte +42 -0
  36. package/dist/components/ui/popup-portal.svelte.d.ts +11 -0
  37. package/dist/components/ui/sheet/sheet.svelte +3 -3
  38. package/dist/components/ui/tooltip/tooltip-content.svelte +5 -1
  39. package/dist/components/ui/tooltip/tooltip-portal.svelte +6 -2
  40. package/dist/components/ui/tooltip/tooltip-portal.svelte.d.ts +4 -1
  41. package/dist/devtools-inspector.d.ts +37 -0
  42. package/dist/devtools-inspector.js +56 -0
  43. package/dist/index.d.ts +7 -5
  44. package/dist/index.js +3 -2
  45. package/dist/notifications.svelte.d.ts +23 -12
  46. package/dist/notifications.svelte.js +118 -39
  47. package/package.json +4 -4
@@ -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;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * 基础控件文案扩展点。
3
+ *
4
+ * 解析顺序:组件 `locale` 属性 > ConfigProvider.locale(仅当树中存在 Provider)> i18n.locale > `en`。
5
+ * 词条先走 i18n key;宿主没有该 key 时,回退到本目录的中文 / 英文表。
6
+ * 新的基础控件不要再写死 Previous、Clear 等英文,改为调用 `componentText`,并在下面的目录补一条词条。
7
+ */
8
+ export declare const componentMessageCatalog: {
9
+ readonly 'datePicker.previous': {
10
+ readonly en: "Previous";
11
+ readonly zh: "上一个";
12
+ };
13
+ readonly 'datePicker.next': {
14
+ readonly en: "Next";
15
+ readonly zh: "下一个";
16
+ };
17
+ readonly 'datePicker.clear': {
18
+ readonly en: "Clear";
19
+ readonly zh: "清除";
20
+ };
21
+ readonly 'datePicker.selectDate': {
22
+ readonly en: "Select date";
23
+ readonly zh: "选择日期";
24
+ };
25
+ readonly 'datePicker.previousMonth': {
26
+ readonly en: "Previous month";
27
+ readonly zh: "上个月";
28
+ };
29
+ readonly 'datePicker.nextMonth': {
30
+ readonly en: "Next month";
31
+ readonly zh: "下个月";
32
+ };
33
+ readonly 'datePicker.previousYear': {
34
+ readonly en: "Previous year";
35
+ readonly zh: "上一年";
36
+ };
37
+ readonly 'datePicker.nextYear': {
38
+ readonly en: "Next year";
39
+ readonly zh: "下一年";
40
+ };
41
+ readonly 'datePicker.calendarDates': {
42
+ readonly en: "Calendar dates";
43
+ readonly zh: "日历日期";
44
+ };
45
+ };
46
+ export type ComponentMessageKey = keyof typeof componentMessageCatalog;
47
+ export interface ComponentMessageSource {
48
+ t: (key: string) => string;
49
+ }
50
+ export declare function resolveComponentLocale(input: {
51
+ explicit?: string | null | undefined;
52
+ configLocale?: string | null | undefined;
53
+ i18nLocale?: string | null | undefined;
54
+ }): string;
55
+ /** 宿主翻译优先;缺失时按已解析 locale 选择内置中文或英文。 */
56
+ export declare function componentText(i18n: ComponentMessageSource, locale: string, key: ComponentMessageKey): string;
57
+ /** 星期一为一周起点的短星期标签,供日期面板使用。 */
58
+ export declare function weekdayLabels(locale: string): string[];
59
+ export declare function monthLabel(locale: string, year: number, monthIndex: number): string;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * 基础控件文案扩展点。
3
+ *
4
+ * 解析顺序:组件 `locale` 属性 > ConfigProvider.locale(仅当树中存在 Provider)> i18n.locale > `en`。
5
+ * 词条先走 i18n key;宿主没有该 key 时,回退到本目录的中文 / 英文表。
6
+ * 新的基础控件不要再写死 Previous、Clear 等英文,改为调用 `componentText`,并在下面的目录补一条词条。
7
+ */
8
+ export const componentMessageCatalog = {
9
+ 'datePicker.previous': { en: 'Previous', zh: '上一个' },
10
+ 'datePicker.next': { en: 'Next', zh: '下一个' },
11
+ 'datePicker.clear': { en: 'Clear', zh: '清除' },
12
+ 'datePicker.selectDate': { en: 'Select date', zh: '选择日期' },
13
+ 'datePicker.previousMonth': { en: 'Previous month', zh: '上个月' },
14
+ 'datePicker.nextMonth': { en: 'Next month', zh: '下个月' },
15
+ 'datePicker.previousYear': { en: 'Previous year', zh: '上一年' },
16
+ 'datePicker.nextYear': { en: 'Next year', zh: '下一年' },
17
+ 'datePicker.calendarDates': { en: 'Calendar dates', zh: '日历日期' },
18
+ };
19
+ export function resolveComponentLocale(input) {
20
+ const explicit = input.explicit?.trim();
21
+ if (explicit)
22
+ return explicit;
23
+ const configLocale = input.configLocale?.trim();
24
+ if (configLocale)
25
+ return configLocale;
26
+ const i18nLocale = input.i18nLocale?.trim();
27
+ if (i18nLocale)
28
+ return i18nLocale;
29
+ return 'en';
30
+ }
31
+ /** 宿主翻译优先;缺失时按已解析 locale 选择内置中文或英文。 */
32
+ export function componentText(i18n, locale, key) {
33
+ const translated = i18n.t(key);
34
+ if (translated !== key)
35
+ return translated;
36
+ const entry = componentMessageCatalog[key];
37
+ return locale.toLowerCase().startsWith('zh') ? entry.zh : entry.en;
38
+ }
39
+ /** 星期一为一周起点的短星期标签,供日期面板使用。 */
40
+ export function weekdayLabels(locale) {
41
+ const format = new Intl.DateTimeFormat(locale, { weekday: 'short' });
42
+ // 2024-01-01 是星期一。取正午,避免时区把日期拨到前一天。
43
+ return Array.from({ length: 7 }, (_, index) => format.format(new Date(2024, 0, 1 + index, 12)));
44
+ }
45
+ export function monthLabel(locale, year, monthIndex) {
46
+ return new Intl.DateTimeFormat(locale, { month: 'short' }).format(new Date(year, monthIndex, 1, 12));
47
+ }
@@ -0,0 +1,8 @@
1
+ <script lang="ts">
2
+ import { useComponentConfig } from './ConfigProvider.svelte';
3
+
4
+ const config = useComponentConfig();
5
+ const pageSize = $derived(config.pagination.pageSize);
6
+ </script>
7
+
8
+ <p data-testid="page-size">{pageSize ?? 'unset'}</p>
@@ -0,0 +1,3 @@
1
+ declare const ConfigProviderDefaults: import("svelte").Component<Record<string, never>, {}, "">;
2
+ type ConfigProviderDefaults = ReturnType<typeof ConfigProviderDefaults>;
3
+ export default ConfigProviderDefaults;
@@ -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,6 @@
1
+ type $$ComponentProps = {
2
+ label: string;
3
+ };
4
+ declare const NotificationScopeProbe: import("svelte").Component<$$ComponentProps, {}, "">;
5
+ type NotificationScopeProbe = ReturnType<typeof NotificationScopeProbe>;
6
+ export default NotificationScopeProbe;
@@ -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
- <AlertDialogPrimitive.Portal {...restProps} />
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 = "default",
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 size = $derived(sizeProp ?? (config.density === "compact" || config.componentSize === "sm" ? "sm" : config.componentSize === "lg" ? "lg" : "default"));
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
- <DialogPrimitive.Portal {...restProps} />
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.appendChild(node);
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
- <PopoverPrimitive.Portal>
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
- </PopoverPrimitive.Portal>
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
+ }
@@ -0,0 +1,42 @@
1
+ <script lang="ts">
2
+ import { Portal, type PortalProps } from 'bits-ui';
3
+ import { resolvePopupContainer, useComponentConfig } from '../ConfigProvider.svelte';
4
+ import { resolvePopupAnchor, type PopupAnchorKind } from './popup-anchor.js';
5
+
6
+ let {
7
+ to,
8
+ anchor = null,
9
+ kind,
10
+ children,
11
+ ...restProps
12
+ }: PortalProps & {
13
+ /** 调用方已知的触发器或定位锚点。优先传给 getPopupContainer。 */
14
+ anchor?: HTMLElement | null;
15
+ /** 用来把打开的触发器配到这一层浮层,避免串到别的弹层。 */
16
+ kind?: PopupAnchorKind;
17
+ } = $props();
18
+
19
+ const config = useComponentConfig();
20
+ // 留在源码树中的标记。触发器尚未绑定时,用它做 closest() 的回退节点。
21
+ let marker = $state<HTMLElement | null>(null);
22
+ const popupNode = $derived(resolvePopupAnchor(anchor, marker, kind));
23
+ const portalReady = $derived(to !== undefined || popupNode !== undefined || typeof document === 'undefined');
24
+ const target = $derived.by(() => {
25
+ if (to !== undefined) return to;
26
+ if (!portalReady) return undefined;
27
+ return resolvePopupContainer(config.getPopupContainer, popupNode);
28
+ });
29
+ </script>
30
+
31
+ <span bind:this={marker} class="svadmin-popup-anchor" data-svadmin-popup-anchor={kind ?? ''} aria-hidden="true"></span>
32
+ {#if portalReady}
33
+ {#if target}
34
+ <Portal to={target} {...restProps}>{@render children?.()}</Portal>
35
+ {:else}
36
+ <Portal {...restProps}>{@render children?.()}</Portal>
37
+ {/if}
38
+ {/if}
39
+
40
+ <style>
41
+ .svadmin-popup-anchor { display: contents; }
42
+ </style>
@@ -0,0 +1,11 @@
1
+ import { type PortalProps } from 'bits-ui';
2
+ import { type PopupAnchorKind } from './popup-anchor.js';
3
+ type $$ComponentProps = PortalProps & {
4
+ /** 调用方已知的触发器或定位锚点。优先传给 getPopupContainer。 */
5
+ anchor?: HTMLElement | null;
6
+ /** 用来把打开的触发器配到这一层浮层,避免串到别的弹层。 */
7
+ kind?: PopupAnchorKind;
8
+ };
9
+ declare const PopupPortal: import("svelte").Component<$$ComponentProps, {}, "">;
10
+ type PopupPortal = ReturnType<typeof PopupPortal>;
11
+ export default PopupPortal;
@@ -33,7 +33,7 @@
33
33
  <script lang="ts">
34
34
  /* eslint-disable svelte/no-unused-svelte-ignore */
35
35
  import { tick } from "svelte";
36
- import { Portal } from "bits-ui";
36
+ import PopupPortal from "../popup-portal.svelte";
37
37
  import { cn, type WithElementRef } from "../../../utils.js";
38
38
  import { useTranslation } from "@svadmin/core/i18n";
39
39
  import type { HTMLAttributes } from "svelte/elements";
@@ -163,7 +163,7 @@
163
163
  <svelte:document onkeydown={handleKeydown} />
164
164
 
165
165
  {#if open}
166
- <Portal>
166
+ <PopupPortal kind="sheet">
167
167
  <!-- Overlay -->
168
168
  <!-- svelte-ignore a11y_click_events_have_key_events a11y_no_static_element_interactions -->
169
169
  <div
@@ -196,5 +196,5 @@
196
196
  <span class="svadmin-sr-only">{closeLabel}</span>
197
197
  </button>
198
198
  </div>
199
- </Portal>
199
+ </PopupPortal>
200
200
  {/if}
@@ -197,6 +197,10 @@
197
197
  );
198
198
  const wrapperProps = $derived({ style: "display: contents" });
199
199
 
200
+ function htmlPopupAnchor(value: unknown): HTMLElement | null {
201
+ return typeof HTMLElement !== 'undefined' && value instanceof HTMLElement ? value : null;
202
+ }
203
+
200
204
  function getAnchor(): ReferenceElement | null {
201
205
  if (typeof customAnchor === "string") return document.querySelector(customAnchor);
202
206
  return customAnchor ?? tooltip.trigger;
@@ -306,7 +310,7 @@
306
310
  </script>
307
311
 
308
312
  {#if tooltip.shouldRender || forceMount}
309
- <TooltipPortal {...portalProps}>
313
+ <TooltipPortal {...portalProps} anchor={htmlPopupAnchor(customAnchor) ?? tooltip.trigger}>
310
314
  {#if child}
311
315
  {@render child({ props: contentProps, wrapperProps, open: tooltip.open })}
312
316
  {:else}
@@ -1,7 +1,11 @@
1
1
  <script lang="ts">
2
2
  import { Tooltip as TooltipPrimitive } from "bits-ui";
3
+ import PopupPortal from "../popup-portal.svelte";
3
4
 
4
- let { ...restProps }: TooltipPrimitive.PortalProps = $props();
5
+ let {
6
+ anchor = null,
7
+ ...restProps
8
+ }: TooltipPrimitive.PortalProps & { anchor?: HTMLElement | null } = $props();
5
9
  </script>
6
10
 
7
- <TooltipPrimitive.Portal {...restProps} />
11
+ <PopupPortal {...restProps} kind="tooltip" {anchor} />
@@ -1,4 +1,7 @@
1
1
  import { Tooltip as TooltipPrimitive } from "bits-ui";
2
- declare const TooltipPortal: import("svelte").Component<TooltipPrimitive.PortalProps, {}, "">;
2
+ type $$ComponentProps = TooltipPrimitive.PortalProps & {
3
+ anchor?: HTMLElement | null;
4
+ };
5
+ declare const TooltipPortal: import("svelte").Component<$$ComponentProps, {}, "">;
3
6
  type TooltipPortal = ReturnType<typeof TooltipPortal>;
4
7
  export default TooltipPortal;
@@ -0,0 +1,37 @@
1
+ export type DevtoolsInspectorKind = 'query' | 'mutation';
2
+ export interface DevtoolsInspectorRow {
3
+ id: string;
4
+ kind: DevtoolsInspectorKind;
5
+ provider: string;
6
+ resource: string;
7
+ operation: string;
8
+ status: string;
9
+ component: string;
10
+ trigger: string;
11
+ source: string;
12
+ requestSummary: string;
13
+ responseSummary: string;
14
+ }
15
+ export interface DevtoolsTimelineEntry {
16
+ at: number;
17
+ kind: DevtoolsInspectorKind;
18
+ resource: string;
19
+ operation: string;
20
+ status: string;
21
+ trigger: string;
22
+ source: string;
23
+ }
24
+ export interface DevtoolsInspectorFilter {
25
+ text: string;
26
+ kind: 'all' | DevtoolsInspectorKind;
27
+ resource: string;
28
+ status: string;
29
+ component: string;
30
+ }
31
+ /** Shape-only summary. String and number values are never copied into the result. */
32
+ export declare function redactValueSummary(value: unknown, depth?: number): string;
33
+ export declare function filterInspectorRows(rows: readonly DevtoolsInspectorRow[], filter: DevtoolsInspectorFilter): DevtoolsInspectorRow[];
34
+ export declare function componentSource(meta: unknown, observerCount: number): {
35
+ component: string;
36
+ source: string;
37
+ };