-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: Add Toast to React Aria Components #7783
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
Changes from all commits
a3a3782
7c02eae
226a872
8634c78
d18d362
6beef9c
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,20 @@ | ||
/* | ||
* Copyright 2025 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
declare module 'react' { | ||
interface CSSProperties { | ||
viewTransitionName?: string, | ||
viewTransitionClass?: string | ||
} | ||
} | ||
|
||
export {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2025 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
interface Document { | ||
startViewTransition(fn: () => void): ViewTransition; | ||
} | ||
|
||
interface ViewTransition { | ||
ready: Promise<void>; | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,19 +39,13 @@ export interface SpectrumToastOptions extends Omit<ToastOptions, 'priority'>, DO | |
|
||
type CloseFunction = () => void; | ||
|
||
function wrapInViewTransition<R>(fn: () => R): R { | ||
function wrapInViewTransition(fn: () => void): void { | ||
if ('startViewTransition' in document) { | ||
let result: R; | ||
// @ts-expect-error | ||
document.startViewTransition(() => { | ||
flushSync(() => { | ||
result = fn(); | ||
}); | ||
flushSync(fn); | ||
}).ready.catch(() => {}); | ||
// @ts-ignore | ||
return result; | ||
} else { | ||
return fn(); | ||
fn(); | ||
} | ||
} | ||
|
||
|
@@ -141,8 +135,7 @@ export function ToastContainer(props: SpectrumToastContainerProps): ReactElement | |
key={toast.key} | ||
className={classNames(toastContainerStyles, 'spectrum-ToastContainer-listitem')} | ||
style={{ | ||
// @ts-expect-error | ||
viewTransitionName: `_${toast.key.slice(2)}`, | ||
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. Moved this hack into the hooks themselves to |
||
viewTransitionName: toast.key, | ||
viewTransitionClass: classNames( | ||
toastContainerStyles, | ||
'toast', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ export interface ToastStateProps { | |
/** The maximum number of toasts to display at a time. */ | ||
maxVisibleToasts?: number, | ||
/** Function to wrap updates in (i.e. document.startViewTransition()). */ | ||
wrapUpdate?: <R>(fn: () => R) => R | ||
wrapUpdate?: (fn: () => void) => void | ||
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 don't actually use the return value of this function so void is fine here |
||
} | ||
|
||
export interface ToastOptions { | ||
|
@@ -88,20 +88,20 @@ export class ToastQueue<T> { | |
private queue: QueuedToast<T>[] = []; | ||
private subscriptions: Set<() => void> = new Set(); | ||
private maxVisibleToasts: number; | ||
private wrapUpdate?: <R>(fn: () => R) => R; | ||
private wrapUpdate?: (fn: () => void) => void; | ||
/** The currently visible toasts. */ | ||
visibleToasts: QueuedToast<T>[] = []; | ||
|
||
constructor(options?: ToastStateProps) { | ||
this.maxVisibleToasts = options?.maxVisibleToasts ?? 1; | ||
this.maxVisibleToasts = options?.maxVisibleToasts ?? Infinity; | ||
this.wrapUpdate = options?.wrapUpdate; | ||
} | ||
|
||
private runWithWrapUpdate<R>(fn: () => R): R { | ||
private runWithWrapUpdate(fn: () => void): void { | ||
if (this.wrapUpdate) { | ||
return this.wrapUpdate(fn); | ||
this.wrapUpdate(fn); | ||
} else { | ||
return fn(); | ||
fn(); | ||
} | ||
} | ||
|
||
|
@@ -113,7 +113,7 @@ export class ToastQueue<T> { | |
|
||
/** Adds a new toast to the queue. */ | ||
add(content: T, options: ToastOptions = {}) { | ||
let toastKey = Math.random().toString(36); | ||
let toastKey = '_' + Math.random().toString(36).slice(2); | ||
let toast: QueuedToast<T> = { | ||
...options, | ||
content, | ||
|
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.
These declarations allow us to avoid
@ts-expect-error
or@ts-ignore
in documentation examples.document.startViewTransition
is included in TS 5.6 so this should go away when we update. Not sure about the React one.