-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24491dd
commit 83fd8b2
Showing
9 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@casual-ui/svelte': minor | ||
--- | ||
|
||
feat: add CImage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
packages/docs/src/routes/features/components/basic/image/+page.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
title: Image | ||
componentName: CImage | ||
--- | ||
|
||
## Basic | ||
|
||
```svelte live | ||
<script> | ||
let src = 'https://picsum.photos/500/300' | ||
const toggle = () => { | ||
src = `https://picsum.photos/500/300?t=${Math.random()}` | ||
} | ||
</script> | ||
<CButton label="Toggle image" on:click={toggle} /> | ||
<CImage width="500px" height="300px" {src} /> | ||
``` | ||
|
||
## Custom placeholder | ||
|
||
```svelte live | ||
<script> | ||
let show = true | ||
</script> | ||
<CButton label="Toggle image" on:click={() => show = !show} /> | ||
{#if show} | ||
<CImage width="500px" height="300px" src="https://picsum.photos/500/300" placeholderSrc="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxZW0iIGhlaWdodD0iMWVtIiB2aWV3Qm94PSIwIDAgMzIgMzIiPjxwYXRoIGZpbGw9IiMyZGNjOWYiIGQ9Ik0zMCA1Ljg1MXYyMC4yOThIMlY1Ljg1MWgyOCIvPjxwYXRoIGZpbGw9IiNmZmYiIGQ9Ik0yNC4yMzIgOC41NDFhMi4yIDIuMiAwIDEgMCAxLjEyNy42MjNhMi4yMTIgMi4yMTIgMCAwIDAtMS4xMjctLjYyM00xOC4xMTEgMjAuMXEtMi43MjQtMy43ODgtNS40NS03LjU3NUw0LjU3OSAyMy43NjZoMTAuOXExLjMxNi0xLjgzMiAyLjYzNC0zLjY2M00yMi4wNTcgMTZxLTIuNzkzIDMuODgyLTUuNTg0IDcuNzY1aDExLjE2OVEyNC44NTEgMTkuODgyIDIyLjA1NyAxNloiLz48L3N2Zz4=" /> | ||
{/if} | ||
``` | ||
|
||
## Custom loading | ||
|
||
```svelte live | ||
<script> | ||
let src = 'https://picsum.photos/500/300' | ||
const toggle = () => { | ||
src = `https://picsum.photos/500/300?t=${Math.random()}` | ||
} | ||
</script> | ||
<CButton label="Toggle image" on:click={toggle} /> | ||
<div class="flex gap-4 mt-4"> | ||
<CImage width="100px" height="100px" {src}> | ||
<div class="text-white"> | ||
<CLoadingLattice slot="loading" /> | ||
</div> | ||
</CImage> | ||
<CImage width="100px" height="100px" {src}> | ||
<CLoadingPuff slot="loading" /> | ||
</CImage> | ||
<CImage width="100px" height="100px" {src}> | ||
<CLoadingPie slot="loading" /> | ||
</CImage> | ||
</div> | ||
``` | ||
|
||
:::important[Img native attrs] | ||
All the props pass to `CImage` that not in prop list would directly add to `<img />` tag. You can add `decoding`, `draggable`, etc.. | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<script> | ||
import { onMount } from 'svelte' | ||
import CLoading from './CLoading.svelte' | ||
/** | ||
* The image src | ||
* @type {string} | ||
*/ | ||
export let src | ||
/** | ||
* The default image src | ||
* @type | ||
*/ | ||
export let placeholderSrc = | ||
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxZW0iIGhlaWdodD0iMWVtIiB2aWV3Qm94PSIwIDAgMTYgMTYiPjxnIGZpbGw9ImN1cnJlbnRDb2xvciI+PHBhdGggZD0iTTYuMDAyIDUuNWExLjUgMS41IDAgMSAxLTMgMGExLjUgMS41IDAgMCAxIDMgMHoiLz48cGF0aCBkPSJNMi4wMDIgMWEyIDIgMCAwIDAtMiAydjEwYTIgMiAwIDAgMCAyIDJoMTJhMiAyIDAgMCAwIDItMlYzYTIgMiAwIDAgMC0yLTJoLTEyem0xMiAxYTEgMSAwIDAgMSAxIDF2Ni41bC0zLjc3Ny0xLjk0N2EuNS41IDAgMCAwLS41NzcuMDkzbC0zLjcxIDMuNzFsLTIuNjYtMS43NzJhLjUuNSAwIDAgMC0uNjMuMDYyTDEuMDAyIDEyVjNhMSAxIDAgMCAxIDEtMWgxMnoiLz48L2c+PC9zdmc+' | ||
/** | ||
* The image width | ||
* @type {string} | ||
*/ | ||
export let width = '100%' | ||
/** | ||
* The image height | ||
* @type {string} | ||
*/ | ||
export let height = '300px' | ||
/** | ||
* The alt prop | ||
* @type {string} | ||
*/ | ||
export let alt = '' | ||
/** | ||
* The class that apply to img tag | ||
* @type {string} | ||
*/ | ||
export let imgClass = '' | ||
/** | ||
* Custom image request headers | ||
* @type {object} | ||
*/ | ||
export let customHeaders = {} | ||
/** | ||
* The last successfully loaded image src | ||
*/ | ||
let lastSrc = placeholderSrc | ||
let mounted = false | ||
const fetchImage = () => { | ||
if (!mounted) return | ||
return fetch(src, { | ||
method: 'GET', | ||
headers: customHeaders, | ||
}) | ||
.then(r => r.blob()) | ||
.then(blob => { | ||
lastSrc = URL.createObjectURL(blob) | ||
return lastSrc | ||
}) | ||
} | ||
let imagePromise | ||
onMount(() => { | ||
mounted = true | ||
imagePromise = fetchImage() | ||
}) | ||
$: { | ||
src | ||
imagePromise = fetchImage() | ||
} | ||
</script> | ||
|
||
<div | ||
class="c-image" | ||
style:width | ||
style:height | ||
style:background-image="url({lastSrc})" | ||
> | ||
{#if imagePromise} | ||
{#await imagePromise} | ||
<div class="c-image--loading"> | ||
<slot name="loading"> | ||
<CLoading /> | ||
</slot> | ||
</div> | ||
{:then src} | ||
<img {src} {alt} {width} class="{imgClass}" {...$$restProps} /> | ||
{:catch err} | ||
<slot name="err" {err}> | ||
<div> | ||
{err} | ||
</div> | ||
</slot> | ||
{/await} | ||
{/if} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters