numori-ui 0.4.0 → 0.6.0

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 (99) hide show
  1. package/CHANGELOG.md +120 -0
  2. package/README.md +27 -20
  3. package/dist/components/AppShell.js +234 -0
  4. package/dist/components/AppShell.js.map +1 -0
  5. package/dist/components/Avatar.js +71 -19
  6. package/dist/components/Avatar.js.map +1 -1
  7. package/dist/components/Checkbox.js +11 -2
  8. package/dist/components/Checkbox.js.map +1 -1
  9. package/dist/components/CopyField.js +195 -0
  10. package/dist/components/CopyField.js.map +1 -0
  11. package/dist/components/DescriptionList.js +116 -0
  12. package/dist/components/DescriptionList.js.map +1 -0
  13. package/dist/components/Dropdown.js +3 -1
  14. package/dist/components/Dropdown.js.map +1 -1
  15. package/dist/components/EmptyState.js +123 -0
  16. package/dist/components/EmptyState.js.map +1 -0
  17. package/dist/components/FormField.js +43 -13
  18. package/dist/components/FormField.js.map +1 -1
  19. package/dist/components/Input.js +15 -1
  20. package/dist/components/Input.js.map +1 -1
  21. package/dist/components/Modal.js +61 -4
  22. package/dist/components/Modal.js.map +1 -1
  23. package/dist/components/PageHeader.js +106 -0
  24. package/dist/components/PageHeader.js.map +1 -0
  25. package/dist/components/Pagination.js +236 -0
  26. package/dist/components/Pagination.js.map +1 -0
  27. package/dist/components/Prompt.js +11 -6
  28. package/dist/components/Prompt.js.map +1 -1
  29. package/dist/components/Segmented.js +10 -2
  30. package/dist/components/Segmented.js.map +1 -1
  31. package/dist/components/Select.js +11 -2
  32. package/dist/components/Select.js.map +1 -1
  33. package/dist/components/Sidebar.js +69 -0
  34. package/dist/components/Sidebar.js.map +1 -0
  35. package/dist/components/SidebarGroup.js +50 -0
  36. package/dist/components/SidebarGroup.js.map +1 -0
  37. package/dist/components/SidebarItem.js +167 -0
  38. package/dist/components/SidebarItem.js.map +1 -0
  39. package/dist/components/Skeleton.js +122 -0
  40. package/dist/components/Skeleton.js.map +1 -0
  41. package/dist/components/SplitPane.js +264 -0
  42. package/dist/components/SplitPane.js.map +1 -0
  43. package/dist/components/StatCard.js +185 -0
  44. package/dist/components/StatCard.js.map +1 -0
  45. package/dist/components/Table.js +182 -50
  46. package/dist/components/Table.js.map +1 -1
  47. package/dist/components/Tabs.js +216 -0
  48. package/dist/components/Tabs.js.map +1 -0
  49. package/dist/components/Toggle.js +11 -2
  50. package/dist/components/Toggle.js.map +1 -1
  51. package/dist/components/Tree.js +417 -0
  52. package/dist/components/Tree.js.map +1 -0
  53. package/dist/components/VirtualList.js +292 -0
  54. package/dist/components/VirtualList.js.map +1 -0
  55. package/dist/components/index.js +105 -81
  56. package/dist/components/index.js.map +1 -1
  57. package/dist/icons/mdi.js +12 -0
  58. package/dist/icons/mdi.js.map +1 -1
  59. package/dist/index.d.ts +16 -1
  60. package/dist/index.js +54 -39
  61. package/dist/index.js.map +1 -1
  62. package/dist/internal/focusTrap.js +163 -0
  63. package/dist/internal/focusTrap.js.map +1 -0
  64. package/dist/internal/formField.js +26 -0
  65. package/dist/internal/formField.js.map +1 -0
  66. package/dist/manifest.js +16 -1
  67. package/dist/manifest.js.map +1 -1
  68. package/package.json +1 -1
  69. package/src/components/AppShell.vue +231 -0
  70. package/src/components/Avatar.vue +69 -18
  71. package/src/components/Checkbox.vue +5 -0
  72. package/src/components/CopyField.vue +178 -0
  73. package/src/components/DescriptionList.vue +114 -0
  74. package/src/components/Dropdown.vue +4 -1
  75. package/src/components/EmptyState.vue +107 -0
  76. package/src/components/FormField.vue +37 -4
  77. package/src/components/Input.vue +9 -0
  78. package/src/components/Modal.vue +51 -3
  79. package/src/components/PageHeader.vue +102 -0
  80. package/src/components/Pagination.vue +199 -0
  81. package/src/components/Prompt.vue +14 -3
  82. package/src/components/Segmented.vue +5 -0
  83. package/src/components/Select.vue +6 -1
  84. package/src/components/Sidebar.vue +61 -0
  85. package/src/components/SidebarGroup.vue +43 -0
  86. package/src/components/SidebarItem.vue +159 -0
  87. package/src/components/Skeleton.vue +91 -0
  88. package/src/components/SplitPane.vue +264 -0
  89. package/src/components/StatCard.vue +165 -0
  90. package/src/components/Table.vue +148 -18
  91. package/src/components/Tabs.vue +217 -0
  92. package/src/components/Toggle.vue +5 -0
  93. package/src/components/Tree.vue +428 -0
  94. package/src/components/VirtualList.vue +250 -0
  95. package/src/components/index.js +45 -6
  96. package/src/icons/mdi.js +12 -0
  97. package/src/internal/focusTrap.js +185 -0
  98. package/src/internal/formField.js +23 -0
  99. package/src/manifest.js +15 -0
