Skip to content

Commit

Permalink
feat: Offset from the X axis of the screen (in px)
Browse files Browse the repository at this point in the history
  • Loading branch information
avrcoelho committed May 10, 2022
1 parent dee099f commit 1993486
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default function Component() {
| icon | React.FunctionComponentElement | no | | Render icon on left side. Obs.: Dimensions: 24x24 |
| titleMaxLines | number | no | 1 | Maximum number of lines for title |
| textMaxLines | number | no | 2 | Maximum number of lines for text |
| xOffset | number | no | 16 | Offset from the X axis of the screen (in px) |
| customStyle | `{ container?: StyleProp<ViewStyle>; title?: StyleProp<TextStyle>; text?: StyleProp<TextStyle>; icon?: StyleProp<ViewStyle>; button?: StyleProp<ViewStyle>;buttonText?: StyleProp<TextStyle>;}` | no | | Styles for custom notificaion type |

## License
Expand Down
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.0.0",
"version": "2.1.0",
"license": "MIT",
"main": "dist/main/index.js",
"typings": "dist/main/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { NotificationType } from '../../../types/Notification';
import { Notification } from '..';
import { Colors } from '../../../constants/Colors';

let mockIsPortrait = false;
let mockTypeAndTheme = 'defaultcolored';
jest.mock('../useController', () => ({
useController: () => ({
Expand All @@ -23,7 +22,6 @@ jest.mock('../useController', () => ({
onFinishAnimation: jest.fn(),
onGetNotificationHeight: jest.fn(),
onPressNotification: jest.fn(),
isPortrait: mockIsPortrait,
}),
}));

Expand Down Expand Up @@ -60,8 +58,8 @@ describe('Notification component', () => {
title: 'title test',
draggable: undefined,
closeOnPress: true,
xOffset: 10,
});
mockIsPortrait = true;
const { getByText } = render(<Notification {...props} />);

expect(getByText('title test')).toBeTruthy();
Expand All @@ -73,7 +71,6 @@ describe('Notification component', () => {
icon: <Icon />,
showButtonClose: true,
});
mockIsPortrait = true;
const { getByText } = render(<Notification {...props} />);

expect(getByText('IC')).toBeTruthy();
Expand Down Expand Up @@ -106,7 +103,6 @@ describe('Notification component', () => {
},
},
});
mockIsPortrait = true;
const { getByText } = render(<Notification {...props} />);

expect(getByText('title test').props.style[1]).toEqual(
Expand Down
4 changes: 2 additions & 2 deletions src/presentation/components/Notification/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const Notification = ({
titleMaxLines = notificationDefaultProps.titleMaxLines,
closeOnPress = notificationDefaultProps.closeOnPress,
textMaxLines = notificationDefaultProps.textMaxLines,
xOffset = notificationDefaultProps.xOffset,
}: NotificationProps): JSX.Element => {
const {
animatedStyle,
Expand All @@ -41,7 +42,6 @@ export const Notification = ({
onFinishAnimation,
onGetNotificationHeight,
onPressNotification,
isPortrait,
width,
} = useController({
dragDirection,
Expand Down Expand Up @@ -77,7 +77,7 @@ export const Notification = ({
{ width },
styles[typeAndTheme] || customStyle.container,
animatedStyle,
getPositionStyles(isPortrait)[position],
getPositionStyles({ xOffset })[position],
]}
>
<TouchableWithoutFeedback
Expand Down
32 changes: 18 additions & 14 deletions src/presentation/components/Notification/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const themeDark = {
backgroundColor: Colors.Black,
};

const topPosisiton = 10;
const yPosisiton = 10;

export const styles = StyleSheet.create({
container: {
Expand Down Expand Up @@ -127,31 +127,35 @@ export const styles = StyleSheet.create({
buttonCloseTextdark: { color: Colors.Black },
});

export const getPositionStyles = (
isPortrait: boolean,
): Record<NotificationPosition, ViewStyle> => ({
type GetPositionStylesProps = {
xOffset: number;
};

export const getPositionStyles = ({
xOffset,
}: GetPositionStylesProps): Record<NotificationPosition, ViewStyle> => ({
'top-right': {
top: isPortrait ? topPosisiton : 16,
right: isPortrait ? 10 : topPosisiton,
top: xOffset,
right: yPosisiton,
},
'top-center': {
top: isPortrait ? topPosisiton : 16,
top: xOffset,
alignSelf: 'center',
},
'top-left': {
top: isPortrait ? topPosisiton : 16,
left: isPortrait ? 10 : topPosisiton,
top: xOffset,
left: yPosisiton,
},
'bottom-right': {
bottom: 24,
right: isPortrait ? 10 : topPosisiton,
bottom: xOffset,
right: yPosisiton,
},
'bottom-center': {
bottom: 24,
bottom: xOffset,
alignSelf: 'center',
},
'bottom-left': {
bottom: 24,
left: isPortrait ? 10 : topPosisiton,
bottom: xOffset,
left: yPosisiton,
},
});
4 changes: 0 additions & 4 deletions src/presentation/components/Notification/useController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {

import { LayoutChangeEvent } from 'react-native';
import { useToggle } from '../../hooks/useToggle';
import { useOrientation } from '../../hooks/useOrientation';
import { AnimationReturn } from '../../types/Animation';
import {
NotificationDragDirection,
Expand Down Expand Up @@ -62,7 +61,6 @@ type UseControllerHook = (props: UseControllerHookProps) => {
| object;
animation: AnimationReturn;
isPaused: boolean;
isPortrait: boolean;
width: number;
onFinishAnimation(value: boolean): void;
onGetNotificationHeight(event: LayoutChangeEvent): void;
Expand Down Expand Up @@ -249,7 +247,6 @@ export const useController: UseControllerHook = ({
}, [closeOnPress, onPress, onRemove]);

const animation = getAnimation({ position, transition });
const isPortrait = useOrientation() === 'portrait';

return {
animatedStyle,
Expand All @@ -259,7 +256,6 @@ export const useController: UseControllerHook = ({
onGetNotificationHeight,
animation,
isPaused,
isPortrait,
width,
onPressNotification,
};
Expand Down
1 change: 1 addition & 0 deletions src/presentation/constants/notificationDefaultProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const notificationDefaultProps = {
draggable: true,
titleMaxLines: 1,
textMaxLines: 2,
xOffset: 16,
customStyle: {
container: {},
title: {},
Expand Down
5 changes: 4 additions & 1 deletion src/presentation/types/Notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ export interface NotificationProps {
* Maximum number of lines for notification title. (Default: 1)
*/
titleMaxLines?: number;

/**
* Maximum number of lines for notification text. (Default: 2)
*/
textMaxLines?: number;
/**
* Offset from the X axis of the screen (in px). (Default: 16)
*/
xOffset?: number;
/**
* Called on Notification press
*/
Expand Down

0 comments on commit 1993486

Please sign in to comment.