Skip to content

Commit

Permalink
* fix issues with justifing elements (notifiaction is not centered)
Browse files Browse the repository at this point in the history
  • Loading branch information
PdoubleU committed Feb 9, 2024
1 parent 09ab8de commit 8e85eed
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
26 changes: 9 additions & 17 deletions src/core/renderers/GestureHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { NotificationState } from '../hooks/useNotificationsStates'
import Animated from 'react-native-reanimated'
import { styles } from '../utils/styles'
import { Constants } from '../config'
import { calculateNotificationPositionCoordintates } from '../utils/calculateNotificationPositionCoordintates'

type Props = {
children: ReactNode
Expand All @@ -30,22 +31,13 @@ export const GestureHandler = ({
}: Props) => {
const { width } = useWindowDimensions()

const fullWidth = width - Constants.notificationSideMargin * 2

const initialNotificationWidth = state?.notificationWidth || Constants.maxNotificationWidth

const isWidthWithinBounds = initialNotificationWidth <= fullWidth

const notificationWidth = isWidthWithinBounds ? initialNotificationWidth : fullWidth

const top =
notificationTopPosition || notificationTopPosition === 0
? notificationTopPosition
: state.notificationOffset.top

const left = state.notificationOffset.left

const right = state.notificationOffset.right
const { calculatedNotificationWidth, top, left, right } =
calculateNotificationPositionCoordintates({
screenWidth: width,
notificationOffset: state.notificationOffset,
notificationWidth: state.notificationWidth,
notificationTopPosition,
})

return (
<PanGestureHandler
Expand All @@ -63,7 +55,7 @@ export const GestureHandler = ({
top,
left,
right,
width: notificationWidth,
width: calculatedNotificationWidth,
},
]}>
{children}
Expand Down
50 changes: 50 additions & 0 deletions src/core/utils/calculateNotificationPositionCoordintates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Constants } from '../config'

type CalculateNotificationPositionCoordintatesProps = {
screenWidth: number
notificationOffset: {
top: number
left?: number
right?: number
}
notificationWidth?: number
notificationTopPosition?: number
}

type CalculateNotificationPositionCoordintatesReturn = {
calculatedNotificationWidth: number
top: number
left?: number
right?: number
}

export const calculateNotificationPositionCoordintates = ({
screenWidth,
notificationOffset,
notificationWidth,
notificationTopPosition,
}: CalculateNotificationPositionCoordintatesProps): CalculateNotificationPositionCoordintatesReturn => {
const fullWidth = screenWidth - Constants.notificationSideMargin * 2

const initialNotificationWidth = notificationWidth || Constants.maxNotificationWidth

const isWidthWithinBounds = initialNotificationWidth <= fullWidth

const calculatedNotificationWidth = isWidthWithinBounds ? initialNotificationWidth : fullWidth

const top =
notificationTopPosition || notificationTopPosition === 0
? notificationTopPosition
: notificationOffset.top

const left = notificationOffset.left

const right = notificationOffset.right

return {
calculatedNotificationWidth,
top,
left,
right,
}
}
9 changes: 5 additions & 4 deletions src/core/utils/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ export const styles = StyleSheet.create({
top: 0,
zIndex: 200,
justifyContent: 'flex-start',
alignSelf: 'center',
},
containerIos: {
left: `auto`,
right: `auto`,
left: 'auto',
right: 'auto',
top: 50,
},
containerAndroid: {
left: `auto`,
right: `auto`,
left: 'auto',
right: 'auto',
top: 30,
},
boxWrapper: {
Expand Down

0 comments on commit 8e85eed

Please sign in to comment.