Skip to content

An opinionated toast component for React.

feat: allow ref forwarding for `Toaster` (#491)

Gianmarco Rengucci5d3a179f4658
diff --git a/src/index.tsx b/src/index.tsx
index 894c0ae..049107a 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,6 +1,6 @@
'use client';
-import React from 'react';
+import React, { forwardRef } from 'react';
import ReactDOM from 'react-dom';
import { CloseIcon, getAsset, Loader } from './assets';
@@ -474,7 +474,7 @@ function useSonner() {
};
}
-const Toaster = (props: ToasterProps) => {
+const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, ref) {
const {
invert,
position = 'bottom-right',
@@ -632,7 +632,7 @@ const Toaster = (props: ToasterProps) => {
return (
// Remove item from normal navigation flow, only available via hotkey
- <section aria-label={`${containerAriaLabel} ${hotkeyLabel}`} tabIndex={-1}>
+ <section aria-label={`${containerAriaLabel} ${hotkeyLabel}`} tabIndex={-1} ref={ref}>
{possiblePositions.map((position, index) => {
const [y, x] = position.split('-');
return (
@@ -731,6 +731,6 @@ const Toaster = (props: ToasterProps) => {
})}
</section>
);
-};
+});
export { toast, Toaster, type ExternalToast, type ToastT, type ToasterProps, useSonner };
export { type ToastClassnames, type ToastToDismiss, type Action } from './types';