Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@
"react-instantsearch": "^7.29.0",
"remove-markdown": "^0.6.3",
"resend": "^6.10.0",
"sonner": "^2.0.7",
"tailwind-merge": "^3.5.0",
"takumi-js": "^2.5.4",
"tar-stream": "^3.1.8",
Expand Down
14 changes: 0 additions & 14 deletions pnpm-lock.yaml

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

89 changes: 89 additions & 0 deletions src/components/ToastProvider.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.viewport {
--toast-gap: 8px;
position: fixed;
right: 1rem;
bottom: 1rem;
z-index: 10000;
width: 24rem;
height: var(--toast-frontmost-height, 0px);
pointer-events: none;
outline: none;
}

.toast {
--toast-y: calc(-1 * var(--toast-index) * var(--toast-gap));
--toast-scale: calc(1 - 0.05 * var(--toast-index));
position: absolute;
bottom: 0;
z-index: calc(3 - var(--toast-index));
width: 100%;
height: var(--toast-frontmost-height, auto);
overflow-wrap: anywhere;
pointer-events: auto;
touch-action: pan-y;
transform: translateY(var(--toast-y)) scale(var(--toast-scale));
transform-origin: bottom center;
transition:
transform 250ms,
opacity 250ms,
height 250ms;
}

.toast[data-expanded] {
--toast-y: calc(
-1 * (var(--toast-offset-y) + var(--toast-index) * var(--toast-gap))
);
--toast-scale: 1;
height: var(--toast-height, auto);
}

/* Keep hover active as the pointer crosses the gap between expanded toasts. */
.toast[data-expanded]::after {
position: absolute;
right: 0;
bottom: calc(-1 * var(--toast-gap) - 1px);
left: 0;
height: calc(var(--toast-gap) + 1px);
content: '';
}

.toast[data-swiping] {
transition: none;
}

.toast[data-starting-style],
.toast[data-ending-style] {
opacity: 0;
transform: translateY(calc(var(--toast-y) + 100%)) scale(var(--toast-scale));
}

.toast[data-ending-style][data-swipe-direction='right'] {
transform: translate(
calc(var(--toast-swipe-movement-x) + 100%),
var(--toast-y)
);
}

.toast[data-limited] {
visibility: hidden;
}

.toast[data-ending-style] {
pointer-events: none;
}

.content[data-behind]:not([data-expanded]) {
opacity: 0;
}

@media (max-width: 600px) {
.viewport {
width: calc(100% - 2rem);
}
}

@media (prefers-reduced-motion: reduce) {
.toast {
transition: none;
}
}
75 changes: 40 additions & 35 deletions src/components/ToastProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Toast } from '@base-ui/react/toast'
import * as React from 'react'
import { Toaster, toast } from 'sonner'
import { useTheme } from '~/components/ThemeProvider'
import styles from './ToastProvider.module.css'

type ToastOptions = {
durationMs?: number
Expand All @@ -11,6 +11,10 @@ type ToastContextValue = {
notify: (content: React.ReactNode, options?: ToastOptions) => string
}

type ToastProviderProps = {
children: React.ReactNode
}

const ToastContext = React.createContext<ToastContextValue | null>(null)

export function useToast(): ToastContextValue {
Expand All @@ -21,47 +25,48 @@ export function useToast(): ToastContextValue {
return ctx
}

export function ToastProvider({
children,
}: {
children: React.ReactNode
}): React.ReactElement {
const { resolvedTheme } = useTheme()
export function ToastProvider({ children }: ToastProviderProps) {
return (
<Toast.Provider timeout={2500}>
<ToastNotifications>{children}</ToastNotifications>
</Toast.Provider>
)
}

function ToastNotifications({ children }: ToastProviderProps) {
const { add, toasts } = Toast.useToastManager()

const notify = React.useCallback(
(content: React.ReactNode, options?: ToastOptions) => {
const id = toast(content, {
duration: options?.durationMs ?? 2500,
id: options?.id,
})
return String(id)
},
[],
(content: React.ReactNode, { durationMs, id }: ToastOptions = {}) =>
add({
title: content,
timeout: durationMs === Infinity ? 0 : durationMs,
id: id?.toString(),
}),
[add],
)

const contextValue = React.useMemo<ToastContextValue>(
() => ({ notify }),
[notify],
)
const contextValue = React.useMemo(() => ({ notify }), [notify])

return (
<ToastContext.Provider value={contextValue}>
{children}
<Toaster
theme={resolvedTheme}
position="bottom-right"
swipeDirections={['right']}
offset={16}
gap={8}
toastOptions={{
unstyled: true,
classNames: {
toast:
'relative w-96 rounded-md border border-gray-200 bg-white px-3 py-2 text-sm text-gray-900 shadow-lg outline-none dark:border-gray-700 dark:bg-gray-800 dark:text-gray-50',
content: 'flex items-start gap-2',
},
}}
/>
<Toast.Portal>
<Toast.Viewport className={styles.viewport}>
{toasts.map((toast) => (
<Toast.Root
key={toast.id}
toast={toast}
swipeDirection="right"
className={`${styles.toast} rounded-md border border-gray-200 bg-white px-3 py-2 text-sm text-gray-900 shadow-lg focus-visible:outline-2 focus-visible:outline-offset-2 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-50`}
>
<Toast.Content className={styles.content}>
<Toast.Title className="flex items-start gap-2" />
</Toast.Content>
</Toast.Root>
))}
</Toast.Viewport>
</Toast.Portal>
</ToastContext.Provider>
)
}
Loading