@happydesigns/ui 0.13.5 → 0.14.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.
Files changed (37) hide show
  1. package/README.md +94 -94
  2. package/app/app.config.ts +168 -169
  3. package/app/app.vue +11 -0
  4. package/app/components/article/ArticleGrid.vue +125 -149
  5. package/app/components/article/ArticleHeaderBody.vue +20 -20
  6. package/app/components/article/ArticleHeadlineMeta.vue +26 -26
  7. package/app/components/article/ArticleList.vue +80 -95
  8. package/app/components/article/ArticlePage.vue +15 -14
  9. package/app/components/footer/FooterColumnHeading.vue +5 -5
  10. package/app/components/footer/FooterCopyright.vue +29 -29
  11. package/app/components/traits/Authors.vue +19 -20
  12. package/app/components/traits/BackButton.vue +15 -26
  13. package/app/components/traits/Category.vue +9 -9
  14. package/app/components/traits/CopyButton.vue +32 -39
  15. package/app/components/traits/Dates.vue +20 -20
  16. package/app/components/traits/Links.vue +17 -17
  17. package/app/components/traits/Location.vue +20 -20
  18. package/app/components/traits/Separator.vue +21 -26
  19. package/app/components/traits/SeparatorButtons.vue +53 -56
  20. package/app/components/traits/Surround.vue +48 -60
  21. package/app/components/traits/Toc.vue +16 -16
  22. package/app/composables/useArticleList.ts +24 -26
  23. package/app/composables/useArticleListQuery.ts +54 -0
  24. package/app/composables/useCategoryBadge.ts +27 -27
  25. package/app/composables/useClipboard.ts +26 -26
  26. package/app/composables/usePageContent.ts +18 -18
  27. package/app/composables/usePageSeo.ts +30 -30
  28. package/app/layouts/article.vue +117 -125
  29. package/app/layouts/content.vue +57 -65
  30. package/app/layouts/default.vue +3 -12
  31. package/app/types/config.ts +13 -6
  32. package/content.config.ts +6 -6
  33. package/nuxt.config.ts +80 -80
  34. package/package.json +3 -3
  35. package/schemas/collections.ts +46 -46
  36. package/schemas/index.ts +2 -2
  37. package/schemas/traits.ts +90 -90
@@ -20,9 +20,11 @@ export interface UseArticleListOptions<C extends keyof PageCollections = 'articl
20
20
  where?: MaybeRefOrGetter<ArticleFilter[] | undefined>
21
21
  /** Field to sort by. Set to false to disable default sorting. Defaults to { field: 'date', direction: 'DESC' }. */
22
22
  sort?: MaybeRefOrGetter<{ field: string, direction: 'ASC' | 'DESC' } | false | undefined>
23
- /** Status to filter by. Set to false to disable default status filtering. Defaults to 'published'. */
24
- status?: MaybeRefOrGetter<string | false | undefined>
25
- }
23
+ /** Status to filter by. Set to false to disable default status filtering. Defaults to 'published'. */
24
+ status?: MaybeRefOrGetter<string | false | undefined>
25
+ /** Whether the list should load lazily during client-side navigation. Defaults to false. */
26
+ lazy?: MaybeRefOrGetter<boolean | undefined>
27
+ }
26
28
 
