Skip to content

Commit

Permalink
fix: notification component
Browse files Browse the repository at this point in the history
  • Loading branch information
avrcoelho committed Oct 17, 2022
1 parent f195d50 commit 6c7450e
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-hook-notification",
"version": "2.2.8",
"version": "2.2.9",
"license": "MIT",
"main": "dist/main/index.js",
"typings": "dist/main/index.d.ts",
Expand Down
54 changes: 27 additions & 27 deletions src/presentation/components/Notification/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,6 @@ export const Notification = ({
</View>
)}

{showButtonClose && (
<TouchableOpacity
onPress={onRemove}
style={[
styles.buttonClose,
styles[buttonCloseStyleIndex] || customStyle.button,
]}
hitSlop={{
bottom: 5,
top: 5,
left: 5,
right: 5,
}}
accessibilityLabel="Close notification"
activeOpacity={0.5}
>
<Text
style={[
styles.buttonCloseText,
styles[buttonCloseTextStyleIndex] || customStyle.buttonText,
]}
>
&#x2715;
</Text>
</TouchableOpacity>
)}

<View style={styles.textContainer}>
{!!title && (
<Text
Expand All @@ -145,6 +118,33 @@ export const Notification = ({
</View>
</>
</TouchableWithoutFeedback>

{showButtonClose && (
<TouchableOpacity
onPress={onRemove}
style={[
styles.buttonClose,
styles[buttonCloseStyleIndex] || customStyle.button,
]}
hitSlop={{
bottom: 5,
top: 5,
left: 5,
right: 5,
}}
accessibilityLabel="Close notification"
activeOpacity={0.5}
>
<Text
style={[
styles.buttonCloseText,
styles[buttonCloseTextStyleIndex] || customStyle.buttonText,
]}
>
&#x2715;
</Text>
</TouchableOpacity>
)}
</Animated.View>
</PanGestureHandler>
);
Expand Down
1 change: 1 addition & 0 deletions src/presentation/components/Notification/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const styles = StyleSheet.create({

buttonCloseText: {
fontSize: 14,
lineHeight: 16,
},

buttonCloseTextcolored: { color: Colors.Grey },
Expand Down
102 changes: 47 additions & 55 deletions src/presentation/components/Notification/useController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { getAnimation } from '../../utils/getAnimation';
import { useNotificationWidth } from '../../hooks/useNotificationWidth';
import { useLimitToRemove } from '../../hooks/useLimitToRemove';
import { useIsMounted } from '../../hooks/useIsMounted';

type UseControllerHookProps = {
dragDirection: NotificationDragDirection;
Expand Down Expand Up @@ -88,67 +89,65 @@ export const useController: UseControllerHook = ({
const positionOnScreen = useSharedValue(0);
const [isPaused, toggleIsPaused] = useToggle(false);

const onTogglePause = useCallback((): void => {
'worklet';
const isMounted = useIsMounted();

if (pauseOnPress) {
runOnJS(toggleIsPaused)();
const onVerifyCanExecuteTogglePause = (): void => {
const canExecute = pauseOnPress && isMounted();
if (canExecute) {
toggleIsPaused();
}
}, [pauseOnPress, toggleIsPaused]);
};

const onTogglePause = (): void => {
'worklet';

runOnJS(onVerifyCanExecuteTogglePause)();
};

const width = useNotificationWidth();
const { limitToRemove, onGetNotificationHeight } = useLimitToRemove({
dragDirection,
width,
});
const onDirectionXRemover = useCallback(
(pos: number): void => {
'worklet';
const onDirectionXRemover = (pos: number): void => {
'worklet';

if (
dragDirection === 'x' &&
(pos > limitToRemove || pos < -limitToRemove)
) {
runOnJS(onRemove)();
}
},
[dragDirection, limitToRemove, onRemove],
);
if (
dragDirection === 'x' &&
(pos > limitToRemove || pos < -limitToRemove)
) {
runOnJS(onRemove)();
}
};

const onDirectionYRemover = useCallback(
(pos: number): void => {
'worklet';
const onDirectionYRemover = (pos: number): void => {
'worklet';

if (
dragDirection === 'y' &&
(pos > limitToRemove || pos < -limitToRemove)
) {
runOnJS(onRemove)();
}
},
[dragDirection, limitToRemove, onRemove],
);
if (
dragDirection === 'y' &&
(pos > limitToRemove || pos < -limitToRemove)
) {
runOnJS(onRemove)();
}
};

const onCanUpdatePosition = useCallback(
(pos: number): boolean => {
'worklet';
const onCanUpdatePosition = (pos: number): boolean => {
'worklet';

if (!draggable) {
return false;
}
if (dragDirection === 'x') {
return true;
}
if (/top/.test(position) && pos > 0) {
return false;
}
if (/bottom/.test(position) && pos < 0) {
return false;
}
if (!draggable) {
return false;
}
if (dragDirection === 'x') {
return true;
},
[dragDirection, position, draggable],
);
}
if (/top/.test(position) && pos > 0) {
return false;
}
if (/bottom/.test(position) && pos < 0) {
return false;
}
return true;
};

const canTogglePause = pauseOnPress && autoClose;
const contextIndex = `position${transitionDirection}` as 'positionX';
Expand Down Expand Up @@ -180,14 +179,7 @@ export const useController: UseControllerHook = ({
positionOnScreen.value = withTiming(0);
},
},
[
onDirectionXRemover,
onCanUpdatePosition,
onDirectionYRemover,
onCanUpdatePosition,
onTogglePause,
canTogglePause,
],
[onTogglePause, canTogglePause],
);

const [animationEnteringFinish, toggleAnimationEnteringFinish] =
Expand Down
19 changes: 19 additions & 0 deletions src/presentation/hooks/__tests__/useIsMounted.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { renderHook } from '@testing-library/react-hooks';

import { useIsMounted } from '../useIsMounted';

describe('useIsMounted hook', () => {
it('should be able to return mounted value', async () => {
const { result, rerender } = renderHook(useIsMounted);
rerender();

expect(result.current()).toBeTruthy();
});

it('should be able to return unmounted value', async () => {
const { result, unmount } = renderHook(useIsMounted);
unmount();

expect(result.current()).toBeFalsy();
});
});
15 changes: 15 additions & 0 deletions src/presentation/hooks/useIsMounted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useCallback, useEffect, useRef } from 'react';

export const useIsMounted = (): (() => boolean) => {
const isMount = useRef(false);

useEffect(() => {
isMount.current = true;

return () => {
isMount.current = false;
};
});

return useCallback((): boolean => isMount.current, []);
};
2 changes: 1 addition & 1 deletion storybook/stories/Notification/Notification.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const App = (): JSX.Element => {
</TouchableOpacity>
</View>

<NotificationContainer theme="light" closeOnPress />
<NotificationContainer theme="light" closeOnPress showButtonClose />
</GestureHandlerRootView>
);
};
Expand Down

0 comments on commit 6c7450e

Please sign in to comment.