-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: snack to show feedback messages (#49)
- Loading branch information
Showing
5 changed files
with
110 additions
and
2 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,6 +1,12 @@ | ||
import { RouterProvider } from 'react-router-dom' | ||
import { Snacks } from './components/snacks' | ||
import routes from './routes' | ||
|
||
export function App() { | ||
return <RouterProvider router={routes} /> | ||
return ( | ||
<> | ||
<RouterProvider router={routes} /> | ||
<Snacks /> | ||
</> | ||
) | ||
} |
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,51 @@ | ||
import { removeSnack, snacks } from '@/lib/snacks' | ||
import clsx from 'clsx' | ||
|
||
const ICONS = { | ||
error: 'emergency_home', | ||
neutral: 'stat_2', | ||
success: 'verified', | ||
} | ||
|
||
function Snacks() { | ||
return ( | ||
<div class="absolute bottom-0 right-0 m-4 w-[20rem]"> | ||
<ul> | ||
{snacks.value.map((snack) => { | ||
if (snack.type === 'custom') return snack.content | ||
|
||
return ( | ||
<li key={snack.id} className="[&+&]:mt-2"> | ||
<div | ||
class={clsx( | ||
'bg-red-500 text-white px-2 py-1 rounded-lg flex justify-between gap-2', | ||
{ | ||
'!bg-green-600': snack.type === 'success', | ||
'bg-zinc-50 dark:bg-neutral-700 border dark:border-neutral-600 text-inherit': | ||
snack.type === 'neutral', | ||
} | ||
)} | ||
> | ||
<div className="flex gap-2 items-start"> | ||
<span className="material-symbols-rounded text-lg opacity-75"> | ||
{ICONS[snack.type]} | ||
</span> | ||
<div>{snack.content}</div> | ||
</div> | ||
<div> | ||
<button onClick={() => removeSnack(snack.id)}> | ||
<span className="material-symbols-rounded text-sm opacity-75"> | ||
close | ||
</span> | ||
</button> | ||
</div> | ||
</div> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
</div> | ||
) | ||
} | ||
|
||
export { Snacks } |
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,29 @@ | ||
import React from 'preact/compat' | ||
import randomStr from './random-str' | ||
import { signal } from '@preact/signals' | ||
|
||
interface Snack { | ||
id: string | ||
type: 'success' | 'error' | 'neutral' | 'custom' | ||
content: string | React.ReactNode | ||
duration: number | ||
} | ||
|
||
const snacks = signal<Snack[]>([]) | ||
|
||
function addSnack(snack: Omit<Snack, 'id'>) { | ||
const id = randomStr(6) | ||
snacks.value = [...snacks.value, { ...snack, id }] | ||
|
||
setTimeout(() => { | ||
removeSnack(id) | ||
}, snack.duration) | ||
|
||
return id | ||
} | ||
|
||
function removeSnack(id: string) { | ||
snacks.value = snacks.value.filter((snack) => snack.id !== id) | ||
} | ||
|
||
export { addSnack, removeSnack, snacks } |
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