Line data Source code
1 : <template>
2 56493 : <div
3 : :class="
4 : cn(
5 : selectedCategory === node.key &&
6 : isExpanded &&
7 : node.children?.length &&
8 : 'rounded-lg bg-secondary'
9 : )
10 : "
11 : >
12 : <RovingFocusItem as-child>
13 : <Button
14 : ref="buttonEl"
15 : type="button"
16 : role="treeitem"
17 : :data-testid="`category-${node.key}`"
18 : :aria-current="selectedCategory === node.key || undefined"
19 : :aria-expanded="node.children?.length ? isExpanded : undefined"
20 : :style="{ paddingLeft: `${0.75 + depth * 1.25}rem` }"
21 : :class="
22 : cn(
23 : 'h-auto w-full bg-transparent py-2.5 pr-3 text-left text-sm font-normal',
24 : selectedCategory === node.key
25 : ? CATEGORY_SELECTED_CLASS
26 : : CATEGORY_UNSELECTED_CLASS
27 : )
28 : "
29 : @click="$emit('select', node.key)"
30 : @keydown.right.prevent="handleRight"
31 : @keydown.left.prevent="handleLeft"
32 : >
33 : <i
34 : v-if="!hideChevrons"
35 : :class="
36 : cn(
37 : 'text-muted-foreground transition-[transform,opacity] duration-150',
38 : node.children?.length
39 : ? 'icon-[lucide--chevron-down] opacity-0 group-hover/categories:opacity-100 [[data-slot=node-search-categories]:has(:focus-visible)_&]:opacity-100'
40 : : 'size-4 shrink-0',
41 : node.children?.length && !isExpanded && '-rotate-90'
42 : )
43 : "
44 : />
45 : <span class="flex-1 truncate">{{ node.label }}</span>
46 : </Button>
47 : </RovingFocusItem>
48 : <div v-if="isExpanded && node.children?.length" role="group">
49 182 : <NodeSearchCategoryTreeNode
50 : v-for="child in node.children"
51 : :key="child.key"
52 : ref="childRefs"
53 : :node="child"
54 : :depth="depth + 1"
55 : :selected-category="selectedCategory"
56 : :expanded-category="expandedCategory"
57 : :hide-chevrons="hideChevrons"
58 : :focus-parent="focusSelf"
59 : @select="$emit('select', $event)"
60 : @collapse="$emit('collapse', $event)"
61 : />
62 : </div>
63 : </div>
64 : </template>
65 :
66 : <script lang="ts">
67 : export interface CategoryNode {
68 : key: string
69 : label: string
70 : children?: CategoryNode[]
71 : }
72 :
73 2355 : export const CATEGORY_SELECTED_CLASS = 'bg-accent'
74 2355 : export const CATEGORY_UNSELECTED_CLASS =
75 : 'text-muted-foreground hover:text-foreground'
76 : </script>
77 :
78 : <script setup lang="ts">
79 : import { computed, nextTick, ref } from 'vue'
80 :
81 : import { RovingFocusItem } from 'reka-ui'
82 :
83 : import Button from '@comfyorg/design-system/button/Button'
84 : import { cn } from '@comfyorg/tailwind-utils'
85 :
86 : const {
87 : node,
88 : depth = 0,
89 : selectedCategory,
90 : expandedCategory,
91 : hideChevrons = false,
92 : focusParent
93 : } = defineProps<{
94 : node: CategoryNode
95 : depth?: number
96 : selectedCategory: string
97 : expandedCategory: string
98 : hideChevrons?: boolean
99 : focusParent?: () => void
100 : }>()
101 :
102 1949 : const emit = defineEmits<{
103 : select: [key: string]
104 : collapse: [key: string]
105 : }>()
106 :
107 1949 : const buttonEl = ref<InstanceType<typeof Button>>()
108 1949 : const childRefs = ref<{ focus?: () => void }[]>([])
109 :
110 0 : function focusSelf() {
111 : const el = buttonEl.value?.$el as HTMLElement | undefined
112 0 : el?.focus()
113 : }
114 :
115 1949 : defineExpose({ focus: focusSelf })
116 :
117 1949 : const isExpanded = computed(
118 : () =>
119 : expandedCategory === node.key || expandedCategory.startsWith(node.key + '/')
120 : )
121 :
122 0 : function handleRight() {
123 0 : if (!node.children?.length) return
124 0 : if (!isExpanded.value) {
125 0 : emit('select', node.key)
126 0 : return
127 : }
128 0 : nextTick(() => {
129 0 : childRefs.value[0]?.focus?.()
130 : })
131 : }
132 :
133 0 : function handleLeft() {
134 0 : if (node.children?.length && isExpanded.value) {
135 0 : if (expandedCategory.startsWith(node.key + '/')) {
136 0 : emit('collapse', node.key)
137 : } else {
138 0 : emit('select', node.key)
139 : }
140 0 : return
141 : }
142 0 : focusParent?.()
143 : }
144 : </script>
|