1- import React , { useCallback , useState } from 'react' ;
2- import { Modal , SafeAreaView } from 'react-native' ;
1+ import React , { PropsWithChildren , useCallback , useState } from 'react' ;
2+ import { Modal , SafeAreaView as RNSafeAreaView , ViewStyle } from 'react-native' ;
3+ import {
4+ SafeAreaProvider ,
5+ SafeAreaView as SafeAreaViewOriginal ,
6+ } from 'react-native-safe-area-context' ;
37
48import { GenericPollButton , PollButtonProps } from './Button' ;
59import { PollAnswersList } from './PollAnswersList' ;
@@ -9,8 +13,23 @@ import { PollAllOptions } from './PollOption';
913import { PollResults } from './PollResults' ;
1014
1115import { useChatContext , usePollContext , useTheme , useTranslationContext } from '../../../contexts' ;
16+ import { getReactNativeVersion } from '../../../utils/getReactNativeVersion' ;
1217import { usePollState } from '../hooks/usePollState' ;
1318
19+ // This is a workaround to support SafeAreaView on React Native 0.81.0+
20+ const SafeAreaViewWrapper = ( { children, style } : PropsWithChildren < { style : ViewStyle } > ) => {
21+ if ( getReactNativeVersion ( ) . minor >= 81 ) {
22+ return (
23+ < SafeAreaProvider >
24+ < SafeAreaViewOriginal edges = { [ 'bottom' , 'top' ] } style = { style } >
25+ { children }
26+ </ SafeAreaViewOriginal >
27+ </ SafeAreaProvider >
28+ ) ;
29+ }
30+ return < RNSafeAreaView style = { style } > { children } </ RNSafeAreaView > ;
31+ } ;
32+
1433export const ViewResultsButton = ( props : PollButtonProps ) => {
1534 const { t } = useTranslationContext ( ) ;
1635 const { message, poll } = usePollContext ( ) ;
@@ -32,19 +51,21 @@ export const ViewResultsButton = (props: PollButtonProps) => {
3251 } ,
3352 } = useTheme ( ) ;
3453
54+ const onRequestClose = useCallback ( ( ) => {
55+ setShowResults ( false ) ;
56+ } , [ ] ) ;
57+
3558 return (
3659 < >
3760 < GenericPollButton onPress = { onPressHandler } title = { t ( 'View Results' ) } />
3861 { showResults ? (
39- < Modal
40- animationType = 'slide'
41- onRequestClose = { ( ) => setShowResults ( false ) }
42- visible = { showResults }
43- >
44- < SafeAreaView style = { { backgroundColor : white , flex : 1 } } >
45- < PollModalHeader onPress = { ( ) => setShowResults ( false ) } title = { t ( 'Poll Results' ) } />
46- < PollResults message = { message } poll = { poll } />
47- </ SafeAreaView >
62+ < Modal animationType = 'slide' onRequestClose = { onRequestClose } visible = { showResults } >
63+ < SafeAreaProvider >
64+ < SafeAreaViewWrapper style = { { backgroundColor : white , flex : 1 } } >
65+ < PollModalHeader onPress = { onRequestClose } title = { t ( 'Poll Results' ) } />
66+ < PollResults message = { message } poll = { poll } />
67+ </ SafeAreaViewWrapper >
68+ </ SafeAreaProvider >
4869 </ Modal >
4970 ) : null }
5071 </ >
@@ -67,6 +88,10 @@ export const ShowAllOptionsButton = (props: PollButtonProps) => {
6788 setShowAllOptions ( true ) ;
6889 } , [ message , onPress , poll ] ) ;
6990
91+ const onRequestClose = useCallback ( ( ) => {
92+ setShowAllOptions ( false ) ;
93+ } , [ ] ) ;
94+
7095 const {
7196 theme : {
7297 colors : { white } ,
@@ -82,15 +107,13 @@ export const ShowAllOptionsButton = (props: PollButtonProps) => {
82107 />
83108 ) : null }
84109 { showAllOptions ? (
85- < Modal
86- animationType = 'slide'
87- onRequestClose = { ( ) => setShowAllOptions ( false ) }
88- visible = { showAllOptions }
89- >
90- < SafeAreaView style = { { backgroundColor : white , flex : 1 } } >
91- < PollModalHeader onPress = { ( ) => setShowAllOptions ( false ) } title = { t ( 'Poll Options' ) } />
92- < PollAllOptions message = { message } poll = { poll } />
93- </ SafeAreaView >
110+ < Modal animationType = 'slide' onRequestClose = { onRequestClose } visible = { showAllOptions } >
111+ < SafeAreaProvider >
112+ < SafeAreaViewWrapper style = { { backgroundColor : white , flex : 1 } } >
113+ < PollModalHeader onPress = { onRequestClose } title = { t ( 'Poll Options' ) } />
114+ < PollAllOptions message = { message } poll = { poll } />
115+ </ SafeAreaViewWrapper >
116+ </ SafeAreaProvider >
94117 </ Modal >
95118 ) : null }
96119 </ >
@@ -119,6 +142,10 @@ export const ShowAllCommentsButton = (props: PollButtonProps) => {
119142 } ,
120143 } = useTheme ( ) ;
121144
145+ const onRequestClose = useCallback ( ( ) => {
146+ setShowAnswers ( false ) ;
147+ } , [ ] ) ;
148+
122149 return (
123150 < >
124151 { answersCount && answersCount > 0 ? (
@@ -128,15 +155,13 @@ export const ShowAllCommentsButton = (props: PollButtonProps) => {
128155 />
129156 ) : null }
130157 { showAnswers ? (
131- < Modal
132- animationType = 'slide'
133- onRequestClose = { ( ) => setShowAnswers ( false ) }
134- visible = { showAnswers }
135- >
136- < SafeAreaView style = { { backgroundColor : white , flex : 1 } } >
137- < PollModalHeader onPress = { ( ) => setShowAnswers ( false ) } title = { t ( 'Poll Comments' ) } />
138- < PollAnswersList message = { message } poll = { poll } />
139- </ SafeAreaView >
158+ < Modal animationType = 'slide' onRequestClose = { onRequestClose } visible = { showAnswers } >
159+ < SafeAreaProvider >
160+ < SafeAreaViewWrapper style = { { backgroundColor : white , flex : 1 } } >
161+ < PollModalHeader onPress = { onRequestClose } title = { t ( 'Poll Comments' ) } />
162+ < PollAnswersList message = { message } poll = { poll } />
163+ </ SafeAreaViewWrapper >
164+ </ SafeAreaProvider >
140165 </ Modal >
141166 ) : null }
142167 </ >
@@ -159,14 +184,18 @@ export const SuggestOptionButton = (props: PollButtonProps) => {
159184 setShowAddOptionDialog ( true ) ;
160185 } , [ message , onPress , poll ] ) ;
161186
187+ const onRequestClose = useCallback ( ( ) => {
188+ setShowAddOptionDialog ( false ) ;
189+ } , [ ] ) ;
190+
162191 return (
163192 < >
164193 { ! isClosed && allowUserSuggestedOptions ? (
165194 < GenericPollButton onPress = { onPressHandler } title = { t ( 'Suggest an option' ) } />
166195 ) : null }
167196 { showAddOptionDialog ? (
168197 < PollInputDialog
169- closeDialog = { ( ) => setShowAddOptionDialog ( false ) }
198+ closeDialog = { onRequestClose }
170199 onSubmit = { addOption }
171200 title = { t ( 'Suggest an option' ) }
172201 visible = { showAddOptionDialog }
@@ -192,14 +221,18 @@ export const AddCommentButton = (props: PollButtonProps) => {
192221 setShowAddCommentDialog ( true ) ;
193222 } , [ message , onPress , poll ] ) ;
194223
224+ const onRequestClose = useCallback ( ( ) => {
225+ setShowAddCommentDialog ( false ) ;
226+ } , [ ] ) ;
227+
195228 return (
196229 < >
197230 { ! isClosed && allowAnswers ? (
198231 < GenericPollButton onPress = { onPressHandler } title = { t ( 'Add a comment' ) } />
199232 ) : null }
200233 { showAddCommentDialog ? (
201234 < PollInputDialog
202- closeDialog = { ( ) => setShowAddCommentDialog ( false ) }
235+ closeDialog = { onRequestClose }
203236 initialValue = { ownAnswer ?. answer_text ?? '' }
204237 onSubmit = { addComment }
205238 title = { t ( 'Add a comment' ) }
0 commit comments