@ternarytech-io/gears-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.
@@ -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
  }