@@ -0,0 +1,231 @@
1
+ <template>
2
+ <div class="min-h-dvh bg-gray-50 text-gray-900 dark:bg-gray-950 dark:text-gray-100">
3
+ <!-- Skip link: the first tab stop jumps past the sidebar to the content. -->
4
+ <a
5
+ :href="`#${mainId}`"
6
+ class="sr-only focus:not-sr-only focus:fixed focus:top-3 focus:left-3 focus:z-[60] focus:rounded-lg focus:bg-white focus:px-3 focus:py-2 focus:text-sm focus:font-medium focus:shadow-card dark:focus:bg-gray-900"
7
+ >{{ skipLabel }}</a
8
+ >
9
+
10
+ <!-- Backdrop behind the off-canvas sidebar on small screens -->
11
+ <Transition
12
+ enter-active-class="transition-opacity duration-200 motion-reduce:transition-none"
13
+ enter-from-class="opacity-0"
14
+ leave-active-class="transition-opacity duration-200 motion-reduce:transition-none"
15
+ leave-to-class="opacity-0"
16
+ >
17
+ <div
18
+ v-if="hasSidebar && isOpen"
19
+ class="fixed inset-0 z-40 bg-gray-950/50 backdrop-blur-sm lg:hidden"
20
+ aria-hidden="true"
21
+ @click="close"
22
+ />
23
+ </Transition>
24
+
25
+ <aside
26
+ v-if="hasSidebar"
27
+ :id="sidebarId"
28
+ class="fixed inset-y-0 left-0 z-50 flex flex-col border-r border-gray-200 bg-white transition-transform duration-200 ease-out motion-reduce:transition-none lg:translate-x-0 dark:border-gray-800 dark:bg-gray-900"
29
+ :class="[widthClass, isOpen ? 'translate-x-0' : '-translate-x-full']"
30
+ :inert="!isDesktop && !isOpen ? true : undefined"
31
+ >
32
+ <slot name="sidebar" :close="close" />
33
+ </aside>
34
+
35
+ <div class="flex min-h-dvh flex-col" :class="hasSidebar ? offsetClass : ''">
36
+ <header
37
+ v-if="$slots.header || hasSidebar"
38
+ class="sticky top-0 z-30 flex h-14 shrink-0 items-center gap-3 border-b border-gray-200 bg-white/80 px-4 backdrop-blur sm:px-6 dark:border-gray-800 dark:bg-gray-900/80"
39
+ >
40
+ <button
41
+ v-if="hasSidebar"
42
+ type="button"
43
+ class="-ml-1 inline-flex size-9 items-center justify-center rounded-lg text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-900 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary-500 lg:hidden dark:text-gray-400 dark:hover:bg-gray-800 dark:hover:text-gray-100"
44
+ :aria-label="menuLabel"
45
+ :aria-expanded="isOpen ? 'true' : 'false'"
46
+ :aria-controls="sidebarId"
47
+ @click="toggle"
48
+ >
49
+ <UiIcon name="mdi:menu" class="size-5" />
50
+ </button>
51
+ <div class="flex min-w-0 flex-1 items-center gap-3">
52
+ <slot name="header" :toggle="toggle" :open="isOpen" />
53
+ </div>
54
+ </header>
55
+
56
+ <main :id="mainId" tabindex="-1" class="flex-1 focus:outline-none">
57
+ <div :class="contentClass">
58
+ <slot />
59
+ </div>
60
+ </main>
61
+
62
+ <footer
63
+ v-if="$slots.footer"
64
+ class="border-t border-gray-200 px-4 py-4 text-sm text-gray-500 sm:px-6 dark:border-gray-800 dark:text-gray-400"
65
+ >
66
+ <slot name="footer" />
67
+ </footer>
68
+ </div>
69
+ </div>
70
+ </template>
71
+
72
+ <script setup>
73
+ /**
74
+ * UiAppShell — the page frame for an application: a sidebar, a sticky top bar
75
+ * and a scrolling content area.
76
+ *
77
+ * On large screens (`lg` and up) the sidebar is pinned to the left and the
78
+ * content is offset beside it. Below that it becomes an off-canvas drawer,
79
+ * opened by a menu button the shell adds to the top bar, closed by the
80
+ * backdrop, by Escape, or by any `UiSidebarItem` being chosen. While the drawer
81
+ * is closed on a small screen the sidebar is `inert`, so keyboard users don't
82
+ * tab into links they can't see.
83
+ *
84
+ * The shell owns layout only. Put a `UiSidebar` in `#sidebar`, page chrome in
85
+ * `#header`, and the routed page in the default slot. A skip link to the main
86
+ * content is the first tab stop.
87
+ *
88
+ * The drawer state is uncontrolled by default; bind `v-model:open` to drive it
89
+ * yourself (e.g. to close it on route change).
90
+ *
91
+ * @example
92
+ * <UiAppShell>
93
+ * <template #sidebar>
94
+ * <UiSidebar label="Main">
95
+ * <template #header><strong>Numori CRDT</strong></template>
96
+ * <UiSidebarItem icon="mdi:view-grid" href="/" active>Dashboard</UiSidebarItem>
97
+ * <UiSidebarItem icon="mdi:sync" href="/apps">Apps</UiSidebarItem>
98
+ * </UiSidebar>
99
+ * </template>
100
+ * <template #header>
101
+ * <span class="flex-1" />
102
+ * <UiThemeToggle :is-dark="dark" @toggle="dark = !dark" />
103
+ * </template>
104
+ * <UiPageHeader title="Dashboard" />
105
+ * </UiAppShell>
106
+ *
107
+ * @emits {boolean} update:open — The drawer opened or closed (small screens).
108
+ *
109
+ * @slot sidebar — Sidebar content, usually a `UiSidebar`. Scope: `{ close }`.
110
+ * @slot header — Top bar content beside the menu button. Scope: `{ toggle, open }`.
111
+ * @slot default — Page content.
112
+ * @slot footer — Optional footer below the content.
113
+ */
114
+ import { computed, onBeforeUnmount, onMounted, provide, ref, useId, useSlots } from 'vue'
115
+ import UiIcon from '../icons/Icon.vue'
116
+
117
+ const props = defineProps({
118
+ /**
119
+ * Drawer state on small screens, for `v-model:open`. Leave unset to let the
120
+ * shell manage it.
121
+ * @type {boolean}
122
+ * @default undefined
123
+ */
124
+ open: { type: Boolean, default: undefined },
125
+
126
+ /**
127
+ * Sidebar width.
128
+ * @type {string}
129
+ * @default 'md'
130
+ * @values 'sm' | 'md' | 'lg'
131
+ */
132
+ sidebarWidth: { type: String, default: 'md' },
133
+
134
+ /**
135
+ * Maximum width of the content column. `'none'` lets it fill the space.
136
+ * @type {string}
137
+ * @default '7xl'
138
+ * @values 'none' | '4xl' | '5xl' | '6xl' | '7xl'
139
+ */
140
+ maxWidth: { type: String, default: '7xl' },
141
+
142
+ /**
143
+ * Pad the content area. Turn off for full-bleed pages that lay out their own
144
+ * spacing.
145
+ * @type {boolean}
146
+ * @default true
147
+ */
148
+ padded: { type: Boolean, default: true },
149
+
150
+ /**
151
+ * Accessible label for the menu button that opens the drawer.
152
+ * @type {string}
153
+ * @default 'Open navigation'
154
+ */
155
+ menuLabel: { type: String, default: 'Open navigation' },
156
+
157
+ /**
158
+ * Text of the skip link (visible only when focused).
159
+ * @type {string}
160
+ * @default 'Skip to content'
161
+ */
162
+ skipLabel: { type: String, default: 'Skip to content' },
163
+ })
164
+
165
+ const emit = defineEmits(['update:open'])
166
+ const slots = useSlots()
167
+
168
+ const baseId = useId()
169
+ const sidebarId = `${baseId}-sidebar`
170
+ const mainId = `${baseId}-main`
171
+
172
+ const hasSidebar = computed(() => !!slots.sidebar)
173
+
174
+ const internalOpen = ref(false)
175
+ const isOpen = computed(() => props.open ?? internalOpen.value)
176
+
177
+ function setOpen(value) {
178
+ internalOpen.value = value
179
+ emit('update:open', value)
180
+ }
181
+ const toggle = () => setOpen(!isOpen.value)
182
+ const close = () => {
183
+ if (isOpen.value) setOpen(false)
184
+ }
185
+
186
+ // UiSidebarItem closes the drawer when chosen, so navigation on a phone does
187
+ // not leave the drawer covering the page it just opened.
188
+ provide('numori-ui:app-shell', { close })
189
+
190
+ // Assume a desktop layout until mounted, so the server render never marks the
191
+ // sidebar inert on a screen where it is visible.
192
+ const isDesktop = ref(true)
193
+ let media = null
194
+ const onMedia = () => {
195
+ isDesktop.value = media.matches
196
+ if (media.matches) close()
197
+ }
198
+ const onKeydown = (event) => {
199
+ if (event.key === 'Escape' && isOpen.value) close()
200
+ }
201
+
202
+ onMounted(() => {
203
+ media = window.matchMedia('(min-width: 64rem)')
204
+ onMedia()
205
+ media.addEventListener('change', onMedia)
206
+ window.addEventListener('keydown', onKeydown)
207
+ })
208
+ onBeforeUnmount(() => {
209
+ media?.removeEventListener('change', onMedia)
210
+ window.removeEventListener('keydown', onKeydown)
211
+ })
212
+
213
+ const widthClass = computed(
214
+ () => ({ sm: 'w-60', md: 'w-64', lg: 'w-72' })[props.sidebarWidth] ?? 'w-64',
215
+ )
216
+ const offsetClass = computed(
217
+ () => ({ sm: 'lg:pl-60', md: 'lg:pl-64', lg: 'lg:pl-72' })[props.sidebarWidth] ?? 'lg:pl-64',
218
+ )
219
+
220
+ const contentClass = computed(() => {
221
+ const width =
222
+ {
223
+ none: '',
224
+ '4xl': 'mx-auto max-w-4xl',
225
+ '5xl': 'mx-auto max-w-5xl',
226
+ '6xl': 'mx-auto max-w-6xl',
227
+ '7xl': 'mx-auto max-w-7xl',
228
+ }[props.maxWidth] ?? 'mx-auto max-w-7xl'
229
+ return [width, props.padded ? 'px-4 py-6 sm:px-6 lg:px-8 lg:py-8' : ''].filter(Boolean).join(' ')
230
+ })
231
+ </script>
@@ -1,48 +1,80 @@
1
1
  <template>
