@happydesigns/ui 0.17.5 → 0.19.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.
@@ -1,21 +1,17 @@
1
- <script setup lang="ts">
2
- import type { PageCollections } from '@nuxt/content'
3
- import type { ButtonProps } from '@nuxt/ui'
4
-
5
- const props = defineProps<{
6
- links?: PageCollections['event']['links']
7
- }>()
8
-
9
- // Nuxt Content preserves the inherited schema shape but widens a few DOM props to unknown.
10
- const buttonLinks = computed(() => props.links as ButtonProps[] | undefined)
11
- </script>
12
-
13
- <template>
14
- <UButton
15
- v-for="(link, index) in buttonLinks"
16
- :key="index"
17
- variant="subtle"
18
- size="sm"
19
- v-bind="link"
20
- />
21
- </template>
1
+ <script setup lang="ts">
2
+ import type { ContentLink } from '../../types/content'
3
+
4
+ defineProps<{
5
+ links?: ContentLink[]
6
+ }>()
7
+ </script>
8
+
9
+ <template>
10
+ <UButton
11
+ v-for="(link, index) in links"
12
+ :key="index"
13
+ variant="subtle"
14
+ size="sm"
15
+ v-bind="link"
16
+ />
17
+ </template>
@@ -1,6 +1,9 @@
1
- <script setup lang="ts" generic="C extends keyof PageCollections ">
1
+ <script setup lang="ts" generic="C extends PageCollectionName">
2
2
  import type { PageCollections } from '@nuxt/content'
3
3
  import type { QueryConfig } from '../../types/config'
4
+ import type { PageCollectionName } from '../../types/content'
5
+
6
+ type PageField = Extract<keyof PageCollections[keyof PageCollections], string>
4
7
 
