From ee8ae4df265aa1a406b059e2284b91048da6f47a Mon Sep 17 00:00:00 2001 From: Jeanne Mas Date: Tue, 4 Jun 2024 12:12:18 -0400 Subject: [PATCH] component: Card --- .../components/calendar/CalendarRoot.svelte | 1 + src/lib/components/card/CardContent.svelte | 32 +++++++++++ .../components/card/CardDescription.svelte | 43 +++++++++++++- src/lib/components/card/CardFooter.svelte | 38 ++++++++++++- src/lib/components/card/CardHeader.svelte | 46 ++++++++++++++- src/lib/components/card/CardRoot.svelte | 56 ++++++++++++++++++- src/lib/components/card/CardTitle.svelte | 41 ++++++++++++++ src/lib/components/card/context.ts | 11 ++++ src/lib/components/card/index.ts | 2 +- src/routes/accordion/+page.ts | 4 +- src/routes/alert-dialog/+page.ts | 14 ++--- src/routes/alert-dialog/props.schema.ts | 12 +++- src/routes/alert/+page.ts | 8 +-- src/routes/alert/props.schema.ts | 7 ++- src/routes/breadcrumb/+page.ts | 8 +-- src/routes/breadcrumb/props.schema.ts | 7 ++- src/routes/card/+page.svelte | 6 +- src/routes/card/+page.ts | 8 +-- src/routes/card/props.schema.ts | 4 +- 19 files changed, 305 insertions(+), 43 deletions(-) create mode 100644 src/lib/components/card/context.ts diff --git a/src/lib/components/calendar/CalendarRoot.svelte b/src/lib/components/calendar/CalendarRoot.svelte index 7f5aa57..9e85b8b 100644 --- a/src/lib/components/calendar/CalendarRoot.svelte +++ b/src/lib/components/calendar/CalendarRoot.svelte @@ -1,4 +1,5 @@ + +
--> + +

diff --git a/src/lib/components/card/CardFooter.svelte b/src/lib/components/card/CardFooter.svelte index e31e0a3..7b1906e 100644 --- a/src/lib/components/card/CardFooter.svelte +++ b/src/lib/components/card/CardFooter.svelte @@ -4,10 +4,12 @@ import type { EmptyObject } from '$lib/utils/types.js'; + import { rootContext } from './context.js'; + /** * The attributes of the footer. */ - export type Attributes = SvelteHTMLElements['div']; + export type Attributes = SvelteHTMLElements['footer']; /** * The props of the footer. */ @@ -33,16 +35,46 @@ type $$Slots = Slots; $: attributes = $$restProps as Attributes; + + const rootCtx = rootContext.get(); + + if (!rootCtx) { + throw new Error('Card.Content must be used within a Card.Root component.'); + } -

