Skip to content

Commit

Permalink
Merge pull request #130 from ljukas/ll/customizeable-close-button
Browse files Browse the repository at this point in the history
feat: added customizeable close button
  • Loading branch information
gunnartorfis authored Oct 4, 2024
2 parents f6d86b4 + 8dc3666 commit 2c68249
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
10 changes: 10 additions & 0 deletions example/src/ToastDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,16 @@ export const ToastDemo: React.FC = () => {
setToastId(id);
}}
/>

<Button
title="Custom close button"
onPress={() => {
const id = toast.success('Custom close button', {
close: <Button title="close" onPress={() => toast.dismiss(id)} />,
closeButton: undefined,
});
}}
/>
</ScrollView>
);
};
Expand Down
3 changes: 1 addition & 2 deletions src/gestures.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import Animated, {
useSharedValue,
withTiming,
} from 'react-native-reanimated';
import { easeInOutCircFn } from './easings';
import { useToastContext } from './context';
import { easeInOutCircFn } from './easings';
import type { ToastPosition, ToastProps } from './types';

const { width: WINDOW_WIDTH } = Dimensions.get('window');
Expand Down Expand Up @@ -113,7 +113,6 @@ export const ToastSwipeHandler: React.FC<

const tap = Gesture.Tap().onEnd(() => {
'worklet';

if (onPress) {
runOnJS(onPress)();
}
Expand Down
69 changes: 48 additions & 21 deletions src/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const Toast = React.forwardRef<ToastRef, ToastProps>(
variant,
action,
cancel,
close,
onDismiss,
onAutoClose,
dismissible = toastDefaultValues.dismissible,
Expand Down Expand Up @@ -263,6 +264,52 @@ export const Toast = React.forwardRef<ToastRef, ToastProps>(
variant,
});

const renderCloseButton = React.useMemo(() => {
if (!dismissible) {
return null;
}
if (close) {
return close;
}
if (closeButton) {
return (
<Pressable
onPress={() => onDismiss?.(id)}
hitSlop={10}
style={[closeButtonStyleCtx, styles?.closeButton]}
className={cn(classNamesCtx?.closeButton, classNames?.closeButton)}
>
<X
size={20}
color={defaultStyles.closeButtonColor}
style={[closeButtonIconStyleCtx, styles?.closeButtonIcon]}
className={cn(
classNamesCtx?.closeButtonIcon,
classNames?.closeButtonIcon
)}
/>
</Pressable>
);
}
return null;
}, [
classNames?.closeButton,
classNames?.closeButtonIcon,
classNamesCtx?.closeButton,
classNamesCtx?.closeButtonIcon,
close,
closeButton,
closeButtonIconStyleCtx,
closeButtonStyleCtx,
cn,
defaultStyles?.closeButtonColor,
dismissible,
id,
onDismiss,
styles?.closeButton,
styles?.closeButtonIcon,
]);

if (jsx) {
return jsx;
}
Expand Down Expand Up @@ -426,27 +473,7 @@ export const Toast = React.forwardRef<ToastRef, ToastProps>(
)}
</View>
</View>
{closeButton && dismissible ? (
<Pressable
onPress={() => onDismiss?.(id)}
hitSlop={10}
style={[closeButtonStyleCtx, styles?.closeButton]}
className={cn(
classNamesCtx?.closeButton,
classNames?.closeButton
)}
>
<X
size={20}
color={defaultStyles.closeButtonColor}
style={[closeButtonIconStyleCtx, styles?.closeButtonIcon]}
className={cn(
classNamesCtx?.closeButtonIcon,
classNames?.closeButtonIcon
)}
/>
</Pressable>
) : null}
{renderCloseButton}
</View>
</Animated.View>
</ToastSwipeHandler>
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type React from 'react';
import type { TextStyle, ViewStyle } from 'react-native';

type StyleProps = {
Expand Down Expand Up @@ -54,7 +55,6 @@ export type ToastProps = StyleProps & {
variant: ToastVariant;
jsx?: React.ReactNode;
description?: string;
closeButton?: boolean;
invert?: boolean;
important?: boolean;
duration?: number;
Expand All @@ -63,6 +63,8 @@ export type ToastProps = StyleProps & {
icon?: React.ReactNode;
action?: ToastAction | React.ReactNode;
cancel?: ToastAction | React.ReactNode;
close?: React.ReactNode;
closeButton?: boolean;
richColors?: boolean;
onDismiss?: (id: string | number) => void;
onAutoClose?: (id: string | number) => void;
Expand Down

0 comments on commit 2c68249

Please sign in to comment.