@celestia-island/hikari 0.40.0 → 0.40.1
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/package.json +1 -1
- package/src/components/HkAdaptiveDialog.tsx +2 -2
- package/src/components/HkCaptchaModal.tsx +2 -2
- package/src/components/HkLogWindow.tsx +2 -2
- package/src/components/HkModal.tsx +81 -2
- package/src/components/HkModal.width.test.tsx +114 -0
- package/src/components/HkProtocolModal.tsx +3 -3
- package/src/index.ts +5 -0
package/package.json
CHANGED
|
@@ -38,7 +38,7 @@ import { useBreakpoint } from "../runtime/useBreakpoint";
|
|
|
38
38
|
import "./HkAdaptiveDialog.scss";
|
|
39
39
|
import HButton from "./HkButton";
|
|
40
40
|
import HDrawer from "./HkDrawer";
|
|
41
|
-
import HModal, { type ModalAction } from "./HkModal";
|
|
41
|
+
import HModal, { type ModalAction, type ModalWidth } from "./HkModal";
|
|
42
42
|
|
|
43
43
|
export default defineComponent({
|
|
44
44
|
name: "HkAdaptiveDialog",
|
|
@@ -47,7 +47,7 @@ export default defineComponent({
|
|
|
47
47
|
title: { type: String, default: undefined },
|
|
48
48
|
closable: { type: Boolean, default: true },
|
|
49
49
|
/** Desktop modal width (HkModal `width`). */
|
|
50
|
-
width: { type: String
|
|
50
|
+
width: { type: String as PropType<ModalWidth>, default: "32rem" },
|
|
51
51
|
/** Mobile drawer height (HkDrawer `size`). HkDrawer's bottom-side CSS
|
|
52
52
|
* clamps panels at maxHeight 70vh, so the default equals the cap;
|
|
53
53
|
* pass `panelClass` with a custom max-height for taller sheets. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defineComponent, type PropType } from "vue";
|
|
2
|
-
import { HModal } from "@celestia-island/hikari";
|
|
2
|
+
import { HModal, type ModalWidth } from "@celestia-island/hikari";
|
|
3
3
|
|
|
4
4
|
import { useI18n } from "../i18n/context";
|
|
5
5
|
|
|
@@ -23,7 +23,7 @@ export const HkCaptchaModal = defineComponent({
|
|
|
23
23
|
scriptUrl: { type: String, default: undefined },
|
|
24
24
|
attempt: { type: Number, default: 0 },
|
|
25
25
|
title: { type: String, default: undefined },
|
|
26
|
-
width: { type: String
|
|
26
|
+
width: { type: String as PropType<ModalWidth>, default: "30rem" },
|
|
27
27
|
},
|
|
28
28
|
emits: {
|
|
29
29
|
"update:modelValue": (_v: boolean) => true,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { computed, defineComponent, nextTick, ref, watch, type PropType } from "vue";
|
|
2
2
|
import { Copy, Eraser, Pause, Play, ScrollText } from "lucide-vue-next";
|
|
3
|
-
import { HModal, HScrollContainer, useClipboard } from "@celestia-island/hikari";
|
|
3
|
+
import { HModal, HScrollContainer, useClipboard, type ModalWidth } from "@celestia-island/hikari";
|
|
4
4
|
|
|
5
5
|
import { useI18n } from "../i18n/context";
|
|
6
6
|
|
|
@@ -47,7 +47,7 @@ export const HkLogWindow = defineComponent({
|
|
|
47
47
|
/** Height of the log body (CSS length, e.g. "55vh"). */
|
|
48
48
|
height: { type: String, default: "55vh" },
|
|
49
49
|
title: { type: String, default: undefined },
|
|
50
|
-
width: { type: String
|
|
50
|
+
width: { type: String as PropType<ModalWidth>, default: "60rem" },
|
|
51
51
|
},
|
|
52
52
|
emits: {
|
|
53
53
|
"update:modelValue": (_v: boolean) => true,
|
|
@@ -34,6 +34,84 @@ export interface ModalAction {
|
|
|
34
34
|
onClick?: () => void;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Named width presets for the `width` prop. Callers kept reaching for
|
|
39
|
+
* semantic sizes (`width="sm"`) which, passed through as raw CSS
|
|
40
|
+
* `max-width`, are silently dropped by the browser — the frame fell back
|
|
41
|
+
* to `width: 100%` and spanned the whole viewport (chest #662-era change
|
|
42
|
+
* password modal rendered 2048px wide). Named values now resolve here;
|
|
43
|
+
* anything else keeps passing through as a CSS max-width value.
|
|
44
|
+
*/
|
|
45
|
+
export const MODAL_WIDTH_PRESETS = {
|
|
46
|
+
xs: "20rem",
|
|
47
|
+
sm: "32rem",
|
|
48
|
+
md: "40rem",
|
|
49
|
+
lg: "56rem",
|
|
50
|
+
xl: "72rem",
|
|
51
|
+
} as const;
|
|
52
|
+
|
|
53
|
+
export type ModalWidthPreset = keyof typeof MODAL_WIDTH_PRESETS;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `width` accepts a named preset or any CSS max-width value. The
|
|
57
|
+
* `(string & {})` arm keeps preset autocomplete in TSX while still
|
|
58
|
+
* allowing arbitrary lengths ("32rem", "560px", "50%").
|
|
59
|
+
*/
|
|
60
|
+
export type ModalWidth = ModalWidthPreset | (string & {});
|
|
61
|
+
|
|
62
|
+
const warnedWidths = new Set<string>();
|
|
63
|
+
/** Dev-only warn-once memory cap: a session feeding dynamically generated
|
|
64
|
+
* garbage widths must not grow the set forever. Prod never reaches this
|
|
65
|
+
* code (DEV is statically false there). */
|
|
66
|
+
const MAX_WARNED_WIDTHS = 50;
|
|
67
|
+
|
|
68
|
+
/** Valid digit-free CSS max-width keywords: the browser honors these, so
|
|
69
|
+
* the "would span the viewport" dev warn must stay silent for them. */
|
|
70
|
+
const WIDTH_KEYWORDS = new Set([
|
|
71
|
+
"auto",
|
|
72
|
+
"none",
|
|
73
|
+
"min-content",
|
|
74
|
+
"max-content",
|
|
75
|
+
"fit-content",
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Resolve the `width` prop to a concrete CSS max-width value: presets map
|
|
80
|
+
* through `MODAL_WIDTH_PRESETS`, everything else passes through untouched.
|
|
81
|
+
* In dev, a value that is neither a preset, a keyword, nor a length (no
|
|
82
|
+
* digit anywhere — e.g. a stray token like `"wide"`) warns once: as a raw
|
|
83
|
+
* max-width it would be dropped by the browser and the modal would span
|
|
84
|
+
* the viewport.
|
|
85
|
+
*/
|
|
86
|
+
export function resolveModalWidth(width: string): string {
|
|
87
|
+
// Coerce defensively: a JS consumer passing width={560} skips the prop
|
|
88
|
+
// type check at runtime and Vue would have rendered the number as px.
|
|
89
|
+
const value = String(width);
|
|
90
|
+
// hasOwnProperty.call instead of Object.hasOwn (ES2022): the package
|
|
91
|
+
// ships TS source, so consumers' bundlers will not polyfill new
|
|
92
|
+
// built-ins — this must not raise the runtime floor. The own-property
|
|
93
|
+
// guard is what keeps inherited members ("constructor", "toString")
|
|
94
|
+
// from resolving as presets past the dev warn.
|
|
95
|
+
const key = value.trim();
|
|
96
|
+
const preset = Object.prototype.hasOwnProperty.call(MODAL_WIDTH_PRESETS, key)
|
|
97
|
+
? MODAL_WIDTH_PRESETS[key as ModalWidthPreset]
|
|
98
|
+
: undefined;
|
|
99
|
+
if (preset !== undefined) return preset;
|
|
100
|
+
if (
|
|
101
|
+
import.meta.env?.DEV &&
|
|
102
|
+
!/\d/.test(value) &&
|
|
103
|
+
!WIDTH_KEYWORDS.has(key) &&
|
|
104
|
+
warnedWidths.size < MAX_WARNED_WIDTHS &&
|
|
105
|
+
!warnedWidths.has(value)
|
|
106
|
+
) {
|
|
107
|
+
warnedWidths.add(value);
|
|
108
|
+
console.warn(
|
|
109
|
+
`[HkModal] width "${value}" is neither a named preset (${Object.keys(MODAL_WIDTH_PRESETS).join("/")}) nor a CSS length — the browser would drop it and the modal would span the viewport.`,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
|
|
37
115
|
const FOCUSABLE_SELECTOR =
|
|
38
116
|
'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
39
117
|
|
|
@@ -44,7 +122,8 @@ export default defineComponent({
|
|
|
44
122
|
modelValue: { type: Boolean, required: true },
|
|
45
123
|
title: { type: String, default: undefined },
|
|
46
124
|
closable: { type: Boolean, default: true },
|
|
47
|
-
|
|
125
|
+
/** Named preset ("xs"…"xl") or any CSS max-width value (see ModalWidth). */
|
|
126
|
+
width: { type: String as PropType<ModalWidth>, default: "32rem" },
|
|
48
127
|
/**
|
|
49
128
|
* Escape hatch: extra class appended to the .hk-modal-content frame
|
|
50
129
|
* so hosts can restyle the surface without forking the modal (e.g.
|
|
@@ -114,7 +193,7 @@ export default defineComponent({
|
|
|
114
193
|
|
|
115
194
|
const overlayZ = computed(() => handle.value?.zIndex ?? 0);
|
|
116
195
|
const contentZ = computed(() => (handle.value?.zIndex ?? 0) + 1);
|
|
117
|
-
const resolvedWidth = computed(() => props.width);
|
|
196
|
+
const resolvedWidth = computed(() => resolveModalWidth(props.width));
|
|
118
197
|
// Layer/dialog name: the explicit surface name wins over the header
|
|
119
198
|
// title (which header-less surfaces never pass).
|
|
120
199
|
const resolvedSurfaceName = computed(() => props.surfaceTitle ?? props.title);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createApp, defineComponent, h, nextTick, ref } from "vue";
|
|
3
|
+
|
|
4
|
+
import HkModal, { MODAL_WIDTH_PRESETS, resolveModalWidth } from "./HkModal";
|
|
5
|
+
|
|
6
|
+
const mounts: ReturnType<typeof createApp>[] = [];
|
|
7
|
+
const containers: HTMLElement[] = [];
|
|
8
|
+
|
|
9
|
+
afterEach(async () => {
|
|
10
|
+
for (const app of mounts.splice(0)) app.unmount();
|
|
11
|
+
for (const el of containers.splice(0)) el.remove();
|
|
12
|
+
vi.restoreAllMocks();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("resolveModalWidth", () => {
|
|
16
|
+
it("maps every named preset to its CSS max-width value", () => {
|
|
17
|
+
for (const [name, css] of Object.entries(MODAL_WIDTH_PRESETS)) {
|
|
18
|
+
expect(resolveModalWidth(name)).toBe(css);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("keeps the default-sized presets bounded and ordered", () => {
|
|
23
|
+
// Sanity on the scale itself: xs < sm(default) < md < lg < xl.
|
|
24
|
+
expect(MODAL_WIDTH_PRESETS.xs).toBe("20rem");
|
|
25
|
+
expect(MODAL_WIDTH_PRESETS.sm).toBe("32rem");
|
|
26
|
+
expect(MODAL_WIDTH_PRESETS.md).toBe("40rem");
|
|
27
|
+
expect(MODAL_WIDTH_PRESETS.lg).toBe("56rem");
|
|
28
|
+
expect(MODAL_WIDTH_PRESETS.xl).toBe("72rem");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("passes arbitrary CSS max-width values through untouched", () => {
|
|
32
|
+
expect(resolveModalWidth("32rem")).toBe("32rem");
|
|
33
|
+
expect(resolveModalWidth("560px")).toBe("560px");
|
|
34
|
+
expect(resolveModalWidth("50%")).toBe("50%");
|
|
35
|
+
expect(resolveModalWidth("min(90vw, 48rem)")).toBe("min(90vw, 48rem)");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("passes digit-free max-width keywords through without warning", () => {
|
|
39
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
40
|
+
for (const keyword of ["auto", "none", "min-content", "max-content", "fit-content"]) {
|
|
41
|
+
expect(resolveModalWidth(keyword)).toBe(keyword);
|
|
42
|
+
}
|
|
43
|
+
expect(warn).not.toHaveBeenCalled();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("coerces a numeric width from JS consumers instead of throwing", () => {
|
|
47
|
+
expect(resolveModalWidth(560 as unknown as string)).toBe("560");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("does not inherit Object.prototype members as presets", () => {
|
|
51
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
52
|
+
// A bare index lookup would resolve "constructor"/"toString" to
|
|
53
|
+
// inherited functions and early-return them past the warn — the
|
|
54
|
+
// guard must treat them as unknown values instead.
|
|
55
|
+
for (const hostile of ["constructor", "toString", "valueOf"]) {
|
|
56
|
+
expect(resolveModalWidth(hostile)).toBe(hostile);
|
|
57
|
+
}
|
|
58
|
+
expect(warn).toHaveBeenCalledTimes(3);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("trims surrounding whitespace before the preset lookup", () => {
|
|
62
|
+
expect(resolveModalWidth(" sm ")).toBe("32rem");
|
|
63
|
+
expect(resolveModalWidth(" 560px ")).toBe(" 560px ");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("warns once in dev for a value that is neither a preset nor a length", () => {
|
|
67
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
68
|
+
// A unique digit-free token per run: the warn-once set is module
|
|
69
|
+
// state, and any digit would classify the value as a length.
|
|
70
|
+
const stray = `wide-${Math.random().toString(36).slice(2).replace(/\d/g, "") || "wide"}`;
|
|
71
|
+
expect(resolveModalWidth(stray)).toBe(stray);
|
|
72
|
+
expect(resolveModalWidth(stray)).toBe(stray);
|
|
73
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
74
|
+
expect(warn.mock.calls[0]?.[0]).toContain(stray);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe("HkModal width rendering", () => {
|
|
79
|
+
async function mountModal(width: string): Promise<HTMLElement> {
|
|
80
|
+
const container = document.createElement("div");
|
|
81
|
+
document.body.appendChild(container);
|
|
82
|
+
containers.push(container);
|
|
83
|
+
|
|
84
|
+
const open = ref(true);
|
|
85
|
+
const Wrapper = defineComponent({
|
|
86
|
+
setup() {
|
|
87
|
+
return () =>
|
|
88
|
+
h(HkModal, {
|
|
89
|
+
modelValue: open.value,
|
|
90
|
+
width,
|
|
91
|
+
}, { default: () => h("div", "content") });
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
const app = createApp(Wrapper);
|
|
95
|
+
mounts.push(app);
|
|
96
|
+
app.mount(container);
|
|
97
|
+
await nextTick();
|
|
98
|
+
|
|
99
|
+
// HkModal Teleports to document.body — the mount container stays empty.
|
|
100
|
+
const content = document.body.querySelector<HTMLElement>(".hk-modal-content");
|
|
101
|
+
expect(content).toBeTruthy();
|
|
102
|
+
return content!;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
it("renders a named preset as the frame's max-width", async () => {
|
|
106
|
+
const content = await mountModal("sm");
|
|
107
|
+
expect(content.style.maxWidth).toBe("32rem");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("renders an arbitrary CSS length as the frame's max-width", async () => {
|
|
111
|
+
const content = await mountModal("560px");
|
|
112
|
+
expect(content.style.maxWidth).toBe("560px");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { computed, defineComponent, onBeforeUnmount, ref, watch } from "vue";
|
|
2
|
-
import { HMarkdownRenderer, HModal, useClipboard, type ModalAction } from "@celestia-island/hikari";
|
|
1
|
+
import { computed, defineComponent, onBeforeUnmount, ref, watch, type PropType } from "vue";
|
|
2
|
+
import { HMarkdownRenderer, HModal, useClipboard, type ModalAction, type ModalWidth } from "@celestia-island/hikari";
|
|
3
3
|
|
|
4
4
|
import { useI18n } from "../i18n/context";
|
|
5
5
|
import { attachOverlayScrollbars, type OverlayScrollbarHandle } from "../composables/useOverlayScrollbar";
|
|
@@ -29,7 +29,7 @@ export const HkProtocolModal = defineComponent({
|
|
|
29
29
|
declineLabel: { type: String, default: undefined },
|
|
30
30
|
/** Allow dismissing without a decision (overlay/ESC/X). Default true. */
|
|
31
31
|
closable: { type: Boolean, default: true },
|
|
32
|
-
width: { type: String
|
|
32
|
+
width: { type: String as PropType<ModalWidth>, default: "48rem" },
|
|
33
33
|
/** Cap the scroll body height (e.g. "60vh"). */
|
|
34
34
|
bodyHeight: { type: String, default: undefined },
|
|
35
35
|
},
|
package/src/index.ts
CHANGED
|
@@ -115,6 +115,11 @@ export { type BoardNodeInput, type BoardEdgeInput } from "./components/HkBoard";
|
|
|
115
115
|
export { type BoardAnchorMode, type BoardEdgeStyle, type BoardPoint } from "./utils/boardEdges";
|
|
116
116
|
export { type BoardCamera, type BoardRect, type BoardViewport } from "./utils/boardCamera";
|
|
117
117
|
export { type ModalAction } from "./components/HkModal";
|
|
118
|
+
export {
|
|
119
|
+
type ModalWidth,
|
|
120
|
+
type ModalWidthPreset,
|
|
121
|
+
resolveModalWidth,
|
|
122
|
+
} from "./components/HkModal";
|
|
118
123
|
export { type TreeNode, type TreeSize, type TreeRowScope } from "./components/HkTree";
|
|
119
124
|
export { type DragListItem } from "./components/HkDraggableList";
|
|
120
125
|
export { type GridItem } from "./components/HkDraggableGrid";
|