-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
279 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import { Style } from '@globals' | ||
import React, { ReactNode } from 'react' | ||
import { Modal, View, Text, TouchableOpacity, StyleSheet } from 'react-native' | ||
import Animated, { FadeInDown } from 'react-native-reanimated' | ||
import { create } from 'zustand' | ||
|
||
import FadeBackrop from './FadeBackdrop' | ||
|
||
type AlertButtonProps = { | ||
label: string | ||
onPress?: () => void | Promise<void> | ||
type?: 'warning' | 'default' | ||
} | ||
|
||
type AlertProps = { | ||
title: string | ||
description: string | ||
buttons: AlertButtonProps[] | ||
alignButtons?: 'left' | 'right' | ||
} | ||
|
||
type AlertState = { | ||
visible: boolean | ||
props: AlertProps | ||
hide: () => void | ||
show: (props: AlertProps) => void | ||
} | ||
|
||
export namespace Alert { | ||
export const alert = (props: AlertProps) => { | ||
useAlert.getState().show(props) | ||
} | ||
} | ||
|
||
const useAlert = create<AlertState>()((set, get) => ({ | ||
visible: false, | ||
props: { | ||
title: 'Are You Sure?', | ||
description: 'LIke `sure` sure?', | ||
buttons: [ | ||
{ label: 'Cancel', onPress: () => {}, type: 'default' }, | ||
{ label: 'Confirm', onPress: () => {}, type: 'warning' }, | ||
], | ||
alignButtons: 'right', | ||
}, | ||
hide: () => { | ||
set((state) => ({ ...state, visible: false })) | ||
}, | ||
show: (props: AlertProps) => { | ||
set((state) => ({ ...state, visible: true, props: props })) | ||
}, | ||
})) | ||
|
||
type AlertProviderProps = { | ||
children: ReactNode | ||
} | ||
|
||
const AlertButton: React.FC<AlertButtonProps> = ({ label, onPress, type = 'default' }) => { | ||
return ( | ||
<TouchableOpacity | ||
onPress={async () => { | ||
onPress && onPress() | ||
useAlert.getState().hide() | ||
}}> | ||
<Text style={buttonStyleMap[type]}>{label}</Text> | ||
</TouchableOpacity> | ||
) | ||
} | ||
|
||
export const AlertBox = () => { | ||
const { visible, props } = useAlert((state) => ({ visible: state.visible, props: state.props })) | ||
|
||
return ( | ||
<Modal visible={visible} transparent style={styles.modal} animationType="fade"> | ||
<FadeBackrop | ||
handleOverlayClick={() => { | ||
useAlert.getState().hide() | ||
}}> | ||
<Animated.View style={styles.textBoxContainer} entering={FadeInDown}> | ||
<View style={styles.textBox}> | ||
<Text style={styles.title}>{props.title}</Text> | ||
<Text style={styles.description}>{props.description}</Text> | ||
<View style={styles.buttonContainer}> | ||
{props.buttons.map((item, index) => ( | ||
<AlertButton {...item} key={index} /> | ||
))} | ||
</View> | ||
</View> | ||
</Animated.View> | ||
</FadeBackrop> | ||
</Modal> | ||
) | ||
} | ||
|
||
export const AlertProvider: React.FC<AlertProviderProps> = ({ children }) => { | ||
return ( | ||
<View style={{ flex: 1 }}> | ||
<AlertBox /> | ||
{children} | ||
</View> | ||
) | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
modal: { | ||
flex: 1, | ||
}, | ||
textBoxContainer: { | ||
flex: 1, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
|
||
textBox: { | ||
backgroundColor: Style.getColor('primary-surface2'), | ||
paddingHorizontal: 32, | ||
paddingBottom: 16, | ||
paddingTop: 24, | ||
borderRadius: 16, | ||
width: '90%', | ||
}, | ||
|
||
title: { | ||
color: Style.getColor('primary-text1'), | ||
fontSize: 20, | ||
fontWeight: '500', | ||
marginBottom: 12, | ||
}, | ||
|
||
description: { | ||
color: Style.getColor('primary-text1'), | ||
marginBottom: 12, | ||
fontSize: 16, | ||
}, | ||
|
||
buttonContainer: { | ||
flexDirection: 'row', | ||
columnGap: 24, | ||
justifyContent: 'flex-end', | ||
alignItems: 'center', | ||
}, | ||
|
||
button: { | ||
paddingHorizontal: 4, | ||
paddingVertical: 8, | ||
color: Style.getColor('primary-text2'), | ||
}, | ||
|
||
buttonWarning: { | ||
paddingHorizontal: 4, | ||
paddingVertical: 8, | ||
color: '#d2574b', | ||
}, | ||
}) | ||
|
||
const buttonStyleMap = { | ||
warning: styles.buttonWarning, | ||
default: styles.button, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ReactNode } from 'react' | ||
import { GestureResponderEvent, TouchableOpacity } from 'react-native' | ||
|
||
type FadeScreenProps = { | ||
handleOverlayClick?: (e: GestureResponderEvent) => void | ||
children: ReactNode | ||
} | ||
|
||
const FadeBackrop: React.FC<FadeScreenProps> = ({ handleOverlayClick, children }) => { | ||
return ( | ||
<TouchableOpacity | ||
activeOpacity={1} | ||
onPress={handleOverlayClick} | ||
style={{ | ||
height: '100%', | ||
justifyContent: 'flex-end', | ||
backgroundColor: 'rgba(0, 0, 0, 0.5)', | ||
}}> | ||
{children} | ||
</TouchableOpacity> | ||
) | ||
} | ||
|
||
export default FadeBackrop |
Oops, something went wrong.