2
- <!-- Circular avatar container with image or fallback icon -->
2
+ <!-- Circular avatar container with an image, initials or a fallback icon -->
3
3
  <div
4
4
  class="rounded-full flex items-center justify-center overflow-hidden shrink-0"
5
5
  :class="[sizeClass, bgClass, ringClass]"
6
+ :role="showImage ? undefined : 'img'"
7
+ :aria-label="showImage ? undefined : alt"
6
8
  >
7
- <img v-if="src" :src="src" :alt="alt" class="w-full h-full object-cover" />
8
- <UiIcon v-else :name="fallbackIcon" :class="iconSizeClass" />
9
+ <img
10
+ v-if="showImage"
11
+ ref="imgRef"
12
+ :src="src"
13
+ :alt="alt"
14
+ class="w-full h-full object-cover"
15
+ @error="failed = true"
16
+ />
17
+ <span
18
+ v-else-if="shownInitials"
19
+ class="font-semibold leading-none tracking-tight select-none"
20
+ :class="textSizeClass"
21
+ aria-hidden="true"
22
+ >{{ shownInitials }}</span
23
+ >
24
+ <UiIcon v-else :name="fallbackIcon" :class="iconSizeClass" aria-hidden="true" />
9
25
  </div>
10
26
  </template>
11
27
 