27
29
  /**
28
30
  * Composable to fetch a paginated and filtered list of articles or any other collection.
@@ -38,15 +40,13 @@ export function useArticleList<C extends keyof PageCollections = 'article'>(opti
38
40
  const category = computed(() => toValue(options.category))
39
41
  const labelAll = computed(() => toValue(options.labelAll) || config.value.list?.labelAll || 'All')
40
42
 
41
- const queryDefaults = computed(() => config.value.query || {})
42
-
43
43
  const where = computed(() => {
44
44
  const w = toValue(options.where)
45
45
  if (w)
46
46
  return w
47
47
 
48
48
  // Fallback to config where but filter out status if handled separately
49
- return queryDefaults.value.where?.filter(f => f.field !== 'status') || []
49
+ return config.value.query?.where?.filter(f => f.field !== 'status') || []
50
50
  })
51
51
 
52
52
  const sort = computed(() => {
@@ -54,7 +54,7 @@ export function useArticleList<C extends keyof PageCollections = 'article'>(opti
54
54
  if (s !== undefined)
55
55
  return s
56
56
 
57
- return queryDefaults.value.order
57
+ return config.value.query?.order
58
58
  })
59
59
 
60
60
  const status = computed(() => {
@@ -66,14 +66,14 @@ export function useArticleList<C extends keyof PageCollections = 'article'>(opti
66
66
  return s
67
67
 
68
68
  // Try to find status in config where
69
- const statusFilter = queryDefaults.value.where?.find(f => f.field === 'status')
69
+ const statusFilter = config.value.query?.where?.find(f => f.field === 'status')
70
70
  return statusFilter?.value || 'published'
71
71
  })
72
72
 
73
73
  // The key must be reactive and stable
74
74
  const queryKey = computed(() => `${String(collection.value)}-list-${page.value}-${itemsPerPage.value}-${category.value || 'all'}-${labelAll.value}-${JSON.stringify(where.value)}-${JSON.stringify(sort.value)}-${status.value}`)
75
75
 
76
- return useAsyncData(queryKey, async () => {
76
+ return useAsyncData(queryKey, async () => {
77
77
  // We cast the query to a version that includes the standard article fields
78
78
  // This avoids 'any' while still allowing the query to work with generic collections
79
79
  type ArticleItem = Collections[C] & Collections['article']
@@ -117,15 +117,9 @@ export function useArticleList<C extends keyof PageCollections = 'article'>(opti
117
117
  let resolvedBadge: BadgeProps | undefined
118
118
 
119
119
  if (categoryKey) {
120
- const categories = config.value.categories || {}
121
- if (categoryKey in categories) {
122
- resolvedBadge = categories[categoryKey] as BadgeProps
123
- }
124
- else {
125
- resolvedBadge = {
126
- label: categoryKey,
127
- color: 'primary',
128
- }
120
+ resolvedBadge = config.value.categories?.[categoryKey] ?? {
121
+ label: categoryKey,
122
+ color: 'primary',
129
123
  }
130
124
  }
131
125
 
@@ -146,11 +140,15 @@ export function useArticleList<C extends keyof PageCollections = 'article'>(opti
146
140
  // Total count for pagination
147
141
  const total = await getDataQuery().count() as number
148
142
 
149
- return {
150
- articles: resolved,
151
- total,
152
- }
153
- }, {
154
- keepPreviousData: true,
155
- })
156
- }
143
+ return {
144
+ articles: resolved,
145
+ total,
146
+ }
147
+ }, {
148
+ lazy: toValue(options.lazy) ?? false,
149
+ default: () => ({
150
+ articles: [],
151
+ total: 0,
152
+ }),
153
+ })
154
+ }
@@ -0,0 +1,54 @@
1
+ interface UseArticleListQueryOptions {
2
+ labelAll: Ref<string>
3
+ fixedCategory?: Ref<string | undefined>
4
+ }
5
+
6
+ /**
7
+ * Manages URL query sync for article list pagination and category filtering.
8
+ * Shared between ArticleList (category tabs) and ArticleGrid (pagination).
9
+ */
10
+ export function useArticleListQuery({ labelAll, fixedCategory }: UseArticleListQueryOptions) {
11
+ const route = useRoute()
12
+
13
+ const page = ref(Number(route.query.page) || 1)
14
+ const selectedCategory = ref(
15
+ fixedCategory?.value || (route.query.category as string) || labelAll.value,
16
+ )
17
+
18
+ // Sync selectedCategory when the fixed prop changes
19
+ watch(
20
+ () => fixedCategory?.value,
21
+ (val) => {
22
+ if (val)
23
+ selectedCategory.value = val
24
+ },
25
+ )
26
+
27
+ function updateQuery({ resetPage = false } = {}) {
28
+ const query: Record<string, string | number> = { ...route.query as Record<string, string> }
29
+
30
+ if (resetPage || page.value === 1)
31
+ delete (query as any).page
32
+ else
33
+ query.page = page.value
34
+
35
+ if (!fixedCategory?.value) {
36
+ if (selectedCategory.value !== labelAll.value)
37
+ query.category = selectedCategory.value
38
+ else
39
+ delete (query as any).category
40
+ }
41
+
42
+ navigateTo({ query })
43
+ }
44
+
45
+ // Sync back on browser navigation
46
+ watch(() => route.query, (newQuery) => {
47
+ page.value = Number(newQuery.page) || 1
48
+ if (!fixedCategory?.value) {
49
+ selectedCategory.value = (newQuery.category as string) || labelAll.value
50
+ }
51
+ })
52
+
53
+ return { page, selectedCategory, updateQuery }
54
+ }
@@ -1,27 +1,27 @@
1
- /**
2
- * Resolves a category badge reactively using the global application configuration.
3
- *
4
- * @param category - The reactive category string to resolve.
5
- * @param collection - The collection to resolve categories for. Defaults to 'article'.
6
- * @returns A computed property containing the resolved badge configuration.
7
- */
8
- export function useCategoryBadge(
9
- category: MaybeRefOrGetter<string | undefined | null>,
10
- collection: MaybeRefOrGetter<string> = 'article',
11
- ) {
12
- const { config } = useVariant(collection)
13
-
14
- return computed(() => {
15
- const categoryKey = toValue(category)
16
- const categories = config.value.categories
17
-
18
- if (categoryKey && categories && categoryKey in categories) {
19
- return categories[categoryKey]
20
- }
21
-
22
- return {
23
- label: categoryKey ?? '',
24
- color: 'primary',
25
- }
26
- })
27
- }
1
+ /**
2
+ * Resolves a category badge reactively using the global application configuration.
3
+ *
4
+ * @param category - The reactive category string to resolve.
5
+ * @param collection - The collection to resolve categories for. Defaults to 'article'.
6
+ * @returns A computed property containing the resolved badge configuration.
7
+ */
8
+ export function useCategoryBadge(
9
+ category: MaybeRefOrGetter<string | undefined | null>,
10
+ collection: MaybeRefOrGetter<string> = 'article',
11
+ ) {
12
+ const { config } = useVariant(collection)
13
+
14
+ return computed(() => {
15
+ const categoryKey = toValue(category)
16
+ const categories = config.value.categories
17
+
18
+ if (categoryKey && categories && categoryKey in categories) {
19
+ return categories[categoryKey]
20
+ }
21
+
22
+ return {
23
+ label: categoryKey ?? '',
24
+ color: 'primary',
25
+ }
26
+ })
27
+ }
@@ -1,26 +1,26 @@
1
- import type { ToastProps } from '#ui/types'
2
- import { useClipboard as _useClipboard } from '@vueuse/core'
3
-
4
- export function useClipboard() {
5
- const { copy: _copy, copied } = _useClipboard()
6
-
7
- const toast = useToast()
8
-
9
- const copy = (source: string, options?: Partial<ToastProps> & { id?: string | number }) => {
10
- _copy(source)
11
- if (options) {
12
- // Check if toast with this ID already exists to prevent spam
13
- if (options.id && toast.toasts.value.some(t => t.id === options.id)) {
14
- toast.update(options.id, options)
15
- }
16
- else {
17
- toast.add(options as any)
18
- }
19
- }
20
- }
21
-
22
- return {
23
- copy,
24
- copied,
25
- }
26
- }
1
+ import type { ToastProps } from '#ui/types'
2
+ import { useClipboard as _useClipboard } from '@vueuse/core'
3
+
4
+ export function useClipboard() {
5
+ const { copy: _copy, copied } = _useClipboard()
6
+
7
+ const toast = useToast()
8
+
9
+ const copy = (source: string, options?: Partial<ToastProps> & { id?: string | number }) => {
10
+ _copy(source)
11
+ if (options) {
12
+ // Check if toast with this ID already exists to prevent spam
13
+ if (options.id && toast.toasts.value.some(t => t.id === options.id)) {
14
+ toast.update(options.id, options)
15
+ }
16
+ else {
17
+ toast.add(options as any)
18
+ }
19
+ }
20
+ }
21
+
22
+ return {
23
+ copy,
24
+ copied,
25
+ }
26
+ }
@@ -1,31 +1,31 @@
1
- import type { Collections } from '@nuxt/content'
2
- import type { AsyncData } from 'nuxt/app'
3
- import type { MaybeRefOrGetter } from 'vue'
4
- import { computed, toValue } from 'vue'
5
-
6
- interface UsePageContentOptions<C extends keyof Collections = 'page'> {
1
+ import type { PageCollections } from '@nuxt/content'
2
+ import type { AsyncData } from 'nuxt/app'
3
+ import type { MaybeRefOrGetter } from 'vue'
4
+ import { computed, toValue } from 'vue'
5
+
6
+ interface UsePageContentOptions<C extends keyof PageCollections = 'page'> {
7
7
  path?: MaybeRefOrGetter<string | undefined>
8
8
  collection?: MaybeRefOrGetter<C>
9
9
  }
10
-
11
- /**
12
- * Composable to fetch content from a Nuxt Content collection.
13
- *
10
+
11
+ /**
12
+ * Composable to fetch content from a Nuxt Content collection.
13
+ *
14
14
  * @param options - Configuration options for fetching content
15
15
  * @param options.path - Optional path to fetch. Defaults to current route path.
16
16
  * @param options.collection - Optional collection to fetch from. Defaults to 'page'.
17
17
  */
18
- export function usePageContent<C extends keyof Collections = 'page', B = object>(
18
+ export function usePageContent<C extends keyof PageCollections = 'page', B = object>(
19
19
  { path, collection }: UsePageContentOptions<C> = {},
20
20
  ) {
21
- const route = useRoute()
22
- const resolvedPath = computed(() => toValue(path) ?? route.path)
23
- const resolvedCollection = computed(() => (toValue(collection) ?? ('page' as C)) as C)
24
- const key = computed(() => `${String(resolvedCollection.value)}:${resolvedPath.value}`)
25
-
26
- const handler = () => queryCollection(resolvedCollection.value as keyof Collections).path(resolvedPath.value).first()
21
+ const route = useRoute()
22
+ const resolvedPath = computed(() => toValue(path) ?? route.path)
23
+ const resolvedCollection = computed(() => (toValue(collection) ?? ('page' as C)) as C)
24
+ const key = computed(() => `${String(resolvedCollection.value)}:${resolvedPath.value}`)
25
+
26
+ const handler = () => queryCollection(resolvedCollection.value as keyof PageCollections).path(resolvedPath.value).first()
27
27
 
28
28
  return useAsyncData(key, handler, {
29
29
  watch: [resolvedPath, resolvedCollection],
30
- }) as unknown as AsyncData<(Collections[C] & B) | null, Error | null>
30
+ }) as unknown as AsyncData<(PageCollections[C] & B) | null, Error | null>
31
31
  }
