Skip to content
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

Improved Behaviour of use-toast.ts #1038

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 9 additions & 3 deletions apps/www/registry/default/ui/use-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ const listeners: Array<(state: State) => void> = []
let memoryState: State = { toasts: [] }

function dispatch(action: Action) {
if (action.type === "ADD_TOAST") {
const toastExists = memoryState.toasts.some((t) => t.id === action.toast.id)
if (toastExists) {
return
}
}
memoryState = reducer(memoryState, action)
listeners.forEach((listener) => {
listener(memoryState)
Expand All @@ -140,10 +146,10 @@ function dispatch(action: Action) {

type Toast = Omit<ToasterToast, "id">

function toast({ ...props }: Toast) {
const id = genId()
function toast({ ...props }: Toast & { id?: string }) {
const id = props?.id || genId()

const update = (props: ToasterToast) =>
const update = (props: Toast) =>
dispatch({
type: "UPDATE_TOAST",
toast: { ...props, id },
Expand Down
12 changes: 9 additions & 3 deletions apps/www/registry/new-york/ui/use-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ const listeners: Array<(state: State) => void> = []
let memoryState: State = { toasts: [] }

function dispatch(action: Action) {
if (action.type === "ADD_TOAST") {
const toastExists = memoryState.toasts.some((t) => t.id === action.toast.id)
if (toastExists) {
return
}
}
memoryState = reducer(memoryState, action)
listeners.forEach((listener) => {
listener(memoryState)
Expand All @@ -140,10 +146,10 @@ function dispatch(action: Action) {

type Toast = Omit<ToasterToast, "id">

function toast({ ...props }: Toast) {
const id = genId()
function toast({ ...props }: Toast & { id?: string }) {
const id = props?.id || genId()

const update = (props: ToasterToast) =>
const update = (props: Toast) =>
dispatch({
type: "UPDATE_TOAST",
toast: { ...props, id },
Expand Down