Skip to content

Commit 225e720

Browse files
committed
fix: extends svelte/element for Img
1 parent a638504 commit 225e720

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

src/lib/typography/Img.svelte

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
<script lang="ts">
22
import { twMerge } from 'tailwind-merge';
3-
interface Props {
3+
import type { HTMLImgAttributes } from 'svelte/elements';
4+
5+
interface Props extends HTMLImgAttributes{
46
caption?: string;
5-
src?: string;
6-
srcset?: string;
7-
size?: string;
8-
alignment?: string;
97
imgClass?: string;
108
figClass?: string;
11-
alt?: string;
12-
effect?: string;
139
captionClass?: string;
1410
class?: string;
1511
}
1612
17-
let { caption='', src='', srcset='', size = 'max-w-full', alignment = '', imgClass = 'h-auto', figClass = 'max-w-lg', alt = '', effect = '', captionClass = 'mt-2 text-sm text-center text-gray-500 dark:text-gray-400', class: className = '', ...attributes } = $props()
13+
let { caption='', imgClass = 'max-w-full h-auto', figClass = 'max-w-lg', captionClass = 'mt-2 text-sm text-center text-gray-500 dark:text-gray-400', class: className = '', ...attributes } = $props()
1814
</script>
1915

2016
{#if caption}
2117
<figure class={figClass}>
22-
<img class={twMerge(imgClass, size, alignment, effect, className)} {src} {srcset} {alt} />
18+
<img {...attributes} class={twMerge(imgClass, className)}/>
2319
<figcaption class={captionClass}>{@html caption}</figcaption>
2420
</figure>
2521
{:else}
26-
<img {...attributes} class={twMerge(imgClass, size, alignment, effect, className)} {src} {srcset} {alt} />
22+
<img {...attributes} class={twMerge(imgClass, className)}/>
2723
{/if}

src/routes/typography/image/+page.svelte

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,31 @@
4545
<p>Use this example to apply rounded corners to the image by using the rounded-size class where the size can be anything from small to extra large.</p>
4646

4747
<CodeWrapper class="flex justify-center">
48-
<Img src="/images/examples/[email protected]" alt="sample 1" size="max-w-lg" class="rounded-lg" />
48+
<Img src="/images/examples/[email protected]" alt="sample 1" class="max-w-lg rounded-lg" />
4949
</CodeWrapper>
5050

5151
<H3>Full circle</H3>
5252

5353
<p>Use this example to mask the image inside a circle using the rounded-full utility class from Tailwind CSS.</p>
5454

5555
<CodeWrapper class="flex justify-center">
56-
<Img src="/images/examples/[email protected]" alt="sample 1" size="w-96" imgClass="h-96" class="rounded-full" />
56+
<Img src="/images/examples/[email protected]" alt="sample 1" imgClass="h-96" class="w-96 rounded-full" />
5757
</CodeWrapper>
5858

5959
<H2>Image shadow</H2>
6060

6161
<p>This example can be used to show a shadow effect for the image using the shadow-size utility class.</p>
6262

6363
<CodeWrapper class="flex justify-center">
64-
<Img src="/images/examples/[email protected]" alt="sample 1" size="max-w-xl" class="shadow-xl dark:shadow-gray-800" />
64+
<Img src="/images/examples/[email protected]" alt="sample 1" class="max-w-xl shadow-xl dark:shadow-gray-800" />
6565
</CodeWrapper>
6666

6767
<H2>Retina-ready</H2>
6868

6969
<p>Use the srcset attribute to set Retina-ready images with double resolution.</p>
7070

71-
<CodeWrapper>svelte example class="flex justify-center" hideScript
72-
<Img srcset="/images/examples/image-1.jpg 1x, /images/examples/[email protected] 2x" alt="sample 1" size="w-full max-w-xl" class="rounded-lg" />
71+
<CodeWrapper class="flex justify-center">
72+
<Img srcset="/images/examples/image-1.jpg 1x, /images/examples/[email protected] 2x" alt="sample 1" class="w-full max-w-xl rounded-lg" />
7373
</CodeWrapper>
7474

7575
<H2>Image card</H2>
@@ -91,7 +91,7 @@ Use the filter option and apply a grayscale to the image element using the grays
9191

9292
<CodeWrapper class="flex justify-center" >
9393

94-
<Img src="/images/examples/content-gallery-3.png" size="max-w-lg" alt="My gallery" class="rounded-lg transition-all duration-300 cursor-pointer filter grayscale hover:grayscale-0" />
94+
<Img src="/images/examples/content-gallery-3.png" alt="My gallery" class="max-w-lg rounded-lg transition-all duration-300 cursor-pointer filter grayscale hover:grayscale-0" />
9595
</CodeWrapper>
9696

9797
<H3>Blur</H3>
@@ -100,7 +100,7 @@ Apply a blur by using the blur-size utility class from Tailwind CSS to an image
100100

101101
<CodeWrapper class="flex justify-center" >
102102

103-
<Img src="/images/examples/content-gallery-3.png" size="max-w-lg" alt="My gallery" class="rounded-lg transition-all duration-300 blur-sm hover:blur-none" />
103+
<Img src="/images/examples/content-gallery-3.png" alt="My gallery" class="max-w-lg rounded-lg transition-all duration-300 blur-sm hover:blur-none" />
104104
</CodeWrapper>
105105

106106
<H2>Alignment</H2>
@@ -113,16 +113,15 @@ By default, the image component will be aligned to the left side of the page.
113113

114114
<CodeWrapper>
115115

116-
<Img src="/images/examples/[email protected]" size="max-w-lg" alt="sample 1" />
116+
<Img src="/images/examples/[email protected]" class="max-w-lg" alt="sample 1" />
117117
</CodeWrapper>
118118

119119
<H3>Center</H3>
120120

121-
Horizontally align the image to the center of the page using the `mx-auto` class.
121+
Horizontally align the image to the center of the page using the `class` class.
122122

123123
<CodeWrapper>
124-
125-
<Img src="/images/examples/[email protected]" size="max-w-lg" alignment="mx-auto" alt="sample 1" />
124+
<Img src="/images/examples/[email protected]" class="mx-auto max-w-lg" alt="sample 1" />
126125
</CodeWrapper>
127126

128127
<H3>Right</H3>
@@ -131,7 +130,7 @@ Use the `ms-auto` class to align the image to the right side of the page.
131130

132131
<CodeWrapper>
133132

134-
<Img src="/images/examples/[email protected]" size="max-w-lg" alignment="ms-auto" alt="sample 1" />
133+
<Img src="/images/examples/[email protected]" class="ms-auto max-w-lg" alt="sample 1" />
135134
</CodeWrapper>
136135

137136
<H2>Sizes</H2>
@@ -144,7 +143,7 @@ Use the `max-w-xs` class to set a small size of the image.
144143

145144
<CodeWrapper class="flex justify-center">
146145

147-
<Img src="/images/examples/[email protected]" size="max-w-xs" alt="sample 1" />
146+
<Img src="/images/examples/[email protected]" class="max-w-xs" alt="sample 1" />
148147
</CodeWrapper>
149148

150149
<H3>Medium</H3>
@@ -153,23 +152,21 @@ Use the `max-w-md` class to set a medium size of the image.
153152

154153
<CodeWrapper class="flex justify-center" >
155154

156-
<Img src="/images/examples/[email protected]" size="max-w-md" alt="sample 1" />
155+
<Img src="/images/examples/[email protected]" class="max-w-md" alt="sample 1" />
157156
</CodeWrapper>
158157

159158
<H3>Large</H3>
160159

161160
Use the max-w-xl class to set a large size of the image.
162161

163162
<CodeWrapper class="flex justify-center" >
164-
165-
<Img src="/images/examples/[email protected]" size="max-w-xl" alt="sample 1" />
163+
<Img src="/images/examples/[email protected]" class="max-w-xl" alt="sample 1" />
166164
</CodeWrapper>
167165

168166
<H3>Full width</H3>
169167

170168
Use the max-w-full class to set the full width of the image as long as it doesn’t become larger than the original source.
171169

172170
<CodeWrapper class="flex justify-center" >
173-
174-
<Img src="/images/examples/[email protected]" size="max-w-full" alt="sample 1" />
171+
<Img src="/images/examples/[email protected]" class="max-w-full" alt="sample 1" />
175172
</CodeWrapper>

0 commit comments

Comments
 (0)