Skip to content

Commit

Permalink
v2 Avatars - restProps and fallback slot support (#2626)
Browse files Browse the repository at this point in the history
  • Loading branch information
endigo9740 authored Apr 22, 2024
1 parent 8d4a2e9 commit 7a9827e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-cherries-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"skeleton.dev": minor
---

Feat: Avatar component updated to support restProps and fallback slot.
11 changes: 6 additions & 5 deletions packages/skeleton/src/lib/components/Avatar/Avatar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Props (initials)
/** Initials only - Provide up to two text characters. */
export let initials = 'AB';
export let initials = '';
/** Initials only - Provide classes to set the SVG text fill color. */
export let fill: CssClasses = 'fill-token';
/** Initials only - Set the base font size for the scalable SVG text. */
Expand Down Expand Up @@ -53,18 +53,17 @@

<!-- FIXME: resolve a11y warnings -->
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
<figure class="avatar {classesBase}" data-testid="avatar" on:click on:keydown on:keyup on:keypress>
{#if src}
<figure class="avatar {classesBase}" data-testid="avatar" {...prunedRestProps()} on:click on:keydown on:keyup on:keypress>
{#if src || fallback}
<img
class="avatar-image {cImage}"
style={$$props.style ?? ''}
{src}
alt={$$props.alt || ''}
use:action={actionParams}
on:error={() => (src = fallback)}
{...prunedRestProps()}
/>
{:else}
{:else if initials}
<svg class="avatar-initials w-full h-full" viewBox="0 0 512 512">
<text
x="50%"
Expand All @@ -78,5 +77,7 @@
{String(initials).substring(0, 2).toUpperCase()}
</text>
</svg>
{:else}
<slot />
{/if}
</figure>
5 changes: 3 additions & 2 deletions packages/skeleton/src/lib/components/Avatar/Avatar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe('Avatar.svelte', () => {
});

it('Initials shown when no image source provided', async () => {
const { getByTestId } = render(Avatar);
expect(getByTestId('avatar').querySelector('.avatar-initials')?.textContent).eq('AB');
const testInitials = 'SK';
const { getByTestId } = render(Avatar, { props: { initials: testInitials } });
expect(getByTestId('avatar').querySelector('.avatar-initials')?.textContent).eq(testInitials);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
8: 'rounded-full'
};
let rangeSliderValue: keyof typeof roundedMapping = 8;
let fallback = '';
let fallback = imgPlaceholder;
// Reactive
$: actionParams = '#Apollo';
Expand Down Expand Up @@ -85,30 +85,11 @@
</svelte:fragment>
</DocsPreview>
</section>
<section class="space-y-4">
<h2 class="h2">Interactive Border</h2>
<p>Apply the following styles using the <code class="code">border</code> and <code class="code">cursor</code> properties.</p>
<DocsPreview background="neutral">
<svelte:fragment slot="preview">
<Avatar border={borderStyles} cursor="cursor-pointer" />
</svelte:fragment>
<svelte:fragment slot="source">
<CodeBlock
language="html"
code={`
<Avatar
border="${borderStyles}"
cursor="cursor-pointer"
/>
`}
/>
</svelte:fragment>
</DocsPreview>
</section>
<section class="space-y-4">
<h2 class="h2">Handling Fallbacks</h2>
<!-- DEPRECATED: use fallback slot instead -->
<!-- <section class="space-y-4">
<h2 class="h2">Fallback Image</h2>
<p>
Use the <code class="code">fallback</code> property to specify a fallback when images fail to load, or supply the user's initials.
Use the <code class="code">fallback</code> property to specify a fallback when images fail to load.
</p>
<DocsPreview background="neutral" regionFooter="text-center">
<svelte:fragment slot="preview">
Expand All @@ -129,6 +110,42 @@
</select>
</svelte:fragment>
</DocsPreview>
</section> -->
<section class="space-y-4">
<h2 class="h2">Fallback</h2>
<p>Use the default slot to provide fallback images, icons, or text.</p>
<DocsPreview background="neutral" regionFooter="text-center">
<svelte:fragment slot="preview">
{#key fallback}
<Avatar background="bg-secondary-500">
<i class="fa-solid fa-skull text-xl"></i>
</Avatar>
{/key}
</svelte:fragment>
<svelte:fragment slot="source">
<CodeBlock language="html" code={`<Avatar background="bg-secondary-500">(fallback)</Avatar>`} />
</svelte:fragment>
</DocsPreview>
</section>
<section class="space-y-4">
<h2 class="h2">Interactive Border</h2>
<p>Apply the following styles using the <code class="code">border</code> and <code class="code">cursor</code> properties.</p>
<DocsPreview background="neutral">
<svelte:fragment slot="preview">
<Avatar initials="SK" border={borderStyles} cursor="cursor-pointer"></Avatar>
</svelte:fragment>
<svelte:fragment slot="source">
<CodeBlock
language="html"
code={`
<Avatar
border="${borderStyles}"
cursor="cursor-pointer"
/>
`}
/>
</svelte:fragment>
</DocsPreview>
</section>
<section class="space-y-4">
<h2 class="h2">Applying Filters</h2>
Expand Down

0 comments on commit 7a9827e

Please sign in to comment.