numori-ui 0.1.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 (130) hide show
  1. package/LICENSE +651 -0
  2. package/README.md +466 -0
  3. package/dist/components/Alert.js +137 -0
  4. package/dist/components/Alert.js.map +1 -0
  5. package/dist/components/Avatar.js +133 -0
  6. package/dist/components/Avatar.js.map +1 -0
  7. package/dist/components/Badge.js +93 -0
  8. package/dist/components/Badge.js.map +1 -0
  9. package/dist/components/Button.js +428 -0
  10. package/dist/components/Button.js.map +1 -0
  11. package/dist/components/ButtonsGroup.js +153 -0
  12. package/dist/components/ButtonsGroup.js.map +1 -0
  13. package/dist/components/Checkbox.js +93 -0
  14. package/dist/components/Checkbox.js.map +1 -0
  15. package/dist/components/Divider.js +79 -0
  16. package/dist/components/Divider.js.map +1 -0
  17. package/dist/components/Dropdown.js +166 -0
  18. package/dist/components/Dropdown.js.map +1 -0
  19. package/dist/components/DropdownItem.js +106 -0
  20. package/dist/components/DropdownItem.js.map +1 -0
  21. package/dist/components/DropdownRow.js +41 -0
  22. package/dist/components/DropdownRow.js.map +1 -0
  23. package/dist/components/DropdownSubmenu.js +142 -0
  24. package/dist/components/DropdownSubmenu.js.map +1 -0
  25. package/dist/components/FileInput.js +122 -0
  26. package/dist/components/FileInput.js.map +1 -0
  27. package/dist/components/FormField.js +112 -0
  28. package/dist/components/FormField.js.map +1 -0
  29. package/dist/components/Input.js +603 -0
  30. package/dist/components/Input.js.map +1 -0
  31. package/dist/components/Kbd.js +35 -0
  32. package/dist/components/Kbd.js.map +1 -0
  33. package/dist/components/ListMenu.js +143 -0
  34. package/dist/components/ListMenu.js.map +1 -0
  35. package/dist/components/ListMenuItem.js +189 -0
  36. package/dist/components/ListMenuItem.js.map +1 -0
  37. package/dist/components/Modal.js +206 -0
  38. package/dist/components/Modal.js.map +1 -0
  39. package/dist/components/Numpad.js +123 -0
  40. package/dist/components/Numpad.js.map +1 -0
  41. package/dist/components/Popup.js +170 -0
  42. package/dist/components/Popup.js.map +1 -0
  43. package/dist/components/ProgressBar.js +118 -0
  44. package/dist/components/ProgressBar.js.map +1 -0
  45. package/dist/components/Prompt.js +201 -0
  46. package/dist/components/Prompt.js.map +1 -0
  47. package/dist/components/Radio.js +81 -0
  48. package/dist/components/Radio.js.map +1 -0
  49. package/dist/components/Select.js +441 -0
  50. package/dist/components/Select.js.map +1 -0
  51. package/dist/components/Slider.js +116 -0
  52. package/dist/components/Slider.js.map +1 -0
  53. package/dist/components/Stepper.js +65 -0
  54. package/dist/components/Stepper.js.map +1 -0
  55. package/dist/components/Toggle.js +133 -0
  56. package/dist/components/Toggle.js.map +1 -0
  57. package/dist/components/Tooltip.js +120 -0
  58. package/dist/components/Tooltip.js.map +1 -0
  59. package/dist/components/index.js +81 -0
  60. package/dist/components/index.js.map +1 -0
  61. package/dist/composables/onClickOutside.js +51 -0
  62. package/dist/composables/onClickOutside.js.map +1 -0
  63. package/dist/composables/useBackButton.js +98 -0
  64. package/dist/composables/useBackButton.js.map +1 -0
  65. package/dist/composables/useEventListener.js +63 -0
  66. package/dist/composables/useEventListener.js.map +1 -0
  67. package/dist/icons/Icon.js +120 -0
  68. package/dist/icons/Icon.js.map +1 -0
  69. package/dist/icons/mdi.js +69 -0
  70. package/dist/icons/mdi.js.map +1 -0
  71. package/dist/icons/registry.js +63 -0
  72. package/dist/icons/registry.js.map +1 -0
  73. package/dist/icons/resolver.js +49 -0
  74. package/dist/icons/resolver.js.map +1 -0
  75. package/dist/index.d.ts +108 -0
  76. package/dist/index.js +157 -0
  77. package/dist/index.js.map +1 -0
  78. package/dist/manifest.js +89 -0
  79. package/dist/manifest.js.map +1 -0
  80. package/dist/numori-ui.css +184 -0
  81. package/dist/nuxt.d.ts +35 -0
  82. package/dist/nuxt.mjs +243 -0
  83. package/dist/nuxt.mjs.map +1 -0
  84. package/dist/vite.d.ts +29 -0
  85. package/dist/vite.mjs +187 -0
  86. package/dist/vite.mjs.map +1 -0
  87. package/package.json +138 -0
  88. package/src/components/Alert.vue +127 -0
  89. package/src/components/Avatar.vue +100 -0
  90. package/src/components/Badge.vue +77 -0
  91. package/src/components/Button.vue +419 -0
  92. package/src/components/ButtonsGroup.vue +140 -0
  93. package/src/components/Checkbox.vue +81 -0
  94. package/src/components/Divider.vue +64 -0
  95. package/src/components/Dropdown.vue +150 -0
  96. package/src/components/DropdownItem.vue +78 -0
  97. package/src/components/DropdownRow.vue +37 -0
  98. package/src/components/DropdownSubmenu.vue +127 -0
  99. package/src/components/FileInput.vue +107 -0
  100. package/src/components/FormField.vue +86 -0
  101. package/src/components/Input.vue +619 -0
  102. package/src/components/Kbd.vue +30 -0
  103. package/src/components/ListMenu.vue +127 -0
  104. package/src/components/ListMenuItem.vue +171 -0
  105. package/src/components/Modal.vue +209 -0
  106. package/src/components/Numpad.vue +118 -0
  107. package/src/components/Popup.vue +156 -0
  108. package/src/components/ProgressBar.vue +107 -0
  109. package/src/components/Prompt.vue +161 -0
  110. package/src/components/Radio.vue +50 -0
  111. package/src/components/Select.vue +470 -0
  112. package/src/components/Slider.vue +93 -0
  113. package/src/components/Stepper.vue +53 -0
  114. package/src/components/Toggle.vue +125 -0
  115. package/src/components/Tooltip.vue +113 -0
  116. package/src/components/index.js +109 -0
  117. package/src/composables/onClickOutside.js +61 -0
  118. package/src/composables/useBackButton.js +98 -0
  119. package/src/composables/useEventListener.js +65 -0
  120. package/src/icons/Icon.vue +139 -0
  121. package/src/icons/mdi.js +67 -0
  122. package/src/icons/registry.js +64 -0
  123. package/src/icons/resolver.js +48 -0
  124. package/src/index.js +167 -0
  125. package/src/manifest.js +86 -0
  126. package/src/nuxt.js +315 -0
  127. package/src/style.css +20 -0
  128. package/src/tailwind.css +18 -0
  129. package/src/theme.css +172 -0
  130. package/src/vite.js +235 -0
