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

Improve unstyled prop, add docs #240

Merged
merged 3 commits into from
Nov 29, 2023
Merged
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
33 changes: 4 additions & 29 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,33 +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;
closeButtonAriaLabel?: string;
}

function cn(...classes: (string | undefined)[]) {
return classes.filter(Boolean).join(' ');
}
Expand All @@ -62,6 +35,7 @@ const Toast = (props: ToastProps) => {
const {
invert: ToasterInvert,
toast,
unstyled,
interacting,
setHeights,
visibleToasts,
Expand Down Expand Up @@ -249,7 +223,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 @@ -661,6 +635,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
28 changes: 27 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 @@ -96,6 +95,33 @@ export interface ToasterProps {
containerAriaLabel?: string;
}

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;
closeButtonAriaLabel?: string;
}

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>
));
```