5
8
  const props = defineProps<{
6
9
  collection: C
@@ -13,25 +16,28 @@ const props = defineProps<{
13
16
  const route = useRoute()
14
17
  const collection = computed(() => props.collection)
15
18
  const path = computed(() => route.path)
16
- const key = computed(() => `surround-${String(collection.value)}-${path.value}`)
19
+ const key = computed(() => `surround-${collection.value}-${path.value}`)
17
20
 
18
21
  const { data: surround } = await useAsyncData(
19
22
  key,
20
23
  () => {
21
24
  const qc = props.query || {}
22
-
23
- const fields = (qc.fields || ['title', 'description', 'published']) as Array<keyof PageCollections[C]>
25
+ const fields = qc.fields || ['title', 'description', 'published']
24
26
  const order = qc.order
25
27
  const where = qc.where || [{ field: 'published', operator: '=', value: true }]
26
28
 
27
- let query = queryCollectionItemSurroundings(collection.value, path.value, { fields })
29
+ let query = queryCollectionItemSurroundings(
30
+ collection.value as keyof PageCollections,
31
+ path.value,
32
+ { fields: fields as PageField[] },
33
+ )
28
34
 
29
35
  where.forEach((filter) => {
30
- query = query.where(filter.field, filter.operator, filter.value)
36
+ query = query.where(filter.field as PageField, filter.operator, filter.value)
31
37
  })
32
38
 
33
39
  if (order) {
34
- query = query.order(order.field as keyof PageCollections[C], order.direction)
40
+ query = query.order(order.field as PageField, order.direction)
35
41
  }
36
42
 
37
43
  return query
@@ -1,6 +1,7 @@
1
- import type { Collections, PageCollections, SQLOperator } from '@nuxt/content'
1
+ import type { CollectionQueryBuilder, Collections, PageCollectionItemBase, PageCollections, SQLOperator } from '@nuxt/content'
2
2
  import type { BadgeProps } from '@nuxt/ui'
3
3
  import type { MaybeRefOrGetter } from 'vue'
4
+ import type { PageCollectionName } from '../types/content'
4
5
  import { computed, toValue } from 'vue'
5
6
 
6
7
  export interface ArticleFilter {
@@ -9,7 +10,7 @@ export interface ArticleFilter {
9
10
  value?: unknown
10
11
  }
11
12
 
12
- export interface UseArticleListOptions<C extends keyof PageCollections = 'article'> {
13
+ export interface UseArticleListOptions<C extends PageCollectionName = 'article'> {
13
14
  page?: MaybeRefOrGetter<number | undefined>
14
15
  itemsPerPage?: MaybeRefOrGetter<number | undefined>
15
16
  category?: MaybeRefOrGetter<string | undefined>
@@ -30,10 +31,11 @@ export interface UseArticleListOptions<C extends keyof PageCollections = 'articl
30
31
  * Composable to fetch a paginated and filtered list of articles or any other collection.
31
32
  * Includes automatic resolution of authors and category badges.
32
33
  */
33
- export function useArticleList<C extends keyof PageCollections = 'article'>(options: UseArticleListOptions<C> = {}) {
34
+ export function useArticleList<C extends PageCollectionName = 'article'>(options: UseArticleListOptions<C> = {}) {
34
35
  const collection = computed(() => (toValue(options.collection) || ('article' as C)) as C)
35
36
 
36
37
  const { config } = useVariant(collection)
38
+ const appConfig = useAppConfig()
37
39
 
38
40
  const page = computed(() => toValue(options.page) || 1)
39
41
  const itemsPerPage = computed(() => toValue(options.itemsPerPage) || config.value.list?.itemsPerPage || 12)
@@ -64,22 +66,30 @@ export function useArticleList<C extends keyof PageCollections = 'article'>(opti
64
66
 
65
67
  return useAsyncData(queryKey, async () => {
66
68
  // The result keeps consumer collection fields together with the article fields used by the UI.
67
- type ArticleItem = Collections[C] & Collections['article']
69
+ type ConsumerItem = PageCollectionItemBase
70
+ & (C extends keyof PageCollections ? PageCollections[C] : object)
71
+ type ArticleItem = ConsumerItem & {
72
+ authors?: string[]
73
+ category?: string
74
+ date?: string
75
+ dateEnd?: string
76
+ published?: boolean
77
+ }
68
78
  const getDataQuery = () => {
69
- let query = queryCollection(collection.value)
79
+ let query = queryCollection(collection.value as keyof Collections) as unknown as CollectionQueryBuilder<ArticleItem>
70
80
 
71
81
  if (publishedOnly.value) {
72
- query = query.where('published', '=', true)
82
+ query = query.where('published' as Extract<keyof ArticleItem, string>, '=', true)
73
83
  }
74
84
 
75
85
  if (category.value && category.value !== labelAll.value) {
76
- query = query.where('category', '=', category.value)
86
+ query = query.where('category' as Extract<keyof ArticleItem, string>, '=', category.value)
77
87
  }
78
88
 
79
89
  // Apply additional filters
80
90
  if (where.value && Array.isArray(where.value)) {
81
91
  where.value.forEach((filter) => {
82
- query = query.where(filter.field, filter.operator, filter.value)
92
+ query = query.where(filter.field as Extract<keyof ArticleItem, string>, filter.operator, filter.value)
83
93
  })
84
94
  }
85
95
  return query
@@ -88,25 +98,26 @@ export function useArticleList<C extends keyof PageCollections = 'article'>(opti
88
98
  let finalQuery = getDataQuery()
89
99
 
90
100
  if (sort.value) {
91
- finalQuery = finalQuery.order(sort.value.field as keyof Collections[C], sort.value.direction)
101
+ finalQuery = finalQuery.order(sort.value.field as Extract<keyof ArticleItem, string>, sort.value.direction)
92
102
  }
93
103
 
94
- const [articles, total] = await Promise.all([
95
- finalQuery
96
- .skip((page.value - 1) * itemsPerPage.value)
97
- .limit(itemsPerPage.value)
98
- .all() as Promise<ArticleItem[]>,
99
- getDataQuery().count() as Promise<number>,
100
- ])
101
-
102
- const userProps = config.value.user ?? {}
103
- const authorsByUsername = await resolveUserMap(
104
- articles.flatMap(article => article.authors ?? []),
105
- userProps,
106
- )
107
-
108
- // Resolve display details without issuing additional collection queries.
109
- const resolved = articles.map((article) => {
104
+ const [articles, total] = await Promise.all([
105
+ finalQuery
106
+ .skip((page.value - 1) * itemsPerPage.value)
107
+ .limit(itemsPerPage.value)
108
+ .all() as Promise<ArticleItem[]>,
109
+ getDataQuery().count() as Promise<number>,
110
+ ])
111
+
112
+ const userProps = config.value.user ?? {}
113
+ const authorsByUsername = await resolveUserMap(
114
+ articles.flatMap(article => article.authors ?? []),
115
+ userProps,
116
+ appConfig.app.content.userCollection,
117
+ )
118
+
119
+ // Resolve display details without issuing additional collection queries.
120
+ const resolved = articles.map((article) => {
110
121
  // Resolve Category Badge directly
111
122
  const categoryKey = article.category
112
123
  let resolvedBadge: BadgeProps | undefined
@@ -118,19 +129,19 @@ export function useArticleList<C extends keyof PageCollections = 'article'>(opti
118
129
  }
119
130
  }
120
131
 
121
- // Preserve the author order declared in front matter.
122
- const resolvedAuthors = (article.authors ?? []).flatMap((username) => {
123
- const author = authorsByUsername.get(username)
124
- return author ? [author] : []
125
- })
126
-
132
+ // Preserve the author order declared in front matter.
133
+ const resolvedAuthors = (article.authors ?? []).flatMap((username) => {
134
+ const author = authorsByUsername.get(username)
135
+ return author ? [author] : []
136
+ })
137
+
127
138
  return {
128
139
  ...article,
129
140
  resolvedBadge,
130
141
  resolvedAuthors,
131
142
  }
132
- })
133
-
143
+ })
144
+
134
145
  return {
135
146
  articles: resolved,
136
147
  total,
@@ -1,29 +1,32 @@
1
- import type { PageCollections } from '@nuxt/content'
1
+ import type { PageCollectionItemBase, PageCollections } from '@nuxt/content'
2
2
  import type { AsyncData } from 'nuxt/app'
3
3
  import type { MaybeRefOrGetter } from 'vue'
4
+ import type { PageCollectionName } from '../types/content'
4
5
  import { computed, toValue } from 'vue'
5
6
 
6
- interface UsePageContentOptions<C extends keyof PageCollections = 'page'> {
7
- path?: MaybeRefOrGetter<string | undefined>
8
- collection?: MaybeRefOrGetter<C>
9
- }
7
+ type PageForCollection<C extends PageCollectionName> = PageCollectionItemBase
8
+ & (C extends keyof PageCollections ? PageCollections[C] : object)
9
+
10
+ interface UsePageContentOptions<C extends PageCollectionName = 'page'> {
11
+ path?: MaybeRefOrGetter<string | undefined>
12
+ collection?: MaybeRefOrGetter<C>
13
+ }
10
14
 
11
15
  /**
12
- * Composable to fetch content from a Nuxt Content collection.
13
- *
14
- * @param options - Configuration options for fetching content
15
- * @param options.path - Optional path to fetch. Defaults to current route path.
16
- * @param options.collection - Optional collection to fetch from. Defaults to 'page'.
17
- */
18
- export function usePageContent<C extends keyof PageCollections = 'page', B = object>(
19
- { path, collection }: UsePageContentOptions<C> = {},
20
- ) {
16
+ * Fetches a page without requiring the layer to own its collection definition.
17
+ * Known consumer collection names retain their generated Nuxt Content type.
18
+ */
19
+ export function usePageContent<C extends PageCollectionName = 'page', B extends object = object>(
20
+ { path, collection }: UsePageContentOptions<C> = {},
21
+ ) {
21
22
  const route = useRoute()
22
23
  const resolvedPath = computed(() => toValue(path) ?? route.path)
23
24
  const resolvedCollection = computed(() => (toValue(collection) ?? ('page' as C)) as C)
24
- const key = computed(() => `${String(resolvedCollection.value)}:${resolvedPath.value}`)
25
+ const key = computed(() => `${resolvedCollection.value}:${resolvedPath.value}`)
26
+
27
+ const handler = () => queryCollection(resolvedCollection.value as keyof PageCollections)
28
+ .path(resolvedPath.value)
29
+ .first()
25
30
 
26
- const handler = () => queryCollection(resolvedCollection.value as keyof PageCollections).path(resolvedPath.value).first()
27
-
28
- return useAsyncData(key, handler) as unknown as AsyncData<(PageCollections[C] & B) | null, Error | null>
29
- }
31
+ return useAsyncData(key, handler) as unknown as AsyncData<(PageForCollection<C> & B) | null, Error | null>
32
+ }
@@ -1,135 +1,132 @@
1
- <script setup lang="ts" generic="C extends keyof PageCollections = 'article'">
2
- import type { PageCollections } from '@nuxt/content'
3
- import type { BreadcrumbItem } from '@nuxt/ui'
4
-
5
- const props = defineProps<{
6
- path?: string
7
- collection?: C
8
- }>()
9
-
10
- const route = useRoute()
11
- const collection = computed(() => props.collection ?? 'article')
12
-
13
- const { data: page } = await usePageContent({
14
- path: () => props.path,
15
- collection: () => collection.value,
16
- })
17
-
18
- const { config, has } = useVariant(collection)
19
- const hasHeader = has('header')
20
- const hasBackButton = has('backButton')
21
- const hasCopyButton = has('copyButton')
22
- const hasSeparator = has('separator')
23
- const hasSurround = has('surround')
24
- const hasToc = has('toc')
25
- const tocEnabled = computed(() => !page.value || !('toc' in page.value) || page.value.toc !== false)
26
- const renderToc = computed(() => hasToc.value
27
- && tocEnabled.value
28
- && Boolean(page.value?.body?.toc?.links?.length))
29
-
30
- if (!page.value) {
31
- throw createError({
32
- statusCode: 404,
33
- statusMessage: `${collection.value} ${props.path || route.path} not found`,
34
- fatal: true,
35
- })
36
- }
37
-
38
- usePageSeo(page)
39
-
40
- const header = computed(() => resolvePageHeader(page.value))
41
-
42
- const breadcrumbsBase = computed(() => (config.value.breadcrumbs ?? []) as BreadcrumbItem[])
43
-
44
- const breadcrumbItems = computed(() => {
45
- const baseItems = breadcrumbsBase.value.map((item, index) => ({
46
- ...item,
47
- ...(index === breadcrumbsBase.value.length - 1
48
- ? { ui: { ...item.ui, separator: 'hidden sm:flex' } }
49
- : {}),
50
- }))
51
-
52
- return [
53
- ...baseItems,
54
- {
55
- label: page.value?.title,
56
- to: page.value?.path,
57
- ui: { item: 'hidden sm:flex' },
58
- },
59
- ]
60
- })
61
-
62
- const backLink = computed(() => {
63
- const lastBreadcrumb = breadcrumbsBase.value.at(-1)
64
-
65
- if (!lastBreadcrumb && !config.value?.backButton)
66
- return null
67
-
68
- return { ...(lastBreadcrumb ?? {}), ...config.value?.backButton }
69
- })
70
- </script>
71
-
72
- <template>
73
- <UContainer v-if="page">
74
- <UPageHeader
75
- v-if="hasHeader && header"
76
- v-bind="header"
77
- :ui="{
78
- headline: 'all:flex flex-col gap-y-8 items-start',
79
- wrapper: 'lg:flex-row',
80
- }"
81
- >
82
- <template #headline>
83
- <UBreadcrumb
84
- :ui="{ root: 'max-w-full' }"
85
- :items="breadcrumbItems"
86
- />
87
- <HArticleHeadlineMeta :page="page" :collection="collection" />
88
- </template>
89
- <HArticleHeaderBody :page="page" :collection="collection">
90
- <slot name="header" />
91
- </HArticleHeaderBody>
92
- </UPageHeader>
93
-
94
- <UPage>
95
- <UPageBody>
96
- <slot />
97
-
98
- <div
99
- v-if="hasBackButton || hasCopyButton"
100
- class="all:flex items-center justify-between mt-12"
101
- >
102
- <HBackButton
103
- v-if="hasBackButton && backLink"
104
- v-bind="backLink"
105
- />
106
- <div class="all:flex justify-end items-center gap-1.5 ml-auto">
107
- <HCopyButton v-if="hasCopyButton" v-bind="config.copyButton" />
108
- </div>
109
- </div>
110
-
111
- <HSeparator
112
- v-if="hasSeparator"
113
- :stem="page?.stem"
114
- :extension="page?.extension"
115
- v-bind="config.separator"
116
- />
117
-
118
- <HSurround
119
- v-if="hasSurround"
120
- :collection="collection"
121
- v-bind="config.surround"
122
- :query="config.query"
123
- />
124
- </UPageBody>
125
-
126
- <template #right>
127
- <HToc
128
- v-if="renderToc"
129
- :links="page?.body?.toc?.links"
130
- :title="page?.body?.toc?.title"
131
- />
132
- </template>
133
- </UPage>
134
- </UContainer>
135
- </template>
1
+ <script setup lang="ts" generic="C extends PageCollectionName = 'article'">
2
+ import type { BreadcrumbItem } from '@nuxt/ui'
3
+ import type { PageCollectionName } from '../types/content'
4
+
5
+ const props = defineProps<{
6
+ path?: string
7
+ collection?: C
8
+ }>()
9
+
10
+ const route = useRoute()
11
+ const collection = computed(() => props.collection ?? 'article')
12
+
13
+ const { data: page } = await usePageContent({
14
+ path: () => props.path,
15
+ collection: () => collection.value,
16
+ })
17
+
18
+ const { config, has } = useVariant(collection)
19
+ const hasHeader = has('header')
20
+ const hasBackButton = has('backButton')
21
+ const hasCopyButton = has('copyButton')
22
+ const hasSeparator = has('separator')
23
+ const hasSurround = has('surround')
24
+ const hasToc = has('toc')
25
+ const tocEnabled = computed(() => !page.value || !('toc' in page.value) || page.value.toc !== false)
26
+ const showTocSidebar = computed(() => hasToc.value && tocEnabled.value)
27
+
28
+ if (!page.value) {
29
+ throw createError({
30
+ statusCode: 404,
31
+ statusMessage: `${collection.value} ${props.path || route.path} not found`,
32
+ fatal: true,
33
+ })
34
+ }
35
+
36
+ usePageSeo(page)
37
+
38
+ const header = computed(() => resolvePageHeader(page.value))
39
+
40
+ const breadcrumbsBase = computed(() => (config.value.breadcrumbs ?? []) as BreadcrumbItem[])
41
+
42
+ const breadcrumbItems = computed(() => {
43
+ const baseItems = breadcrumbsBase.value.map((item, index) => ({
44
+ ...item,
45
+ ...(index === breadcrumbsBase.value.length - 1
46
+ ? { ui: { ...item.ui, separator: 'hidden sm:flex' } }
47
+ : {}),
48
+ }))
49
+
50
+ return [
51
+ ...baseItems,
52
+ {
53
+ label: page.value?.title,
54
+ to: page.value?.path,
55
+ ui: { item: 'hidden sm:flex' },
56
+ },
57
+ ]
58
+ })
59
+
60
+ const backLink = computed(() => {
61
+ const lastBreadcrumb = breadcrumbsBase.value.at(-1)
62
+
63
+ if (!lastBreadcrumb && !config.value?.backButton)
64
+ return null
65
+
66
+ return { ...(lastBreadcrumb ?? {}), ...config.value?.backButton }
67
+ })
68
+ </script>
69
+
70
+ <template>
71
+ <UContainer v-if="page">
72
+ <UPageHeader
73
+ v-if="hasHeader && header"
74
+ v-bind="header"
75
+ :ui="{
76
+ headline: 'all:flex flex-col gap-y-8 items-start',
77
+ wrapper: 'lg:flex-row',
78
+ }"
79
+ >
80
+ <template #headline>
81
+ <UBreadcrumb
82
+ :ui="{ root: 'max-w-full' }"
83
+ :items="breadcrumbItems"
84
+ />
85
+ <HArticleHeadlineMeta :page="page" :collection="collection" />
86
+ </template>
87
+ <HArticleHeaderBody :page="page" :collection="collection">
88
+ <slot name="header" />
89
+ </HArticleHeaderBody>
90
+ </UPageHeader>
91
+
92
+ <UPage>
93
+ <UPageBody>
94
+ <slot />
95
+
96
+ <div
97
+ v-if="hasBackButton || hasCopyButton"
98
+ class="all:flex items-center justify-between mt-12"
99
+ >
100
+ <HBackButton
101
+ v-if="hasBackButton && backLink"
102
+ v-bind="backLink"
103
+ />
104
+ <div class="all:flex justify-end items-center gap-1.5 ml-auto">
105
+ <HCopyButton v-if="hasCopyButton" v-bind="config.copyButton" />
106
+ </div>
107
+ </div>
108
+
109
+ <HSeparator
110
+ v-if="hasSeparator"
111
+ :stem="page?.stem"
112
+ :extension="page?.extension"
113
+ v-bind="config.separator"
114
+ />
115
+
116
+ <HSurround
117
+ v-if="hasSurround"
118
+ :collection="collection"
119
+ v-bind="config.surround"
120
+ :query="config.query"
121
+ />
122
+ </UPageBody>
123
+
124
+ <template v-if="showTocSidebar" #right>
125
+ <HToc
126
+ :links="page?.body?.toc?.links"
127
+ :title="page?.body?.toc?.title"
128
+ />
129
+ </template>
130
+ </UPage>
131
+ </UContainer>
132
+ </template>