Skip to content

An opinionated toast component for React.

feature: custom cn function (#310)

Jeff Huangd39170f70373
diff --git a/src/index.tsx b/src/index.tsx
index 7c2367c..c197836 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -28,7 +28,7 @@ const SWIPE_THRESHOLD = 20;
const TIME_BEFORE_UNMOUNT = 200;
-function cn(...classes: (string | undefined)[]) {
+function _cn(...classes: (string | undefined)[]) {
return classes.filter(Boolean).join(' ');
}
@@ -60,6 +60,7 @@ const Toast = (props: ToastProps) => {
icons,
closeButtonAriaLabel = 'Close toast',
pauseWhenPageIsHidden,
+ cn,
} = props;
const [mounted, setMounted] = React.useState(false);
const [removed, setRemoved] = React.useState(false);
@@ -457,6 +458,7 @@ const Toaster = (props: ToasterProps) => {
icons,
containerAriaLabel = 'Notifications',
pauseWhenPageIsHidden,
+ cn = _cn,
} = props;
const [toasts, setToasts] = React.useState<ToastT[]>([]);
const possiblePositions = React.useMemo(() => {
@@ -471,10 +473,10 @@ const Toaster = (props: ToasterProps) => {
theme !== 'system'
? theme
: typeof window !== 'undefined'
- ? window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
- ? 'dark'
- : 'light'
- : 'light',
+ ? window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
+ ? 'dark'
+ : 'light'
+ : 'light',
);
const listRef = React.useRef<HTMLOListElement>(null);
@@ -679,6 +681,7 @@ const Toaster = (props: ToasterProps) => {
loadingIcon={loadingIcon}
expanded={expanded}
pauseWhenPageIsHidden={pauseWhenPageIsHidden}
+ cn={cn}
/>
))}
</ol>
diff --git a/src/types.ts b/src/types.ts
index c5d7321..e239363 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -91,6 +91,8 @@ interface ToastOptions {
classNames?: ToastClassnames;
}
+type CnFunction = (...classes: Array<string | undefined>) => string;
+
export interface ToasterProps {
invert?: boolean;
theme?: 'light' | 'dark' | 'system';
@@ -119,6 +121,7 @@ export interface ToasterProps {
icons?: ToastIcons;
containerAriaLabel?: string;
pauseWhenPageIsHidden?: boolean;
+ cn?: CnFunction;
}
export interface ToastProps {
@@ -148,6 +151,7 @@ export interface ToastProps {
icons?: ToastIcons;
closeButtonAriaLabel?: string;
pauseWhenPageIsHidden: boolean;
+ cn: CnFunction;
}
export enum SwipeStateTypes {
diff --git a/website/src/pages/toaster.mdx b/website/src/pages/toaster.mdx
index 9f63604..18614cd 100644
--- a/website/src/pages/toaster.mdx
+++ b/website/src/pages/toaster.mdx
@@ -49,20 +49,21 @@ Changes the directionality of the toast's text.
## API Reference
-| Property | Description | Default |
-| :-------------------- | :-----------------------------------------------------------------------------------------------------------------------------: | -------------: |
-| theme | Toast's theme, either `light`, `dark`, or `system` | `light` |
-| richColors | Makes error and success state more colorful | `false` |
-| expand | Toasts will be expanded by default | `false` |
-| visibleToasts | Amount of visible toasts | `3` |
-| position | Place where the toasts will be rendered | `bottom-right` |
-| closeButton | Adds a close button to all toasts | `false` |
-| offset | Offset from the edges of the screen. | `32px` |
-| dir | Directionality of toast's text | `ltr` |
-| hotkey | Keyboard shortcut that will move focus to the toaster area. | `⌥/alt + T` |
-| invert | Dark toasts in light mode and vice versa. | `false` |
-| toastOptions | These will act as default options for all toasts. See [toast()](/toast) for all available options. | `4000` |
-| gap | Gap between toasts when expanded | `14` |
-| loadingIcon | Changes the default loading icon | `-` |
-| pauseWhenPageIsHidden | Pauses toast timers when the page is hidden, e.g., when the tab is backgrounded, the browser is minimized, or the OS is locked. | `false` |
-| icons | Changes the default icons | `-` |
+| Property | Description | Default |
+| :-------------------- | :-----------------------------------------------------------------------------------------------------------------------------: | ----------------------------------: |
+| theme | Toast's theme, either `light`, `dark`, or `system` | `light` |
+| richColors | Makes error and success state more colorful | `false` |
+| expand | Toasts will be expanded by default | `false` |
+| visibleToasts | Amount of visible toasts | `3` |
+| position | Place where the toasts will be rendered | `bottom-right` |
+| closeButton | Adds a close button to all toasts | `false` |
+| offset | Offset from the edges of the screen. | `32px` |
+| dir | Directionality of toast's text | `ltr` |
+| hotkey | Keyboard shortcut that will move focus to the toaster area. | `⌥/alt + T` |
+| invert | Dark toasts in light mode and vice versa. | `false` |
+| toastOptions | These will act as default options for all toasts. See [toast()](/toast) for all available options. | `4000` |
+| gap | Gap between toasts when expanded | `14` |
+| loadingIcon | Changes the default loading icon | `-` |
+| pauseWhenPageIsHidden | Pauses toast timers when the page is hidden, e.g., when the tab is backgrounded, the browser is minimized, or the OS is locked. | `false` |
+| icons | Changes the default icons | `-` |
+| cn | Custom function for constructing/merging classes. | `classes.filter(Boolean).join(' ')` |