From 2560331889c267767fa1a856dd178cb199d38a37 Mon Sep 17 00:00:00 2001 From: Gunnar Torfi Date: Sat, 14 Sep 2024 08:08:18 +0000 Subject: [PATCH] refactor: move styles to a use-default-styles hook --- src/toast.tsx | 100 ++++++++---------------------------- src/use-default-styles.ts | 103 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 80 deletions(-) create mode 100644 src/use-default-styles.ts diff --git a/src/toast.tsx b/src/toast.tsx index 37d7296..e48e0fa 100644 --- a/src/toast.tsx +++ b/src/toast.tsx @@ -14,6 +14,7 @@ import { CircleCheck, CircleX, Info, TriangleAlert, X } from './icons'; import { isToastAction, type ToastProps, type ToastRef } from './types'; import { useAppStateListener } from './use-app-state'; import { useColors } from './use-colors'; +import { useDefaultStyles } from './use-default-styles'; export const Toast = React.forwardRef( ( @@ -48,6 +49,7 @@ export const Toast = React.forwardRef( unstyled: unstyledProps, important, invert: invertProps, + // richColors: richColorsProps, }, ref ) => { @@ -59,6 +61,7 @@ export const Toast = React.forwardRef( pauseWhenPageIsHidden, cn, invert: invertCtx, + // richColors: richColorsCtx, toastOptions: { unstyled: unstyledCtx, toastContainerStyle: toastContainerStyleCtx, @@ -77,7 +80,7 @@ export const Toast = React.forwardRef( }, } = useToastContext(); const invert = invertProps ?? invertCtx; - + // const richColors = richColorsProps ?? richColorsCtx; const unstyled = unstyledProps ?? unstyledCtx; const duration = durationProps ?? durationCtx; const closeButton = closeButtonProps ?? closeButtonCtx; @@ -251,6 +254,13 @@ export const Toast = React.forwardRef( }; }, [id]); + const defaultStyles = useDefaultStyles({ + invert, + // richColors, + unstyled, + description, + }); + if (jsx) { return jsx; } @@ -291,16 +301,7 @@ export const Toast = React.forwardRef( toastStyleCtx, styles?.toast, style, - unstyled - ? undefined - : { - justifyContent: 'center', - padding: 16, - borderRadius: 16, - marginHorizontal: 16, - backgroundColor: colors['background-primary'], - borderCurve: 'continuous', - }, + defaultStyles.toast, wiggleAnimationStyle, ]} entering={entering} @@ -308,14 +309,7 @@ export const Toast = React.forwardRef( > ( )} {title} @@ -357,14 +341,7 @@ export const Toast = React.forwardRef( {description ? ( ( style={[ unstyled || (!action && !cancel) ? undefined - : { - flexDirection: 'row', - alignItems: 'center', - gap: 16, - marginTop: 16, - }, + : defaultStyles.buttons, buttonsStyleCtx, styles?.buttons, ]} @@ -396,19 +368,7 @@ export const Toast = React.forwardRef( onPress={action.onClick} className={actionButtonClassName} style={[ - unstyled - ? undefined - : { - flexGrow: 0, - alignSelf: 'flex-start', - borderRadius: 999, - borderWidth: 1, - borderColor: colors['border-secondary'], - paddingHorizontal: 14, - paddingVertical: 6, - borderCurve: 'continuous', - backgroundColor: colors['background-secondary'], - }, + defaultStyles.actionButton, actionButtonStyleCtx, actionButtonStyle, ]} @@ -416,15 +376,7 @@ export const Toast = React.forwardRef( ( }} className={cancelButtonClassName} style={[ - unstyled - ? undefined - : { - flexGrow: 0, - }, + defaultStyles.cancelButton, cancelButtonStyleCtx, cancelButtonStyle, ]} @@ -456,15 +404,7 @@ export const Toast = React.forwardRef( { + const colors = useColors(invert); + + if (unstyled) { + return { + toast: {}, + toastContent: {}, + title: {}, + description: {}, + buttons: {}, + actionButton: {}, + actionButtonText: {}, + cancelButton: {}, + cancelButtonText: {}, + }; + } + + return { + toast: { + justifyContent: 'center', + padding: 16, + borderRadius: 16, + marginHorizontal: 16, + backgroundColor: colors['background-primary'], + borderCurve: 'continuous', + }, + toastContent: { + flexDirection: 'row', + gap: 16, + alignItems: description?.length === 0 ? 'center' : undefined, + }, + title: { + fontWeight: '600', + lineHeight: 20, + color: colors['text-primary'], + }, + description: { + fontSize: 14, + lineHeight: 20, + marginTop: 2, + color: colors['text-tertiary'], + }, + buttons: { + flexDirection: 'row', + alignItems: 'center', + gap: 16, + marginTop: 16, + }, + actionButton: { + flexGrow: 0, + alignSelf: 'flex-start', + borderRadius: 999, + borderWidth: 1, + borderColor: colors['border-secondary'], + paddingHorizontal: 14, + paddingVertical: 6, + borderCurve: 'continuous', + backgroundColor: colors['background-secondary'], + }, + actionButtonText: { + fontSize: 14, + lineHeight: 20, + fontWeight: '600', + alignSelf: 'flex-start', + color: colors['text-primary'], + }, + cancelButton: { + flexGrow: 0, + }, + cancelButtonText: { + fontSize: 14, + lineHeight: 20, + fontWeight: '600', + alignSelf: 'flex-start', + color: colors['text-secondary'], + }, + }; +};