Skip to content

Commit 7a9827e

Browse files
authored
v2 Avatars - restProps and fallback slot support (#2626)
1 parent 8d4a2e9 commit 7a9827e

File tree

4 files changed

+55
-31
lines changed

4 files changed

+55
-31
lines changed

.changeset/four-cherries-arrive.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"skeleton.dev": minor
3+
---
4+
5+
Feat: Avatar component updated to support restProps and fallback slot.

packages/skeleton/src/lib/components/Avatar/Avatar.svelte

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
// Props (initials)
77
/** Initials only - Provide up to two text characters. */
8-
export let initials = 'AB';
8+
export let initials = '';
99
/** Initials only - Provide classes to set the SVG text fill color. */
1010
export let fill: CssClasses = 'fill-token';
1111
/** Initials only - Set the base font size for the scalable SVG text. */
@@ -53,18 +53,17 @@
5353

5454
<!-- FIXME: resolve a11y warnings -->
5555
<!-- svelte-ignore a11y-no-noninteractive-element-interactions -->
56-
<figure class="avatar {classesBase}" data-testid="avatar" on:click on:keydown on:keyup on:keypress>
57-
{#if src}
56+
<figure class="avatar {classesBase}" data-testid="avatar" {...prunedRestProps()} on:click on:keydown on:keyup on:keypress>
57+
{#if src || fallback}
5858
<img
5959
class="avatar-image {cImage}"
6060
style={$$props.style ?? ''}
6161
{src}
6262
alt={$$props.alt || ''}
6363
use:action={actionParams}
6464
on:error={() => (src = fallback)}
65-
{...prunedRestProps()}
6665
/>
67-
{:else}
66+
{:else if initials}
6867
<svg class="avatar-initials w-full h-full" viewBox="0 0 512 512">
6968
<text
7069
x="50%"
@@ -78,5 +77,7 @@
7877
{String(initials).substring(0, 2).toUpperCase()}
7978
</text>
8079
</svg>
80+
{:else}
81+
<slot />
8182
{/if}
8283
</figure>

packages/skeleton/src/lib/components/Avatar/Avatar.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ describe('Avatar.svelte', () => {
3636
});
3737

3838
it('Initials shown when no image source provided', async () => {
39-
const { getByTestId } = render(Avatar);
40-
expect(getByTestId('avatar').querySelector('.avatar-initials')?.textContent).eq('AB');
39+
const testInitials = 'SK';
40+
const { getByTestId } = render(Avatar, { props: { initials: testInitials } });
41+
expect(getByTestId('avatar').querySelector('.avatar-initials')?.textContent).eq(testInitials);
4142
});
4243
});

sites/skeleton.dev/src/routes/(inner)/components/avatars/+page.svelte

+41-24
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
8: 'rounded-full'
3838
};
3939
let rangeSliderValue: keyof typeof roundedMapping = 8;
40-
let fallback = '';
40+
let fallback = imgPlaceholder;
4141
4242
// Reactive
4343
$: actionParams = '#Apollo';
@@ -85,30 +85,11 @@
8585
</svelte:fragment>
8686
</DocsPreview>
8787
</section>
88-
<section class="space-y-4">
89-
<h2 class="h2">Interactive Border</h2>
90-
<p>Apply the following styles using the <code class="code">border</code> and <code class="code">cursor</code> properties.</p>
91-
<DocsPreview background="neutral">
92-
<svelte:fragment slot="preview">
93-
<Avatar border={borderStyles} cursor="cursor-pointer" />
94-
</svelte:fragment>
95-
<svelte:fragment slot="source">
96-
<CodeBlock
97-
language="html"
98-
code={`
99-
<Avatar
100-
border="${borderStyles}"
101-
cursor="cursor-pointer"
102-
/>
103-
`}
104-
/>
105-
</svelte:fragment>
106-
</DocsPreview>
107-
</section>
108-
<section class="space-y-4">
109-
<h2 class="h2">Handling Fallbacks</h2>
88+
<!-- DEPRECATED: use fallback slot instead -->
89+
<!-- <section class="space-y-4">
90+
<h2 class="h2">Fallback Image</h2>
11091
<p>
111-
Use the <code class="code">fallback</code> property to specify a fallback when images fail to load, or supply the user's initials.
92+
Use the <code class="code">fallback</code> property to specify a fallback when images fail to load.
11293
</p>
11394
<DocsPreview background="neutral" regionFooter="text-center">
11495
<svelte:fragment slot="preview">
@@ -129,6 +110,42 @@
129110
</select>
130111
</svelte:fragment>
131112
</DocsPreview>
113+
</section> -->
114+
<section class="space-y-4">
115+
<h2 class="h2">Fallback</h2>
116+
<p>Use the default slot to provide fallback images, icons, or text.</p>
117+
<DocsPreview background="neutral" regionFooter="text-center">
118+
<svelte:fragment slot="preview">
119+
{#key fallback}
120+
<Avatar background="bg-secondary-500">
121+
<i class="fa-solid fa-skull text-xl"></i>
122+
</Avatar>
123+
{/key}
124+
</svelte:fragment>
125+
<svelte:fragment slot="source">
126+
<CodeBlock language="html" code={`<Avatar background="bg-secondary-500">(fallback)</Avatar>`} />
127+
</svelte:fragment>
128+
</DocsPreview>
129+
</section>
130+
<section class="space-y-4">
131+
<h2 class="h2">Interactive Border</h2>
132+
<p>Apply the following styles using the <code class="code">border</code> and <code class="code">cursor</code> properties.</p>
133+
<DocsPreview background="neutral">
134+
<svelte:fragment slot="preview">
135+
<Avatar initials="SK" border={borderStyles} cursor="cursor-pointer"></Avatar>
136+
</svelte:fragment>
137+
<svelte:fragment slot="source">
138+
<CodeBlock
139+
language="html"
140+
code={`
141+
<Avatar
142+
border="${borderStyles}"
143+
cursor="cursor-pointer"
144+
/>
145+
`}
146+
/>
147+
</svelte:fragment>
148+
</DocsPreview>
132149
</section>
133150
<section class="space-y-4">
134151
<h2 class="h2">Applying Filters</h2>

0 commit comments

Comments
 (0)