@@ -1,30 +1,30 @@
1
- import { watchEffect } from 'vue'
2
-
3
- interface SeoRelevantPageData {
4
- seo?: {
5
- title?: string
6
- description?: string
7
- ogTitle?: string
8
- ogDescription?: string
9
- }
10
- title?: string
11
- description?: string
12
- }
13
-
14
- export function usePageSeo(page: Ref<SeoRelevantPageData | null | undefined>) {
15
- watchEffect(() => {
16
- if (page.value) {
17
- const title = page.value.seo?.title || page.value.title
18
- const description = page.value.seo?.description || page.value.description
19
- const ogTitle = page.value.seo?.ogTitle || title
20
- const ogDescription = page.value.seo?.ogDescription || description
21
-
22
- useSeoMeta({
23
- title,
24
- ogTitle,
25
- description,
26
- ogDescription,
27
- })
28
- }
29
- })
30
- }
1
+ import { watchEffect } from 'vue'
2
+
3
+ interface SeoRelevantPageData {
4
+ seo?: {
5
+ title?: string
6
+ description?: string
7
+ ogTitle?: string
8
+ ogDescription?: string
9
+ }
10
+ title?: string
11
+ description?: string
12
+ }
13
+
14
+ export function usePageSeo(page: Ref<SeoRelevantPageData | null | undefined>) {
15
+ watchEffect(() => {
16
+ if (page.value) {
17
+ const title = page.value.seo?.title || page.value.title
18
+ const description = page.value.seo?.description || page.value.description
19
+ const ogTitle = page.value.seo?.ogTitle || title
20
+ const ogDescription = page.value.seo?.ogDescription || description
21
+
22
+ useSeoMeta({
23
+ title,
24
+ ogTitle,
25
+ description,
26
+ ogDescription,
27
+ })
28
+ }
29
+ })
30
+ }
@@ -1,128 +1,120 @@
1
- <script setup lang="ts" generic="C extends keyof PageCollections & ('article' | 'event') = 'article'">
2
- import type { PageCollections } from '@nuxt/content'
3
-
4
- const {
5
- path,
6
- collection = 'article' as C,
7
- } = defineProps<{
8
- path?: string
9
- collection?: C
10
- }>()
11
-
12
- const route = useRoute()
13
-
14
- const { data: page } = await usePageContent<C>({
15
- path: () => path,
16
- collection: () => collection,
17
- })
18
-
19
- const { config, has } = useVariant(collection)
20
- const hasHeader = has('header')
21
- const hasBackButton = has('backButton')
22
- const hasCopyButton = has('copyButton')
23
- const hasSeparator = has('separator')
24
- const hasSurround = has('surround')
25
- const hasToc = has('toc')
26
-
27
- if (!page.value) {
28
- throw createError({
29
- statusCode: 404,
30
- statusMessage: `${collection} ${path || route.path} not found`,
31
- fatal: true,
32
- })
33
- }
34
-
35
- usePageSeo(page)
36
-
37
- const header = computed(() => resolvePageHeader(page.value))
38
-
39
- const breadcrumbsBase = computed(() => config.value.breadcrumbs ?? [])
40
-
41
- const breadcrumbItems = computed(() => {
42
- return [
43
- ...breadcrumbsBase.value,
44
- { label: page.value?.title, to: page.value?.path },
45
- ]
46
- })
47
-
48
- const backLink = computed(() => {
49
- const lastBreadcrumb = breadcrumbsBase.value.at(-1)
50
-
51
- if (!lastBreadcrumb && !config.value?.backButton)
52
- return null
53
-
54
- return { ...(lastBreadcrumb ?? {}), ...config.value?.backButton }
55
- })
56
- </script>
57
-
1
+ <script setup lang="ts" generic="C extends keyof PageCollections = 'article'">
2
+ import type { PageCollections } from '@nuxt/content'
3
+
4
+ const props = defineProps<{
5
+ path?: string
6
+ collection?: C
7
+ }>()
8
+
9
+ const route = useRoute()
10
+ const collection = computed(() => props.collection ?? 'article')
11
+
12
+ const { data: page } = await usePageContent({
13
+ path: () => props.path,
14
+ collection: () => collection.value,
15
+ })
16
+
17
+ const { config, has } = useVariant(collection)
18
+ const hasHeader = has('header')
19
+ const hasBackButton = has('backButton')
20
+ const hasCopyButton = has('copyButton')
21
+ const hasSeparator = has('separator')
22
+ const hasSurround = has('surround')
23
+ const hasToc = has('toc')
24
+ const tocEnabled = computed(() => !page.value || !('toc' in page.value) || page.value.toc !== false)
25
+
26
+ if (!page.value) {
27
+ throw createError({
28
+ statusCode: 404,
29
+ statusMessage: `${collection.value} ${props.path || route.path} not found`,
30
+ fatal: true,
31
+ })
32
+ }
33
+
34
+ usePageSeo(page)
35
+
36
+ const header = computed(() => resolvePageHeader(page.value))
37
+
38
+ const breadcrumbsBase = computed(() => config.value.breadcrumbs ?? [])
39
+
40
+ const breadcrumbItems = computed(() => {
41
+ return [
42
+ ...breadcrumbsBase.value,
43
+ { label: page.value?.title, to: page.value?.path },
44
+ ]
45
+ })
46
+
47
+ const backLink = computed(() => {
48
+ const lastBreadcrumb = breadcrumbsBase.value.at(-1)
49
+
50
+ if (!lastBreadcrumb && !config.value?.backButton)
51
+ return null
52
+
53
+ return { ...(lastBreadcrumb ?? {}), ...config.value?.backButton }
54
+ })
55
+ </script>
56
+
58
57
  <template>