12
28
  <script setup>
13
29
  /**
14
- * UiAvatar — Circular avatar component displaying an image or a fallback icon.
30
+ * UiAvatar — Circular avatar component displaying an image, initials or a fallback icon.
15
31
  *
16
- * When a `src` URL is provided, renders a cover-fit image. Otherwise, displays
17
- * a configurable fallback icon on a colored background. Supports five sizes,
18
- * two color themes for the fallback state, and an optional ring border.
32
+ * When a `src` URL is provided, renders a cover-fit image; if the image fails
33
+ * to load, it falls back rather than showing a broken-image glyph. Without an
34
+ * image it shows `initials` (at most two characters, upper-cased), and without
35
+ * those a configurable fallback icon, on a colored background. Supports five
36
+ * sizes, two color themes for the fallback state, and an optional ring border.
37
+ *
38
+ * Without an image the avatar is exposed as `role="img"` named by `alt`, so
39
+ * pass the person's name there rather than the default.
19
40
  *
20
41
  * @example Image avatar
21
42
  * <UiAvatar src="/img/user.jpg" alt="Jane Doe" size="lg" />
22
43
  *
44
+ * @example Initials
45
+ * <UiAvatar initials="JD" alt="Jane Doe" />
46
+ *
23
47
  * @example Fallback icon avatar with ring
24
48
  * <UiAvatar size="md" color="primary" ring />
25
49
  *
26
50
  * @example Small gray avatar with custom icon
27
51
  * <UiAvatar size="sm" color="gray" fallback-icon="mdi:account-group" />
28
52
  */
