-
-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic Toast component #487
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte-ux': minor | ||
--- | ||
|
||
Add toast components |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<script lang="ts"> | ||
import toastStore from '$lib/stores/toastStore.js'; | ||
import Notification from './Notification.svelte'; | ||
</script> | ||
|
||
<div class="fixed bottom-0 left-1/2 translate-x-[-50%] z-50 gap-y-4"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would also be good if To support this, you would need to add a <script>
...
export let classes: {
root?: string;
actions?: string;
// ...any other internal element/component you might want to pass a class to
} = {};
const settingsClasses = getComponentClasses('Toast');
</script> and change... <div class="fixed bottom-0 left-1/2 translate-x-[-50%] z-50 gap-y-4"> to <div
class={cls(
'Toast',
'fixed bottom-0 left-1/2 translate-x-[-50%] z-50 gap-y-4',
settingsClasses.root,
classes.root,
$$props.class
)}
> |
||
{#each $toastStore as toast} | ||
<div class="mt-4"> | ||
<Notification open={true} closeIcon> | ||
<span slot="title">{toast.text}</span> | ||
</Notification> | ||
</div> | ||
{/each} | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { get, writable } from 'svelte/store'; | ||
|
||
const DEFAULT_TOAST_TIME_IN_MS = 3000; | ||
|
||
export type Toast = { | ||
text: string; | ||
timeAdded: Date; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to feature creep this too much, but also consider a little bit more future state, I'm thinking the the type could be: export type Toast = {
title: string;
description?: string;
actions?: Record<string, Function>; // this will need more thought to support primary styling, etc
icon?: IconInput;
variant?: string;
timeAdded: Date;
};
|
||
|
||
/** | ||
* Create a global store to save information on toast components | ||
*/ | ||
const toastStore = writable<Toast[]>([]); | ||
|
||
export function addToast(text: string, durationInMS = DEFAULT_TOAST_TIME_IN_MS) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would want to change |
||
const timeAdded = new Date(); | ||
const toast: Toast = { | ||
text, | ||
timeAdded, | ||
}; | ||
toastStore.set([...get(toastStore), toast]); | ||
|
||
setTimeout(() => { | ||
const remainingToasts = [...get(toastStore)].filter((toast) => { | ||
return toast.timeAdded != timeAdded; | ||
}); | ||
toastStore.set(remainingToasts); | ||
}, durationInMS); | ||
} | ||
|
||
export default toastStore; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<script lang="ts"> | ||
import { Button } from 'svelte-ux'; | ||
import { Toast } from 'svelte-ux'; | ||
import { addToast } from '$lib/stores/toastStore.js'; | ||
|
||
function addSimpleToast() { | ||
addToast('This is a simple toast!'); | ||
} | ||
|
||
function addLongerToast() { | ||
addToast('This is a longer toast!', 10000); | ||
} | ||
</script> | ||
|
||
<h1>Examples</h1> | ||
|
||
<Toast /> | ||
<h2>Add Simple Toast</h2> | ||
<Button variant="fill-outline" color="primary" on:click={addSimpleToast}>Click Me</Button> | ||
|
||
<h2>Add Longer Toast</h2> | ||
<Button variant="fill-outline" color="primary" on:click={addLongerToast}>Click Me</Button> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import api from '$lib/components/Toast.svelte?raw&sveld'; | ||
import source from '$lib/components/Toast.svelte?raw'; | ||
import pageSource from './+page.svelte?raw'; | ||
|
||
export async function load() { | ||
return { | ||
meta: { | ||
api, | ||
source, | ||
pageSource, | ||
description: | ||
'Adds the ability to include Toast popup notifications that go away after a couple of seconds.', | ||
related: ['components/Notification', '/customization'], | ||
}, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do not select any packages when prompted for
major
andminor
, it will produce apatch
changeset (I think the flow is a little odd to be honest, but that's driven by the changesets project).The flow is different when using
changesets
in a multi-package monorepo (like Svelte UX)...vs a single-package repo (like LayerChart at the moment)...
The single package is much more straight forward.
At some point LayerChart might become multi-package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With that said you can manually edit this
.md
file and just changeminor
topatch
(all the CLI does is help you write this file, including generating a filename)