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

[Toast] Create new Toast component #1467

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions docs/reference/generated/toast-close.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ToastClose",
"description": "Closes the toast when clicked.\nRenders a `<button>` element.",
"props": {
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
},
"render": {
"type": "React.ReactElement | (props, state) => React.ReactElement",
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render."
}
},
"dataAttributes": {},
"cssVariables": {}
}
16 changes: 16 additions & 0 deletions docs/reference/generated/toast-description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ToastDescription",
"description": "A description that describes the toast.\nRenders a `<p>` element.",
"props": {
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
},
"render": {
"type": "React.ReactElement | (props, state) => React.ReactElement",
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render."
}
},
"dataAttributes": {},
"cssVariables": {}
}
22 changes: 22 additions & 0 deletions docs/reference/generated/toast-provider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "ToastProvider",
"description": "Provides a context for creating and managing toasts.",
"props": {
"limit": {
"type": "number",
"default": "3",
"description": "The maximum number of toasts that can be displayed at once.\nWhen the limit is reached, the oldest toast will be removed to make room for the new one."
},
"toastManager": {
"type": "createToastManager.ToastManager",
"description": "A global manager for toasts to use outside of a React component."
},
"timeout": {
"type": "number",
"default": "5000",
"description": "The default amount of time (in ms) before a toast is auto dismissed.\nA value of `0` will prevent the toast from being dismissed automatically."
}
},
"dataAttributes": {},
"cssVariables": {}
}
56 changes: 56 additions & 0 deletions docs/reference/generated/toast-root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "ToastRoot",
"description": "Groups all parts of an individual toast.\nRenders a `<div>` element.",
"props": {
"swipeDirection": {
"type": "'down' | 'left' | 'right' | 'up' | Array<'down' | 'left' | 'right' | 'up'>",
"default": "'up'",
"description": "Direction(s) in which the toast can be swiped to dismiss."
},
"toast": {
"type": "Toast",
"required": true,
"description": "The toast to render."
},
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
},
"render": {
"type": "React.ReactElement | (props, state) => React.ReactElement",
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render."
}
},
"dataAttributes": {
"data-swipe": {
"description": "Indicates the current swipe state.",
"type": "'start' | 'move' | 'end' | 'cancel'"
},
"data-swipe-direction": {
"description": "Indicates the direction of the swipe.",
"type": "'up' | 'down' | 'left' | 'right'"
}
},
"cssVariables": {
"--toast-index": {
"description": "Indicates the index of the toast in the list.",
"type": "number"
},
"--toast-offset": {
"description": "Indicates the offset of the toast in the list.",
"type": "number"
},
"--toast-swipe-direction": {
"description": "Indicates the direction the toast is swiped.",
"type": "'up' | 'down' | 'left' | 'right'"
},
"--toast-swipe-move-x": {
"description": "Indicates the horizontal swipe movement of the toast.",
"type": "number"
},
"--toast-swipe-move-y": {
"description": "Indicates the vertical swipe movement of the toast.",
"type": "number"
}
}
}
16 changes: 16 additions & 0 deletions docs/reference/generated/toast-title.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ToastTitle",
"description": "A title that labels the toast.\nRenders an `<h2>` element.",
"props": {
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
},
"render": {
"type": "React.ReactElement | (props, state) => React.ReactElement",
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render."
}
},
"dataAttributes": {},
"cssVariables": {}
}
16 changes: 16 additions & 0 deletions docs/reference/generated/toast-viewport.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ToastViewport",
"description": "A container viewport for toasts.\nRenders a `<div>` element.",
"props": {
"className": {
"type": "string | (state) => string",
"description": "CSS class applied to the element, or a function that\nreturns a class based on the component’s state."
},
"render": {
"type": "React.ReactElement | (props, state) => React.ReactElement",
"description": "Allows you to replace the component’s HTML element\nwith a different tag, or compose it with another component.\n\nAccepts a `ReactElement` or a function that returns the element to render."
}
},
"dataAttributes": {},
"cssVariables": {}
}
8 changes: 8 additions & 0 deletions docs/reference/overrides/toast-provider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "ToastProvider",
"props": {
"toastManager": {
"type": "createToastManager.ToastManager"
}
}
}
8 changes: 8 additions & 0 deletions docs/reference/overrides/toast-root.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "ToastRoot",
"props": {
"toast": {
"type": "Toast"
}
}
}
77 changes: 77 additions & 0 deletions docs/src/app/(private)/experiments/toast.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
.viewport {
width: 300px;
position: fixed;
left: 50%;
top: 20px;
transform: translateX(-50%);
}

