Skip to content
GitSiteEmail

The Toast component is a notification element that displays brief messages to the user. It allows users to receive feedback about an operation or to display important information.

Every component ships with the boilerplate. If you are working in your own project, add Toast with the Gameface CLI:

Terminal window
npx gameface-cli add Toast

The CLI pulls in whatever Toast depends on. To refresh an existing copy, update it instead.

To include a toast in your UI, call the createToast function with the desired options and include the Toaster component in your JSX.

import useToast from '@components/Feedback/Toast/toast';
const App = () => {
const [Toaster, createToast] = useToast();
// Example of creating a toast
const showToast = () => {
createToast({
body: () => <>This is a toast message</>,
position: 'top-right', // Position of the toast on the screen
timeout: 3000, // Duration in milliseconds before the toast disappears
});
};
return (
<>
<button onClick={showToast}>Show Toast</button>
<Toaster />
</>
);
};

Creates a new toast notification with the specified options.

Option NameTypeDefaultDescription
bodystring | JSX.Element""The content of the toast message. Can be a string or JSX element.
position'top-left' | 'top-center' | 'top-right' | 'middle-left' | 'middle-center' | 'middle-right' | 'bottom-left' | 'bottom-center' | 'bottom-right''top-right'The position on the screen where the toast will appear.
timeoutnumber0Duration in milliseconds before the toast disappears. 0 makes the toast persistent.

When defining the body function, you receive the following helpers:

ArgumentTypeDescription
close(children: JSX.Element) => JSX.ElementA wrapper function that attaches a click-to-close listener to any element.
progress() => numberA signal returning the current lifetime of the toast (0 to 20).
dismiss() => voidA function to programmatically remove the toast from the UI.

To allow the user to close the toast manually you can include the Close button inside the body:

import useToast from '@components/Feedback/Toast/toast';
const App = () => {
const [Toaster, createToast] = useToast();
// Example of creating a toast
const showToast = () => {
createToast({
body: (close) => (
<>
This is a toast message
{close(<button>Close</button>)}
</>
),
position: 'top-right', // Position of the toast on the screen
timeout: 3000, // Duration in milliseconds before the toast disappears
});
};
return (
<>
<button onClick={showToast}>Show Toast</button>
<Toaster />
</>
);
};
export default App;

You can display a progress bar inside your toast by using the progress signal within the body function, together with the Progress.Bar component:

import useToast from '@components/Feedback/Toast/toast';
import Progress from '@components/Feedback/Progress/Progress';
const App = () => {
const [Toaster, createToast] = useToast();
// Example of creating a toast
const showToast = () => {
createToast({
body: (close, progress) => (
<>
This is a toast message
<div style={{ width: '100%', height: '5px', background: '#ccc' }}>
<Progress.Bar progress={progress() * 5} />
</div>
{close(<button>Close</button>)}
</>
),
position: 'top-right', // Position of the toast on the screen
timeout: 3000, // Duration in milliseconds before the toast disappears
});
};
return (
<>
<button onClick={showToast}>Show Toast</button>
<Toaster />
</>
);
};
export default App;

You can add entrance animations to the toast by adding CSS classes to the body element:

import useToast from '@components/Feedback/Toast/toast';
const App = () => {
const [Toaster, createToast] = useToast();
// Example of creating a toast
const showToast = () => {
createToast({
body: (close) => (
<div class="animate-fade-in">
This is a toast message
{close(<button>Close</button>)}
</div>
),
position: 'top-right', // Position of the toast on the screen
timeout: 3000, // Duration in milliseconds before the toast disappears
});
};
return (
<>
<button onClick={showToast}>Show Toast</button>
<Toaster />
</>
);
};
export default App;
@keyframes fade-in {
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.animate-fade-in {
animation: fade-in 0.5s ease-out;
}

While the close wrapper is convenient for standard buttons, you may need to close a toast programmatically after an logic-heavy action (like starting a tutorial or responding to a gamepad event). The dismiss function allows you to trigger the removal logic directly without relying on DOM click events.

import useToast from '@components/Feedback/Toast/toast';
const App = () => {
const [Toaster, createToast] = useToast();
const showToast = () => {
createToast({
body: (close, progress, dismiss) => (
<div>
Proceed with Action?
<button onClick={() => {
// 1. Perform your logic
console.log("Action Triggered");
// 2. Programmatically close the toast
dismiss();
}}>
Confirm
</button>
{/* Fallback for simple UI closing */}
{close(<button>Cancel</button>)}
</div>
),
position: 'top-center',
timeout: 0, // Persistent until dismissed
});
};
return (
<>
<button onClick={showToast}>Trigger Complex Toast</button>
<Toaster />
</>
);
};