59
- <div>
60
- <AppHeader />
61
-
62
- <UMain v-if="page">
63
- <UContainer>
64
- <UPageHeader
65
- v-if="hasHeader && header"
66
- v-bind="header"
67
- :ui="{
68
- headline: 'all:flex flex-col gap-y-8 items-start',
69
- wrapper: 'lg:flex-row',
70
- }"
58
+ <UContainer v-if="page">
59
+ <UPageHeader
60
+ v-if="hasHeader && header"
61
+ v-bind="header"
62
+ :ui="{
63
+ headline: 'all:flex flex-col gap-y-8 items-start',
64
+ wrapper: 'lg:flex-row',
65
+ }"
66
+ >
67
+ <template #headline>
68
+ <UBreadcrumb
69
+ :ui="{ root: 'max-w-full' }"
70
+ :items="breadcrumbItems"
71
+ />
72
+ <HArticleHeadlineMeta :page="page" :collection="collection" />
73
+ </template>
74
+ <HArticleHeaderBody :page="page" :collection="collection">
75
+ <slot name="header" />
76
+ </HArticleHeaderBody>
77
+ </UPageHeader>
78
+
79
+ <UPage>
80
+ <UPageBody>
81
+ <slot />
82
+
83
+ <div
84
+ v-if="hasBackButton || hasCopyButton"
85
+ class="all:flex items-center justify-between mt-12"
71
86
  >
