Skip to content

Instantly share code, notes, and snippets.

@shinokada
Created November 6, 2024 06:05
Show Gist options
  • Save shinokada/7b93927a6bd21d58ba0531e00be261e0 to your computer and use it in GitHub Desktop.
Save shinokada/7b93927a6bd21d58ba0531e00be261e0 to your computer and use it in GitHub Desktop.
Working example with draw
<script lang="ts">
import { draw } from 'svelte/transition';
import { getContext } from 'svelte';
import type { Props } from './types.ts'
let {
size = 24,
role = 'img',
color = 'currentColor',
strokeWidth = 1.5,
title,
desc,
ariaLabel = "archive box" ,
...restProps
}: Props = $props();
// Get the context as a function
const getTransitionParams = getContext<() => {
params: any;
shouldAnimate: boolean;
visible: boolean;
}>('transition-params');
const { params, shouldAnimate, visible } = $derived(getTransitionParams());
let ariaDescribedby = `${title?.id || ''} ${desc?.id || ''}`;
const hasDescription = $derived(!!(title?.id || desc?.id));
// Set CSS variable for the placeholder size
$effect(() => {
document.documentElement.style.setProperty('--size', `${size}px`);
});
</script>
{#if visible}
<svg
xmlns="http://www.w3.org/2000/svg"
{...restProps}
{role}
width={size}
height={size}
fill="none"
aria-label={ariaLabel}
aria-describedby={hasDescription ? ariaDescribedby : undefined}
viewBox="0 0 24 24"
stroke-width={strokeWidth}
>
{#if title?.id && title.title}
<title id="{title.id}">{title.title}</title>
{/if}
{#if desc?.id && desc.desc}
<desc id="{desc.id}">{desc.desc}</desc>
{/if}
<path d="M20.25 7.5L19.6246 18.1321C19.5546 19.3214 18.5698 20.25 17.3785 20.25H6.62154C5.43022 20.25 4.44538 19.3214 4.37542 18.1321L3.75 7.5M9.99976 11.25H13.9998M3.375 7.5H20.625C21.2463 7.5 21.75 6.99632 21.75 6.375V4.875C21.75 4.25368 21.2463 3.75 20.625 3.75H3.375C2.75368 3.75 2.25 4.25368 2.25 4.875V6.375C2.25 6.99632 2.75368 7.5 3.375 7.5Z" stroke={color} stroke-width={strokeWidth}
transition:draw={shouldAnimate ? params : undefined}
stroke-linecap="round" stroke-linejoin="round"/>
</svg>
{/if}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment