Skip to content

Commit

Permalink
🏎 simple animations for toasts
Browse files Browse the repository at this point in the history
  • Loading branch information
olgam4 committed Jul 28, 2022
1 parent 91ea9e9 commit cadf559
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"type": "module",
"license": "MIT",
"devDependencies": {
"@formkit/auto-animate": "1.0.0-beta.1",
"@iconify-json/carbon": "1.1.7",
"@mdx-js/rollup": "2.1.2",
"@rollup/plugin-replace": "4.0.0",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 24 additions & 10 deletions src/components/toaster/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Component } from 'solid-js'
import type { Component, Ref } from 'solid-js'
import autoAnimate from '@formkit/auto-animate'

type Toast = {
id: string
Expand All @@ -10,28 +11,41 @@ const createToaster = () => {

const toast = (message: string) => {
const id = Math.random().toString()
setTimeout(() => {
setToasts(toasts => toasts.filter(t => t.id !== id))
}, 1000)
setToasts(toasts => [...toasts, { id, message }])
}

const removeToast = (id: string) => {
setToasts(toasts => toasts.filter(t => t.id !== id))
}

return {
toasts,
removeToast,
toast,
}
}

interface Props extends ReturnType<typeof createToaster> {}
interface Props extends ReturnType<typeof createToaster> { }

const Toaster: Component<Props> = (props) => {
let toasterRef: Ref<any>

createEffect(() => {
toasterRef && autoAnimate(toasterRef)
})

return (
<div class="absolute p-5 top-0 right-0">
<For each={props.toasts()} children={({ message }) => (
<div class="bg-white p-2 mb-5 rounded">
{message}
<div ref={toasterRef} class="absolute p-5 top-0 right-0">
<For each={props.toasts()} children={({ message, id }) => (
<div class="flex space-x-2 bg-white p-2 mb-5 rounded">
<button class="text-red-600 font-black" onClick={() => props.removeToast(id)}>
X
</button>
<p>
{message}
</p>
</div>
)}/>
)} />
</div>
)
}
Expand Down

1 comment on commit cadf559

@vercel
Copy link

@vercel vercel bot commented on cadf559 Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

bat – ./

bat-brown.vercel.app
bat-git-main-olgam4.vercel.app
bat.glo.quebec
bat-olgam4.vercel.app

Please sign in to comment.