-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(List): forward all events including child components
fix(List.Item.Leading.Avatar): added graceful fail
- Loading branch information
Showing
10 changed files
with
280 additions
and
12 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 |
---|---|---|
@@ -1,9 +1,123 @@ | ||
<script lang="ts" context="module"> | ||
export const LIST_ITEM_LEADING_AVATAR_CONTEXT_ID = 'list-item-leading-avatar-context-id'; | ||
</script> | ||
|
||
<script lang="ts"> | ||
import Avatar from '../avatar'; | ||
import { setContext, onMount } from 'svelte/internal'; | ||
import { twMerge } from 'tailwind-merge'; | ||
import Placeholder from './Placeholder.svelte'; | ||
import { get_current_component } from 'svelte/internal'; | ||
import { forwardEventsBuilder, useActions, type ActionArray } from '../../actions'; | ||
export let use: ActionArray = []; | ||
import { exclude } from '../../utils/exclude'; | ||
const forwardEvents = forwardEventsBuilder(get_current_component()); | ||
export let src: string; | ||
export let alt = 'user-avatar'; | ||
export let src: string | undefined = undefined; | ||
export let alt = 'avatar'; | ||
export let shape: 'circle' | 'rounded' | 'square' = 'circle'; | ||
export let size: 'xs' | 'sm' | 'md' | 'lg' | 'xl' = 'md'; | ||
export let initials: string | undefined = undefined; | ||
let loaded = false; | ||
let failed = false; | ||
let loading = true; | ||
setContext(LIST_ITEM_LEADING_AVATAR_CONTEXT_ID, { | ||
avatar: true, | ||
src, | ||
alt, | ||
shape, | ||
size | ||
}); | ||
let defaultClass = ''; | ||
let containerDefaultClass = ''; | ||
if (src) { | ||
defaultClass = 'inline-block absolute'; | ||
containerDefaultClass = 'inline-block relative align-middle'; | ||
} else if (initials) { | ||
defaultClass = | ||
'inline-flex items-center justify-center align-middle bg-light-icon-background dark:bg-dark-icon-background text-light-content dark:text-dark-content'; | ||
} | ||
if (size === 'xs') { | ||
defaultClass += ' h-6 w-6'; | ||
containerDefaultClass += ' h-6 w-6'; | ||
} else if (size === 'sm') { | ||
defaultClass += ' h-8 w-8'; | ||
containerDefaultClass += ' h-8 w-8'; | ||
} else if (size === 'md') { | ||
defaultClass += ' h-10 w-10'; | ||
containerDefaultClass += ' h-10 w-10'; | ||
} else if (size === 'lg') { | ||
defaultClass += ' h-12 w-12'; | ||
containerDefaultClass += ' h-12 w-12'; | ||
} else if (size === 'xl') { | ||
defaultClass += ' h-16 w-16'; | ||
containerDefaultClass += ' h-16 w-16'; | ||
} | ||
if (shape === 'circle') { | ||
defaultClass += ' rounded-full'; | ||
containerDefaultClass += ' rounded-full'; | ||
} else if (shape === 'rounded') { | ||
defaultClass += ' rounded-md'; | ||
containerDefaultClass += ' rounded-md'; | ||
} | ||
$: finalClass = twMerge(defaultClass, $$props.class); | ||
$: finalContainerClass = twMerge(containerDefaultClass, $$props.class); | ||
onMount(() => { | ||
if (src) { | ||
const image = new Image(); | ||
image.src = src; | ||
loading = true; | ||
image.onload = () => { | ||
loading = false; | ||
loaded = true; | ||
}; | ||
image.onerror = () => { | ||
loading = false; | ||
failed = true; | ||
}; | ||
} | ||
}); | ||
</script> | ||
|
||
<Avatar {src} {alt} {size} /> | ||
{#if src} | ||
<span | ||
class={finalContainerClass} | ||
use:useActions={use} | ||
use:forwardEvents | ||
{...exclude($$props, ['use', 'class'])} | ||
> | ||
{#if loaded} | ||
<img class={finalClass} style={$$props.style} src={src || ''} {alt} /> | ||
{:else if failed} | ||
{#if $$slots.placeholder} | ||
<slot name="placeholder" /> | ||
{:else} | ||
<Placeholder /> | ||
{/if} | ||
{:else if loading} | ||
<Placeholder loading /> | ||
{/if} | ||
|
||
<slot name="indicator" /> | ||
</span> | ||
{:else if initials} | ||
<span | ||
class={finalClass} | ||
use:useActions={use} | ||
use:forwardEvents | ||
{...exclude($$props, ['use', 'class'])} | ||
> | ||
<span | ||
class="font-bold leading-none text-current" | ||
class:text-xs={size === 'xs'} | ||
class:text-sm={size === 'sm'} | ||
class:text-xl={size === 'lg'} | ||
class:text-2xl={size === 'xl'}>{initials}</span | ||
> | ||
</span> | ||
{/if} |
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 |
---|---|---|
@@ -1,10 +1,20 @@ | ||
<script lang="ts"> | ||
import { twMerge } from 'tailwind-merge'; | ||
import { get_current_component } from 'svelte/internal'; | ||
import { forwardEventsBuilder, useActions, type ActionArray } from '../../actions'; | ||
export let use: ActionArray = []; | ||
import { exclude } from '../../utils/exclude'; | ||
const forwardEvents = forwardEventsBuilder(get_current_component()); | ||
const defaultClass = 'text-sm mb-0 text-light-secondary-content dark:text-dark-secondary-content'; | ||
$: finalClass = twMerge(defaultClass, $$props.class); | ||
</script> | ||
|
||
<p class={finalClass} style={$$props.style}> | ||
<p | ||
class={finalClass} | ||
use:useActions={use} | ||
use:forwardEvents | ||
{...exclude($$props, ['use', 'class'])} | ||
> | ||
<slot /> | ||
</p> |
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 |
---|---|---|
@@ -1,11 +1,21 @@ | ||
<script lang="ts"> | ||
import type { MaterialIcon } from '../../types'; | ||
import { twMerge } from 'tailwind-merge'; | ||
import { get_current_component } from 'svelte/internal'; | ||
import { forwardEventsBuilder, useActions, type ActionArray } from '../../actions'; | ||
export let use: ActionArray = []; | ||
import { exclude } from '../../utils/exclude'; | ||
const forwardEvents = forwardEventsBuilder(get_current_component()); | ||
export let icon: MaterialIcon; | ||
const defaultClass = 'material-icons text-2xl text-white bg-primary'; | ||
$: finalClass = twMerge(defaultClass, $$props.class); | ||
</script> | ||
|
||
<span class={finalClass} style={$$props.style}>{icon}</span> | ||
<span | ||
class={finalClass} | ||
use:useActions={use} | ||
use:forwardEvents | ||
{...exclude($$props, ['use', 'class'])}>{icon}</span | ||
> |
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,77 @@ | ||
<script lang="ts"> | ||
import { fade } from 'svelte/transition'; | ||
import { LIST_ITEM_LEADING_AVATAR_CONTEXT_ID } from './Avatar.svelte'; | ||
import { useContext } from '../../utils/useContext'; | ||
import { getContext } from 'svelte/internal'; | ||
import { twMerge } from 'tailwind-merge'; | ||
import Icon from './Icon.svelte'; | ||
import { get_current_component } from 'svelte/internal'; | ||
import { forwardEventsBuilder, useActions, type ActionArray } from '../../actions'; | ||
export let use: ActionArray = []; | ||
import { exclude } from '../../utils/exclude'; | ||
const forwardEvents = forwardEventsBuilder(get_current_component()); | ||
export let loading = false; | ||
useContext({ | ||
context_id: LIST_ITEM_LEADING_AVATAR_CONTEXT_ID, | ||
parent: 'List.Item.Leading.Avatar', | ||
component: 'List.Item.Leading.Avatar.Placeholder' | ||
}); | ||
const { shape }: { shape: 'circle' | 'rounded' | 'square' } = getContext( | ||
LIST_ITEM_LEADING_AVATAR_CONTEXT_ID | ||
); | ||
let defaultClass = | ||
'absolute inset-0 h-full w-full flex items-center justify-center overflow-hidden bg-light-icon-background dark:bg-dark-icon-background'; | ||
if (shape === 'circle') { | ||
defaultClass += ' rounded-full'; | ||
} else if (shape === 'rounded') { | ||
defaultClass += ' rounded-md'; | ||
} | ||
if (loading) { | ||
defaultClass += ' loading'; | ||
} | ||
$: finalClass = twMerge(defaultClass, $$props.class); | ||
</script> | ||
|
||
<div | ||
transition:fade|local | ||
class={finalClass} | ||
use:useActions={use} | ||
use:forwardEvents | ||
{...exclude($$props, ['use', 'class'])} | ||
> | ||
{#if $$slots.icon || $$slots.default} | ||
<slot name="icon" /> | ||
<slot /> | ||
{:else} | ||
<Icon class="text-light-icon dark:text-dark-icon" icon="person" /> | ||
{/if} | ||
</div> | ||
|
||
<style> | ||
.loading::after { | ||
position: absolute; | ||
transform: translateX(-100%); | ||
background: linear-gradient( | ||
90deg, | ||
rgba(190, 190, 190, 0.2) 25%, | ||
rgba(129, 129, 129, 0.24) 37%, | ||
rgba(190, 190, 190, 0.2) 63% | ||
); | ||
inset: 0 -150%; | ||
animation: shimmer 2s infinite; | ||
content: ''; | ||
} | ||
@keyframes shimmer { | ||
0% { | ||
transform: translate(-37.5%); | ||
} | ||
to { | ||
transform: translate(37.5%); | ||
} | ||
} | ||
</style> |
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 |
---|---|---|
@@ -1,10 +1,20 @@ | ||
<script lang="ts"> | ||
import { twMerge } from 'tailwind-merge'; | ||
import { get_current_component } from 'svelte/internal'; | ||
import { forwardEventsBuilder, useActions, type ActionArray } from '../../actions'; | ||
export let use: ActionArray = []; | ||
import { exclude } from '../../utils/exclude'; | ||
const forwardEvents = forwardEventsBuilder(get_current_component()); | ||
const defaultClass = 'text-sm font-semibold text-light-content dark:text-dark-content'; | ||
$: finalClass = twMerge(defaultClass, $$props.class); | ||
</script> | ||
|
||
<h3 class={finalClass} style={$$props.style}> | ||
<h3 | ||
class={finalClass} | ||
use:useActions={use} | ||
use:forwardEvents | ||
{...exclude($$props, ['use', 'class'])} | ||
> | ||
<slot /> | ||
</h3> |