@ternarytech-io/gears-ui 0.11.0 → 0.13.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ternarytech-io/gears-ui",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "type": "module",
5
5
  "description": "Gears UI: Vue 3 components on Tabler, used by the Gears Studio and the applications it builds.",
6
6
  "license": "MPL-2.0",
@@ -0,0 +1,100 @@
1
+ <script setup lang="ts">
2
+ // A trail of where you are: each step a link (`to`, `href`) or an action (`onSelect`), the last
3
+ // one plain text (aria-current). With `max` and more steps than that, the middle ones fold into a
4
+ // "…" menu, so a deep path keeps its first and last steps in view.
5
+ import { computed } from 'vue'
6
+ import { RouterLink } from 'vue-router'
7
+ import TIcon from './TIcon.vue'
8
+ import TDropdown from './TDropdown.vue'
9
+ import type { BreadcrumbItem, DropdownItem } from '../types'
10
+
11
+ const props = withDefaults(defineProps<{
12
+ items: BreadcrumbItem[]
13
+ /** The most steps shown before the middle ones fold into a menu; 0 for no limit. */
14
+ max?: number
15
+ /** The icon between steps. */
16
+ separator?: string
17
+ size?: 'sm' | 'md'
18
+ }>(), { max: 0, separator: 'chevron-right', size: 'md' })
19
+
20
+ const emit = defineEmits<{ select: [item: BreadcrumbItem, index: number] }>()
21
+
22
+ // The first step, the folded ones, and the last (max - 1) steps.
23
+ const shown = computed(() => {
24
+ const items = props.items
25
+ if (!props.max || items.length <= props.max || props.max < 2) return { head: items, folded: [] as BreadcrumbItem[], tail: [] as BreadcrumbItem[] }
26
+ const tail = Math.max(1, props.max - 1)
27
+ return { head: items.slice(0, 1), folded: items.slice(1, items.length - tail), tail: items.slice(items.length - tail) }
28
+ })
29
+ const foldedItems = computed<DropdownItem[]>(() => shown.value.folded.map((item, i) => ({
30
+ label: item.label, icon: item.icon, onSelect: (event: Event) => choose(item, i + 1, event)
31
+ })))
32
+
33
+ function indexOf(item: BreadcrumbItem) {
34
+ return props.items.indexOf(item)
35
+ }
36
+ function isLast(item: BreadcrumbItem) {
37
+ return indexOf(item) === props.items.length - 1
38
+ }
39
+ function choose(item: BreadcrumbItem, index: number, event: Event) {
40
+ item.onSelect?.(event)
41
+ emit('select', item, index)
42
+ }
43
+ function tag(item: BreadcrumbItem) {
44
+ return item.to ? RouterLink : item.href ? 'a' : 'button'
45
+ }
46
+ function attrs(item: BreadcrumbItem) {
47
+ return item.to ? { to: item.to } : item.href ? { href: item.href } : { type: 'button' }
48
+ }
49
+ </script>
50
+
51
+ <template>
52
+ <nav aria-label="Breadcrumbs" :class="['t-breadcrumbs', `t-breadcrumbs-${size}`]">
53
+ <ol class="t-breadcrumbs-list">
54
+ <template v-for="(item, i) in shown.head" :key="`h${i}`">
55
+ <li class="t-breadcrumbs-item">
56
+ <span v-if="isLast(item)" class="t-breadcrumbs-current" aria-current="page">
57
+ <TIcon v-if="item.icon" :name="item.icon" />{{ item.label }}
58
+ </span>
59
+ <component :is="tag(item)" v-else v-bind="attrs(item)" class="t-breadcrumbs-link" @click="choose(item, indexOf(item), $event)">
60
+ <TIcon v-if="item.icon" :name="item.icon" />{{ item.label }}
61
+ </component>
62
+ </li>
63
+ <li v-if="!isLast(item)" class="t-breadcrumbs-separator" aria-hidden="true"><TIcon :name="separator" /></li>
64
+ </template>
65
+ <template v-if="shown.folded.length">
66
+ <li class="t-breadcrumbs-item">
67
+ <TDropdown :items="foldedItems">
68
+ <button type="button" class="t-breadcrumbs-link" :aria-label="`${shown.folded.length} more steps`">…</button>
69
+ </TDropdown>
70
+ </li>
71
+ <li class="t-breadcrumbs-separator" aria-hidden="true"><TIcon :name="separator" /></li>
72
+ </template>
73
+ <template v-for="(item, i) in shown.tail" :key="`t${i}`">
74
+ <li class="t-breadcrumbs-item">
75
+ <span v-if="isLast(item)" class="t-breadcrumbs-current" aria-current="page">
76
+ <TIcon v-if="item.icon" :name="item.icon" />{{ item.label }}
77
+ </span>
78
+ <component :is="tag(item)" v-else v-bind="attrs(item)" class="t-breadcrumbs-link" @click="choose(item, indexOf(item), $event)">
79
+ <TIcon v-if="item.icon" :name="item.icon" />{{ item.label }}
80
+ </component>
81
+ </li>
82
+ <li v-if="!isLast(item)" class="t-breadcrumbs-separator" aria-hidden="true"><TIcon :name="separator" /></li>
83
+ </template>
84
+ </ol>
85
+ </nav>
86
+ </template>
87
+
88
+ <style scoped>
89
+ .t-breadcrumbs-list { display: flex; flex-wrap: wrap; align-items: center; gap: .25rem; list-style: none; margin: 0; padding: 0; min-width: 0; }
90
+ .t-breadcrumbs-sm { font-size: .8125rem; }
91
+ .t-breadcrumbs-item { display: inline-flex; align-items: center; min-width: 0; }
92
+ .t-breadcrumbs-link, .t-breadcrumbs-current { display: inline-flex; align-items: center; gap: .25rem; max-width: 16rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
93
+ .t-breadcrumbs-link {
94
+ background: none; border: 0; padding: .125rem .25rem; border-radius: var(--tblr-border-radius-sm, 4px);
95
+ color: var(--tblr-secondary); text-decoration: none; cursor: pointer;
96
+ }
97
+ .t-breadcrumbs-link:hover, .t-breadcrumbs-link:focus-visible { color: var(--tblr-body-color); background: var(--tblr-bg-surface-secondary); }
98
+ .t-breadcrumbs-current { padding: .125rem .25rem; color: var(--tblr-body-color); font-weight: var(--tblr-font-weight-medium, 500); }
99
+ .t-breadcrumbs-separator { display: inline-flex; color: var(--tblr-secondary); opacity: .6; font-size: .75em; }
100
+ </style>
@@ -0,0 +1,101 @@
1
+ <script setup lang="ts">
2
+ // A menu where you right-click, or press the Menu key or Shift+F10 on something focused: the
3
+ // default slot is the area it belongs to. Items are a dropdown's (groups split by dividers,
4
+ // `label` headings) with a shortcut to show (`kbd`), submenus (`children`) and, for a disabled
5
+ // item, why (`reason`). `items` can be a function of the event, so a list's rows each get their
6
+ // own menu (find the row from `event.target`); an empty list opens nothing. The menu is drawn on
7
+ // the body, at the pointer (or the focused element), kept inside the window; arrows, Enter and
8
+ // Escape work it, and focus goes back where it was when it closes. `open(x, y, items?)` opens it
9
+ // from code, such as a "…" button.
10
+ import { onBeforeUnmount, ref } from 'vue'
11
+ import ContextMenuPanel from './internal/ContextMenuPanel.vue'
12
+ import type { ContextMenuItem } from '../types'
13
+
14
+ // The area takes the attributes (class, style), not the menu drawn on the body.
15
+ defineOptions({ inheritAttrs: false })
16
+
17
+ type Items = ContextMenuItem[] | ContextMenuItem[][]
18
+
19
+ const props = withDefaults(defineProps<{
20
+ items: Items | ((event: MouseEvent | KeyboardEvent) => Items)
21
+ disabled?: boolean
22
+ /** The area's element. */
23
+ as?: string
24
+ /** The menu's accessible name. */
25
+ label?: string
26
+ }>(), { as: 'div', label: 'Actions' })
27
+
28
+ const emit = defineEmits<{ select: [item: ContextMenuItem, event: Event]; 'update:open': [open: boolean] }>()
29
+
30
+ const shown = ref<{ items: Items; x: number; y: number } | null>(null)
31
+ let returnTo: HTMLElement | null = null
32
+
33
+ const isEmpty = (items: Items) => (Array.isArray(items[0]) ? (items as ContextMenuItem[][]).every(g => g.length === 0) : items.length === 0)
34
+
35
+ /** Opens the menu at a point on the screen, with these items (or the menu's own). */
36
+ function open(x: number, y: number, items?: Items, event?: MouseEvent | KeyboardEvent) {
37
+ const list = items ?? (typeof props.items === 'function' ? props.items(event ?? new MouseEvent('contextmenu')) : props.items)
38
+ if (props.disabled || isEmpty(list)) return false
39
+ returnTo = document.activeElement instanceof HTMLElement ? document.activeElement : null
40
+ shown.value = { items: list, x, y }
41
+ emit('update:open', true)
42
+ document.addEventListener('pointerdown', outside, true)
43
+ window.addEventListener('blur', close)
44
+ window.addEventListener('resize', close)
45
+ document.addEventListener('scroll', close, true)
46
+ return true
47
+ }
48
+
49
+ /** Closes the menu, putting focus back where it was. */
50
+ function close() {
51
+ if (!shown.value) return
52
+ shown.value = null
53
+ emit('update:open', false)
54
+ document.removeEventListener('pointerdown', outside, true)
55
+ window.removeEventListener('blur', close)
56
+ window.removeEventListener('resize', close)
57
+ document.removeEventListener('scroll', close, true)
58
+ returnTo?.focus?.()
59
+ returnTo = null
60
+ }
61
+ defineExpose({ open, close })
62
+
63
+ function outside(event: Event) {
64
+ if (!(event.target instanceof Element) || !event.target.closest('.t-context-menu')) close()
65
+ }
66
+ function itemsFor(event: MouseEvent | KeyboardEvent): Items {
67
+ return typeof props.items === 'function' ? props.items(event) : props.items
68
+ }
69
+ function onContextMenu(event: MouseEvent) {
70
+ if (props.disabled) return
71
+ const items = itemsFor(event)
72
+ if (isEmpty(items)) return
73
+ event.preventDefault()
74
+ close()
75
+ open(event.clientX, event.clientY, items, event)
76
+ }
77
+ function onKey(event: KeyboardEvent) {
78
+ if (props.disabled || !(event.key === 'ContextMenu' || (event.key === 'F10' && event.shiftKey))) return
79
+ const items = itemsFor(event)
80
+ if (isEmpty(items)) return
81
+ event.preventDefault()
82
+ const rect = (event.target as HTMLElement).getBoundingClientRect()
83
+ close()
84
+ open(rect.left + Math.min(24, rect.width / 2), rect.top + Math.min(rect.height, 28), items, event)
85
+ }
86
+ function choose(item: ContextMenuItem, event: Event) {
87
+ close()
88
+ item.onSelect?.(event)
89
+ emit('select', item, event)
90
+ }
91
+ onBeforeUnmount(close)
92
+ </script>
93
+
94
+ <template>
95
+ <component :is="as" v-bind="$attrs" class="t-context-menu-area" @contextmenu="onContextMenu" @keydown="onKey">
96
+ <slot />
97
+ </component>
98
+ <Teleport to="body">
99
+ <ContextMenuPanel v-if="shown" :items="shown.items" :x="shown.x" :y="shown.y" :level="0" :label="label" @choose="choose" @close="close" />
100
+ </Teleport>
101
+ </template>