.root {
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
transform-origin: top center;
background: white;
padding: 1rem;
width: 300px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
z-index: calc(2147483647 - var(--toast-index));
transition:
transform 0.5s cubic-bezier(0.22, 1, 0.36, 1),
opacity 0.5s;
transform: translateY(calc(var(--toast-index) * 15px)) scale(calc(1 - (var(--toast-index) * 0.1)));
opacity: 1;
--gap: 10px;
user-select: none;

&::after {
content: '';
position: absolute;
left: 0;
top: 100%;
width: 100%;
height: calc(var(--gap) + 1px);
}

&[data-expanded] {
transform: translateY(calc(var(--toast-offset) + calc(var(--toast-index) * var(--gap))));
}

&[data-starting-style],
&[data-ending-style] {
transform: translateY(-150%);
}

&[data-ending-style] {
transition-duration: 0.2s;
transition-timing-function: ease-in;
}

&[data-swipe='end'][data-ending-style] {
transition-duration: calc(0.3s / var(--toast-momentum, 1));
transition-timing-function: linear;
}
}

.title {
font-weight: 600;
margin-bottom: 4px;
}

.button {
background: royalblue;
color: white;
padding: 0.5rem;
border-radius: 5px;
text-align: center;
margin-top: 1rem;
}

.close {
position: absolute;
right: 0;
top: 0;
padding: 0.5rem;
}
129 changes: 129 additions & 0 deletions docs/src/app/(private)/experiments/toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
'use client';
import * as React from 'react';
import { Toast } from '@base-ui-components/react/toast';
import styles from './toast.module.css';

const globalToastManager = Toast.createToastManager();

function showGlobalToast() {
globalToastManager.promise(fetchUserData(), {
error: 'Failed to fetch user data',
success: 'User data loaded!',
loading: 'Fetching user data...',
});
}

function fetchUserData() {
return new Promise((resolve, reject) => {
// Simulate API call with a 50% chance of success
setTimeout(() => {
const success = Math.random() > 0.5;
if (success) {
resolve({ name: 'John Doe', email: '[email protected]' });
} else {
reject(new Error('Failed to fetch user data'));
}
}, 1000);
});
}

export default function Page() {
return (
<Toast.Provider toastManager={globalToastManager}>
<Toast.Viewport className={styles.viewport}>
<Toasts />
</Toast.Viewport>
<ToastButtons />
</Toast.Provider>
);
}

function ToastButtons() {
const toast = Toast.useToast();

function showRegularToast() {
toast.add({
title: 'Toast created',
description: 'This is a toast.',
});
}

function showActionToast() {
toast.add({
type: 'undo',
title: 'Message deleted',
});
}

return (
<div
style={{
position: 'fixed',
bottom: 20,
display: 'flex',
gap: 10,
justifyContent: 'center',
}}
>
<button className={styles.button} type="button" onClick={showRegularToast}>
Show regular toast
</button>
<button className={styles.button} type="button" onClick={showActionToast}>
Show action toast
</button>
<button className={styles.button} type="button" onClick={showGlobalToast}>
Show global toast
</button>
<ToastPromiseExample />
</div>
);
}

function Toasts() {
const { toasts } = Toast.useToast();
return toasts.map((toast) => (
<Toast.Root key={toast.id} toast={toast} className={styles.root}>
{toast.title && <Toast.Title>{toast.title}</Toast.Title>}
{toast.description && (
<Toast.Description>{toast.description}</Toast.Description>
)}
{toast.type === 'undo' && (
<button
className={styles.button}
type="button"
onClick={() => alert('Action undone')}
>
Undo
</button>
)}
<Toast.Close className={styles.close} aria-label="Close">
x
</Toast.Close>
</Toast.Root>
));
}

function ToastPromiseExample() {
const toast = Toast.useToast();

const handlePromiseClick = () => {
toast
.promise(fetchUserData(), {
loading: 'Fetching user data...',
success: 'User data loaded!',
error: 'Failed to load user data',
})
.then((data) => {
console.log('User data:', data);
})
.catch((err) => {
console.error('Error handled:', err);
});
};

return (
<button className={styles.button} type="button" onClick={handlePromiseClick}>
Show promise toast
</button>
);
}
Loading