Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<script setup lang="ts">
import { computed, ref } from 'vue';
interface Props {
items?: {
text: string;
link?: string;
color?: string;
children?: {
text: string;
link?: string;
color?: string;
children?: {
text: string;
link?: string;
color?: string;
}[];
}[];
}[];
maxWidth?: number;
expand?: boolean;
theme?:
| 'white'
| 'slate'
| 'blue'
| 'sky'
| 'teal'
| 'lime'
| 'green'
| 'yellow'
| 'orange'
| 'pink'
| 'fuchsia'
| 'purple'
| 'indigo'
| 'rose'
| 'red'
| 'black';
}
const props = defineProps<Props>();
const items = computed(() => props.items);
const themeColor = computed(() => {
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
if (!props?.theme) return '#ffffff';
switch (props?.theme) {
case 'white':
return '#ffffff';
case 'slate':
return '#64748b';
case 'blue':
return '#3b82f6';
case 'sky':
return '#0ea5e9';
case 'teal':
return '#14b8a6';
case 'lime':
return '#84cc16';
case 'green':
return '#22c55e';
case 'yellow':
return '#eab308';
case 'orange':
return '#f97316';
case 'pink':
return '#ec4899';
case 'fuchsia':
return '#d946ef';
case 'purple':
return '#a855f7';
case 'indigo':
return '#6366f1';
case 'rose':
return '#f43f5e';
case 'red':
return '#ef4444';
case 'black':
return '#000000';
}
return '#ffffff';
});
const textColor = computed(() => {
if (!props.theme) return '#000000';
if (props.theme === 'white') return '#000000';
return '#ffffff';
});
const state = ref([]);
const setInitialState = () => {
if (!props?.items) return;
for (let item of props.items) {
state.value.push({
isOpen: props?.expand ?? false,
text: item.text
});
if (item.children) {
for (let child of item.children) {
state.value.push({
isOpen: props?.expand ?? false,
text: child.text
});
console.log('child', child);
if (child.children) {
for (let grandChild of child.children) {
state.value.push({
isOpen: props?.expand ?? false,
text: grandChild.text
});
}
}
}
}
}
};
watch([items], () => {
if (items.value) setInitialState();
});
const toggleIsOpen = (item) =>
state.value.map((itemState) => {
if (itemState.text === item.text) itemState.isOpen = !itemState.isOpen;
});
</script>
<template>
<ul
:style="`background-color: ${themeColor ?? 'white'}; max-width: ${maxWidth ?? 300}px`"
class="tree"
>
<li
v-for="(item, index) of items"
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
:key="item.text"
:class="[
'item flex',
{
openItem: state.find((itemState) => itemState.text === item.text && itemState.isOpen)
}
]"
>
<svg
v-if="item.children"
:class="[
'openButton',
{
openButtonOpened: state.find(
(itemState) => itemState.text === item.text && itemState.isOpen
)
}
]"
width="25px"
height="25px"
viewBox="0 -0.5 28 28"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sketch="http://www.bohemiancoding.com/sketch/ns"
@click.prevent="toggleIsOpen(item)"
>
<g
id="Page-1"
stroke="none"
stroke-width="1"
fill="none"
fill-rule="evenodd"
sketch:type="MSPage"
>
<g
id="Icon-Set-Filled"
sketch:type="MSLayerGroup"
transform="translate(-156.000000, -623.000000)"
:fill="textColor ?? '#000000'"
>
<path
id="open"
d="M183,647.998 L157,647.998 C156.448,647.998 156,648.446 156,648.999 C156,649.552 156.448,650 157,650 L183,650 C183.552,650 184,649.552 184,648.999 C184,648.446 183.552,647.998 183,647.998 L183,647.998 Z M158.014,645.995 L182.018,645.995 C184.375,645.995 184.296,644.608 183.628,643.574 L171.44,624.555 C170.882,623.771 169.22,623.703 168.56,624.555 L156.372,643.574 C155.768,644.703 155.687,645.995 158.014,645.995 L158.014,645.995 Z"
sketch:type="MSShapeGroup"
></path>
</g>
</g>
</svg>
<div
:class="[
'content',
{
openContent: state.find((itemState) => itemState.text === item.text && itemState.isOpen)
}
]"
>
<div class="textContainer">
<slot :name="`${index + 1}IconBefore`" />
<a :href="item.link" class="text">{{ item.text }}</a>
<slot :name="`${index + 1}IconAfter`" />
</div>
<div v-for="(child, indexChild) of item.children" :key="child.text" class="flex item">
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<svg
v-if="child.children"
:class="[
'openButton',
{
openButtonOpened: state.find(
(itemState) => itemState.text === child.text && itemState.isOpen
)
}
]"
width="25px"
height="25px"
viewBox="0 -0.5 28 28"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sketch="http://www.bohemiancoding.com/sketch/ns"
@click.prevent="toggleIsOpen(child)"
>
<g
id="Page-1"
stroke="none"
stroke-width="1"
fill="none"
fill-rule="evenodd"
sketch:type="MSPage"
>
<g
id="Icon-Set-Filled"
sketch:type="MSLayerGroup"
transform="translate(-156.000000, -623.000000)"
:fill="textColor ?? '#000000'"
>
<path
id="open"
d="M183,647.998 L157,647.998 C156.448,647.998 156,648.446 156,648.999 C156,649.552 156.448,650 157,650 L183,650 C183.552,650 184,649.552 184,648.999 C184,648.446 183.552,647.998 183,647.998 L183,647.998 Z M158.014,645.995 L182.018,645.995 C184.375,645.995 184.296,644.608 183.628,643.574 L171.44,624.555 C170.882,623.771 169.22,623.703 168.56,624.555 L156.372,643.574 C155.768,644.703 155.687,645.995 158.014,645.995 L158.014,645.995 Z"
sketch:type="MSShapeGroup"
></path>
</g>
</g>
</svg>
<div
:class="[
'content',
{
openContent: state.find(
(itemState) => itemState.text === child.text && itemState.isOpen
)
}
]"
>
<div class="textContainer">
<slot :name="`${index + 1}-${indexChild + 1}IconBefore`" />
<a :href="child.link" class="text">{{ child.text }}</a>
<slot :name="`${index + 1}-${indexChild + 1}IconAfter`" />
</div>
<div
v-for="(grandChild, indexGrandChild) of child.children"
:key="grandChild.text"
class="flex item"
>
<div
:class="[
'content',
{
openContent: state.find(
(itemState) => itemState.text === grandChild.text && itemState.isOpen
)
}
]"
>
<div class="textContainer">
<slot
:name="`${index + 1}-${indexChild + 1}-${indexGrandChild + 1}IconBefore`"
/>
<p :href="grandChild.link" class="text">{{ grandChild.text }}</p>
<slot
:name="`${index + 1}-${indexChild + 1}-${indexGrandChild + 1}IconAfter`"
/>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</li>
</ul>
</template>
<style scoped>
.tree {
padding: 15px 25px 15px 15px;
}
.item {
position: relative;
}
.content {
width: 100%;
padding-left: 25px;
position: relative;
overflow: hidden;
transition: all 0.3s ease;
background-color: v-bind(themeColor);
display: inline-block;
padding: 4px 0;
z-index: 3;
color: v-bind(textColor);
background-color: v-bind(themeColor);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
word-break: break-word;
}
.openButton {
position: absolute;
z-index: 3;
top: 5px;
left: 0;
cursor: pointer;
padding: 5px;
margin: -5px -5px -5px 0;
transition: transform 0.3s ease;
}
.openButtonOpened {
transform: rotate(180deg);
}
.children {
width: 100%;
padding-left: 10px;
opacity: 0;
max-height: 0;
transform: translateY(-100%);
transition: all 0.3s ease;
}
.openContent > .children {
transform: translateY(0);
opacity: 1;
max-height: 1000px;
}
.textContainer {
display: flex;
gap: 10px;
margin-left: 10px;
}
.flex {
display: flex;
align-items: start;
justify-content: end;
}
</style>