parent
fb3f632c61
commit
9695854b49
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,22 @@ |
|||||||
|
import { View, ViewProps } from 'react-native'; |
||||||
|
import React from 'react'; |
||||||
|
import { centerStyle } from './styles'; |
||||||
|
import type { VariantProps } from '@gluestack-ui/utils/nativewind-utils'; |
||||||
|
|
||||||
|
type ICenterProps = ViewProps & VariantProps<typeof centerStyle>; |
||||||
|
|
||||||
|
const Center = React.forwardRef<React.ComponentRef<typeof View>, ICenterProps>( |
||||||
|
function Center({ className, ...props }, ref) { |
||||||
|
return ( |
||||||
|
<View |
||||||
|
className={centerStyle({ class: className })} |
||||||
|
{...props} |
||||||
|
ref={ref} |
||||||
|
/> |
||||||
|
); |
||||||
|
} |
||||||
|
); |
||||||
|
|
||||||
|
Center.displayName = 'Center'; |
||||||
|
|
||||||
|
export { Center }; |
@ -0,0 +1,20 @@ |
|||||||
|
import React from 'react'; |
||||||
|
import { centerStyle } from './styles'; |
||||||
|
|
||||||
|
import type { VariantProps } from '@gluestack-ui/utils/nativewind-utils'; |
||||||
|
|
||||||
|
type ICenterProps = React.ComponentPropsWithoutRef<'div'> & |
||||||
|
VariantProps<typeof centerStyle>; |
||||||
|
|
||||||
|
const Center = React.forwardRef<HTMLDivElement, ICenterProps>(function Center( |
||||||
|
{ className, ...props }, |
||||||
|
ref |
||||||
|
) { |
||||||
|
return ( |
||||||
|
<div className={centerStyle({ class: className })} {...props} ref={ref} /> |
||||||
|
); |
||||||
|
}); |
||||||
|
|
||||||
|
Center.displayName = 'Center'; |
||||||
|
|
||||||
|
export { Center }; |
@ -0,0 +1,8 @@ |
|||||||
|
import { tva } from '@gluestack-ui/utils/nativewind-utils'; |
||||||
|
import { isWeb } from '@gluestack-ui/utils/nativewind-utils'; |
||||||
|
|
||||||
|
const baseStyle = isWeb ? 'flex flex-col relative z-0' : ''; |
||||||
|
|
||||||
|
export const centerStyle = tva({ |
||||||
|
base: `justify-center items-center ${baseStyle}`, |
||||||
|
}); |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,28 @@ |
|||||||
|
import { Box } from '@/components/ui/box'; |
||||||
|
import { Button, ButtonText } from '@/components/ui/button'; |
||||||
|
import { Center } from '@/components/ui/center'; |
||||||
|
import { HStack } from '@/components/ui/hstack'; |
||||||
|
import { Text } from '@/components/ui/text'; |
||||||
|
import { VStack } from '@/components/ui/vstack'; |
||||||
|
import { useCounterDispatch, useCounterState } from '@/states/ConterProvider'; |
||||||
|
|
||||||
|
export default function IndexPage() { |
||||||
|
const state = useCounterState(); |
||||||
|
const dispatch = useCounterDispatch(); |
||||||
|
|
||||||
|
return ( |
||||||
|
<Box className="flex-1 justify-center items-center"> |
||||||
|
<Center style={{ width: '50%', height: '30%' }} className="p-3 bg-secondary-500 h-1/2 w-1/2"> |
||||||
|
<Text className="text-lg font-semibold">Count: {state.count}</Text> |
||||||
|
<HStack className="gap-2"> |
||||||
|
<Button className="w-[200px]" onPress={() => dispatch({ type: 'INCREMENT' })}> |
||||||
|
<ButtonText>+</ButtonText> |
||||||
|
</Button> |
||||||
|
<Button className="w-[150px]" onPress={() => dispatch({ type: 'DECREMENT' })}> |
||||||
|
<ButtonText>-</ButtonText> |
||||||
|
</Button> |
||||||
|
</HStack> |
||||||
|
</Center> |
||||||
|
</Box> |
||||||
|
); |
||||||
|
} |
@ -0,0 +1,45 @@ |
|||||||
|
import { createContext, Dispatch, ReactNode, useContext, useReducer } from 'react'; |
||||||
|
|
||||||
|
type State = { count: number }; |
||||||
|
type Action = { type: 'INCREMENT' | 'DECREMENT' }; |
||||||
|
|
||||||
|
const initialState: State = { count: 0 }; |
||||||
|
|
||||||
|
function counterReducer(state: State, action: Action): State { |
||||||
|
switch (action.type) { |
||||||
|
case 'INCREMENT': |
||||||
|
return { count: state.count + 1 }; |
||||||
|
case 'DECREMENT': |
||||||
|
return { count: state.count - 1 }; |
||||||
|
default: |
||||||
|
throw new Error('Unhandled action'); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const CounterStateContext = createContext<State | undefined>(undefined); |
||||||
|
const CounterDispatchContext = createContext<Dispatch<Action> | undefined>(undefined); |
||||||
|
|
||||||
|
export function CounterProvider({ children }: { children: ReactNode }) { |
||||||
|
const [state, dispatch] = useReducer(counterReducer, initialState); |
||||||
|
return ( |
||||||
|
<CounterStateContext.Provider value={state}> |
||||||
|
<CounterDispatchContext.Provider value={dispatch}>{children}</CounterDispatchContext.Provider> |
||||||
|
</CounterStateContext.Provider> |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
export function useCounterState() { |
||||||
|
const context = useContext(CounterStateContext); |
||||||
|
if (context === undefined) |
||||||
|
throw new Error('useCounterState must be used within a CounterProvider'); |
||||||
|
|
||||||
|
return context; |
||||||
|
} |
||||||
|
|
||||||
|
export function useCounterDispatch() { |
||||||
|
const context = useContext(CounterDispatchContext); |
||||||
|
if (context === undefined) |
||||||
|
throw new Error('useCounterDispatch must be used within a CounterProvider'); |
||||||
|
|
||||||
|
return context; |
||||||
|
} |
Loading…
Reference in new issue