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' ;
@@ -11,6 +15,20 @@ import { PollResults } from './PollResults';
1115import { useChatContext , usePollContext , useTheme , useTranslationContext } from '../../../contexts' ;
1216import { usePollState } from '../hooks/usePollState' ;
1317
18+ // This is a workaround to support SafeAreaView on React Native 0.81.0+
19+ const SafeAreaViewWrapper = ( { children, style } : PropsWithChildren < { style : ViewStyle } > ) => {
20+ if ( SafeAreaViewOriginal ) {
21+ return (
22+ < SafeAreaProvider >
23+ < SafeAreaViewOriginal edges = { [ 'bottom' , 'top' ] } style = { style } >
24+ { children }
25+ </ SafeAreaViewOriginal >
26+ </ SafeAreaProvider >
27+ ) ;
28+ }
29+ return < RNSafeAreaView style = { style } > { children } </ RNSafeAreaView > ;
30+ } ;
31+
1432export const ViewResultsButton = ( props : PollButtonProps ) => {
1533 const { t } = useTranslationContext ( ) ;
1634 const { message, poll } = usePollContext ( ) ;
@@ -32,19 +50,21 @@ export const ViewResultsButton = (props: PollButtonProps) => {
3250 } ,
3351 } = useTheme ( ) ;
3452
53+ const onRequestClose = useCallback ( ( ) => {
54+ setShowResults ( false ) ;
55+ } , [ ] ) ;
56+
3557 return (
3658 < >
3759 < GenericPollButton onPress = { onPressHandler } title = { t ( 'View Results' ) } />
3860 { 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 >
61+ < Modal animationType = 'slide' onRequestClose = { onRequestClose } visible = { showResults } >
62+ < SafeAreaProvider >
63+ < SafeAreaViewWrapper style = { { backgroundColor : white , flex : 1 } } >
64+ < PollModalHeader onPress = { onRequestClose } title = { t ( 'Poll Results' ) } />
65+ < PollResults message = { message } poll = { poll } />
66+ </ SafeAreaViewWrapper >
67+ </ SafeAreaProvider >
4868 </ Modal >
4969 ) : null }
5070 </ >
@@ -67,6 +87,10 @@ export const ShowAllOptionsButton = (props: PollButtonProps) => {
6787 setShowAllOptions ( true ) ;
6888 } , [ message , onPress , poll ] ) ;
6989
90+ const onRequestClose = useCallback ( ( ) => {
91+ setShowAllOptions ( false ) ;
92+ } , [ ] ) ;
93+
7094 const {
7195 theme : {
7296 colors : { white } ,
@@ -82,15 +106,13 @@ export const ShowAllOptionsButton = (props: PollButtonProps) => {
82106 />
83107 ) : null }
84108 { 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 >
109+ < Modal animationType = 'slide' onRequestClose = { onRequestClose } visible = { showAllOptions } >
110+ < SafeAreaProvider >
111+ < SafeAreaViewWrapper style = { { backgroundColor : white , flex : 1 } } >
112+ < PollModalHeader onPress = { onRequestClose } title = { t ( 'Poll Options' ) } />
113+ < PollAllOptions message = { message } poll = { poll } />
114+ </ SafeAreaViewWrapper >
115+ </ SafeAreaProvider >
94116 </ Modal >
95117 ) : null }
96118 </ >
@@ -119,6 +141,10 @@ export const ShowAllCommentsButton = (props: PollButtonProps) => {
119141 } ,
120142 } = useTheme ( ) ;
121143
144+ const onRequestClose = useCallback ( ( ) => {
145+ setShowAnswers ( false ) ;
146+ } , [ ] ) ;
147+
122148 return (
123149 < >
124150 { answersCount && answersCount > 0 ? (
@@ -128,15 +154,13 @@ export const ShowAllCommentsButton = (props: PollButtonProps) => {
128154 />
129155 ) : null }
130156 { 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 >
157+ < Modal animationType = 'slide' onRequestClose = { onRequestClose } visible = { showAnswers } >
158+ < SafeAreaProvider >
159+ < SafeAreaViewWrapper style = { { backgroundColor : white , flex : 1 } } >
160+ < PollModalHeader onPress = { onRequestClose } title = { t ( 'Poll Comments' ) } />
161+ < PollAnswersList message = { message } poll = { poll } />
162+ </ SafeAreaViewWrapper >
163+ </ SafeAreaProvider >
140164 </ Modal >
141165 ) : null }
142166 </ >
@@ -159,14 +183,18 @@ export const SuggestOptionButton = (props: PollButtonProps) => {
159183 setShowAddOptionDialog ( true ) ;
160184 } , [ message , onPress , poll ] ) ;
161185
186+ const onRequestClose = useCallback ( ( ) => {
187+ setShowAddOptionDialog ( false ) ;
188+ } , [ ] ) ;
189+
162190 return (
163191 < >
164192 { ! isClosed && allowUserSuggestedOptions ? (
165193 < GenericPollButton onPress = { onPressHandler } title = { t ( 'Suggest an option' ) } />
166194 ) : null }
167195 { showAddOptionDialog ? (
168196 < PollInputDialog
169- closeDialog = { ( ) => setShowAddOptionDialog ( false ) }
197+ closeDialog = { onRequestClose }
170198 onSubmit = { addOption }
171199 title = { t ( 'Suggest an option' ) }
172200 visible = { showAddOptionDialog }
@@ -192,14 +220,18 @@ export const AddCommentButton = (props: PollButtonProps) => {
192220 setShowAddCommentDialog ( true ) ;
193221 } , [ message , onPress , poll ] ) ;
194222
223+ const onRequestClose = useCallback ( ( ) => {
224+ setShowAddCommentDialog ( false ) ;
225+ } , [ ] ) ;
226+
195227 return (
196228 < >
197229 { ! isClosed && allowAnswers ? (
198230 < GenericPollButton onPress = { onPressHandler } title = { t ( 'Add a comment' ) } />
199231 ) : null }
200232 { showAddCommentDialog ? (
201233 < PollInputDialog
202- closeDialog = { ( ) => setShowAddCommentDialog ( false ) }
234+ closeDialog = { onRequestClose }
203235 initialValue = { ownAnswer ?. answer_text ?? '' }
204236 onSubmit = { addComment }
205237 title = { t ( 'Add a comment' ) }
0 commit comments