@ternarytech-io/gears-ui 0.5.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.
@@ -0,0 +1,48 @@
1
+ <script setup lang="ts">
2
+ // Buttons joined in a row (or a column, `vertical`), with Tabler's btn-group: each has a label
3
+ // and/or an icon, a tooltip and Disabled; they share a color, style and size. `click` reports the
4
+ // button clicked and its index. To choose one of several, use a segmented TTabs instead.
5
+ import { nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
6
+ import { Tooltip } from '@tabler/core'
7
+ import TButton from './TButton.vue'
8
+ import type { ButtonGroupItem, Color, Size } from '../types'
9
+
10
+ const props = withDefaults(defineProps<{
11
+ items: ButtonGroupItem[]
12
+ color?: Color
13
+ variant?: 'solid' | 'outline' | 'ghost' | 'soft'
14
+ size?: Size
15
+ vertical?: boolean
16
+ /** Full width: the buttons share it. */
17
+ block?: boolean
18
+ /** The group's name, for screen readers. */
19
+ label?: string
20
+ }>(), { color: 'neutral', variant: 'solid', size: 'md' })
21
+
22
+ const emit = defineEmits<{ click: [item: ButtonGroupItem, index: number, event: MouseEvent] }>()
23
+
24
+ const root = ref<HTMLElement>()
25
+ let tips: Tooltip[] = []
26
+
27
+ // Each button's tooltip is Tabler's, on the button itself, so the group's joined edges stay.
28
+ function tooltips() {
29
+ tips.forEach(t => t.dispose())
30
+ tips = []
31
+ const buttons = [...(root.value?.children ?? [])] as HTMLElement[]
32
+ props.items.forEach((item, i) => {
33
+ const el = buttons[i]
34
+ if (el && item.tooltip) tips.push(new Tooltip(el, { title: item.tooltip, placement: props.vertical ? 'right' : 'top', trigger: 'hover focus', delay: { show: 150, hide: 0 } }))
35
+ })
36
+ }
37
+ onMounted(tooltips)
38
+ watch(() => props.items.map(i => i.tooltip).join('\n') + props.vertical, () => nextTick(tooltips))
39
+ onBeforeUnmount(() => tips.forEach(t => t.dispose()))
40
+ </script>
41
+
42
+ <template>
43
+ <div ref="root" :class="[vertical ? 'btn-group-vertical' : 'btn-group', { 'w-100 d-flex': block }]" role="group" :aria-label="label">
44
+ <TButton v-for="(item, i) in items" :key="item.value ?? i" :label="item.label" :icon="item.icon" :color="item.color ?? color" :variant="variant"
45
+ :size="size" :disabled="item.disabled" :class="{ 'flex-fill': block }" :aria-label="!item.label ? item.tooltip : undefined"
46
+ @click="(event: MouseEvent) => emit('click', item, i, event)" />
47
+ </div>
48
+ </template>
@@ -1,14 +1,13 @@
1
1
  <script setup lang="ts">
2
2
  // A menu under a button. Items come in groups (an array of arrays), split by dividers; an item is
3
3
  // a link (`to`, `href`), an action (`onSelect`) or a `label` heading. The slot replaces the button.
4
- import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
5
- import { RouterLink } from 'vue-router'
4
+ import { onBeforeUnmount, onMounted, ref } from 'vue'
6
5
  import { Dropdown } from '@tabler/core'
7
6
  import TButton from './TButton.vue'
8
- import TIcon from './TIcon.vue'
9
- import { toTabler, type Color, type DropdownItem, type Size } from '../types'
7
+ import DropdownItems from './internal/DropdownItems.vue'
8
+ import type { Color, DropdownItem, Size } from '../types'
10
9
 
11
- const props = withDefaults(defineProps<{
10
+ withDefaults(defineProps<{
12
11
  items: DropdownItem[] | DropdownItem[][]
13
12
  align?: 'start' | 'end'
14
13
  label?: string
@@ -24,17 +23,6 @@ const emit = defineEmits<{ 'update:open': [open: boolean] }>()
24
23
  const toggle = ref<HTMLElement>()
25
24
  let dropdown: Dropdown | null = null
26
25
 
27
- const groups = computed(() => (Array.isArray(props.items[0]) ? props.items : [props.items]) as DropdownItem[][])
28
-
29
- function tag(item: DropdownItem) {
30
- return item.to && !item.disabled ? RouterLink : item.href ? 'a' : 'button'
31
- }
32
- function attrs(item: DropdownItem) {
33
- if (item.to && !item.disabled) return { to: item.to }
34
- if (item.href) return { href: item.disabled ? undefined : item.href }
35
- return { type: 'button', disabled: item.disabled }
36
- }
37
-
38
26
  onMounted(() => {
39
27
  // A fixed popper escapes containers that clip (cards, tables, the sidebar).
40
28
  dropdown = new Dropdown(toggle.value!, { popperConfig: (config: object) => ({ ...config, strategy: 'fixed' }) })
@@ -52,25 +40,7 @@ onBeforeUnmount(() => dropdown?.dispose())
52
40
  </slot>
53
41
  </span>
54
42
  <div :class="['dropdown-menu', align === 'end' && 'dropdown-menu-end', menuClass]" role="menu">
55
- <template v-for="(group, g) in groups" :key="g">
56
- <div v-if="g > 0 && group.length" class="dropdown-divider" />
57
- <template v-for="(item, i) in group" :key="`${g}-${i}`">
58
- <div v-if="item.type === 'label'" class="dropdown-header d-block">
59
- <div class="text-body fw-medium text-truncate">{{ item.label }}</div>
60
- <div v-if="item.description" class="text-secondary text-truncate fw-normal">{{ item.description }}</div>
61
- </div>
62
- <component :is="tag(item)" v-else v-bind="attrs(item)" role="menuitem"
63
- :class="['dropdown-item', { disabled: item.disabled }, item.color && `text-${toTabler(item.color)}`]"
64
- @click="item.onSelect?.($event)">
65
- <TIcon v-if="item.icon" :name="item.icon" class="dropdown-item-icon" />
66
- <span class="min-w-0">
67
- <span class="d-block text-truncate">{{ item.label }}</span>
68
- <span v-if="item.description" class="d-block small text-secondary text-truncate">{{ item.description }}</span>
69
- </span>
70
- <TIcon v-if="item.trailingIcon" :name="item.trailingIcon" class="ms-auto ps-2" />
71
- </component>
72
- </template>
73
- </template>
43
+ <DropdownItems :items="items" />
74
44
  </div>
75
45
  </div>
76
46
  </template>
@@ -240,7 +240,10 @@ defineExpose({ setPage: goTo, download: downloadFile })
240
240
  </template>
241
241
 
242
242
  <style scoped>
243
- .t-pdf { display: flex; flex-direction: column; overflow: hidden; margin: 0; }
243
+ .t-pdf { display: flex; flex-direction: column; overflow: hidden; margin: 0; container-type: inline-size; }
244
+ /* A narrow viewer drops its zoom choice, then its zoom buttons, so the page and download stay. */
245
+ @container (max-width: 440px) { .t-pdf-zoom { display: none; } }
246
+ @container (max-width: 340px) { .t-pdf-toolbar button[aria-label^="Zoom"] { display: none; } }
244
247
  .t-pdf-toolbar { display: flex; align-items: center; gap: 0.25rem; padding: 0.375rem 0.5rem; border-bottom: 1px solid var(--tblr-border-color); }
245
248
  .t-pdf-page-number { display: inline-flex; align-items: center; gap: 0.375rem; margin: 0 0 0 0.25rem; }
246
249
  .t-pdf-page-number input { width: 3.25rem; text-align: center; }
@@ -0,0 +1,54 @@
1
+ <script setup lang="ts">
2
+ // A main action with a menu of others beside it (Tabler's split dropdown): the button runs `click`;
3
+ // the arrow opens the menu, whose items are TDropdown's (groups split by dividers, icons,
4
+ // Disabled, a color such as error for a dangerous one, `onSelect`, `to`, `href`). `select` reports
5
+ // the item chosen.
6
+ import { onBeforeUnmount, onMounted, ref } from 'vue'
7
+ import { Dropdown } from '@tabler/core'
8
+ import TButton from './TButton.vue'
9
+ import DropdownItems from './internal/DropdownItems.vue'
10
+ import type { Color, DropdownItem, Size } from '../types'
11
+
12
+ withDefaults(defineProps<{
13
+ label?: string
14
+ icon?: string
15
+ items: DropdownItem[] | DropdownItem[][]
16
+ color?: Color
17
+ variant?: 'solid' | 'outline' | 'ghost' | 'soft'
18
+ size?: Size
19
+ disabled?: boolean
20
+ /** Only the menu is off. */
21
+ menuDisabled?: boolean
22
+ loading?: boolean
23
+ block?: boolean
24
+ align?: 'start' | 'end'
25
+ /** The arrow's name, for screen readers. */
26
+ menuLabel?: string
27
+ }>(), { color: 'primary', variant: 'solid', size: 'md', align: 'end', menuLabel: 'More actions' })
28
+
29
+ const emit = defineEmits<{ click: [event: MouseEvent]; select: [item: DropdownItem, event: Event]; 'update:open': [open: boolean] }>()
30
+
31
+ const toggle = ref<InstanceType<typeof TButton>>()
32
+ let dropdown: Dropdown | null = null
33
+
34
+ onMounted(() => {
35
+ const el = toggle.value!.$el as HTMLElement
36
+ // A fixed popper escapes containers that clip (cards, tables, the sidebar).
37
+ dropdown = new Dropdown(el, { popperConfig: (config: object) => ({ ...config, strategy: 'fixed' }) })
38
+ el.addEventListener('show.bs.dropdown', () => emit('update:open', true))
39
+ el.addEventListener('hide.bs.dropdown', () => emit('update:open', false))
40
+ })
41
+ onBeforeUnmount(() => dropdown?.dispose())
42
+ </script>
43
+
44
+ <template>
45
+ <div :class="['btn-group', { 'w-100 d-flex': block }]" role="group">
46
+ <TButton :label="label" :icon="icon" :color="color" :variant="variant" :size="size" :disabled="disabled" :loading="loading"
47
+ :class="{ 'flex-fill': block }" @click="(event: MouseEvent) => emit('click', event)" />
48
+ <TButton ref="toggle" :color="color" :variant="variant" :size="size" :disabled="disabled || menuDisabled" class="dropdown-toggle dropdown-toggle-split flex-grow-0"
49
+ data-bs-toggle="dropdown" aria-haspopup="menu" aria-expanded="false" :aria-label="menuLabel" />
50
+ <div :class="['dropdown-menu', align === 'end' && 'dropdown-menu-end']" role="menu">
51
+ <DropdownItems :items="items" @select="(item, event) => emit('select', item, event)" />
52
+ </div>
53
+ </div>
54
+ </template>
@@ -0,0 +1,159 @@
1
+ <script setup lang="ts">
2
+ // A time of day, typed (masked with IMask: hours, minutes, seconds if wanted, and AM or PM in 12
3
+ // hours) or picked from a list at `step` minutes. v-model is always "HH:mm" (or "HH:mm:ss" with
4
+ // `seconds`), 24-hour, however it's shown; null when empty. `min` and `max` ("HH:mm") limit the
5
+ // list, and a typed time outside them shows as invalid. IMask loads on first use.
6
+ import { computed, onBeforeUnmount, onMounted, ref, shallowRef, watch } from 'vue'
7
+ import { Dropdown } from '@tabler/core'
8
+ import TInputMask from './TInputMask.vue'
9
+ import TIcon from './TIcon.vue'
10
+ import { useField } from '../composables/useField'
11
+
12
+ const props = withDefaults(defineProps<{
13
+ /** Shown with AM and PM. */
14
+ hour12?: boolean
15
+ seconds?: boolean
16
+ /** The list's step, in minutes (none: no list). */
17
+ step?: 1 | 5 | 10 | 15 | 30 | 60 | null
18
+ min?: string | null
19
+ max?: string | null
20
+ placeholder?: string
21
+ size?: 'sm' | 'md' | 'lg'
22
+ disabled?: boolean
23
+ invalid?: boolean
24
+ id?: string
25
+ }>(), { hour12: false, seconds: false, step: 30, min: null, max: null, size: 'md' })
26
+
27
+ const model = defineModel<string | null | undefined>()
28
+ const field = useField(props)
29
+
30
+ // ---- Times as seconds since midnight ----
31
+ const parse = (text: string | null | undefined): number | null => {
32
+ const m = /^(\d{1,2}):(\d{2})(?::(\d{2}))?$/.exec(text ?? '')
33
+ if (!m) return null
34
+ const [h, mi, s] = [Number(m[1]), Number(m[2]), Number(m[3] ?? 0)]
35
+ return h < 24 && mi < 60 && s < 60 ? h * 3600 + mi * 60 + s : null
36
+ }
37
+ const pad = (n: number) => String(n).padStart(2, '0')
38
+ /** The model's text: 24-hour, with seconds or not. */
39
+ const toModel = (t: number) => `${pad(Math.floor(t / 3600))}:${pad(Math.floor(t / 60) % 60)}${props.seconds ? `:${pad(t % 60)}` : ''}`
40
+ /** As it's shown and typed. */
41
+ function toText(t: number) {
42
+ const h = Math.floor(t / 3600)
43
+ const rest = `${pad(Math.floor(t / 60) % 60)}${props.seconds ? `:${pad(t % 60)}` : ''}`
44
+ return props.hour12 ? `${pad(h % 12 || 12)}:${rest} ${h < 12 ? 'AM' : 'PM'}` : `${pad(h)}:${rest}`
45
+ }
46
+ /** A typed text's time, when it's complete. */
47
+ function fromText(text: string): number | null {
48
+ const m = props.hour12
49
+ ? /^(\d{2}):(\d{2})(?::(\d{2}))? (AM|PM)$/i.exec(text.trim())
50
+ : /^(\d{2}):(\d{2})(?::(\d{2}))?$/.exec(text.trim())
51
+ if (!m || (props.seconds && m[3] === undefined)) return null
52
+ let h = Number(m[1])
53
+ if (props.hour12) {
54
+ if (h < 1 || h > 12) return null
55
+ h = (h % 12) + (m[4]!.toUpperCase() === 'PM' ? 12 : 0)
56
+ }
57
+ const t = h * 3600 + Number(m[2]) * 60 + Number(m[3] ?? 0)
58
+ return h < 24 && Number(m[2]) < 60 && Number(m[3] ?? 0) < 60 ? t : null
59
+ }
60
+
61
+ const lowest = computed(() => parse(props.min))
62
+ const highest = computed(() => parse(props.max))
63
+ const outside = (t: number) => (lowest.value !== null && t < lowest.value) || (highest.value !== null && t > highest.value)
64
+ const current = computed(() => parse(model.value))
65
+
66
+ // ---- The masked text ----
67
+ const text = ref('')
68
+ watch([() => model.value, () => props.hour12, () => props.seconds], () => {
69
+ const t = current.value
70
+ if (t === null ? text.value !== '' && fromText(text.value) !== null : fromText(text.value) !== t) text.value = t === null ? '' : toText(t)
71
+ }, { immediate: true })
72
+ function typed(value: string | null | undefined) {
73
+ text.value = value ?? ''
74
+ if (!text.value.trim()) {
75
+ if (model.value != null) model.value = null
76
+ return
77
+ }
78
+ const t = fromText(text.value)
79
+ if (t !== null && toModel(t) !== model.value) model.value = toModel(t)
80
+ }
81
+ // Left half-typed: back to the time it holds.
82
+ function settle() {
83
+ if (text.value.trim() && fromText(text.value) === null) text.value = current.value === null ? '' : toText(current.value)
84
+ }
85
+
86
+ // IMask's ranges and AM/PM, once it's loaded.
87
+ const imask = shallowRef<{ MaskedRange: unknown; MaskedEnum: unknown } | null>(null)
88
+ onMounted(async () => { imask.value = (await import('imask')).default as unknown as { MaskedRange: unknown; MaskedEnum: unknown } })
89
+ const options = computed(() => {
90
+ const I = imask.value
91
+ if (!I) return undefined
92
+ const range = (from: number, to: number) => ({ mask: I.MaskedRange, from, to, maxLength: 2, autofix: 'pad' })
93
+ return {
94
+ mask: `HH:MM${props.seconds ? ':SS' : ''}${props.hour12 ? ' aa' : ''}`,
95
+ blocks: {
96
+ HH: props.hour12 ? range(1, 12) : range(0, 23), MM: range(0, 59), SS: range(0, 59),
97
+ aa: { mask: I.MaskedEnum, enum: ['AM', 'PM'], prepareChar: (c: string) => c.toUpperCase() }
98
+ },
99
+ lazy: true, overwrite: true
100
+ }
101
+ })
102
+ const shownPlaceholder = computed(() => props.placeholder ?? `${props.hour12 ? 'hh' : 'HH'}:mm${props.seconds ? ':ss' : ''}${props.hour12 ? ' AM' : ''}`)
103
+
104
+ // ---- The list ----
105
+ const times = computed(() => {
106
+ if (!props.step) return []
107
+ const from = lowest.value ?? 0
108
+ const to = highest.value ?? 24 * 3600 - 1
109
+ const list: number[] = []
110
+ for (let t = Math.ceil(from / (props.step * 60)) * props.step * 60; t <= to && list.length < 1440; t += props.step * 60) list.push(t)
111
+ return list
112
+ })
113
+ const toggle = ref<HTMLButtonElement>()
114
+ const menu = ref<HTMLElement>()
115
+ let dropdown: Dropdown | null = null
116
+ onMounted(() => {
117
+ if (!toggle.value) return
118
+ dropdown = new Dropdown(toggle.value, { popperConfig: (config: object) => ({ ...config, strategy: 'fixed' }) })
119
+ // The chosen time (or the nearest after it) is scrolled into view.
120
+ toggle.value.addEventListener('shown.bs.dropdown', () => {
121
+ const at = current.value ?? 0
122
+ const index = Math.max(0, times.value.findIndex(t => t >= at))
123
+ const item = menu.value?.children[index] as HTMLElement | undefined
124
+ if (item && menu.value) menu.value.scrollTop = item.offsetTop - menu.value.clientHeight / 2 + item.clientHeight / 2
125
+ })
126
+ })
127
+ onBeforeUnmount(() => dropdown?.dispose())
128
+ function pick(t: number) {
129
+ model.value = toModel(t)
130
+ text.value = toText(t)
131
+ }
132
+
133
+ const wrong = computed(() => field.invalid.value || (current.value !== null && outside(current.value)))
134
+ </script>
135
+
136
+ <template>
137
+ <div :class="['input-group flex-nowrap t-time-input', size !== 'md' && `input-group-${size}`]">
138
+ <TInputMask :id="field.id.value" :model-value="text" :options="options" :placeholder="shownPlaceholder" :size="size" :disabled="disabled" :invalid="wrong"
139
+ class="flex-fill" @update:model-value="typed" @focusout="settle" />
140
+ <template v-if="step">
141
+ <button ref="toggle" type="button" class="btn btn-icon" data-bs-toggle="dropdown" aria-haspopup="listbox" aria-expanded="false"
142
+ :disabled="disabled" aria-label="Choose a time">
143
+ <TIcon name="clock" />
144
+ </button>
145
+ <div ref="menu" class="dropdown-menu dropdown-menu-end t-time-list" role="listbox">
146
+ <button v-for="t in times" :key="t" type="button" role="option" :aria-selected="t === current"
147
+ :class="['dropdown-item', { active: t === current }]" @click="pick(t)">{{ toText(t) }}</button>
148
+ </div>
149
+ </template>
150
+ </div>
151
+ </template>
152
+
153
+ <style scoped>
154
+ .t-time-input :deep(.form-control) { min-width: 0; }
155
+ .t-time-input > :first-child { min-width: 0; }
156
+ /* The field joins the clock button, as an input group's own fields do. */
157
+ .t-time-input:has(> .btn) > :first-child :deep(.form-control) { border-top-right-radius: 0; border-bottom-right-radius: 0; }
158
+ .t-time-list { max-height: 16rem; overflow-y: auto; min-width: 8rem; }
159
+ </style>
@@ -1,23 +1,47 @@
1
1
  <script setup lang="ts">
2
- // Shows the toasts added with useToast(), stacked in the bottom corner. Put one in the app's frame.
2
+ // Shows the toasts added with useToast(), stacked where each asks to be (`position`), or where this
3
+ // says: a bottom or top corner, or the middle of the top or bottom. Put one in the app's frame.
4
+ import { computed } from 'vue'
3
5
  import TIcon from './TIcon.vue'
4
- import { useToast } from '../composables/useToast'
6
+ import { useToast, type Toast, type ToastPosition } from '../composables/useToast'
5
7
  import { toTabler } from '../types'
6
8
 
9
+ const props = withDefaults(defineProps<{ position?: ToastPosition }>(), { position: 'bottom-end' })
10
+
7
11
  const { toasts, remove } = useToast()
12
+
13
+ const places: Record<ToastPosition, string> = {
14
+ 'top-start': 'top-0 start-0',
15
+ 'top-center': 'top-0 start-50 translate-middle-x',
16
+ 'top-end': 'top-0 end-0',
17
+ 'bottom-start': 'bottom-0 start-0',
18
+ 'bottom-center': 'bottom-0 start-50 translate-middle-x',
19
+ 'bottom-end': 'bottom-0 end-0'
20
+ }
21
+ const stacks = computed(() => {
22
+ const by = new Map<ToastPosition, Toast[]>()
23
+ for (const toast of toasts.value) {
24
+ const at = toast.position ?? props.position
25
+ by.set(at, [...(by.get(at) ?? []), toast])
26
+ }
27
+ return [...by].map(([position, list]) => ({ position, list }))
28
+ })
29
+ const iconClass = (toast: Toast) => (toast.accent ? 't-toast-accent-icon' : toast.color && toast.color !== 'neutral' ? `text-${toTabler(toast.color)}` : 'text-secondary')
8
30
  </script>
9
31
 
10
32
  <template>
11
33
  <Teleport to="body">
12
- <div class="toast-container position-fixed bottom-0 end-0 p-3" aria-live="polite">
34
+ <div v-for="stack in stacks" :key="stack.position" :class="['toast-container position-fixed p-3', places[stack.position]]" aria-live="polite">
13
35
  <TransitionGroup name="t-toast">
14
- <div v-for="toast in toasts" :key="toast.id" class="toast show t-toast" role="status">
36
+ <div v-for="toast in stack.list" :key="toast.id" :class="['toast show t-toast', { 't-toast-accent': toast.accent, 't-toast-clickable': toast.onClick }]"
37
+ :style="toast.accent ? { '--t-toast-accent': toast.accent } : undefined" role="status">
15
38
  <div class="toast-body d-flex align-items-start gap-2">
16
- <TIcon v-if="toast.icon" :name="toast.icon" :class="['mt-1', toast.color && toast.color !== 'neutral' ? `text-${toTabler(toast.color)}` : 'text-secondary']" />
17
- <div class="flex-fill min-w-0">
39
+ <TIcon v-if="toast.icon" :name="toast.icon" :class="['mt-1', iconClass(toast)]" />
40
+ <component :is="toast.onClick ? 'button' : 'div'" :type="toast.onClick ? 'button' : undefined" class="flex-fill min-w-0 t-toast-text"
41
+ @click="toast.onClick?.()">
18
42
  <div class="fw-medium">{{ toast.title }}</div>
19
43
  <div v-if="toast.description" class="text-secondary small mt-1">{{ toast.description }}</div>
20
- </div>
44
+ </component>
21
45
  <button type="button" class="btn-close ms-2" aria-label="Close" @click="remove(toast.id)" />
22
46
  </div>
23
47
  </div>
@@ -25,3 +49,10 @@ const { toasts, remove } = useToast()
25
49
  </div>
26
50
  </Teleport>
27
51
  </template>
52
+
53
+ <style scoped>
54
+ .t-toast-accent { border-inline-start: 3px solid var(--t-toast-accent); }
55
+ .t-toast-accent-icon { color: var(--t-toast-accent); }
56
+ .t-toast-text { text-align: start; padding: 0; border: 0; background: none; color: inherit; font: inherit; }
57
+ .t-toast-clickable .t-toast-text { cursor: pointer; }
58
+ </style>
@@ -0,0 +1,49 @@
1
+ <script setup lang="ts">
2
+ // A dropdown menu's items, in groups split by dividers: links (`to`, `href`), actions (`onSelect`)
3
+ // and `label` headings. Shared by TDropdown and TSplitButton; `select` reports the item chosen.
4
+ import { computed } from 'vue'
5
+ import { RouterLink } from 'vue-router'
6
+ import TIcon from '../TIcon.vue'
7
+ import { toTabler, type DropdownItem } from '../../types'
8
+
9
+ const props = defineProps<{ items: DropdownItem[] | DropdownItem[][] }>()
10
+ const emit = defineEmits<{ select: [item: DropdownItem, event: Event] }>()
11
+
12
+ const groups = computed(() => (Array.isArray(props.items[0]) ? props.items : [props.items]) as DropdownItem[][])
13
+
14
+ function tag(item: DropdownItem) {
15
+ return item.to && !item.disabled ? RouterLink : item.href ? 'a' : 'button'
16
+ }
17
+ function attrs(item: DropdownItem) {
18
+ if (item.to && !item.disabled) return { to: item.to }
19
+ if (item.href) return { href: item.disabled ? undefined : item.href }
20
+ return { type: 'button', disabled: item.disabled }
21
+ }
22
+ function choose(item: DropdownItem, event: Event) {
23
+ if (item.disabled) return
24
+ item.onSelect?.(event)
25
+ emit('select', item, event)
26
+ }
27
+ </script>
28
+
29
+ <template>
30
+ <template v-for="(group, g) in groups" :key="g">
31
+ <div v-if="g > 0 && group.length" class="dropdown-divider" />
32
+ <template v-for="(item, i) in group" :key="`${g}-${i}`">
33
+ <div v-if="item.type === 'label'" class="dropdown-header d-block">
34
+ <div class="text-body fw-medium text-truncate">{{ item.label }}</div>
35
+ <div v-if="item.description" class="text-secondary text-truncate fw-normal">{{ item.description }}</div>
36
+ </div>
37
+ <component :is="tag(item)" v-else v-bind="attrs(item)" role="menuitem"
38
+ :class="['dropdown-item', { disabled: item.disabled }, item.color && `text-${toTabler(item.color)}`]"
39
+ @click="choose(item, $event)">
40
+ <TIcon v-if="item.icon" :name="item.icon" class="dropdown-item-icon" />
41
+ <span class="min-w-0">
42
+ <span class="d-block text-truncate">{{ item.label }}</span>
43
+ <span v-if="item.description" class="d-block small text-secondary text-truncate">{{ item.description }}</span>
44
+ </span>
45
+ <TIcon v-if="item.trailingIcon" :name="item.trailingIcon" class="ms-auto ps-2" />
46
+ </component>
47
+ </template>
48
+ </template>
49
+ </template>
@@ -1,33 +1,55 @@
1
- // Short notices in the corner ("Saved.", "Deleted."), shown by a TToaster in the app's frame:
1
+ // Short notices ("Saved.", "Deleted."), shown by a TToaster in the app's frame:
2
2
  // `useToast().add({ title, description, color, icon })`. They close by themselves after
3
- // `duration` milliseconds (5 seconds; 0 keeps one until it's closed).
3
+ // `duration` milliseconds (5 seconds; 0 keeps one until it's closed), and always have a close
4
+ // button. `color` is a preset (neutral, success, warning, error, info, primary); `accent` is any CSS
5
+ // color instead, for the icon and the edge. `position` puts it in a corner, or the middle of the top
6
+ // or bottom (the TToaster's `position` when left out). `onClick` makes it clickable; `onClose` is
7
+ // called however it closes.
4
8
  import { ref } from 'vue'
5
9
  import type { Color } from '../types'
6
10
 
11
+ export type ToastPosition = 'top-start' | 'top-center' | 'top-end' | 'bottom-start' | 'bottom-center' | 'bottom-end'
12
+
7
13
  export interface Toast {
8
14
  id: number
9
15
  title: string
10
16
  description?: string
11
17
  color?: Color
18
+ /** A CSS color (#d63939, rgb(…), var(--tblr-…)) for the icon and the edge, instead of `color`. */
19
+ accent?: string
12
20
  icon?: string
13
21
  duration?: number
22
+ position?: ToastPosition
23
+ onClick?: () => void
24
+ onClose?: () => void
14
25
  }
15
26
 
16
27
  const toasts = ref<Toast[]>([])
17
28
  let nextId = 1
29
+ const timers = new Map<number, ReturnType<typeof setTimeout>>()
18
30
 
19
31
  function remove(id: number) {
32
+ const toast = toasts.value.find(t => t.id === id)
33
+ if (!toast) return
34
+ clearTimeout(timers.get(id))
35
+ timers.delete(id)
20
36
  toasts.value = toasts.value.filter(t => t.id !== id)
37
+ toast.onClose?.()
21
38
  }
22
39
 
23
40
  function add(toast: Omit<Toast, 'id'>) {
24
41
  const id = nextId++
25
42
  toasts.value = [...toasts.value, { ...toast, id }]
26
43
  const duration = toast.duration ?? 5000
27
- if (duration > 0) setTimeout(() => remove(id), duration)
44
+ if (duration > 0) timers.set(id, setTimeout(() => remove(id), duration))
28
45
  return id
29
46
  }
30
47
 
48
+ /** Closes every toast. */
49
+ function clear() {
50
+ for (const t of [...toasts.value]) remove(t.id)
51
+ }
52
+
31
53
  export function useToast() {
32
- return { toasts, add, remove }
54
+ return { toasts, add, remove, clear }
33
55
  }