From a60162f5797f5aeb77fd2c3e7e71de11657c24d7 Mon Sep 17 00:00:00 2001 From: emilkowalski Date: Wed, 29 Nov 2023 05:21:12 +0100 Subject: [PATCH] Add unstled prop --- src/index.tsx | 32 +++-------------- src/types.ts | 27 ++++++++++++++- website/src/components/Hero/index.tsx | 1 + website/src/pages/styling.mdx | 49 +++++++++++---------------- 4 files changed, 51 insertions(+), 58 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 9403a85..2fed979 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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 @@ -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>; - 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(' '); } @@ -61,6 +35,7 @@ const Toast = (props: ToastProps) => { const { invert: ToasterInvert, toast, + unstyled, interacting, setHeights, visibleToasts, @@ -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} @@ -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} diff --git a/src/types.ts b/src/types.ts index db126ee..200abe0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -25,7 +25,6 @@ export interface ToastClassnames { warning?: string; } -// TODO: CLEANUP TYPES export interface ToastT { id: number | string; title?: string | React.ReactNode; @@ -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>; + 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', diff --git a/website/src/components/Hero/index.tsx b/website/src/components/Hero/index.tsx index 2e9dbcc..472ffe2 100644 --- a/website/src/components/Hero/index.tsx +++ b/website/src/components/Hero/index.tsx @@ -19,6 +19,7 @@ export const Hero = () => { onClick={() => { toast('Sonner', { description: 'An opinionated toast component for React.', + unstyled: true, }); }} className={styles.button} diff --git a/website/src/pages/styling.mdx b/website/src/pages/styling.mdx index f74d7ad..c5427d2 100644 --- a/website/src/pages/styling.mdx +++ b/website/src/pages/styling.mdx @@ -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 @@ -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', }, }); ``` @@ -63,24 +65,13 @@ Styling per toast type is also possible. ```jsx ``` - -## No styles - -You can also disable default styles by going headless. - -```jsx -toast.custom((t) => ( -
- This is a custom component -
-)); -```