@@ -0,0 +1,125 @@
1
+ <template>
2
+ <!-- Accessible toggle switch rendered as a button with role="switch" -->
3
+ <button
4
+ type="button"
5
+ :disabled="disabled"
6
+ class="relative inline-flex items-center shrink-0 rounded-full transition-colors"
7
+ :class="[
8
+ sizeClasses.track,
9
+ disabled ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer',
10
+ modelValue ? activeColor : inactiveColor,
11
+ ]"
12
+ role="switch"
13
+ :aria-checked="modelValue"
14
+ @click="toggle"
15
+ >
16
+ <span
17
+ class="inline-block transform rounded-full bg-white transition-transform"
18
+ :class="[sizeClasses.dot, modelValue ? sizeClasses.translateOn : sizeClasses.translateOff]"
19
+ />
20
+ </button>
21
+ </template>
22
+
23
+ <script setup>
24
+ /**
25
+ * UiToggle — Accessible toggle switch component with two sizes and multiple color themes.
26
+ *
27
+ * Renders as a `<button role="switch">` with smooth sliding-dot animation.
28
+ * Supports disabled and read-only states. The `sm` size matches ProfileModal styling
29
+ * (600-shade active colors), while `md` matches SettingsModal styling (500-shade).
30
+ *
31
+ * @example Basic toggle
32
+ * <UiToggle v-model="darkMode" />
33
+ *
34
+ * @example Small green toggle
35
+ * <UiToggle v-model="notifications" size="sm" color="green" />
36
+ *
37
+ * @example Read-only toggle (parent controls state)
38
+ * <UiToggle :model-value="isActive" readonly @click="handleToggle" />
39
+ *
40
+ * @emits {boolean} update:modelValue — Emitted when the toggle is switched (v-model)
41
+ */
42
+ import { computed } from 'vue'
43
+ const props = defineProps({
44
+ /**
45
+ * Current on/off state (v-model).
46
+ * @type {boolean}
47
+ * @default false
48
+ */
49
+ modelValue: { type: Boolean, default: false },
50
+
51
+ /**
52
+ * Disabled state — dims the toggle and prevents interaction.
53
+ * @type {boolean}
54
+ * @default false
55
+ */
56
+ disabled: { type: Boolean, default: false },
57
+
58
+ /**
59
+ * Read-only mode — renders visually but ignores clicks (parent handles toggling).
60
+ * @type {boolean}
61
+ * @default false
62
+ */
63
+ readonly: { type: Boolean, default: false },
64
+
65
+ /**
66
+ * Toggle size. 'sm' uses a compact track (h-5 w-9) with 600-shade active colors;
67
+ * 'md' uses a larger track (h-6 w-11) with 500-shade active colors.
68
+ * @type {string}
69
+ * @default 'md'
70
+ * @values 'sm' | 'md'
71
+ */
72
+ size: { type: String, default: 'md' },
73
+
74
+ /**
75
+ * Active (on) track color theme.
76
+ * @type {string}
77
+ * @default 'primary'
78
+ * @values 'primary' | 'green' | 'red'
79
+ */
80
+ color: { type: String, default: 'primary' },
81
+ })
82
+
83
+ const emit = defineEmits(['update:modelValue'])
84
+
85
+ // Active color uses different shade intensities per size for visual consistency
86
+ const activeColor = computed(() => {
87
+ if (props.size === 'sm') {
88
+ // ProfileModal style uses 600
89
+ const map = { primary: 'bg-primary-600', green: 'bg-success-600', red: 'bg-red-600' }
90
+ return map[props.color] ?? map.primary
91
+ }
92
+ // SettingsModal style uses 500
93
+ const map = { primary: 'bg-primary-500', green: 'bg-success-500', red: 'bg-red-500' }
94
+ return map[props.color] ?? map.primary
95
+ })
96
+
97
+ const inactiveColor = computed(() => {
98
+ // sm (ProfileModal): bg-gray-300 dark:bg-gray-600
99
+ // md (SettingsModal): bg-gray-300 dark:bg-gray-700
100
+ return props.size === 'sm' ? 'bg-gray-300 dark:bg-gray-600' : 'bg-gray-300 dark:bg-gray-700'
101
+ })
102
+
103
+ const sizeClasses = computed(() => {
104
+ if (props.size === 'sm') {
105
+ return {
106
+ track: 'h-5 w-9 border-2 border-transparent',
107
+ dot: 'h-4 w-4 shadow',
108
+ translateOn: 'translate-x-4',
109
+ translateOff: 'translate-x-0',
110
+ }
111
+ }
112
+ return {
113
+ track: 'h-6 w-11',
114
+ dot: 'h-4 w-4',
115
+ translateOn: 'translate-x-6',
116
+ translateOff: 'translate-x-1',
117
+ }
118
+ })
119
+
120
+ const toggle = () => {
121
+ if (!props.disabled && !props.readonly) {
122
+ emit('update:modelValue', !props.modelValue)
123
+ }
124
+ }
125
+ </script>
@@ -0,0 +1,113 @@
1
+ <template>
2
+ <!-- Wrapper with group hover to show/hide the tooltip -->
3
+ <div class="relative group" :class="inline ? 'inline-flex' : ''">
4
+ <!-- Trigger content (default slot) -->
5
+ <slot />
6
+ <!-- Tooltip bubble: hidden by default, shown on group hover -->
7
+ <div
8
+ class="absolute px-3 py-2 rounded-lg text-white text-xs leading-relaxed shadow-lg opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all pointer-events-none"
9
+ :class="[positionClass, widthClass, 'bg-gray-900 dark:bg-gray-700 z-50']"
10
+ >
11
+ <slot name="content">
12
+ {{ text }}
13
+ </slot>
14
+ <div v-if="arrow" :class="arrowClass" />
15
+ </div>
16
+ </div>
17
+ </template>
18
+
19
+ <script setup>
20
+ /**
21
+ * UiTooltip — CSS-only tooltip that appears on hover with configurable position and arrow.
22
+ *
23
+ * Wraps any content in a `group` container and shows a tooltip bubble on hover.
24
+ * Supports four positions (top, bottom, left, right), optional directional arrow,
25
+ * and either plain text or rich content via the `#content` slot.
26
+ * No JavaScript event handling — uses pure CSS `group-hover` for show/hide.
27
+ *
28
+ * @example Simple text tooltip
29
+ * <UiTooltip text="Save your changes">
30
+ * <UiButton>Save</UiButton>
31
+ * </UiTooltip>
32
+ *
33
+ * @example Bottom tooltip without arrow
34
+ * <UiTooltip text="More info" position="bottom" :arrow="false">
35
+ * <UiIcon name="mdi:information" class="w-5 h-5" />
36
+ * </UiTooltip>
37
+ *
38
+ * @example Rich content tooltip
39
+ * <UiTooltip position="right">
40
+ * <UiButton>Hover me</UiButton>
41
+ * <template #content>
42
+ * <strong>Title</strong>
43
+ * <p>Detailed description here.</p>
44
+ * </template>
45
+ * </UiTooltip>
46
+ *
47
+ * @slot default — Trigger element that the tooltip wraps
48
+ * @slot content — Rich tooltip content (alternative to the text prop)
49
+ */
50
+ import { computed } from 'vue'
51
+ const props = defineProps({
52
+ /**
53
+ * Plain text content for the tooltip. Ignored when the `#content` slot is used.
54
+ * @type {string}
55
+ * @default ''
56
+ */
57
+ text: { type: String, default: '' },
58
+
59
+ /**
60
+ * Position of the tooltip relative to the trigger element.
61
+ * @type {string}
62
+ * @default 'top'
63
+ * @values 'top' | 'bottom' | 'left' | 'right'
64
+ */
65
+ position: { type: String, default: 'top' },
66
+
67
+ /**
68
+ * Width class for the tooltip bubble.
69
+ * @type {string}
70
+ * @default 'w-56'
71
+ */
72
+ width: { type: String, default: 'w-56' },
73
+
74
+ /**
75
+ * Show a directional arrow pointing from the tooltip toward the trigger.
76
+ * @type {boolean}
77
+ * @default true
78
+ */
79
+ arrow: { type: Boolean, default: true },
80
+
81
+ /**
82
+ * Render the wrapper as inline-flex instead of block for inline trigger elements.
83
+ * @type {boolean}
84
+ * @default true
85
+ */
86
+ inline: { type: Boolean, default: true },
87
+ })
88
+
89
+ const widthClass = computed(() => props.width)
90
+
91
+ // Position classes map tooltip placement using absolute positioning + transforms
92
+ const positionClass = computed(() => {
93
+ const map = {
94
+ top: 'bottom-full left-1/2 -translate-x-1/2 mb-2',
95
+ bottom: 'top-full left-1/2 -translate-x-1/2 mt-2',
96
+ left: 'right-full top-1/2 -translate-y-1/2 mr-2',
97
+ right: 'left-full top-1/2 -translate-y-1/2 ml-2',
98
+ }
99
+ return map[props.position] || map.top
100
+ })
101
+
102
+ // Arrow uses CSS border trick to create a triangle pointing toward the trigger
103
+ const arrowClass = computed(() => {
104
+ const base = 'absolute border-4 border-transparent'
105
+ const map = {
106
+ top: `${base} top-full left-1/2 -translate-x-1/2 border-t-gray-900 dark:border-t-gray-700`,
107
+ bottom: `${base} bottom-full left-1/2 -translate-x-1/2 border-b-gray-900 dark:border-b-gray-700`,
108
+ left: `${base} left-full top-1/2 -translate-y-1/2 border-l-gray-900 dark:border-l-gray-700`,
109
+ right: `${base} right-full top-1/2 -translate-y-1/2 border-r-gray-900 dark:border-r-gray-700`,
110
+ }
111
+ return map[props.position] || map.top
112
+ })
113
+ </script>
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Real component imports, re-exported under the default `Ui` prefix.
3
+ *
4
+ * The prefix is baked into the export names so that `import { UiButton }`
5
+ * reads the same as `<UiButton>` in a template. Consumers who register the
6
+ * library with a different prefix still import these names — only the
7
+ * globally registered tag changes.
8
+ *
9
+ * `../manifest.js` carries the same list as plain data for build-time
10
+ * consumers; `scripts/verify.mjs` keeps the two in sync.
11
+ */
12
+ import Alert from './Alert.vue'
13
+ import Avatar from './Avatar.vue'
14
+ import Badge from './Badge.vue'
15
+ import Button from './Button.vue'
16
+ import ButtonsGroup from './ButtonsGroup.vue'
17
+ import Checkbox from './Checkbox.vue'
18
+ import Divider from './Divider.vue'
19
+ import Dropdown from './Dropdown.vue'
20
+ import DropdownItem from './DropdownItem.vue'
21
+ import DropdownRow from './DropdownRow.vue'
22
+ import DropdownSubmenu from './DropdownSubmenu.vue'
23
+ import FileInput from './FileInput.vue'
24
+ import FormField from './FormField.vue'
25
+ import Input from './Input.vue'
26
+ import Kbd from './Kbd.vue'
27
+ import ListMenu from './ListMenu.vue'
28
+ import ListMenuItem from './ListMenuItem.vue'
29
+ import Modal from './Modal.vue'
30
+ import Numpad from './Numpad.vue'
31
+ import Popup from './Popup.vue'
32
+ import ProgressBar from './ProgressBar.vue'
33
+ import Prompt from './Prompt.vue'
34
+ import Radio from './Radio.vue'
35
+ import Select from './Select.vue'
36
+ import Slider from './Slider.vue'
37
+ import Stepper from './Stepper.vue'
38
+ import Toggle from './Toggle.vue'
39
+ import Tooltip from './Tooltip.vue'
40
+ import Icon from '../icons/Icon.vue'
41
+
42
+ export {
43
+ Alert as UiAlert,
44
+ Avatar as UiAvatar,
45
+ Badge as UiBadge,
46
+ Button as UiButton,
47
+ ButtonsGroup as UiButtonsGroup,
48
+ Checkbox as UiCheckbox,
49
+ Divider as UiDivider,
50
+ Dropdown as UiDropdown,
51
+ DropdownItem as UiDropdownItem,
52
+ DropdownRow as UiDropdownRow,
53
+ DropdownSubmenu as UiDropdownSubmenu,
54
+ FileInput as UiFileInput,
55
+ FormField as UiFormField,
56
+ Input as UiInput,
57
+ Kbd as UiKbd,
58
+ ListMenu as UiListMenu,
59
+ ListMenuItem as UiListMenuItem,
60
+ Modal as UiModal,
61
+ Numpad as UiNumpad,
62
+ Popup as UiPopup,
63
+ ProgressBar as UiProgressBar,
64
+ Prompt as UiPrompt,
65
+ Radio as UiRadio,
66
+ Select as UiSelect,
67
+ Slider as UiSlider,
68
+ Stepper as UiStepper,
69
+ Toggle as UiToggle,
70
+ Tooltip as UiTooltip,
71
+ Icon as UiIcon,
72
+ }
73
+
74
+ /**
75
+ * Base-name -> component map, used by the Vue plugin to register everything
76
+ * under whatever prefix the host chose.
77
+ * @type {Record<string, import('vue').Component>}
78
+ */
79
+ export const components = {
80
+ Alert,
81
+ Avatar,
82
+ Badge,
83
+ Button,
84
+ ButtonsGroup,
85
+ Checkbox,
86
+ Divider,
87
+ Dropdown,
88
+ DropdownItem,
89
+ DropdownRow,
90
+ DropdownSubmenu,
91
+ FileInput,
92
+ FormField,
93
+ Input,
94
+ Kbd,
95
+ ListMenu,
96
+ ListMenuItem,
97
+ Modal,
98
+ Numpad,
99
+ Popup,
100
+ ProgressBar,
101
+ Prompt,
102
+ Radio,
103
+ Select,
104
+ Slider,
105
+ Stepper,
106
+ Toggle,
107
+ Tooltip,
108
+ Icon,
109
+ }
@@ -0,0 +1,61 @@
1
+ import { toValue } from 'vue'
2
+ import { useEventListener } from './useEventListener.js'
3
+
4
+ /**
5
+ * onClickOutside — runs a handler when a pointer event lands outside an element.
6
+ *
7
+ * A dependency-free stand-in for VueUse's helper, covering the subset Numori UI
8
+ * needs (dismissing dropdowns and popovers).
9
+ *
10
+ * Two details matter for correctness inside this library:
11
+ *
12
+ * - The listener is registered on `document` in the **capture** phase, so a
13
+ * dropdown closes even when the click target stops propagation.
14
+ * - `event.composedPath()` is used rather than `element.contains(target)` so
15
+ * clicks originating inside a shadow root, or on content teleported into a
16
+ * listed `ignore` element, are still recognised as "inside".
17
+ *
18
+ * @param {import('vue').Ref<HTMLElement | null | undefined>} target
19
+ * Element that defines "inside".
20
+ * @param {(event: PointerEvent | MouseEvent) => void} handler
21
+ * Called when the event happened outside the target.
22
+ * @param {object} [options]
23
+ * @param {Array<import('vue').Ref<HTMLElement | null | undefined> | HTMLElement>} [options.ignore]
24
+ * Extra elements that also count as "inside" — use for teleported panels.
25
+ * @returns {() => void} stop — Removes the listener.
26
+ *
27
+ * Safe to call during SSR: nothing is bound on the server.
28
+ *
29
+ * @example
30
+ * const containerRef = ref(null)
31
+ * onClickOutside(containerRef, () => (isOpen.value = false))
32
+ *
33
+ * @example Teleported panel treated as inside
34
+ * onClickOutside(triggerRef, close, { ignore: [panelRef] })
35
+ */
36
+ export function onClickOutside(target, handler, options = {}) {
37
+ const { ignore = [] } = options
38
+
39
+ // `globalThis.document` rather than a bare `document`: the latter is a
40
+ // ReferenceError during SSR, and this composable is called from setup.
41
+ return useEventListener(
42
+ globalThis.document,
43
+ 'click',
44
+ (event) => {
45
+ const el = toValue(target)
46
+ if (!el) return
47
+
48
+ const path = typeof event.composedPath === 'function' ? event.composedPath() : []
49
+ const isInside = (candidate) =>
50
+ candidate && (path.includes(candidate) || candidate.contains?.(event.target))
51
+
52
+ if (isInside(el)) return
53
+ for (const other of ignore) {
54
+ if (isInside(toValue(other))) return
55
+ }
56
+
57
+ handler(event)
58
+ },
59
+ { capture: true },
60
+ )
61
+ }
@@ -0,0 +1,98 @@
1
+ import { computed, ref } from 'vue'
2
+
3
+ /**
4
+ * useBackButton — a LIFO stack of "go back" handlers that overlays register
5
+ * themselves with while they are open.
6
+ *
7
+ * Numori UI's overlays (modals, prompts) should close when the user triggers a
8
+ * platform back gesture — the Android hardware button under Capacitor, a
9
+ * browser popstate, or anything else the host decides. The library must not
10
+ * depend on Capacitor to do that, so it only maintains the stack and exposes
11
+ * `invoke()`. **The host application is responsible for calling `invoke()`**
12
+ * from whatever back signal it actually has. Until it does, the stack simply
13
+ * sits idle and overlays behave normally.
14
+ *
15
+ * Handlers run most-recently-registered first, so a prompt opened on top of a
16
+ * modal consumes the event before the modal underneath sees it. A handler
17
+ * returns `true` to consume the event, or `false` to let it fall through.
18
+ *
19
+ * The stack is module-level (shared across every consumer) because back
20
+ * navigation is inherently a single global concern. Registration is a no-op
21
+ * outside the browser, which keeps server rendering free of cross-request
22
+ * leakage.
23
+ *
24
+ * @example Wiring it up in a Capacitor host
25
+ * import { App } from '@capacitor/app'
26
+ * import { useBackButton } from 'numori-ui'
27
+ *
28
+ * const { invoke } = useBackButton()
29
+ * App.addListener('backButton', ({ canGoBack }) => {
30
+ * if (invoke()) return // an overlay handled it
31
+ * if (canGoBack) history.back()
32
+ * else App.exitApp()
33
+ * })
34
+ *
35
+ * @example Wiring it up in a browser host
36
+ * const { invoke, hasHandlers } = useBackButton()
37
+ * window.addEventListener('popstate', () => {
38
+ * if (hasHandlers.value) invoke()
39
+ * })
40
+ */
41
+
42
+ /** @type {import('vue').Ref<Array<{ id: number, handler: () => boolean, priority: number }>>} */
43
+ const handlers = ref([])
44
+ let nextId = 0
45
+
46
+ /**
47
+ * Evaluated per call rather than once at module load. Module-load evaluation
48
+ * would bake in whatever environment first imported the file, which is wrong
49
+ * for SSR (the same module instance serves the server render and, in tests, a
50
+ * simulated browser).
51
+ */
52
+ const isBrowser = () => typeof window !== 'undefined'
53
+
54
+ export const useBackButton = () => {
55
+ /**
56
+ * Register a back handler.
57
+ * @param {() => boolean} handler Return true if the event was consumed.
58
+ * @param {number} [priority=0] Higher priority runs first among peers.
59
+ * @returns {number} id — Pass to `unregister` on close/unmount.
60
+ */
61
+ const register = (handler, priority = 0) => {
62
+ if (!isBrowser()) return -1
63
+ const id = nextId++
64
+ handlers.value.push({ id, handler, priority })
65
+ return id
66
+ }
67
+
68
+ /**
69
+ * Unregister a previously registered handler.
70
+ * @param {number} id
71
+ */
72
+ const unregister = (id) => {
73
+ if (id === -1) return
74
+ const idx = handlers.value.findIndex((h) => h.id === id)
75
+ if (idx !== -1) handlers.value.splice(idx, 1)
76
+ }
77
+
78
+ /**
79
+ * Invoke the topmost handler that consumes the event.
80
+ * Call this from the host's back-navigation signal.
81
+ * @returns {boolean} true if a handler consumed the event.
82
+ */
83
+ const invoke = () => {
84
+ if (handlers.value.length === 0) return false
85
+ // Priority descending, then id descending so the most recently opened
86
+ // overlay of equal priority gets first refusal.
87
+ const sorted = [...handlers.value].sort((a, b) => b.priority - a.priority || b.id - a.id)
88
+ for (const entry of sorted) {
89
+ if (entry.handler()) return true
90
+ }
91
+ return false
92
+ }
93
+
94
+ /** Whether any overlay currently wants to intercept back navigation. */
95
+ const hasHandlers = computed(() => handlers.value.length > 0)
96
+
97
+ return { register, unregister, invoke, hasHandlers }
98
+ }
@@ -0,0 +1,65 @@
1
+ import { getCurrentScope, onScopeDispose, watch, toValue } from 'vue'
2
+
3
+ /**
4
+ * useEventListener — attaches a DOM event listener that cleans itself up when
5
+ * the owning component (or effect scope) is torn down.
6
+ *
7
+ * A dependency-free stand-in for VueUse's helper of the same name, covering
8
+ * the subset Numori UI needs. Numori Notes gets VueUse auto-imported from
9
+ * `@vueuse/nuxt`; a library should not force that on its consumers for two
10
+ * small utilities, so this ships in-package instead.
11
+ *
12
+ * The target may be a plain value, a ref, or a getter, so listeners follow an
13
+ * element as it is mounted, swapped or unmounted. A nullish target is a no-op,
14
+ * which is what keeps this safe to call during setup under SSR — pass a getter
15
+ * such as `() => globalThis.document` and it simply does nothing on the server,
16
+ * then binds on the client.
17
+ *
18
+ * @param {EventTarget | import('vue').Ref<EventTarget | null | undefined> | (() => EventTarget | null | undefined) | null | undefined} target
19
+ * Element or event target to bind to. May be a ref or a getter.
20
+ * @param {string} event Event name, e.g. 'keydown'.
21
+ * @param {(event: Event) => void} handler Listener callback.
22
+ * @param {boolean | AddEventListenerOptions} [options] Native listener options.
23
+ * @returns {() => void} stop — Removes the listener early.
24
+ *
25
+ * @example
26
+ * useEventListener(globalThis.document, 'keydown', (e) => {
27
+ * if (e.key === 'Escape') close()
28
+ * })
29
+ *
30
+ * @example Conditional binding via a getter
31
+ * useEventListener(
32
+ * () => (props.capture ? globalThis.window : null),
33
+ * 'keydown',
34
+ * onKeydown,
35
+ * )
36
+ */
37
+ export function useEventListener(target, event, handler, options) {
38
+ let detach = null
39
+
40
+ const remove = () => {
41
+ detach?.()
42
+ detach = null
43
+ }
44
+
45
+ const attach = (el) => {
46
+ remove()
47
+ if (!el || typeof el.addEventListener !== 'function') return
48
+ el.addEventListener(event, handler, options)
49
+ detach = () => el.removeEventListener(event, handler, options)
50
+ }
51
+
52
+ // Watched rather than bound once, so a ref or getter target can change: the
53
+ // listener moves with the element, and a getter that returns null on the
54
+ // server starts working after hydration.
55
+ const stopWatch = watch(() => toValue(target), attach, { immediate: true, flush: 'post' })
56
+
57
+ const stop = () => {
58
+ stopWatch()
59
+ remove()
60
+ }
61
+
62
+ if (getCurrentScope()) onScopeDispose(stop)
63
+
64
+ return stop
65
+ }