|
1 | 1 | <script lang="ts"> |
2 | 2 | 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"; |
4 | 4 | import HighlightCompo from "../../utils/HighlightCompo.svelte"; |
5 | 5 | import DynamicCodeBlockHighlight from "../../utils/DynamicCodeBlockHighlight.svelte"; |
6 | 6 | import CodeWrapper from "../../utils/CodeWrapper.svelte"; |
|
55 | 55 | const changeClass = () => { |
56 | 56 | cardClass = cardClass === "" ? "pl-10" : ""; |
57 | 57 | }; |
58 | | - let cardImage = $state({}); |
| 58 | + let cardImage = $state<ImgType | undefined>(undefined); |
59 | 59 | 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; |
61 | 66 | }; |
62 | 67 |
|
63 | 68 | // code generator |
|
72 | 77 | if (link) props.push(` href="${link}"`); |
73 | 78 | if (horizontal) props.push(` horizontal`); |
74 | 79 | if (reverse) props.push(` reverse`); |
75 | | - if (Object.keys(cardImage).length > 0) { |
| 80 | + if (cardImage && typeof cardImage === "object") { |
76 | 81 | const imgString = Object.entries(cardImage) |
77 | 82 | .map(([key, value]) => `${key}:"${value}"`) |
78 | 83 | .join(","); |
|
105 | 110 | exampleExpand = codeBlock.isOpen; |
106 | 111 | builderExpand = builder.isOpen; |
107 | 112 | }); |
| 113 | +
|
| 114 | + // helper function |
| 115 | + const hasImageContent = (img: ImgType | undefined): boolean => { |
| 116 | + return !!img && !!img.src; |
| 117 | + }; |
108 | 118 | </script> |
109 | 119 |
|
110 | 120 | <H1>Cards</H1> |
|
115 | 125 | <H2>Interactive Card Builder</H2> |
116 | 126 | <CodeWrapper> |
117 | 127 | <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}> |
119 | 129 | <h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">Noteworthy technology acquisitions</h5> |
120 | 130 | <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> |
121 | 131 | </Card> |
|
147 | 157 | <div class="flex flex-wrap justify-center gap-2 md:justify-start"> |
148 | 158 | <Button class="w-40" color="sky" onclick={changeLink}>{link === "" ? "Add link" : "Remove link"}</Button> |
149 | 159 | <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> |
153 | 172 | </div> |
154 | 173 | {#snippet codeblock()} |
155 | 174 | <DynamicCodeBlockHighlight handleExpandClick={handleBuilderExpandClick} expand={builderExpand} showExpandButton={showBuilderExpandButton} code={generatedCode} /> |
|
0 commit comments