72
- <template #headline>
73
- <UBreadcrumb
74
- :ui="{ root: 'max-w-full' }"
75
- :items="breadcrumbItems"
76
- />
77
- <HArticleHeadlineMeta :page="page" :collection="collection" />
78
- </template>
79
- <HArticleHeaderBody :page="page" :collection="collection">
80
- <slot name="header" />
81
- </HArticleHeaderBody>
82
- </UPageHeader>
83
-
84
- <UPage>
85
- <UPageBody>
86
- <slot />
87
-
88
- <div
89
- v-if="hasBackButton || hasCopyButton"
90
- class="all:flex items-center justify-between mt-12"
91
- >
92
- <HBackButton
93
- v-if="hasBackButton && backLink"
94
- :to="backLink.to"
95
- :label="backLink.label"
96
- :icon="backLink.icon"
97
- />
98
- <div class="all:flex justify-end items-center gap-1.5 ml-auto">
99
- <HCopyButton v-if="hasCopyButton" />
100
- </div>
101
- </div>
102
-
103
- <HSeparator
104
- v-if="hasSeparator"
105
- :stem="page?.stem"
106
- :extension="page?.extension"
107
- />
108
-
109
- <HSurround
110
- v-if="hasSurround"
111
- :collection="collection"
112
- />
113
- </UPageBody>
114
-
115
- <template #right>
116
- <HToc
117
- v-if="hasToc && page?.toc !== false"
118
- :links="page?.body?.toc?.links"
119
- :title="page?.body?.toc?.title"
120
- />
121
- </template>
122
- </UPage>
123
- </UContainer>
124
- </UMain>
125
-
126
- <AppFooter />
127
- </div>
87
+ <HBackButton
88
+ v-if="hasBackButton && backLink"
89
+ v-bind="backLink"
90
+ />
91
+ <div class="all:flex justify-end items-center gap-1.5 ml-auto">
92
+ <HCopyButton v-if="hasCopyButton" v-bind="config.copyButton" />
93
+ </div>
94
+ </div>
95
+
96
+ <HSeparator
97
+ v-if="hasSeparator"
98
+ :stem="page?.stem"
99
+ :extension="page?.extension"
100
+ v-bind="config.separator"
101
+ />
102
+
103
+ <HSurround
104
+ v-if="hasSurround"
105
+ :collection="collection"
106
+ v-bind="config.surround"
107
+ :query="config.query"
108
+ />
109
+ </UPageBody>
110
+
111
+ <template #right>
112
+ <HToc
113
+ v-if="hasToc && tocEnabled"
114
+ :links="page?.body?.toc?.links"
115
+ :title="page?.body?.toc?.title"
116
+ />
117
+ </template>
118
+ </UPage>
119
+ </UContainer>
128
120
  </template>