@ternarytech-io/gears-ui 0.11.0 → 0.12.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 +1 -1
- package/src/components/TBreadcrumbs.vue +100 -0
- package/src/components/TContextMenu.vue +101 -0
- package/src/components/TTree.vue +226 -0
- package/src/components/internal/ContextMenuPanel.vue +157 -0
- package/src/index.ts +10 -1
- package/src/types.ts +38 -0
package/package.json
CHANGED
|
@@ -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>
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// A tree that opens and closes, such as folders: v-model:selected is the chosen node's id, and
|
|
3
|
+
// v-model:expanded the open ones'. A node's children are its `children`, or, the first time it
|
|
4
|
+
// opens, what `load` gives (a node with neither is openable until loaded; `leaf` says it has
|
|
5
|
+
// none). It follows the ARIA tree pattern: Up and Down move, Right opens or goes in, Left closes or
|
|
6
|
+
// goes up, Home and End, Enter or Space chooses, and typing a letter jumps to the next node it
|
|
7
|
+
// starts. Nodes can take drops (`droppable` says which, for a dragover) and be dragged
|
|
8
|
+
// (`draggable`), for moving things into folders. The `label` slot draws a row's text, `actions`
|
|
9
|
+
// its trailing buttons. `reload(id)` reads a node's children again; `reveal(ids)` opens a path.
|
|
10
|
+
import { computed, nextTick, reactive, ref } from 'vue'
|
|
11
|
+
import TIcon from './TIcon.vue'
|
|
12
|
+
import TSpinner from './TSpinner.vue'
|
|
13
|
+
import type { TreeNode } from '../types'
|
|
14
|
+
|
|
15
|
+
const props = withDefaults(defineProps<{
|
|
16
|
+
nodes: TreeNode[]
|
|
17
|
+
load?: (node: TreeNode) => Promise<TreeNode[]>
|
|
18
|
+
/** Whether a drag over a node may drop there. */
|
|
19
|
+
droppable?: (node: TreeNode, event: DragEvent) => boolean
|
|
20
|
+
/** Whether a node can be dragged. */
|
|
21
|
+
draggable?: (node: TreeNode) => boolean
|
|
22
|
+
size?: 'sm' | 'md'
|
|
23
|
+
/** The tree's accessible name. */
|
|
24
|
+
label?: string
|
|
25
|
+
}>(), { size: 'md', label: 'Tree' })
|
|
26
|
+
|
|
27
|
+
const selected = defineModel<string | null>('selected', { default: null })
|
|
28
|
+
const expanded = defineModel<string[]>('expanded', { default: () => [] })
|
|
29
|
+
const emit = defineEmits<{
|
|
30
|
+
select: [node: TreeNode]
|
|
31
|
+
open: [node: TreeNode]
|
|
32
|
+
drop: [drop: { node: TreeNode; event: DragEvent }]
|
|
33
|
+
dragstart: [drag: { node: TreeNode; event: DragEvent }]
|
|
34
|
+
}>()
|
|
35
|
+
defineSlots<{ label?(scope: { node: TreeNode; depth: number }): unknown; actions?(scope: { node: TreeNode; depth: number }): unknown }>()
|
|
36
|
+
|
|
37
|
+
// Children loaded (by id), loading, or failed.
|
|
38
|
+
const loaded = reactive(new Map<string, TreeNode[]>())
|
|
39
|
+
const loading = reactive(new Set<string>())
|
|
40
|
+
const failed = reactive(new Map<string, string>())
|
|
41
|
+
const focused = ref<string | null>(null)
|
|
42
|
+
const over = ref<string | null>(null)
|
|
43
|
+
const root = ref<HTMLElement>()
|
|
44
|
+
|
|
45
|
+
const childrenOf = (node: TreeNode) => node.children ?? loaded.get(node.id)
|
|
46
|
+
// Openable unless it's a leaf or was given no children; one loaded empty stays openable, and shows Empty.
|
|
47
|
+
const openable = (node: TreeNode) => node.leaf !== true && !(node.children && node.children.length === 0)
|
|
48
|
+
const isOpen = (node: TreeNode) => expanded.value.includes(node.id)
|
|
49
|
+
|
|
50
|
+
interface Row { node: TreeNode; depth: number; parent: TreeNode | null }
|
|
51
|
+
// The nodes shown, in order: each open node's children under it.
|
|
52
|
+
const rows = computed(() => {
|
|
53
|
+
const list: Row[] = []
|
|
54
|
+
const walk = (nodes: TreeNode[], depth: number, parent: TreeNode | null) => {
|
|
55
|
+
for (const node of nodes) {
|
|
56
|
+
list.push({ node, depth, parent })
|
|
57
|
+
const children = childrenOf(node)
|
|
58
|
+
if (isOpen(node) && children) walk(children, depth + 1, node)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
walk(props.nodes, 1, null)
|
|
62
|
+
return list
|
|
63
|
+
})
|
|
64
|
+
const tabStop = computed(() => focused.value ?? selected.value ?? rows.value[0]?.node.id ?? null)
|
|
65
|
+
|
|
66
|
+
async function fetch(node: TreeNode, again = false) {
|
|
67
|
+
if (!props.load || node.children || (!again && loaded.has(node.id)) || loading.has(node.id)) return
|
|
68
|
+
loading.add(node.id)
|
|
69
|
+
failed.delete(node.id)
|
|
70
|
+
try {
|
|
71
|
+
loaded.set(node.id, await props.load(node))
|
|
72
|
+
} catch (e) {
|
|
73
|
+
failed.set(node.id, e instanceof Error ? e.message : 'It couldn\'t be read.')
|
|
74
|
+
} finally {
|
|
75
|
+
loading.delete(node.id)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async function openNode(node: TreeNode) {
|
|
79
|
+
if (isOpen(node) || !openable(node)) return
|
|
80
|
+
expanded.value = [...expanded.value, node.id]
|
|
81
|
+
emit('open', node)
|
|
82
|
+
await fetch(node)
|
|
83
|
+
}
|
|
84
|
+
function closeNode(node: TreeNode) {
|
|
85
|
+
expanded.value = expanded.value.filter(id => id !== node.id)
|
|
86
|
+
}
|
|
87
|
+
function toggle(node: TreeNode) {
|
|
88
|
+
if (isOpen(node)) closeNode(node)
|
|
89
|
+
else void openNode(node)
|
|
90
|
+
}
|
|
91
|
+
function choose(node: TreeNode) {
|
|
92
|
+
if (node.disabled) return
|
|
93
|
+
selected.value = node.id
|
|
94
|
+
focus(node.id)
|
|
95
|
+
emit('select', node)
|
|
96
|
+
}
|
|
97
|
+
async function focus(id: string) {
|
|
98
|
+
focused.value = id
|
|
99
|
+
await nextTick()
|
|
100
|
+
root.value?.querySelector<HTMLElement>(`[data-node="${CSS.escape(id)}"]`)?.focus()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Reads a node's children again (after one is added, renamed or moved); its own id, or the top for none. */
|
|
104
|
+
async function reload(id?: string) {
|
|
105
|
+
if (!id) return
|
|
106
|
+
const row = rows.value.find(r => r.node.id === id)
|
|
107
|
+
if (row) await fetch(row.node, true)
|
|
108
|
+
else loaded.delete(id)
|
|
109
|
+
}
|
|
110
|
+
/** Opens each node of a path, top first, loading as it goes, and chooses the last. */
|
|
111
|
+
async function reveal(ids: string[]) {
|
|
112
|
+
for (const [i, id] of ids.entries()) {
|
|
113
|
+
const row = rows.value.find(r => r.node.id === id)
|
|
114
|
+
if (!row) return
|
|
115
|
+
if (i < ids.length - 1) await openNode(row.node)
|
|
116
|
+
else choose(row.node)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
defineExpose({ reload, reveal })
|
|
120
|
+
|
|
121
|
+
function key(event: KeyboardEvent, row: Row) {
|
|
122
|
+
const index = rows.value.indexOf(row)
|
|
123
|
+
const at = (i: number) => rows.value[Math.max(0, Math.min(rows.value.length - 1, i))]!
|
|
124
|
+
switch (event.key) {
|
|
125
|
+
case 'ArrowDown': void focus(at(index + 1).node.id); break
|
|
126
|
+
case 'ArrowUp': void focus(at(index - 1).node.id); break
|
|
127
|
+
case 'Home': void focus(at(0).node.id); break
|
|
128
|
+
case 'End': void focus(at(rows.value.length - 1).node.id); break
|
|
129
|
+
case 'ArrowRight':
|
|
130
|
+
if (!isOpen(row.node) && openable(row.node)) void openNode(row.node)
|
|
131
|
+
else if (childrenOf(row.node)?.[0]) void focus(childrenOf(row.node)![0]!.id)
|
|
132
|
+
break
|
|
133
|
+
case 'ArrowLeft':
|
|
134
|
+
if (isOpen(row.node)) closeNode(row.node)
|
|
135
|
+
else if (row.parent) void focus(row.parent.id)
|
|
136
|
+
break
|
|
137
|
+
case 'Enter':
|
|
138
|
+
case ' ':
|
|
139
|
+
choose(row.node)
|
|
140
|
+
break
|
|
141
|
+
default:
|
|
142
|
+
// Typing a letter: the next node whose label starts with it.
|
|
143
|
+
if (event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
144
|
+
const letter = event.key.toLowerCase()
|
|
145
|
+
const after = [...rows.value.slice(index + 1), ...rows.value.slice(0, index + 1)]
|
|
146
|
+
const next = after.find(r => r.node.label.toLowerCase().startsWith(letter))
|
|
147
|
+
if (next) void focus(next.node.id)
|
|
148
|
+
break
|
|
149
|
+
}
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
event.preventDefault()
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function dragOver(event: DragEvent, node: TreeNode) {
|
|
156
|
+
if (!props.droppable?.(node, event)) return
|
|
157
|
+
event.preventDefault()
|
|
158
|
+
over.value = node.id
|
|
159
|
+
}
|
|
160
|
+
function dropped(event: DragEvent, node: TreeNode) {
|
|
161
|
+
over.value = null
|
|
162
|
+
if (!props.droppable?.(node, event)) return
|
|
163
|
+
event.preventDefault()
|
|
164
|
+
emit('drop', { node, event })
|
|
165
|
+
}
|
|
166
|
+
function dragStart(event: DragEvent, node: TreeNode) {
|
|
167
|
+
emit('dragstart', { node, event })
|
|
168
|
+
}
|
|
169
|
+
</script>
|
|
170
|
+
|
|
171
|
+
<template>
|
|
172
|
+
<div ref="root" :class="['t-tree', `t-tree-${size}`]" role="tree" :aria-label="label">
|
|
173
|
+
<template v-for="row in rows" :key="row.node.id">
|
|
174
|
+
<div :data-node="row.node.id" role="treeitem" :aria-level="row.depth" :aria-expanded="openable(row.node) ? isOpen(row.node) : undefined"
|
|
175
|
+
:aria-selected="selected === row.node.id" :aria-disabled="row.node.disabled || undefined" :tabindex="tabStop === row.node.id ? 0 : -1"
|
|
176
|
+
:draggable="draggable?.(row.node) || undefined"
|
|
177
|
+
:class="['t-tree-row', { selected: selected === row.node.id, over: over === row.node.id, disabled: row.node.disabled }]"
|
|
178
|
+
:style="{ paddingInlineStart: `${(row.depth - 1) * 1.125 + 0.25}rem` }"
|
|
179
|
+
@click="choose(row.node)" @keydown="key($event, row)" @focus="focused = row.node.id"
|
|
180
|
+
@dragover="dragOver($event, row.node)" @dragleave="over = over === row.node.id ? null : over" @drop="dropped($event, row.node)"
|
|
181
|
+
@dragstart="dragStart($event, row.node)">
|
|
182
|
+
<button v-if="openable(row.node)" type="button" class="t-tree-toggle" tabindex="-1" :aria-label="isOpen(row.node) ? 'Close' : 'Open'" @click.stop="toggle(row.node)">
|
|
183
|
+
<TSpinner v-if="loading.has(row.node.id)" size="sm" />
|
|
184
|
+
<TIcon v-else :name="isOpen(row.node) ? 'chevron-down' : 'chevron-right'" />
|
|
185
|
+
</button>
|
|
186
|
+
<span v-else class="t-tree-toggle" aria-hidden="true" />
|
|
187
|
+
<TIcon v-if="row.node.icon" :name="isOpen(row.node) && row.node.openIcon ? row.node.openIcon : row.node.icon" class="t-tree-icon" />
|
|
188
|
+
<span class="t-tree-label">
|
|
189
|
+
<slot name="label" :node="row.node" :depth="row.depth">{{ row.node.label }}</slot>
|
|
190
|
+
</span>
|
|
191
|
+
<span v-if="$slots.actions" class="t-tree-actions" @click.stop><slot name="actions" :node="row.node" :depth="row.depth" /></span>
|
|
192
|
+
</div>
|
|
193
|
+
<div v-if="failed.has(row.node.id) && isOpen(row.node)" class="t-tree-problem" :style="{ paddingInlineStart: `${row.depth * 1.125 + 0.5}rem` }">
|
|
194
|
+
{{ failed.get(row.node.id) }}
|
|
195
|
+
<button type="button" class="btn btn-link btn-sm p-0 ms-1" @click="fetch(row.node, true)">Try again</button>
|
|
196
|
+
</div>
|
|
197
|
+
<div v-else-if="isOpen(row.node) && childrenOf(row.node)?.length === 0" class="t-tree-empty" :style="{ paddingInlineStart: `${row.depth * 1.125 + 1.25}rem` }">
|
|
198
|
+
Empty
|
|
199
|
+
</div>
|
|
200
|
+
</template>
|
|
201
|
+
</div>
|
|
202
|
+
</template>
|
|
203
|
+
|
|
204
|
+
<style scoped>
|
|
205
|
+
.t-tree { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
|
|
206
|
+
.t-tree-sm { font-size: .8125rem; }
|
|
207
|
+
.t-tree-row {
|
|
208
|
+
display: flex; align-items: center; gap: .25rem; min-height: 1.875rem; padding-inline-end: .25rem;
|
|
209
|
+
border-radius: var(--tblr-border-radius, 4px); cursor: pointer; user-select: none; min-width: 0;
|
|
210
|
+
}
|
|
211
|
+
.t-tree-sm .t-tree-row { min-height: 1.625rem; }
|
|
212
|
+
.t-tree-row:hover { background: var(--tblr-bg-surface-secondary); }
|
|
213
|
+
.t-tree-row:focus-visible { outline: 2px solid var(--tblr-primary); outline-offset: -2px; }
|
|
214
|
+
.t-tree-row.selected { background: var(--tblr-primary-lt); color: var(--tblr-primary); }
|
|
215
|
+
.t-tree-row.over { box-shadow: inset 0 0 0 2px var(--tblr-primary); background: var(--tblr-primary-lt); }
|
|
216
|
+
.t-tree-row.disabled { opacity: .55; cursor: default; }
|
|
217
|
+
.t-tree-toggle { display: inline-flex; align-items: center; justify-content: center; width: 1.25rem; height: 1.25rem; flex: none; padding: 0; border: 0; background: none; color: var(--tblr-secondary); border-radius: 3px; }
|
|
218
|
+
button.t-tree-toggle:hover { background: var(--tblr-bg-surface-tertiary, rgba(0, 0, 0, .06)); }
|
|
219
|
+
.t-tree-icon { flex: none; color: var(--tblr-secondary); }
|
|
220
|
+
.t-tree-row.selected .t-tree-icon { color: inherit; }
|
|
221
|
+
.t-tree-label { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
222
|
+
.t-tree-actions { display: inline-flex; gap: .125rem; opacity: 0; }
|
|
223
|
+
.t-tree-row:hover .t-tree-actions, .t-tree-row:focus-within .t-tree-actions, .t-tree-row.selected .t-tree-actions { opacity: 1; }
|
|
224
|
+
.t-tree-problem { font-size: .8125rem; color: var(--tblr-danger); padding-block: .125rem; }
|
|
225
|
+
.t-tree-empty { font-size: .8125rem; color: var(--tblr-secondary); padding-block: .125rem; }
|
|
226
|
+
</style>
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
// One level of a TContextMenu: its items in groups split by dividers, at a point on the screen
|
|
3
|
+
// (kept inside the window), with the keyboard: arrows move, Enter and Space choose, Right opens a
|
|
4
|
+
// submenu, Left and Escape close this level. A submenu is another panel, beside its item.
|
|
5
|
+
import { computed, nextTick, onMounted, ref, watch } from 'vue'
|
|
6
|
+
import TIcon from '../TIcon.vue'
|
|
7
|
+
import { toTabler, type ContextMenuItem } from '../../types'
|
|
8
|
+
|
|
9
|
+
defineOptions({ name: 'ContextMenuPanel' })
|
|
10
|
+
|
|
11
|
+
// `backX`: for a submenu, where its item starts, to open to the left of it when there's no room on the right.
|
|
12
|
+
const props = defineProps<{ items: ContextMenuItem[] | ContextMenuItem[][]; x: number; y: number; level: number; label?: string; backX?: number }>()
|
|
13
|
+
const emit = defineEmits<{ choose: [item: ContextMenuItem, event: Event]; close: [all: boolean] }>()
|
|
14
|
+
|
|
15
|
+
const groups = computed(() => (Array.isArray(props.items[0]) ? props.items : [props.items]) as ContextMenuItem[][])
|
|
16
|
+
// Every row in order, for the keyboard: choosable items only.
|
|
17
|
+
const rows = computed(() => groups.value.flat().filter(i => i.type !== 'label'))
|
|
18
|
+
const panel = ref<HTMLElement>()
|
|
19
|
+
const place = ref({ left: props.x, top: props.y })
|
|
20
|
+
const active = ref(-1)
|
|
21
|
+
const sub = ref<{ item: ContextMenuItem; x: number; y: number; backX: number } | null>(null)
|
|
22
|
+
|
|
23
|
+
// Inside the window: a menu that would go off the right or the bottom opens the other way.
|
|
24
|
+
async function fit() {
|
|
25
|
+
await nextTick()
|
|
26
|
+
const el = panel.value
|
|
27
|
+
if (!el) return
|
|
28
|
+
const { width, height } = el.getBoundingClientRect()
|
|
29
|
+
const margin = 4
|
|
30
|
+
let left = props.x
|
|
31
|
+
let top = props.y
|
|
32
|
+
if (left + width > window.innerWidth - margin) left = props.backX !== undefined ? props.backX - width : window.innerWidth - width - margin
|
|
33
|
+
if (top + height > window.innerHeight - margin) top = Math.max(margin, window.innerHeight - height - margin)
|
|
34
|
+
place.value = { left: Math.max(margin, left), top: Math.max(margin, top) }
|
|
35
|
+
}
|
|
36
|
+
onMounted(async () => {
|
|
37
|
+
await fit()
|
|
38
|
+
panel.value?.focus()
|
|
39
|
+
move(1)
|
|
40
|
+
})
|
|
41
|
+
watch(() => [props.x, props.y], fit)
|
|
42
|
+
|
|
43
|
+
function rowEl(index: number) {
|
|
44
|
+
return panel.value?.querySelectorAll<HTMLElement>('[data-row]')[index]
|
|
45
|
+
}
|
|
46
|
+
function move(by: number) {
|
|
47
|
+
const count = rows.value.length
|
|
48
|
+
if (!count) return
|
|
49
|
+
let next = active.value
|
|
50
|
+
for (let step = 0; step < count; step++) {
|
|
51
|
+
next = (next + by + count) % count
|
|
52
|
+
if (!rows.value[next]!.disabled) break
|
|
53
|
+
}
|
|
54
|
+
active.value = next
|
|
55
|
+
rowEl(next)?.focus()
|
|
56
|
+
}
|
|
57
|
+
function openSub(item: ContextMenuItem, index: number) {
|
|
58
|
+
const el = rowEl(index)
|
|
59
|
+
if (!el || !item.children?.length) return
|
|
60
|
+
const rect = el.getBoundingClientRect()
|
|
61
|
+
sub.value = { item, x: rect.right - 2, y: rect.top - 4, backX: rect.left + 2 }
|
|
62
|
+
}
|
|
63
|
+
function choose(item: ContextMenuItem, event: Event) {
|
|
64
|
+
if (item.disabled) return
|
|
65
|
+
if (item.children?.length) {
|
|
66
|
+
openSub(item, rows.value.indexOf(item))
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
emit('choose', item, event)
|
|
70
|
+
}
|
|
71
|
+
function key(event: KeyboardEvent) {
|
|
72
|
+
const item = rows.value[active.value]
|
|
73
|
+
switch (event.key) {
|
|
74
|
+
case 'ArrowDown': move(1); break
|
|
75
|
+
case 'ArrowUp': move(-1); break
|
|
76
|
+
case 'Home': active.value = -1; move(1); break
|
|
77
|
+
case 'End': active.value = rows.value.length; move(-1); break
|
|
78
|
+
case 'ArrowRight':
|
|
79
|
+
if (item?.children?.length && !item.disabled) openSub(item, active.value)
|
|
80
|
+
break
|
|
81
|
+
case 'ArrowLeft':
|
|
82
|
+
if (props.level > 0) emit('close', false)
|
|
83
|
+
break
|
|
84
|
+
case 'Enter':
|
|
85
|
+
case ' ':
|
|
86
|
+
if (item) choose(item, event)
|
|
87
|
+
break
|
|
88
|
+
case 'Escape':
|
|
89
|
+
emit('close', props.level === 0)
|
|
90
|
+
break
|
|
91
|
+
case 'Tab':
|
|
92
|
+
emit('close', true)
|
|
93
|
+
break
|
|
94
|
+
default:
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
event.preventDefault()
|
|
98
|
+
event.stopPropagation()
|
|
99
|
+
}
|
|
100
|
+
function hover(item: ContextMenuItem, index: number) {
|
|
101
|
+
active.value = index
|
|
102
|
+
rowEl(index)?.focus()
|
|
103
|
+
if (item.children?.length && !item.disabled) openSub(item, index)
|
|
104
|
+
else sub.value = null
|
|
105
|
+
}
|
|
106
|
+
function subClosed(all: boolean) {
|
|
107
|
+
const back = sub.value ? rows.value.indexOf(sub.value.item) : -1
|
|
108
|
+
sub.value = null
|
|
109
|
+
if (all) emit('close', true)
|
|
110
|
+
else if (back >= 0) rowEl(back)?.focus()
|
|
111
|
+
}
|
|
112
|
+
</script>
|
|
113
|
+
|
|
114
|
+
<template>
|
|
115
|
+
<div ref="panel" class="t-context-menu" role="menu" tabindex="-1" :aria-label="label"
|
|
116
|
+
:style="{ position: 'fixed', left: `${place.left}px`, top: `${place.top}px` }" @keydown="key" @contextmenu.prevent>
|
|
117
|
+
<template v-for="(group, g) in groups" :key="g">
|
|
118
|
+
<div v-if="g > 0 && group.length" class="dropdown-divider" />
|
|
119
|
+
<template v-for="(item, i) in group" :key="`${g}-${i}`">
|
|
120
|
+
<div v-if="item.type === 'label'" class="dropdown-header d-block">
|
|
121
|
+
<div class="text-body fw-medium text-truncate">{{ item.label }}</div>
|
|
122
|
+
<div v-if="item.description" class="text-secondary text-truncate fw-normal">{{ item.description }}</div>
|
|
123
|
+
</div>
|
|
124
|
+
<button v-else type="button" data-row role="menuitem" :aria-disabled="item.disabled || undefined" :aria-haspopup="item.children?.length ? 'menu' : undefined"
|
|
125
|
+
:aria-expanded="item.children?.length ? sub?.item === item : undefined" tabindex="-1"
|
|
126
|
+
:class="['dropdown-item', { disabled: item.disabled, active: sub?.item === item }, item.color && !item.disabled && `text-${toTabler(item.color)}`]"
|
|
127
|
+
@click="choose(item, $event)" @mouseenter="hover(item, rows.indexOf(item))">
|
|
128
|
+
<TIcon v-if="item.icon" :name="item.icon" class="dropdown-item-icon" />
|
|
129
|
+
<span class="min-w-0 flex-fill">
|
|
130
|
+
<span class="d-block text-truncate">{{ item.label }}</span>
|
|
131
|
+
<span v-if="item.disabled && item.reason" class="d-block small text-secondary text-truncate">{{ item.reason }}</span>
|
|
132
|
+
<span v-else-if="item.description" class="d-block small text-secondary text-truncate">{{ item.description }}</span>
|
|
133
|
+
</span>
|
|
134
|
+
<kbd v-if="item.kbd" class="t-context-menu-kbd ms-3">{{ item.kbd }}</kbd>
|
|
135
|
+
<TIcon v-if="item.children?.length" name="chevron-right" class="ms-2" />
|
|
136
|
+
<TIcon v-else-if="item.trailingIcon" :name="item.trailingIcon" class="ms-auto ps-2" />
|
|
137
|
+
</button>
|
|
138
|
+
</template>
|
|
139
|
+
</template>
|
|
140
|
+
</div>
|
|
141
|
+
<ContextMenuPanel v-if="sub" :items="sub.item.children!" :x="sub.x" :y="sub.y" :back-x="sub.backX" :level="level + 1" :label="sub.item.label"
|
|
142
|
+
@choose="(item, event) => emit('choose', item, event)" @close="subClosed" />
|
|
143
|
+
</template>
|
|
144
|
+
|
|
145
|
+
<style scoped>
|
|
146
|
+
/* A dropdown menu's look, without Bootstrap's .dropdown-menu, whose keyboard handler expects a toggle. */
|
|
147
|
+
.t-context-menu {
|
|
148
|
+
min-width: 12rem; max-width: 22rem; z-index: 1090; margin: 0; padding: .25rem 0;
|
|
149
|
+
font-size: .875rem; color: var(--tblr-body-color); background: var(--tblr-bg-surface);
|
|
150
|
+
border: var(--tblr-border-width) solid var(--tblr-border-color-translucent, var(--tblr-border-color));
|
|
151
|
+
border-radius: var(--tblr-border-radius); box-shadow: var(--tblr-box-shadow-dropdown, 0 16px 24px 2px rgba(0, 0, 0, .07));
|
|
152
|
+
}
|
|
153
|
+
.t-context-menu:focus { outline: none; }
|
|
154
|
+
.t-context-menu .dropdown-item { display: flex; align-items: center; width: 100%; }
|
|
155
|
+
.t-context-menu .dropdown-item.disabled { pointer-events: auto; cursor: default; opacity: .55; }
|
|
156
|
+
.t-context-menu-kbd { font-size: .6875rem; padding: .0625rem .3rem; color: var(--tblr-secondary); background: transparent; border: 1px solid var(--tblr-border-color); box-shadow: none; }
|
|
157
|
+
</style>
|
package/src/index.ts
CHANGED
|
@@ -25,6 +25,9 @@ import TDataGrid from './components/TDataGrid.vue'
|
|
|
25
25
|
import TDataGridItem from './components/TDataGridItem.vue'
|
|
26
26
|
import TDensityPicker from './components/TDensityPicker.vue'
|
|
27
27
|
import TDropdown from './components/TDropdown.vue'
|
|
28
|
+
import TBreadcrumbs from './components/TBreadcrumbs.vue'
|
|
29
|
+
import TContextMenu from './components/TContextMenu.vue'
|
|
30
|
+
import TTree from './components/TTree.vue'
|
|
28
31
|
import TEditableTable from './components/TEditableTable.vue'
|
|
29
32
|
import TEmpty from './components/TEmpty.vue'
|
|
30
33
|
import TFieldGroup from './components/TFieldGroup.vue'
|
|
@@ -99,6 +102,9 @@ export {
|
|
|
99
102
|
TDataGridItem,
|
|
100
103
|
TDensityPicker,
|
|
101
104
|
TDropdown,
|
|
105
|
+
TBreadcrumbs,
|
|
106
|
+
TContextMenu,
|
|
107
|
+
TTree,
|
|
102
108
|
TEditableTable,
|
|
103
109
|
TEmpty,
|
|
104
110
|
TFieldGroup,
|
|
@@ -164,7 +170,7 @@ export { useToast, type Toast, type ToastPosition } from './composables/useToast
|
|
|
164
170
|
const components = {
|
|
165
171
|
TAccentPicker, TAdvancedTable, TAlert, TAppLayout, TAuthLayout, TAvatar, TBadge, TButton, TButtonGroup, TCard,
|
|
166
172
|
TCheckbox, TCheckboxTree, TCodeBlock, TColorModeButton, TCommandPalette, TContainerPicker, TCopyButton, TDataGrid, TDataGridItem, TDensityPicker,
|
|
167
|
-
TDropdown, TEditableTable, TEmpty, TFieldGroup, TFileUpload, TFontPicker, TFormField, TIcon, TIconPicker, TImageUpload,
|
|
173
|
+
TDropdown, TBreadcrumbs, TContextMenu, TTree, TEditableTable, TEmpty, TFieldGroup, TFileUpload, TFontPicker, TFormField, TIcon, TIconPicker, TImageUpload,
|
|
168
174
|
TInput, TInputNumber, TInputTags, TLightbox, TListBox, TLogView, TMethodBadge, TModal, TNav, TNavPositionPicker, TPage, TPagination,
|
|
169
175
|
TPanelList, TProgress, TRadioGroup, TRadiusPicker, TRating, TScaledFrame, TSearchButton, TSectionLayout,
|
|
170
176
|
TSelect, TSelectMenu, TSeparator, TShell, TSkeleton, TSlideover, TSparkline, TSpinner, TSplitButton, TSplitter, TStatCard,
|
|
@@ -201,6 +207,9 @@ declare module 'vue' {
|
|
|
201
207
|
TDataGridItem: typeof TDataGridItem
|
|
202
208
|
TDensityPicker: typeof TDensityPicker
|
|
203
209
|
TDropdown: typeof TDropdown
|
|
210
|
+
TBreadcrumbs: typeof TBreadcrumbs
|
|
211
|
+
TContextMenu: typeof TContextMenu
|
|
212
|
+
TTree: typeof TTree
|
|
204
213
|
TEditableTable: typeof TEditableTable
|
|
205
214
|
TEmpty: typeof TEmpty
|
|
206
215
|
TFieldGroup: typeof TFieldGroup
|
package/src/types.ts
CHANGED
|
@@ -34,6 +34,44 @@ export interface DropdownItem {
|
|
|
34
34
|
onSelect?: (event: Event) => void
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
/** A step of a TBreadcrumbs trail: a link (`to`, `href`), an action (`onSelect`), or plain text (the last, where you are). */
|
|
38
|
+
export interface BreadcrumbItem {
|
|
39
|
+
label: string
|
|
40
|
+
icon?: string
|
|
41
|
+
to?: Target
|
|
42
|
+
href?: string
|
|
43
|
+
onSelect?: (event: Event) => void
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* An item of a TContextMenu: a dropdown item, with a keyboard shortcut to show beside it
|
|
48
|
+
* (`kbd`, shown only), a submenu (`children`), and, for a disabled one, why (`reason`, shown
|
|
49
|
+
* under its label).
|
|
50
|
+
*/
|
|
51
|
+
export interface ContextMenuItem extends Omit<DropdownItem, 'to' | 'href'> {
|
|
52
|
+
kbd?: string
|
|
53
|
+
children?: ContextMenuItem[] | ContextMenuItem[][]
|
|
54
|
+
reason?: string
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* A node of a TTree: its id and label, an icon (and one for when it's open), and its children.
|
|
59
|
+
* Children may come later: `leaf: false` with no `children` makes it openable, and the tree's
|
|
60
|
+
* `load` fetches them the first time it opens.
|
|
61
|
+
*/
|
|
62
|
+
export interface TreeNode {
|
|
63
|
+
id: string
|
|
64
|
+
label: string
|
|
65
|
+
icon?: string
|
|
66
|
+
openIcon?: string
|
|
67
|
+
/** A node with no children to open; false (or children) makes it openable. Without either, it's openable until loaded. */
|
|
68
|
+
leaf?: boolean
|
|
69
|
+
children?: TreeNode[]
|
|
70
|
+
disabled?: boolean
|
|
71
|
+
/** Anything of the caller's: the tree only passes it back. */
|
|
72
|
+
data?: unknown
|
|
73
|
+
}
|
|
74
|
+
|
|
37
75
|
/** A button of a TButtonGroup: a label and/or an icon (give an icon-only one a tooltip). */
|
|
38
76
|
export interface ButtonGroupItem {
|
|
39
77
|
label?: string
|