29
- import { computed } from 'vue'
53
+ import { computed, onMounted, ref, watch } from 'vue'
30
54
  import UiIcon from '../icons/Icon.vue'
55
+
31
56
  const props = defineProps({
32
57
  /**
33
- * Image source URL. When empty, the fallback icon is shown.
58
+ * Image source URL. When empty (or it fails to load), initials or the
59
+ * fallback icon are shown.
34
60
  * @type {string}
35
61
  * @default ''
36
62
  */
37
63
  src: { type: String, default: '' },
38
-
39
64
  /**
40
- * Alt text for the avatar image (accessibility).
65
+ * Alt text for the avatar image (accessibility), and the accessible name
66
+ * when initials or the icon are shown.
41
67
  * @type {string}
42
68
  * @default 'Avatar'
43
69
  */
44
70
  alt: { type: String, default: 'Avatar' },
45
-
71
+ /**
72
+ * Initials shown when there is no image. Trimmed to two characters and
73
+ * upper-cased.
74
+ * @type {string}
75
+ * @default ''
76
+ */
77
+ initials: { type: String, default: '' },
46
78
  /**
47
79
  * Avatar size controlling width, height, and icon dimensions.
48
80
  * @type {string}
@@ -50,14 +82,12 @@ const props = defineProps({
50
82
  * @values 'xs' (24px) | 'sm' (32px) | 'md' (36px) | 'lg' (48px) | 'xl' (64px)
51
83
  */
52
84
  size: { type: String, default: 'md' },
53
-
54
85
  /**
55
- * MDI icon name used when no image src is provided.
86
+ * MDI icon name used when there is neither an image nor initials.
56
87
  * @type {string}
57
88
  * @default 'mdi:account'
58
89
  */
59
90
  fallbackIcon: { type: String, default: 'mdi:account' },
60
-
61
91
  /**
62
92
  * Background color theme for the fallback icon state.
63
93
  * @type {string}
@@ -65,7 +95,6 @@ const props = defineProps({
65
95
  * @values 'primary' | 'gray'
66
96
  */
67
97
  color: { type: String, default: 'primary' },
68
-
69
98
  /**
70
99
  * Show a ring border around the avatar for emphasis.
71
100
  * @type {boolean}
@@ -74,6 +103,23 @@ const props = defineProps({
74
103
  ring: { type: Boolean, default: false },
75
104
  })
76
105
 
106
+ // A failed load falls back to initials or the icon; a new src retries.
107
+ const failed = ref(false)
108
+ const imgRef = ref(null)
109
+ watch(
110
+ () => props.src,
111
+ () => (failed.value = false),
112
+ )
113
+ // Server-rendered images can fail before hydration attaches `@error`, so the
114
+ // event is never seen. Check once on mount for an image that already failed.
115
+ onMounted(() => {
116
+ const img = imgRef.value
117
+ if (img && img.complete && img.naturalWidth === 0) failed.value = true
118
+ })
119
+ const showImage = computed(() => !!props.src && !failed.value)
120
+
121
+ const shownInitials = computed(() => [...props.initials.trim()].slice(0, 2).join('').toUpperCase())
122
+
77
123
  const sizeClass = computed(() => {
78
124
  const map = { xs: 'w-6 h-6', sm: 'w-8 h-8', md: 'w-9 h-9', lg: 'w-12 h-12', xl: 'w-16 h-16' }
79
125
  return map[props.size] || map.md
@@ -84,9 +130,14 @@ const iconSizeClass = computed(() => {
84
130
  return map[props.size] || map.md
85
131
  })
86
132
 
87
- // Only apply background color when showing the fallback icon (not when an image is loaded)
133
+ const textSizeClass = computed(() => {
134
+ const map = { xs: 'text-[10px]', sm: 'text-xs', md: 'text-sm', lg: 'text-base', xl: 'text-xl' }
135
+ return map[props.size] || map.md
136
+ })
137
+
138
+ // Only apply background color when not showing an image
88
139
  const bgClass = computed(() => {
89
- if (props.src) return ''
140
+ if (showImage.value) return ''
90
141
  const map = {
91
142
  primary: 'bg-primary-100 dark:bg-primary-900/30 text-primary-600 dark:text-primary-400',
92
143
  gray: 'bg-gray-200 dark:bg-gray-700 text-gray-500 dark:text-gray-400',
@@ -1,6 +1,8 @@
1
1
  <template>
2
2
  <input
3
+ :id="field?.controlId.value"
3
4
  type="checkbox"
5
+ :aria-describedby="field?.describedBy.value"
4
6
  :checked="modelValue ?? checked"
5
7
  :disabled="disabled"
6
8
  class="rounded border-gray-300 dark:border-gray-600 text-primary-600 focus:ring-primary-500"
@@ -31,6 +33,7 @@
31
33
  */
32
34
  import { computed } from 'vue'
33
35
  import { checkableSizeClass } from '../internal/controlSizes.js'
36
+ import { useFormField } from '../internal/formField.js'
34
37
  const props = defineProps({
35
38
  /**
36
39
  * Controlled checked state (v-model). Takes priority over `checked` when defined.
@@ -64,6 +67,8 @@ const props = defineProps({
64
67
 
65
68
  const emit = defineEmits(['update:modelValue', 'change'])
66
69
 
70
+ const field = useFormField()
71
+
67
72
  const sizeClass = computed(() => checkableSizeClass(props.size))
68
73
 
69
74
  function onChange(e) {
@@ -0,0 +1,178 @@
1
+ <template>
2
+ <div :class="mergedClasses" v-bind="restAttrs">
3
+ <span
4
+ class="min-w-0 flex-1 px-3 text-gray-800 select-all dark:text-gray-200"
5
+ :class="[
6
+ size === 'sm' ? 'py-1.5 text-xs' : 'py-2 text-sm',
7
+ mono ? 'font-mono' : '',
8
+ wrap ? 'break-all whitespace-pre-wrap' : 'truncate',
9
+ masked ? 'tracking-widest text-gray-500 select-none dark:text-gray-400' : '',
10
+ ]"
11
+ :aria-label="masked ? hiddenLabel : undefined"
12
+ >{{ masked ? mask : value }}</span
13
+ >
14
+
15
+ <button
16
+ v-if="secret"
17
+ type="button"
18
+ :class="buttonClass"
19
+ :aria-label="masked ? revealLabel : hideLabel"
20
+ :aria-pressed="masked ? 'false' : 'true'"
21
+ @click="toggleReveal"
22
+ >
23
+ <UiIcon :name="masked ? 'mdi:eye-outline' : 'mdi:eye-off-outline'" class="size-4" />
24
+ </button>
25
+
26
+ <button
27
+ type="button"
28
+ :class="buttonClass"
29
+ :aria-label="copied ? copiedLabel : copyLabel"
30
+ @click="copy"
31
+ >
32
+ <UiIcon
33
+ :name="copied ? 'mdi:check' : 'mdi:content-copy'"
34
+ class="size-4"
35
+ :class="copied ? 'text-success-600 dark:text-success-400' : ''"
36
+ />
37
+ <span v-if="showCopyText" class="hidden sm:inline">{{
38
+ copied ? copiedLabel : copyLabel
39
+ }}</span>
40
+ </button>
41
+ </div>
42
+ </template>
43
+
44
+ <script setup>
45
+ /**
46
+ * UiCopyField — a read-only value with a copy button, and optional masking for
47
+ * secrets.
48
+ *
49
+ * For connect URLs, ids, API keys and generated secrets. With `secret`, the
50
+ * value is masked until revealed and the real text is not in the DOM while it
51
+ * is masked, so a screen share or a screenshot does not leak it. Copying works
52
+ * either way.
53
+ *
54
+ * Copy uses the async Clipboard API and fails silently when unavailable (an
55
+ * insecure context or denied permission). Every label is a prop, so nothing
56
+ * is baked into one language.
57
+ *
58
+ * @example
59
+ * <UiFormField label="Connect URL">
60
+ * <UiCopyField value="wss://crdt.numori.app/notes" />
61
+ * </UiFormField>
62
+ *
63
+ * @example A secret shown once
64
+ * <UiCopyField :value="signingKey" secret @copy="markCopied" />
65
+ *
66
+ * @emits {string} copy — The value was copied.
67
+ * @emits {boolean} update:revealed — The secret was revealed or hidden.
68
+ */
69
+ import { computed, ref, watch } from 'vue'
70
+ import UiIcon from '../icons/Icon.vue'
71
+ import { useMergedClasses } from '../composables/useMergedClasses.js'
72
+
73
+ const props = defineProps({
74
+ /**
75
+ * The text to show and copy.
76
+ * @type {string}
77
+ */
78
+ value: { type: String, required: true },
79
+
80
+ /**
81
+ * Mask the value until revealed.
82
+ * @type {boolean}
83
+ * @default false
84
+ */
85
+ secret: { type: Boolean, default: false },
86
+
87
+ /**
88
+ * Revealed state of a secret, for `v-model:revealed`.
89
+ * @type {boolean}
90
+ * @default false
91
+ */
92
+ revealed: { type: Boolean, default: false },
93
+
94
+ /**
95
+ * Monospace text.
96
+ * @type {boolean}
97
+ * @default true
98
+ */
99
+ mono: { type: Boolean, default: true },
100
+
101
+ /**
102
+ * Wrap long values onto several lines instead of truncating.
103
+ * @type {boolean}
104
+ * @default false
105
+ */
106
+ wrap: { type: Boolean, default: false },
107
+
108
+ /**
109
+ * Control size.
110
+ * @type {string}
111
+ * @default 'md'
112
+ * @values 'sm' | 'md'
113
+ */
114
+ size: { type: String, default: 'md' },
115
+
116
+ /**
117
+ * Show the copy label beside the icon (from `sm` up).
118
+ * @type {boolean}
119
+ * @default false
120
+ */
121
+ showCopyText: { type: Boolean, default: false },
122
+
123
+ /** @type {string} @default 'Copy' */
124
+ copyLabel: { type: String, default: 'Copy' },
125
+ /** @type {string} @default 'Copied' */
126
+ copiedLabel: { type: String, default: 'Copied' },
127
+ /** @type {string} @default 'Reveal' */
128
+ revealLabel: { type: String, default: 'Reveal' },
129
+ /** @type {string} @default 'Hide' */
130
+ hideLabel: { type: String, default: 'Hide' },
131
+ /** Accessible text read in place of a masked value. @type {string} @default 'Hidden value' */
132
+ hiddenLabel: { type: String, default: 'Hidden value' },
133
+ })
134
+
135
+ const emit = defineEmits(['copy', 'update:revealed'])
136
+
137
+ defineOptions({ inheritAttrs: false })
138
+
139
+ const isRevealed = ref(props.revealed)
140
+ watch(
141
+ () => props.revealed,
142
+ (value) => {
143
+ isRevealed.value = value
144
+ },
145
+ )
146
+
147
+ const masked = computed(() => props.secret && !isRevealed.value)
148
+ const mask = computed(() => '•'.repeat(Math.min(Math.max(props.value.length, 8), 24)))
149
+
150
+ function toggleReveal() {
151
+ isRevealed.value = !isRevealed.value
152
+ emit('update:revealed', isRevealed.value)
153
+ }
154
+
155
+ const copied = ref(false)
156
+ let timer = null
157
+
158
+ async function copy() {
159
+ try {
160
+ await navigator.clipboard.writeText(props.value)
161
+ copied.value = true
162
+ emit('copy', props.value)
163
+ clearTimeout(timer)
164
+ timer = setTimeout(() => {
165
+ copied.value = false
166
+ }, 1600)
167
+ } catch {
168
+ // Clipboard unavailable; the value is still selectable by hand.
169
+ }
170
+ }
171
+
172
+ const buttonClass =
173
+ 'inline-flex shrink-0 items-center gap-1.5 border-l border-gray-200 px-3 text-xs font-medium text-gray-600 transition-colors hover:bg-white hover:text-gray-900 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-primary-500 dark:border-gray-700 dark:text-gray-300 dark:hover:bg-gray-800 dark:hover:text-gray-100'
174
+
175
+ const { mergedClasses, restAttrs } = useMergedClasses(
176
+ 'flex w-full min-w-0 items-stretch overflow-hidden rounded-lg border border-gray-200 bg-gray-50 dark:border-gray-700 dark:bg-gray-800/60',
177
+ )
178
+ </script>