Skip to content

Commit 721e10c

Browse files
committed
fix: card page
1 parent 7e56bb3 commit 721e10c

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ yarn.lock
55
samples/
66
/src/routes/md/
77
*.html
8+
/src/routes/utils/highlight/*

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default [
3838
}
3939
},
4040
{
41-
ignores: [".vercel/", "build/", ".svelte-kit/", "dist/", "test-results/", "*.md", "src/**/*/Setup.svelte"]
41+
ignores: [".vercel/", "src/routes/utils/highlight", "build/", ".svelte-kit/", "dist/", "test-results/", "*.md", "src/**/*/Setup.svelte"]
4242
},
4343
{
4444
rules: {

src/lib/cards/index.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,10 @@ type ShadowType = "sm" | "normal" | "lg" | "md" | "xl" | "2xl" | "inner" | undef
1010
type ColorType = "gray" | "primary" | "secondary" | "red" | "orange" | "amber" | "yellow" | "lime" | "green" | "emerald" | "teal" | "cyan" | "sky" | "blue" | "indigo" | "violet" | "purple" | "fuchsia" | "pink" | "rose" | undefined;
1111

1212
type ImgType = {
13-
src: string | undefined | null;
14-
alt: string | undefined | null;
13+
src?: string | undefined | null;
14+
alt?: string | undefined | null;
1515
};
1616

17-
// interface CardProps extends HTMLAttributes<HTMLDivElement> {
18-
// children: Snippet;
19-
// href?: string;
20-
// horizontal?: boolean;
21-
// color?: ColorType;
22-
// target?: string;
23-
// shadow?: ShadowType;
24-
// reverse?: boolean;
25-
// img?: ImgType;
26-
// padding?: PaddingType;
27-
// size?: CardSizeType;
28-
// class?: string;
29-
// onclick?: () => void;
30-
// imgClass?: string;
31-
// contentClass?: string;
32-
// }
33-
3417
interface BaseCardProps {
3518
children: Snippet;
3619
horizontal?: boolean;

src/routes/components/card/+page.svelte

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
22
import { type Component } from "svelte";
3-
import { Card, card, Button, Toggle, Label, Radio, uiHelpers, type RadioColorType, type CardProps } from "$lib";
3+
import { Card, card, Button, Toggle, Label, Radio, uiHelpers, type RadioColorType, type CardProps, type ImgType } from "$lib";
44
import HighlightCompo from "../../utils/HighlightCompo.svelte";
55
import DynamicCodeBlockHighlight from "../../utils/DynamicCodeBlockHighlight.svelte";
66
import CodeWrapper from "../../utils/CodeWrapper.svelte";
@@ -55,9 +55,14 @@
5555
const changeClass = () => {
5656
cardClass = cardClass === "" ? "pl-10" : "";
5757
};
58-
let cardImage = $state({});
58+
let cardImage = $state<ImgType | undefined>(undefined);
5959
const changeImage = () => {
60-
cardImage = Object.keys(cardImage).length === 0 ? { src: "/images/image-1.webp", alt: "my image" } : {};
60+
cardImage = !cardImage
61+
? {
62+
src: "/images/image-1.webp",
63+
alt: "my image"
64+
}
65+
: undefined;
6166
};
6267
6368
// code generator
@@ -72,7 +77,7 @@
7277
if (link) props.push(` href="${link}"`);
7378
if (horizontal) props.push(` horizontal`);
7479
if (reverse) props.push(` reverse`);
75-
if (Object.keys(cardImage).length > 0) {
80+
if (cardImage && typeof cardImage === "object") {
7681
const imgString = Object.entries(cardImage)
7782
.map(([key, value]) => `${key}:"${value}"`)
7883
.join(",");
@@ -105,6 +110,11 @@
105110
exampleExpand = codeBlock.isOpen;
106111
builderExpand = builder.isOpen;
107112
});
113+
114+
// helper function
115+
const hasImageContent = (img: ImgType | undefined): boolean => {
116+
return !!img && !!img.src;
117+
};
108118
</script>
109119

110120
<H1>Cards</H1>
@@ -115,7 +125,7 @@
115125
<H2>Interactive Card Builder</H2>
116126
<CodeWrapper>
117127
<div class="flex justify-center">
118-
<Card size={cardSize} {color} padding={cardPadding} shadow={cardShadow} href={link ? link : ""} class={cardClass} img={cardImage as CardProps["img"]} {horizontal} {reverse}>
128+
<Card size={cardSize} {color} padding={cardPadding} shadow={cardShadow} href={link ? link : ""} class={cardClass} img={cardImage} {horizontal} {reverse}>
119129
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">Noteworthy technology acquisitions</h5>
120130
<p class="font-normal leading-tight text-gray-700 dark:text-gray-300">Here are the biggest enterprise technology acquisitions of so far, in reverse chronological order.</p>
121131
</Card>
@@ -147,9 +157,18 @@
147157
<div class="flex flex-wrap justify-center gap-2 md:justify-start">
148158
<Button class="w-40" color="sky" onclick={changeLink}>{link === "" ? "Add link" : "Remove link"}</Button>
149159
<Button class="w-40" color="green" onclick={changeClass}>{cardClass ? "Remove class" : "Add class"}</Button>
150-
<Button class="w-40" color="blue" onclick={changeImage}>{Object.keys(cardImage).length === 0 ? "Add image" : "Remove image"}</Button>
151-
<Button disabled={Object.keys(cardImage).length === 0} class="w-40" color="violet" onclick={changeImgLayout}>{horizontal ? "Vertical" : "Horizontal"}</Button>
152-
<Toggle bind:checked={reverse} labelClass="italic dark:text-gray-500 {Object.keys(cardImage).length === 0 ? 'opacity-50 cursor-not-allowed' : ''}" disabled={Object.keys(cardImage).length === 0}>Reverse: {reverse}</Toggle>
160+
161+
<Button class="w-40" color="blue" onclick={changeImage}>
162+
{hasImageContent(cardImage) ? "Remove image" : "Add image"}
163+
</Button>
164+
165+
<Button disabled={!hasImageContent(cardImage)} class="w-40" color="violet" onclick={changeImgLayout}>
166+
{horizontal ? "Vertical" : "Horizontal"}
167+
</Button>
168+
169+
<Toggle bind:checked={reverse} labelClass="italic dark:text-gray-500 {!hasImageContent(cardImage) ? 'opacity-50 cursor-not-allowed' : ''}" disabled={!hasImageContent(cardImage)}>
170+
Reverse: {reverse}
171+
</Toggle>
153172
</div>
154173
{#snippet codeblock()}
155174
<DynamicCodeBlockHighlight handleExpandClick={handleBuilderExpandClick} expand={builderExpand} showExpandButton={showBuilderExpandButton} code={generatedCode} />

0 commit comments

Comments
 (0)