1- import React , { useCallback } from 'react' ;
1+ import React , { useCallback , useState } from 'react' ;
22import {
33 Pressable ,
44 ScrollView ,
@@ -8,16 +8,21 @@ import {
88 View ,
99 useWindowDimensions ,
1010} from 'react-native' ;
11+ import { SafeAreaView } from 'react-native-safe-area-context' ;
1112import { ToastProvider , useToast } from 'react-native-rooster' ;
1213import type { ToastType } from 'react-native-rooster' ;
13- import { SafeAreaView } from 'react-native-safe-area-context' ;
1414
1515type ButtonConfig = {
1616 label : string ;
1717 type : ToastType ;
1818 persistent ?: boolean ;
1919} ;
2020
21+ type ToggleOption < T > = {
22+ label : string ;
23+ value : T ;
24+ } ;
25+
2126const BUTTONS : ButtonConfig [ ] = [
2227 { label : 'Success toast' , type : 'success' } ,
2328 { label : 'Warning toast' , type : 'warning' } ,
@@ -26,10 +31,42 @@ const BUTTONS: ButtonConfig[] = [
2631 { label : 'Persistent toast' , type : 'info' , persistent : true } ,
2732] ;
2833
34+ const VERTICAL_OPTIONS : ToggleOption < 'top' | 'bottom' > [ ] = [
35+ { label : 'Top' , value : 'top' } ,
36+ { label : 'Bottom' , value : 'bottom' } ,
37+ ] ;
38+
39+ const HORIZONTAL_OPTIONS : ToggleOption < 'left' | 'center' | 'right' > [ ] = [
40+ { label : 'Left' , value : 'left' } ,
41+ { label : 'Center' , value : 'center' } ,
42+ { label : 'Right' , value : 'right' } ,
43+ ] ;
44+
2945const ToastDemo : React . FC = ( ) => {
30- const { addToast } = useToast ( ) ;
46+ const { addToast, setToastConfig } = useToast ( ) ;
3147 const { width } = useWindowDimensions ( ) ;
3248 const isWide = width >= 768 ;
49+ const [ verticalPosition , setVerticalPosition ] = useState < 'top' | 'bottom' > (
50+ 'bottom'
51+ ) ;
52+ const [ horizontalPosition , setHorizontalPosition ] = useState <
53+ 'left' | 'center' | 'right'
54+ > ( 'center' ) ;
55+
56+ const updatePosition = useCallback (
57+ (
58+ nextVertical : 'top' | 'bottom' ,
59+ nextHorizontal : 'left' | 'center' | 'right'
60+ ) => {
61+ setToastConfig ( {
62+ position : {
63+ vertical : nextVertical ,
64+ horizontal : nextHorizontal ,
65+ } ,
66+ } ) ;
67+ } ,
68+ [ setToastConfig ]
69+ ) ;
3370
3471 const handlePress = useCallback (
3572 ( config : ButtonConfig ) => {
@@ -52,6 +89,22 @@ const ToastDemo: React.FC = () => {
5289 [ addToast ]
5390 ) ;
5491
92+ const handleVerticalChange = useCallback (
93+ ( option : ToggleOption < 'top' | 'bottom' > ) => {
94+ setVerticalPosition ( option . value ) ;
95+ updatePosition ( option . value , horizontalPosition ) ;
96+ } ,
97+ [ horizontalPosition , updatePosition ]
98+ ) ;
99+
100+ const handleHorizontalChange = useCallback (
101+ ( option : ToggleOption < 'left' | 'center' | 'right' > ) => {
102+ setHorizontalPosition ( option . value ) ;
103+ updatePosition ( verticalPosition , option . value ) ;
104+ } ,
105+ [ updatePosition , verticalPosition ]
106+ ) ;
107+
55108 return (
56109 < SafeAreaView style = { styles . screen } >
57110 < StatusBar barStyle = "light-content" />
@@ -76,6 +129,44 @@ const ToastDemo: React.FC = () => {
76129 Trigger different variants to see Rooster in action.
77130 </ Text >
78131
132+ < View style = { styles . settingsGroup } >
133+ < Text style = { styles . settingsLabel } > Vertical placement</ Text >
134+ < View style = { styles . toggleRow } >
135+ { VERTICAL_OPTIONS . map ( ( option ) => (
136+ < Pressable
137+ key = { option . value }
138+ accessibilityRole = "button"
139+ style = { ( { pressed } ) => [
140+ styles . toggle ,
141+ option . value === verticalPosition && styles . toggleActive ,
142+ pressed && styles . togglePressed ,
143+ ] }
144+ onPress = { ( ) => handleVerticalChange ( option ) }
145+ >
146+ < Text style = { styles . toggleText } > { option . label } </ Text >
147+ </ Pressable >
148+ ) ) }
149+ </ View >
150+
151+ < Text style = { styles . settingsLabel } > Horizontal alignment</ Text >
152+ < View style = { styles . toggleRow } >
153+ { HORIZONTAL_OPTIONS . map ( ( option ) => (
154+ < Pressable
155+ key = { option . value }
156+ accessibilityRole = "button"
157+ style = { ( { pressed } ) => [
158+ styles . toggle ,
159+ option . value === horizontalPosition && styles . toggleActive ,
160+ pressed && styles . togglePressed ,
161+ ] }
162+ onPress = { ( ) => handleHorizontalChange ( option ) }
163+ >
164+ < Text style = { styles . toggleText } > { option . label } </ Text >
165+ </ Pressable >
166+ ) ) }
167+ </ View >
168+ </ View >
169+
79170 < View style = { [ styles . buttonGroup , isWide && styles . buttonGroupWide ] } >
80171 { BUTTONS . map ( ( button ) => (
81172 < Pressable
@@ -108,14 +199,6 @@ const ToastDemo: React.FC = () => {
108199 ) ;
109200} ;
110201
111- export default function App ( ) {
112- return (
113- < ToastProvider >
114- < ToastDemo />
115- </ ToastProvider >
116- ) ;
117- }
118-
119202const styles = StyleSheet . create ( {
120203 screen : {
121204 flex : 1 ,
@@ -186,6 +269,41 @@ const styles = StyleSheet.create({
186269 fontSize : 15 ,
187270 textAlign : 'center' ,
188271 } ,
272+ settingsGroup : {
273+ gap : 12 ,
274+ } ,
275+ settingsLabel : {
276+ color : '#d1d5db' ,
277+ fontSize : 14 ,
278+ fontWeight : '600' ,
279+ } ,
280+ toggleRow : {
281+ flexDirection : 'row' ,
282+ gap : 8 ,
283+ } ,
284+ toggle : {
285+ flex : 1 ,
286+ borderRadius : 999 ,
287+ borderWidth : StyleSheet . hairlineWidth ,
288+ borderColor : 'rgba(255,255,255,0.08)' ,
289+ paddingVertical : 10 ,
290+ alignItems : 'center' ,
291+ backgroundColor : '#202020' ,
292+ } ,
293+ toggleActive : {
294+ backgroundColor : '#4338ca' ,
295+ borderColor : '#6366f1' ,
296+ } ,
297+ togglePressed : {
298+ opacity : 0.85 ,
299+ } ,
300+ toggleText : {
301+ color : '#f9fafb' ,
302+ fontSize : 13 ,
303+ fontWeight : '600' ,
304+ textTransform : 'uppercase' ,
305+ letterSpacing : 0.5 ,
306+ } ,
189307 buttonGroup : {
190308 gap : 12 ,
191309 } ,
@@ -240,3 +358,11 @@ const styles = StyleSheet.create({
240358 textAlign : 'center' ,
241359 } ,
242360} ) ;
361+
362+ const App : React . FC = ( ) => (
363+ < ToastProvider >
364+ < ToastDemo />
365+ </ ToastProvider >
366+ ) ;
367+
368+ export default App ;
0 commit comments