Skip to content

Commit

Permalink
[AlertCenter]: adds dismiss button ui to AlertCenter alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanBreck committed Feb 15, 2024
1 parent 72044c0 commit 39fd582
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/plain-html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@

showAlert({
content: `progress is ${progress}`,
canDismiss: true,
actions: [{
text: 'dismiss',
action: a => a.dismiss()
Expand Down
32 changes: 22 additions & 10 deletions src/components/alert/alert.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
let content = 'Hello World'
let title = 'Title'
let canDismiss = true
let hasAction = false
let duration = 2000
$: alertUser = (mode, type) =>
Expand All @@ -20,17 +21,16 @@
title,
mode: mode ?? 'simple',
type: type ?? 'error',
actions: canDismiss
? [
{
text: 'dismiss',
icon: 'check-normal',
action: (a) => a.dismiss()
}
]
: []
actions: hasAction ? [
{
text: 'Retry',
kind: 'filled',
action: () => {}
}
] : []
},
duration
duration,
canDismiss
)
</script>

Expand All @@ -50,6 +50,14 @@
Some content
</Alert>
</Slot>
<Slot name="content-after" explanation="optional content after the main content of the alert">
<Alert {...args} mode="simple">
Some content
<Button kind='plain-faint' fab slot="content-after">
<Icon name="close" />
</Button>
</Alert>
</Slot>
<Slot
name="title"
explanation="the icon on the left hand side of the Alert.
Expand Down Expand Up @@ -144,6 +152,10 @@
Dismissable
<input type="checkbox" bind:checked={canDismiss} />
</label>
<label>
Has action
<input type="checkbox" bind:checked={hasAction} />
</label>
<Button
on:click={() => {
alertUser(args.mode, args.type)
Expand Down
16 changes: 15 additions & 1 deletion src/components/alert/alert.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
export let mode: AlertMode = 'simple'
export let isToast = false
export let hasActions = $$slots.actions
export let hasContentAfter = $$slots['content-after']
$: currentType = type ?? 'error'
$: currentMode = mode ?? 'simple'
Expand Down Expand Up @@ -47,6 +48,11 @@
{/if}
<slot />
</div>
{#if hasContentAfter && $$slots['content-after']}
<div class="content-after">
<slot name="content-after" />
</div>
{/if}
{#if hasActions && $$slots.actions}
<div class="actions">
<slot name="actions" />
Expand Down Expand Up @@ -78,7 +84,11 @@
font: var(--leo-font-default-regular);
display: grid;
grid-template-columns: min-content auto;
grid-template-columns: min-content 1fr;
&:has(.content-after) {
grid-template-columns: min-content 1fr auto;
}
& .icon {
--leo-icon-size: var(--leo-icon-m);
Expand All @@ -93,6 +103,10 @@
grid-column: 2;
}
& .content-after {
grid-column: 3;
}
& .actions {
grid-column: 2;
}
Expand Down
17 changes: 13 additions & 4 deletions src/components/alert/alertCenter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
actions: Action[] = []
duration?: number
canDismiss?: boolean
#timeout: NodeJS.Timeout
constructor(options: AlertOptions, duration?: number) {
constructor(options: AlertOptions, duration?: number, canDismiss?: boolean) {
Object.assign(this, options)
this.actions = this.actions ?? []
this.duration = duration
this.canDismiss = canDismiss
this.resumeDismiss()
}
Expand All @@ -62,8 +64,8 @@
const alerts = writable<AlertInfo[]>([])
export const showAlert = (options: AlertOptions, duration = 2000) => {
alerts.update((a) => [...a, new AlertInfo(options, duration)])
export const showAlert = (options: AlertOptions, duration = 2000, canDismiss = true) => {
alerts.update((a) => [...a, new AlertInfo(options, duration, canDismiss)])
}
const transitionOptions = { y: -64, duration: 120 }
Expand Down Expand Up @@ -93,10 +95,17 @@
on:mouseenter={() => alert.pauseDismiss()}
on:mouseleave={() => alert.resumeDismiss()}
>
<svelte:component this={alert.component || Alert} {...alert} hasActions={alert.actions.length > 0} isToast>
<svelte:component this={alert.component || Alert} {...alert} hasActions={alert.actions.length > 0} hasContentAfter={alert.canDismiss} isToast>
<div slot="title">
{alert.title}
</div>
<svelte:fragment slot='content-after'>
{#if alert.canDismiss}
<Button kind="plain-faint" fab on:click={() => alert.dismiss()}>
<Icon name="close" />
</Button>
{/if}
</svelte:fragment>
{alert.content}
<div slot="actions">
{#each alert.actions as action}
Expand Down

0 comments on commit 39fd582

Please sign in to comment.