| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- import {
- type PointerEvent,
- useEffect,
- useState,
- type FC,
- useRef
- } from "react";
- import {
- TouchableWithoutFeedback,
- useWindowDimensions,
- PanResponder,
- Platform,
- Animated,
- Easing,
- View
- } from "react-native";
- import type ISnackBarProps from "./type";
- import stylesheet, {
- getSnackBarType,
- useStyles
- } from "./stylesheet";
- import {
- NCoreUIKitTheme
- } from "../../core/hooks";
- import {
- useSafeAreaInsets
- } from "react-native-safe-area-context";
- import {
- type SafePointerEvent,
- safeGlobal
- } from "../../types";
- import {
- X as XIcon
- } from "lucide-react-native";
- import {
- Portal
- } from "../../helpers/portalize";
- import {
- windowHeight
- } from "../../utils";
- import Button from "../button";
- import Text from "../text";
- const SnackBar: FC<ISnackBarProps> = ({
- isCloseOnPressActionButton = true,
- closeAnimationDelay = 350,
- openAnimationDelay = 200,
- spreadBehaviour = "free",
- autoCloseDelay = 5000,
- contentContainerStyle,
- isCloseOnPress = true,
- isFullWidth = false,
- isShowAction = true,
- icon: CustomIcon,
- isInlineSafeArea,
- type = "neutral",
- customTheme,
- children,
- onClosed,
- subTitle,
- onPress,
- action,
- style,
- title,
- id
- }) => {
- const {
- radiuses,
- colors,
- spaces
- } = NCoreUIKitTheme.useContext(customTheme);
- const {
- top
- } = useSafeAreaInsets();
- const [
- isMeasured,
- setIsMeasured
- ] = useState(false);
- const {
- width: windowWidth
- } = useWindowDimensions();
- const contentHeight = useRef<number>(windowHeight);
- const transformAnim = useRef(new Animated.Value(-contentHeight.current + -top + -spaces.spacingSm)).current;
- const opacityAnim = useRef(new Animated.Value(0)).current;
- const currentType = getSnackBarType({
- type
- });
- const {
- contentContainer: contentContainerDynamicStyle,
- containerObject: containerObjectDynamicStyle,
- iconContainer: iconContainerDynamicStyle,
- container: containerDynamicStyle,
- subTitle: subTitleDynamicStyle,
- action: actionDynamicStyle,
- title: titleDynamicStyle
- } = useStyles({
- isInlineSafeArea,
- safeAreaTop: top,
- spreadBehaviour,
- currentType,
- isFullWidth,
- radiuses,
- colors,
- spaces,
- type
- });
- useEffect(() => {
- if(isMeasured) {
- transformAnim.setValue(-contentHeight.current + -top + -spaces.spacingSm);
- Animated.parallel([
- Animated.timing(opacityAnim, {
- duration: openAnimationDelay,
- easing: Easing.linear,
- useNativeDriver: true,
- toValue: 1
- }),
- Animated.timing(transformAnim, {
- duration: openAnimationDelay,
- easing: Easing.linear,
- useNativeDriver: true,
- toValue: 0
- })
- ]).start();
- setTimeout(() => {
- closeAnimation();
- }, autoCloseDelay);
- }
- }, [
- isMeasured
- ]);
- const panResponder = useRef(
- PanResponder.create({
- onMoveShouldSetPanResponderCapture: (_, gestureState) => {
- const {
- dy
- } = gestureState;
- if(Math.abs(dy) < 20) {
- return false;
- }
- return true;
- },
- onPanResponderTerminationRequest: () => false,
- onPanResponderMove: (_, gestureState) => {
- const {
- moveY,
- y0,
- dy
- } = gestureState;
- if(moveY > y0) {
- return;
- }
- const op = moveY / y0;
- opacityAnim.setValue(op);
- transformAnim.setValue(dy);
- },
- onStartShouldSetPanResponder: () => false,
- onPanResponderEnd: (_, gestureState) => {
- const {
- moveY,
- y0,
- vy
- } = gestureState;
- if(vy < -1) {
- closeAnimation();
- return;
- }
- if(y0 - moveY > 25) {
- closeAnimation();
- return;
- }
- Animated.parallel([
- Animated.timing(opacityAnim, {
- duration: openAnimationDelay,
- easing: Easing.linear,
- useNativeDriver: true,
- toValue: 1
- }),
- Animated.timing(transformAnim, {
- duration: openAnimationDelay,
- easing: Easing.linear,
- useNativeDriver: true,
- toValue: 0
- })
- ]).start();
- },
- onShouldBlockNativeResponder: () => true
- })
- ).current;
- const closeAnimation = (_onClosed?: (props: {
- id: string;
- }) => void) => {
- Animated.parallel([
- Animated.timing(transformAnim, {
- toValue: -contentHeight.current + -top + -spaces.spacingSm,
- duration: closeAnimationDelay,
- useNativeDriver: true,
- easing: Easing.linear
- }),
- Animated.timing(opacityAnim, {
- duration: closeAnimationDelay,
- easing: Easing.linear,
- useNativeDriver: true,
- toValue: 0
- })
- ]).start(({
- finished
- }) => {
- if(finished) {
- if(onClosed) onClosed({
- id
- });
- if(_onClosed) _onClosed({
- id
- });
- }
- });
- };
- const renderIcon = () => {
- if(!CustomIcon) {
- return null;
- }
- return <View
- style={[
- stylesheet.iconContainer,
- iconContainerDynamicStyle
- ]}
- >
- <CustomIcon
- color="default"
- size={18}
- />
- </View>;
- };
- const renderContent = () => {
- return <View
- style={[
- contentContainerStyle,
- stylesheet.contentContainer,
- contentContainerDynamicStyle
- ]}
- >
- <Text
- style={{
- ...stylesheet.title,
- ...titleDynamicStyle
- }}
- numberOfLines={3}
- >
- {title}
- </Text>
- {
- subTitle ?
- <Text
- style={{
- ...stylesheet.subTitle,
- ...subTitleDynamicStyle
- }}
- variant="labelSmallSize"
- numberOfLines={2}
- color="low"
- >
- {subTitle}
- </Text>
- :
- null
- }
- </View>;
- };
- const renderAction = () => {
- if(!isShowAction) {
- return null;
- }
- return <Button
- icon={({
- color
- }) => {
- if(action?.title) {
- return null;
- }
- return <XIcon
- color={colors.content.icon[color ? color : "default"]}
- size={18}
- />;
- }}
- title={action && action.title ? action.title : undefined}
- onPress={() => {
- if(isCloseOnPressActionButton) closeAnimation();
- if(action && action.onPress) action.onPress({
- closeAnimation: ({
- onClosed: _onClosed
- }) => closeAnimation(_onClosed)
- });
- }}
- style={{
- ...action?.style,
- ...actionDynamicStyle
- }}
- isCustomPadding={true}
- spreadBehaviour="free"
- variant="ghost"
- type="neutral"
- size="small"
- />;
- };
- const renderContainer = () => {
- return <View
- style={[
- stylesheet.containerObject,
- containerObjectDynamicStyle
- ]}
- >
- {renderIcon()}
- {renderContent()}
- {renderAction()}
- </View>;
- };
- return <Portal
- name="snack-bar-system"
- >
- <Animated.View
- {...(Platform.OS === "web" ? {} : panResponder.panHandlers)}
- style={[
- style,
- stylesheet.container,
- containerDynamicStyle,
- {
- width: isFullWidth ? windowWidth : (spreadBehaviour === "stretch" ? windowWidth - (spaces.spacingMd * 2) : undefined),
- transform: [
- {
- translateY: transformAnim
- }
- ],
- opacity: opacityAnim
- }
- ]}
- onPointerDown={Platform.OS === "web" ? (evt) => {
- const nativeEvt = evt as unknown as PointerEvent;
- const startY = nativeEvt.clientY;
- let lastY = startY;
- let lastTime = Date.now();
- let velocityY = 0;
- const handlePointerMove = (moveEvt: SafePointerEvent) => {
- const dy = moveEvt.clientY - startY;
- if (dy > 0) return; // sadece yukarı swipe
- const now = Date.now();
- const dt = now - lastTime;
- if (dt > 0) velocityY = (moveEvt.clientY - lastY) / dt;
- lastY = moveEvt.clientY;
- lastTime = now;
- const op = Math.max(0, 1 + (dy / contentHeight.current));
- opacityAnim.setValue(op);
- transformAnim.setValue(dy);
- };
- const handlePointerUp = () => {
- safeGlobal.removeEventListener("pointermove", handlePointerMove);
- safeGlobal.removeEventListener("pointerup", handlePointerUp);
- const dy = lastY - startY;
- if (velocityY < -0.5 || dy < -25) {
- closeAnimation();
- return;
- }
- Animated.parallel([
- Animated.timing(opacityAnim, {
- duration: openAnimationDelay,
- easing: Easing.linear,
- useNativeDriver: true,
- toValue: 1
- }),
- Animated.timing(transformAnim, {
- duration: openAnimationDelay,
- easing: Easing.linear,
- useNativeDriver: true,
- toValue: 0
- })
- ]).start();
- };
- safeGlobal.addEventListener("pointermove", handlePointerMove);
- safeGlobal.addEventListener("pointerup", handlePointerUp);
- } : undefined}
- onLayout={(event) => {
- const _contentHeight = event.nativeEvent.layout.height;
- contentHeight.current = _contentHeight;
- setIsMeasured(true);
- }}
- >
- <TouchableWithoutFeedback
- onPress={() => {
- if(onPress) onPress({
- id
- });
- if(isCloseOnPress) {
- closeAnimation();
- }
- }}
- >
- {children ? children : renderContainer()}
- </TouchableWithoutFeedback>
- </Animated.View>
- </Portal>;
- };
- export default SnackBar;
|