+ +
-
+ diff --git a/src/lib/components/card/CardHeader.svelte b/src/lib/components/card/CardHeader.svelte index b0f5c9d..df3e9e1 100644 --- a/src/lib/components/card/CardHeader.svelte +++ b/src/lib/components/card/CardHeader.svelte @@ -1,13 +1,16 @@ -
+ +
-
+ diff --git a/src/lib/components/card/CardRoot.svelte b/src/lib/components/card/CardRoot.svelte index c85a24b..bfcb0ae 100644 --- a/src/lib/components/card/CardRoot.svelte +++ b/src/lib/components/card/CardRoot.svelte @@ -1,9 +1,12 @@ + +
--> + + diff --git a/src/lib/components/card/context.ts b/src/lib/components/card/context.ts new file mode 100644 index 0000000..14c1ad5 --- /dev/null +++ b/src/lib/components/card/context.ts @@ -0,0 +1,11 @@ +import type { Writable } from 'svelte/store'; + +import { Context } from '$lib/utils/context.js'; + +import type { HeaderProps, RootProps } from './index.js'; + +type HeaderContext = RootContext & Pick; +type RootContext = Pick; + +export const headerContext = new Context>(); +export const rootContext = new Context>(); diff --git a/src/lib/components/card/index.ts b/src/lib/components/card/index.ts index fd44703..078fee7 100644 --- a/src/lib/components/card/index.ts +++ b/src/lib/components/card/index.ts @@ -32,7 +32,7 @@ export { type Attributes as RootAttributes, type Props as RootProps, type Slots as RootSlots, - type Variant as RootVariant, + type Variant, } from './CardRoot.svelte'; export { default as Title, diff --git a/src/routes/accordion/+page.ts b/src/routes/accordion/+page.ts index 950cac7..6a4c465 100644 --- a/src/routes/accordion/+page.ts +++ b/src/routes/accordion/+page.ts @@ -1,7 +1,7 @@ import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; -import { rootStyles } from '$lib/components/accordion/index.js'; +import * as Accordion from '$lib/components/accordion/index.js'; import type { PageLoad } from './$types.js'; import { schema } from './props.schema.js'; @@ -9,7 +9,7 @@ import { schema } from './props.schema.js'; export const load = (async ({ fetch }) => { const form = await superValidate(zod(schema), { defaults: { - disabled: rootStyles.defaultVariants.disabled!, + disabled: Accordion.rootStyles.defaultVariants.disabled!, }, }); const posts: { diff --git a/src/routes/alert-dialog/+page.ts b/src/routes/alert-dialog/+page.ts index fc8f7c2..1b67650 100644 --- a/src/routes/alert-dialog/+page.ts +++ b/src/routes/alert-dialog/+page.ts @@ -2,7 +2,7 @@ import type { Selected } from 'bits-ui'; import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; -import { rootStyles, type Breakpoint, type Variant } from '$lib/components/alert-dialog/index.js'; +import * as AlertDialog from '$lib/components/alert-dialog/index.js'; import type { PageLoad } from './$types.js'; import { schema } from './props.schema.js'; @@ -10,21 +10,21 @@ import { schema } from './props.schema.js'; export const load = (async () => { const form = await superValidate(zod(schema), { defaults: { - breakpoint: rootStyles.defaultVariants.breakpoint!, + breakpoint: AlertDialog.rootStyles.defaultVariants.breakpoint!, closeOnEscape: true, closeOnOutsideClick: false, preventScroll: true, - variant: rootStyles.defaultVariants.variant!, + variant: AlertDialog.rootStyles.defaultVariants.variant!, }, }); - const breakpoints = Object.keys(rootStyles.variants.breakpoint).map((breakpoint) => ({ + const breakpoints = Object.keys(AlertDialog.rootStyles.variants.breakpoint).map((breakpoint) => ({ label: breakpoint, value: breakpoint, - })) as Selected[]; - const variants = Object.keys(rootStyles.variants.variant).map((variant) => ({ + })) as Selected[]; + const variants = Object.keys(AlertDialog.rootStyles.variants.variant).map((variant) => ({ label: variant, value: variant, - })) as Selected[]; + })) as Selected[]; return { breakpoints, diff --git a/src/routes/alert-dialog/props.schema.ts b/src/routes/alert-dialog/props.schema.ts index a4aee1b..1314c32 100644 --- a/src/routes/alert-dialog/props.schema.ts +++ b/src/routes/alert-dialog/props.schema.ts @@ -1,9 +1,15 @@ import z from 'zod'; -import { rootStyles, type Breakpoint, type Variant } from '$lib/components/alert-dialog/index.js'; +import * as AlertDialog from '$lib/components/alert-dialog/index.js'; -const breakpoints = Object.keys(rootStyles.variants.breakpoint) as [Breakpoint, ...Breakpoint[]]; -const variants = Object.keys(rootStyles.variants.variant) as [Variant, ...Variant[]]; +const breakpoints = Object.keys(AlertDialog.rootStyles.variants.breakpoint) as [ + AlertDialog.Breakpoint, + ...AlertDialog.Breakpoint[], +]; +const variants = Object.keys(AlertDialog.rootStyles.variants.variant) as [ + AlertDialog.Variant, + ...AlertDialog.Variant[], +]; export const schema = z.object({ breakpoint: z.enum(breakpoints, { diff --git a/src/routes/alert/+page.ts b/src/routes/alert/+page.ts index 65a1e11..cfa595f 100644 --- a/src/routes/alert/+page.ts +++ b/src/routes/alert/+page.ts @@ -2,7 +2,7 @@ import type { Selected } from 'bits-ui'; import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; -import { rootStyles, type Variant } from '$lib/components/alert/index.js'; +import * as Alert from '$lib/components/alert/index.js'; import type { PageLoad } from './$types.js'; import { schema } from './props.schema.js'; @@ -10,13 +10,13 @@ import { schema } from './props.schema.js'; export const load = (async () => { const form = await superValidate(zod(schema), { defaults: { - variant: rootStyles.defaultVariants.variant!, + variant: Alert.rootStyles.defaultVariants.variant!, }, }); - const variants = Object.keys(rootStyles.variants.variant).map((variant) => ({ + const variants = Object.keys(Alert.rootStyles.variants.variant).map((variant) => ({ label: variant, value: variant, - })) as Selected[]; + })) as Selected[]; return { form, diff --git a/src/routes/alert/props.schema.ts b/src/routes/alert/props.schema.ts index 2b53643..28c71c1 100644 --- a/src/routes/alert/props.schema.ts +++ b/src/routes/alert/props.schema.ts @@ -1,8 +1,11 @@ import z from 'zod'; -import { rootStyles, type Variant } from '$lib/components/alert/index.js'; +import * as Alert from '$lib/components/alert/index.js'; -const variants = Object.keys(rootStyles.variants.variant) as [Variant, ...Variant[]]; +const variants = Object.keys(Alert.rootStyles.variants.variant) as [ + Alert.Variant, + ...Alert.Variant[], +]; export const schema = z.object({ variant: z.enum(variants, { diff --git a/src/routes/breadcrumb/+page.ts b/src/routes/breadcrumb/+page.ts index 6ca7422..c242511 100644 --- a/src/routes/breadcrumb/+page.ts +++ b/src/routes/breadcrumb/+page.ts @@ -2,7 +2,7 @@ import type { Selected } from 'bits-ui'; import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; -import { rootStyles, type Breakpoint } from '$lib/components/breadcrumb/index.js'; +import * as Breadcrumb from '$lib/components/breadcrumb/index.js'; import type { PageLoad } from './$types.js'; import { schema } from './props.schema.js'; @@ -10,13 +10,13 @@ import { schema } from './props.schema.js'; export const load = (async () => { const form = await superValidate(zod(schema), { defaults: { - breakpoint: rootStyles.defaultVariants.breakpoint!, + breakpoint: Breadcrumb.rootStyles.defaultVariants.breakpoint!, }, }); - const breakpoints = Object.keys(rootStyles.variants.breakpoint).map((breakpoint) => ({ + const breakpoints = Object.keys(Breadcrumb.rootStyles.variants.breakpoint).map((breakpoint) => ({ label: breakpoint, value: breakpoint, - })) as Selected[]; + })) as Selected[]; return { breakpoints, diff --git a/src/routes/breadcrumb/props.schema.ts b/src/routes/breadcrumb/props.schema.ts index e6a65c1..701b52e 100644 --- a/src/routes/breadcrumb/props.schema.ts +++ b/src/routes/breadcrumb/props.schema.ts @@ -1,8 +1,11 @@ import z from 'zod'; -import { rootStyles, type Breakpoint } from '$lib/components/breadcrumb/index.js'; +import * as Breadcrumb from '$lib/components/breadcrumb/index.js'; -const breakpoints = Object.keys(rootStyles.variants.breakpoint) as [Breakpoint, ...Breakpoint[]]; +const breakpoints = Object.keys(Breadcrumb.rootStyles.variants.breakpoint) as [ + Breadcrumb.Breakpoint, + ...Breadcrumb.Breakpoint[], +]; export const schema = z.object({ breakpoint: z.enum(breakpoints, { diff --git a/src/routes/card/+page.svelte b/src/routes/card/+page.svelte index 2259e6c..7c6e8af 100644 --- a/src/routes/card/+page.svelte +++ b/src/routes/card/+page.svelte @@ -27,9 +27,9 @@ $: selectedVariant = { label: $props.variant, value: $props.variant, - } satisfies Selected; + } satisfies Selected; - function handleVariantthange(selected?: Selected) { + function handleVariantChange(selected?: Selected) { $props.variant = selected!.value; } @@ -46,7 +46,7 @@ diff --git a/src/routes/card/+page.ts b/src/routes/card/+page.ts index 245cfcf..8cc2840 100644 --- a/src/routes/card/+page.ts +++ b/src/routes/card/+page.ts @@ -2,7 +2,7 @@ import type { Selected } from 'bits-ui'; import { superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; -import { rootStyles, type RootVariant } from '$lib/components/card/index.js'; +import * as Card from '$lib/components/card/index.js'; import type { PageLoad } from './$types.js'; import { schema } from './props.schema.js'; @@ -10,13 +10,13 @@ import { schema } from './props.schema.js'; export const load = (async () => { const form = await superValidate(zod(schema), { defaults: { - variant: rootStyles.defaultVariants.variant!, + variant: Card.rootStyles.defaultVariants.variant!, }, }); - const variants = Object.keys(rootStyles.variants.variant).map((variant) => ({ + const variants = Object.keys(Card.rootStyles.variants.variant).map((variant) => ({ label: variant, value: variant, - })) as Selected[]; + })) as Selected[]; return { form, diff --git a/src/routes/card/props.schema.ts b/src/routes/card/props.schema.ts index a6fe08e..88e25e4 100644 --- a/src/routes/card/props.schema.ts +++ b/src/routes/card/props.schema.ts @@ -1,8 +1,8 @@ import z from 'zod'; -import { rootStyles, type RootVariant } from '$lib/components/card/index.js'; +import * as Card from '$lib/components/card/index.js'; -const variants = Object.keys(rootStyles.variants.variant) as [RootVariant, ...RootVariant[]]; +const variants = Object.keys(Card.rootStyles.variants.variant) as [Card.Variant, ...Card.Variant[]]; export const schema = z.object({ variant: z.enum(variants, {