Skip to content

Commit

Permalink
Add unstled prop
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski committed Nov 29, 2023
1 parent 167b92d commit a60162f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 58 deletions.
32 changes: 4 additions & 28 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ReactDOM from 'react-dom';

import './styles.css';
import { getAsset, Loader } from './assets';
import type { HeightT, Position, ToastT, ToastToDismiss, ExternalToast, ToasterProps, ToastClassnames } from './types';
import type { HeightT, ToastT, ToastToDismiss, ExternalToast, ToasterProps, ToastProps } from './types';
import { ToastState, toast } from './state';

// Visible toasts amount
Expand All @@ -27,32 +27,6 @@ const SWIPE_THRESHOLD = 20;

const TIME_BEFORE_UNMOUNT = 200;

interface ToastProps {
toast: ToastT;
toasts: ToastT[];
index: number;
expanded: boolean;
invert: boolean;
heights: HeightT[];
setHeights: React.Dispatch<React.SetStateAction<HeightT[]>>;
removeToast: (toast: ToastT) => void;
gap?: number;
position: Position;
visibleToasts: number;
expandByDefault: boolean;
closeButton: boolean;
interacting: boolean;
style?: React.CSSProperties;
cancelButtonStyle?: React.CSSProperties;
actionButtonStyle?: React.CSSProperties;
duration?: number;
className?: string;
unstyled?: boolean;
descriptionClassName?: string;
loadingIcon?: React.ReactNode;
classNames?: ToastClassnames;
}

function cn(...classes: (string | undefined)[]) {
return classes.filter(Boolean).join(' ');
}
Expand All @@ -61,6 +35,7 @@ const Toast = (props: ToastProps) => {
const {
invert: ToasterInvert,
toast,
unstyled,
interacting,
setHeights,
visibleToasts,
Expand Down Expand Up @@ -247,7 +222,7 @@ const Toast = (props: ToastProps) => {
toast?.classNames?.[toastType],
)}
data-sonner-toast=""
data-styled={!Boolean(toast.jsx || toast.unstyled)}
data-styled={!Boolean(toast.jsx || toast.unstyled || unstyled)}
data-mounted={mounted}
data-promise={Boolean(toast.promise)}
data-removed={removed}
Expand Down Expand Up @@ -658,6 +633,7 @@ const Toaster = (props: ToasterProps) => {
interacting={interacting}
position={position}
style={toastOptions?.style}
unstyled={toastOptions?.unstyled}
classNames={toastOptions?.classNames}
cancelButtonStyle={toastOptions?.cancelButtonStyle}
actionButtonStyle={toastOptions?.actionButtonStyle}
Expand Down
27 changes: 26 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export interface ToastClassnames {
warning?: string;
}

// TODO: CLEANUP TYPES
export interface ToastT {
id: number | string;
title?: string | React.ReactNode;
Expand Down Expand Up @@ -95,6 +94,32 @@ export interface ToasterProps {
loadingIcon?: React.ReactNode;
}

export interface ToastProps {
toast: ToastT;
toasts: ToastT[];
index: number;
expanded: boolean;
invert: boolean;
heights: HeightT[];
setHeights: React.Dispatch<React.SetStateAction<HeightT[]>>;
removeToast: (toast: ToastT) => void;
gap?: number;
position: Position;
visibleToasts: number;
expandByDefault: boolean;
closeButton: boolean;
interacting: boolean;
style?: React.CSSProperties;
cancelButtonStyle?: React.CSSProperties;
actionButtonStyle?: React.CSSProperties;
duration?: number;
className?: string;
unstyled?: boolean;
descriptionClassName?: string;
loadingIcon?: React.ReactNode;
classNames?: ToastClassnames;
}

export enum SwipeStateTypes {
SwipedOut = 'SwipedOut',
SwipedBack = 'SwipedBack',
Expand Down
1 change: 1 addition & 0 deletions website/src/components/Hero/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const Hero = () => {
onClick={() => {
toast('Sonner', {
description: 'An opinionated toast component for React.',
unstyled: true,
});
}}
className={styles.button}
Expand Down
49 changes: 20 additions & 29 deletions website/src/pages/styling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,19 @@ toast('Hello World', {

## Tailwind CSS

The preferred way to style the toasts with tailwind is by using the `classNames` prop. Exclamation mark in front of the class is required to override the default styles.
The preferred way to style the toasts with tailwind is by using the `unstyled` prop. That will give you an unstyled toast which you can then style with tailwind.

```jsx
<Toaster
toastOptions={{
unstyled: true,
classNames: {
toast: '!bg-blue-400',
title: '!text-red-400',
description: '!text-red-400',
actionButton: '!bg-zinc-400',
cancelButton: '!bg-orange-400',
closeButton: '!bg-lime-400',
toast: 'bg-blue-400',
title: 'text-red-400',
description: 'text-red-400',
actionButton: 'bg-zinc-400',
cancelButton: 'bg-orange-400',
closeButton: 'bg-lime-400',
},
}}
/>
Expand All @@ -47,13 +48,14 @@ You can do the same when calling `toast()`.

```jsx
toast('Hello World', {
unstyled: true,
classNames: {
toast: '!bg-blue-400',
title: '!text-red-400 !text-2xl',
description: '!text-red-400',
actionButton: '!bg-zinc-400',
cancelButton: '!bg-orange-400',
closeButton: '!bg-lime-400',
toast: 'bg-blue-400',
title: 'text-red-400 text-2xl',
description: 'text-red-400',
actionButton: 'bg-zinc-400',
cancelButton: 'bg-orange-400',
closeButton: 'bg-lime-400',
},
});
```
Expand All @@ -63,24 +65,13 @@ Styling per toast type is also possible.
```jsx
<Toaster
toastOptions={{
unstyled: true,
classNames: {
error: '!bg-red-400',
success: '!text-green-400',
warning: '!text-yellow-400',
info: '!bg-blue-400',
error: 'bg-red-400',
success: 'text-green-400',
warning: 'text-yellow-400',
info: 'bg-blue-400',
},
}}
/>
```

## No styles

You can also disable default styles by going headless.

```jsx
toast.custom((t) => (
<div>
This is a custom component <button onClick={() => toast.dismiss(t)}>close</button>
</div>
));
```

0 comments on commit a60162f

Please sign in to comment.