Skip to content

Open source personal finance application

feat(categories): chevron-on-left tree view with grid mode for children

Ilya Komichev267e679ff50c
diff --git a/app/components/categories/Form.vue b/app/components/categories/Form.vue
index 34e935b9..def6eb01 100644
--- a/app/components/categories/Form.vue
+++ b/app/components/categories/Form.vue
@@ -33,14 +33,17 @@ watch(prevChildIds, (ids) => {
const hasChildren = computed(() => selectedChildIds.value.length > 0)
const hadChildrenInitially = computed(() => prevChildIds.value.length > 0)
const isRootCategory = computed(() => props.categoryForm.parentId === 0)
+const isNewCategory = computed(() => !props.categoryId)
const isAllowChangeParent = computed(() =>
!hasChildren.value && categoriesStore.categoriesForBeParent.length > 0,
)
// A root can manage children only if it has no direct transactions of its own,
// otherwise adopting children would hide its trns from stats.
-// We allow it also if it already has children (legacy safety valve).
+// New categories have no transactions yet, so they always qualify.
+// Existing root with own children also qualifies (legacy safety valve).
const canHaveChildren = computed(() =>
- hadChildrenInitially.value
+ isNewCategory.value
+ || hadChildrenInitially.value
|| (!!props.categoryId && categoriesStore.categoriesForBeParent.includes(props.categoryId)),
)
@@ -137,11 +140,15 @@ function getParentName(id: CategoryId): string {
const isAllowManageChildren = computed(() =>
isRootCategory.value
- && !!props.categoryId
&& canHaveChildren.value
&& (childrenCandidateIds.value.length > 0 || prevChildIds.value.length > 0),
)
+watch(() => props.categoryForm.parentId, (parentId) => {
+ if (parentId !== 0 && selectedChildIds.value.length > 0)
+ selectedChildIds.value = [...prevChildIds.value]
+})
+
const modals = ref({
children: false,
colors: false,
@@ -585,7 +592,6 @@ async function onSave() {
<CategoriesList
:activeItemId="props.categoryForm.parentId"
- :categoriesItemProps="{ class: 'group' }"
:ids="categoriesStore.categoriesForBeParent.filter(id => id !== categoryId)"
class="!gap-x-1"
@click="id => onParentSelect(id, close)"
diff --git a/app/components/categories/Item.vue b/app/components/categories/Item.vue
index a623bfc4..b2e67607 100644
--- a/app/components/categories/Item.vue
+++ b/app/components/categories/Item.vue
@@ -7,6 +7,7 @@ export type CategoryItemProps = {
activeItemId?: string | 0 | false | null
category: CategoryItem
categoryId: CategoryId
+ chevronOnLeft?: boolean
class?: string
contextMenuItems?: ContextMenuItem[][]
insideClasses?: string
@@ -34,6 +35,7 @@ const emit = defineEmits<{
:stacked="props.stacked"
:category="props.category"
:categoryId="props.categoryId"
+ :chevronOnLeft="props.chevronOnLeft"
:class="props.class"
:insideClasses="props.insideClasses"
:isExpanded="props.isExpanded"
@@ -53,6 +55,7 @@ const emit = defineEmits<{
:stacked="props.stacked"
:category="props.category"
:categoryId="props.categoryId"
+ :chevronOnLeft="props.chevronOnLeft"
:class="props.class"
:insideClasses="props.insideClasses"
:isExpanded="props.isExpanded"
diff --git a/app/components/categories/ItemBody.vue b/app/components/categories/ItemBody.vue
index a45c80aa..5e2efd10 100644
--- a/app/components/categories/ItemBody.vue
+++ b/app/components/categories/ItemBody.vue
@@ -7,6 +7,7 @@ const props = defineProps<{
activeItemId?: string | 0 | false | null
category: CategoryItem
categoryId: CategoryId
+ chevronOnLeft?: boolean
class?: string
insideClasses?: string
isExpanded?: boolean
@@ -29,6 +30,9 @@ const childCategoriesIds = computed(() => categoriesStore.getChildrenIds(props.c
const parentCategory = computed(() => categoriesStore.items[props.category?.parentId])
const hasChildren = computed(() => childCategoriesIds.value.length > 0)
+const showLeftChevron = computed(() => !!props.chevronOnLeft && !!props.isShowChevron && hasChildren.value)
+const showRightChevron = computed(() => !props.chevronOnLeft && !!props.isShowChevron && hasChildren.value)
+
function onToggleClick(e: Event) {
e.stopPropagation()
e.preventDefault()
@@ -37,12 +41,55 @@ function onToggleClick(e: Event) {
</script>
<template>
+ <div v-if="props.chevronOnLeft && props.category" :class="cn('flex items-center', props.class)">
+ <button
+ v-if="showLeftChevron"
+ type="button"
+ class="flex-center text-muted hover:bg-elevated h-9 w-9 shrink-0 cursor-pointer rounded-full"
+ :aria-label="$t('base.toggleExpand')"
+ @click="onToggleClick"
+ >
+ <Icon
+ :name="props.isExpanded ? 'lucide:chevron-down' : 'lucide:chevron-right'"
+ size="18"
+ />
+ </button>
+ <div v-else class="h-9 w-9 shrink-0" />
+ <UiElement
+ :isActive="props.activeItemId === props.categoryId"
+ class="min-w-0 grow"
+ :lineWidth="props.lineWidth"
+ :insideClasses="`min-h-[46px] ${props.insideClasses ?? ''}`"
+ :to="props.to"
+ @click="(e: Event) => emit('click', e)"
+ >
+ <template #leftIcon>
+ <UiIconBase
+ :color="props.category.color"
+ :name="props.category.icon"
+ invert
+ @click="emit('filter', props.categoryId ?? '')"
+ />
+ </template>
+
+ <div class="grid grow gap-0.5 overflow-hidden">
+ <CategoriesName
+ :stacked="props.stacked"
+ :category="props.category"
+ :parentCategory="parentCategory"
+ :childrenCount="childCategoriesIds.length"
+ :isShowParent="props.isShowParent"
+ />
+ </div>
+ </UiElement>
+ </div>
+
<UiElement
- v-if="props.category"
+ v-else-if="props.category"
:isActive="props.activeItemId === props.categoryId"
:class="props.class"
:lineWidth="props.lineWidth"
- :insideClasses="`min-h-[46px] ${props.insideClasses}`"
+ :insideClasses="`min-h-[46px] ${props.insideClasses ?? ''}`"
:to="props.to"
@click="(e: Event) => emit('click', e)"
>
@@ -66,9 +113,9 @@ function onToggleClick(e: Event) {
</div>
<button
- v-if="props.isShowChevron && hasChildren"
+ v-if="showRightChevron"
type="button"
- class="flex-center text-muted -mr-1 h-9 w-9 shrink-0 cursor-pointer rounded-sm hover:bg-(--item-3)"
+ class="flex-center text-muted hover:bg-elevated -mr-1 h-9 w-9 shrink-0 cursor-pointer rounded-sm"
:aria-label="$t('base.toggleExpand')"
@click="onToggleClick"
>
diff --git a/app/components/categories/List.vue b/app/components/categories/List.vue
index 1fa59137..82ff9f72 100644
--- a/app/components/categories/List.vue
+++ b/app/components/categories/List.vue
@@ -12,6 +12,7 @@ export type CategoriesListExpanded = {
const props = defineProps<{
activeItemId?: string | 0 | false | null
categoriesItemProps?: Partial<CategoryItemProps>
+ childrenView?: 'list' | 'grid'
expanded?: CategoriesListExpanded
getContextMenuItems?: (categoryId: CategoryId) => any[][] | undefined
getTo?: (categoryId: CategoryId) => string
@@ -25,6 +26,22 @@ const emit = defineEmits<{
const categoriesStore = useCategoriesStore()
+const anyRootHasChildren = computed(() =>
+ props.ids.some(id => categoriesStore.hasChildren(id)),
+)
+
+const rootChevronOnLeft = computed(() =>
+ !!props.categoriesItemProps?.chevronOnLeft && anyRootHasChildren.value,
+)
+
+const isChildrenGrid = computed(() => props.childrenView === 'grid')
+
+function getRootLineWidth(categoryId: CategoryId) {
+ if (props.expanded?.isExpanded(categoryId) && categoriesStore.hasChildren(categoryId))
+ return 0
+ return props.categoriesItemProps?.lineWidth ?? 1
+}
+
function getChildrenIds(categoryId: CategoryId) {
return categoriesStore.getChildrenIds(categoryId)
}
@@ -45,29 +62,48 @@ function getChildrenIds(categoryId: CategoryId) {
:isExpanded="props.expanded?.isExpanded(categoryId)"
:isShowChevron="!!props.expanded && categoriesStore.hasChildren(categoryId)"
:to="props.getTo?.(categoryId)"
- :lineWidth="1"
v-bind="categoriesItemProps"
+ class="group"
+ :chevronOnLeft="rootChevronOnLeft"
+ :lineWidth="getRootLineWidth(categoryId)"
@click="emit('click', categoryId)"
@toggle="props.expanded?.toggle(categoryId)"
/>
<div
v-if="props.expanded?.isExpanded(categoryId) && categoriesStore.hasChildren(categoryId)"
- class="-mt-px ml-5 pb-1 pl-3"
+ :class="isChildrenGrid
+ ? 'ml-9 pb-4 pl-10 pr-2'
+ : (rootChevronOnLeft ? '-mt-px ml-9 pb-1 pl-10' : '-mt-px ml-5 pb-1 pl-3')"
>
- <CategoriesItem
- v-for="childId in getChildrenIds(categoryId)"
- :key="childId"
- :activeItemId="activeItemId"
- :category="categoriesStore.items[childId]!"
- :categoryId="childId"
- :contextMenuItems="props.getContextMenuItems?.(childId)"
- :insideClasses="props.insideClasses"
- :to="props.getTo?.(childId)"
- :lineWidth="1"
- v-bind="categoriesItemProps"
- @click="emit('click', childId)"
- />
+ <template v-if="!isChildrenGrid">
+ <CategoriesItem
+ v-for="childId in getChildrenIds(categoryId)"
+ :key="childId"
+ :activeItemId="activeItemId"
+ :category="categoriesStore.items[childId]!"
+ :categoryId="childId"
+ :contextMenuItems="props.getContextMenuItems?.(childId)"
+ :insideClasses="props.insideClasses"
+ :to="props.getTo?.(childId)"
+ :lineWidth="1"
+ v-bind="categoriesItemProps"
+ class="group"
+ :chevronOnLeft="false"
+ @click="emit('click', childId)"
+ />
+ </template>
+
+ <div v-else class="flex flex-wrap gap-1">
+ <CategoriesRoundLink
+ v-for="childId in getChildrenIds(categoryId)"
+ :key="childId"
+ :categoryId="childId"
+ :contextMenuItems="props.getContextMenuItems?.(childId)"
+ :to="props.getTo?.(childId)"
+ @click="emit('click', childId)"
+ />
+ </div>
</div>
</template>
</div>
diff --git a/app/components/categories/RoundLink.vue b/app/components/categories/RoundLink.vue
new file mode 100644
index 00000000..724066ad
--- /dev/null
+++ b/app/components/categories/RoundLink.vue
@@ -0,0 +1,36 @@
+<script setup lang="ts">
+import type { ContextMenuItem } from '#ui/components/ContextMenu.vue'
+
+import type { CategoryId } from '~/components/categories/types'
+
+const props = defineProps<{
+ categoryId: CategoryId
+ contextMenuItems?: ContextMenuItem[][]
+ to?: string
+}>()
+
+const emit = defineEmits<{
+ click: []
+}>()
+</script>
+
+<template>
+ <UContextMenu v-if="props.contextMenuItems" :items="props.contextMenuItems">
+ <NuxtLink
+ :to="props.to"
+ class="text-inherit no-underline"
+ @click="emit('click')"
+ >
+ <CategoriesRoundPill :categoryId="props.categoryId" />
+ </NuxtLink>
+ </UContextMenu>
+
+ <NuxtLink
+ v-else
+ :to="props.to"
+ class="text-inherit no-underline"
+ @click="emit('click')"
+ >
+ <CategoriesRoundPill :categoryId="props.categoryId" />
+ </NuxtLink>
+</template>
diff --git a/app/components/categories/RoundPill.vue b/app/components/categories/RoundPill.vue
new file mode 100644
index 00000000..cb17c7fe
--- /dev/null
+++ b/app/components/categories/RoundPill.vue
@@ -0,0 +1,60 @@
+<script setup lang="ts">
+import type { CategoryId } from '~/components/categories/types'
+
+import { useCategoriesStore } from '~/components/categories/useCategoriesStore'
+
+const props = defineProps<{
+ categoryId: CategoryId
+ isIconBg?: boolean
+ isShowParent?: boolean
+}>()
+
+const categoriesStore = useCategoriesStore()
+const category = computed(() => categoriesStore.items[props.categoryId])
+const parentCategory = computed(() => {
+ const parentId = category.value?.parentId
+ return parentId ? categoriesStore.items[parentId] : undefined
+})
+</script>
+
+<template>
+ <div
+ v-if="category"
+ class="bg-elevated/10 hover:bg-elevated/30 relative flex items-center overflow-hidden rounded-2xl border border-transparent p-1 pr-3"
+ :class="props.isIconBg ? 'gap-2' : 'gap-1'"
+ >
+ <div
+ :style="{ backgroundColor: category.color }"
+ class="absolute inset-0 size-full opacity-10"
+ />
+
+ <div class="relative size-6">
+ <UiIconBase
+ v-if="props.isIconBg"
+ :name="category.icon"
+ :color="category.color"
+ :size="14"
+ class="!w-6 p-1"
+ invert
+ />
+ <UiIconBase
+ v-else
+ :name="category.icon"
+ :color="category.color"
+ :size="14"
+ class="!w-6 p-1"
+ />
+ </div>
+
+ <div class="relative">
+ <CategoriesName
+ :category="category"
+ :isShowParent="props.isShowParent"
+ :parentCategory="parentCategory"
+ stacked
+ size="xs"
+ />
+ <slot />
+ </div>
+ </div>
+</template>
diff --git a/app/components/categories/page/List.vue b/app/components/categories/page/List.vue
index c51cbe5b..530fbc19 100644
--- a/app/components/categories/page/List.vue
+++ b/app/components/categories/page/List.vue
@@ -84,7 +84,6 @@ const { getCategoryContextMenuItems } = useCategoryContextMenu({
<UiHeaderTitle>{{ t('categories.name') }}</UiHeaderTitle>
<template #actions>
<UiActionButton
- v-if="categoriesView === 'list'"
:ariaLabel="$t('base.toggleFolders')"
@click="toggleAll"
>
@@ -129,15 +128,12 @@ const { getCategoryContextMenuItems } = useCategoryContextMenu({
<CategoriesList
:ids="categoriesStore.categoriesRootIds"
:categoriesItemProps="{
- class: 'group',
- lineWidth: categoriesView === 'list' ? 1 : 0,
+ chevronOnLeft: true,
+ lineWidth: 1,
}"
- :class="{
- 'grid gap-1.5 @sm:grid-cols-2 @2xl/page:grid-cols-3': categoriesView === 'grid',
- }"
- :expanded="categoriesView === 'list' ? { isExpanded, toggle } : undefined"
+ :childrenView="categoriesView"
+ :expanded="{ isExpanded, toggle }"
:getContextMenuItems="getCategoryContextMenuItems"
- :insideClasses="categoriesView === 'grid' ? 'bg-item-2' : ''"
:getTo="(categoryId: CategoryId) => `/categories/${categoryId}`"
/>
</div>
diff --git a/app/components/categories/page/id/Index.vue b/app/components/categories/page/id/Index.vue
index 7852acfe..aee442c3 100644
--- a/app/components/categories/page/id/Index.vue
+++ b/app/components/categories/page/id/Index.vue
@@ -274,9 +274,6 @@ const categoriesIds = computed(() => categoriesStore.getChildrenIds(categoryId.v
>
<CategoriesList
:ids="categoriesIds"
- :categoriesItemProps="{
- class: 'group',
- }"
:getContextMenuItems="getCategoryContextMenuItems"
:getTo="(categoryId: CategoryId) => `/categories/${categoryId}`"
/>