This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #511 from gluestack/release/@gluestack-style/react…
…@1.0.11 Release/@gluestack style/[email protected]
- Loading branch information
Showing
8 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: useTheme | gluestack-style | ||
description: useTheme | ||
showHeader: false | ||
--- | ||
|
||
import { Canvas, Meta, Story } from '@storybook/addon-docs'; | ||
|
||
<Meta title="hooks/useMedia" /> | ||
|
||
# useMedia | ||
|
||
**useMedia** is a custom hook that returns the breakpoints object with boolean value if your current screen size is a particular breakpoint or not. It is also responsive to window resizing and returning the appropriate value according to the new window size. | ||
|
||
### Import | ||
|
||
To use the `useMedia` in your project, import `useMedia` from `@gluestack-style/react`. as Demonstrated below. | ||
|
||
```jsx | ||
import { useMedia } from '@gluestack-style/react'; | ||
``` | ||
|
||
```jsx | ||
const MediaExample = () => { | ||
const media = useMedia(); | ||
|
||
return ( | ||
<View | ||
style={{ | ||
flexDirection: media.lg ? 'row' : 'column', | ||
gap: 10, | ||
}} | ||
> | ||
<StyledBox> | ||
<StyledText>Universal</StyledText> | ||
</StyledBox> | ||
<StyledBox> | ||
<StyledText>Performant</StyledText> | ||
</StyledBox> | ||
<StyledBox> | ||
<StyledText>Accessible</StyledText> | ||
</StyledBox> | ||
</View> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { ComponentMeta } from '@storybook/react-native'; | ||
import { MediaHookStory } from './useMedia'; | ||
|
||
const MediaQueryMeta: ComponentMeta<typeof MediaHookStory> = { | ||
title: 'hooks/stories/useMedia', | ||
component: MediaHookStory, | ||
}; | ||
|
||
export { MediaHookStory } from './useMedia'; | ||
|
||
export default MediaQueryMeta; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React from 'react'; | ||
import { | ||
useMediaQuery, | ||
styled, | ||
useBreakpointValue, | ||
useMedia, | ||
} from '@gluestack-style/react'; | ||
import Wrapper from '../../components/Wrapper'; | ||
import { View, Text } from 'react-native'; | ||
|
||
const StyledBox = styled(View, { | ||
w: 100, | ||
h: 100, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
bg: '$cyan500', | ||
rounded: '$md', | ||
}) as any; | ||
|
||
const StyledText = styled(Text, { | ||
color: '$white', | ||
fontSize: '$md', | ||
}); | ||
|
||
export const MediaHookStory = () => { | ||
return ( | ||
<Wrapper> | ||
<BreakPointValue /> | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
const BreakPointValue = () => { | ||
const media = useMedia(); | ||
|
||
return ( | ||
<View | ||
style={{ | ||
flexDirection: media.lg ? 'row' : 'column', | ||
gap: 10, | ||
}} | ||
> | ||
<StyledBox> | ||
<StyledText>Universal</StyledText> | ||
</StyledBox> | ||
<StyledBox> | ||
<StyledText>Performant</StyledText> | ||
</StyledBox> | ||
<StyledBox> | ||
<StyledText>Accessible</StyledText> | ||
</StyledBox> | ||
</View> | ||
); | ||
}; | ||
export { useMediaQuery }; | ||
export default MediaHookStory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { useWindowDimensions } from 'react-native'; | ||
import { isValidBreakpoint } from '../generateStylePropsFromCSSIds'; | ||
import { useStyled } from '../StyledProvider'; | ||
import type { GSConfig } from '../types'; | ||
|
||
type BreakPointValue = Partial<{ | ||
[key in keyof GSConfig['tokens']['breakpoints']]: boolean; | ||
}>; | ||
|
||
export const useMedia = (): BreakPointValue => { | ||
const theme = useStyled(); | ||
const { width } = useWindowDimensions(); | ||
const mediaQueries = theme?.config?.tokens?.mediaQueries; | ||
|
||
const breakpoints: any = {}; | ||
|
||
Object.keys(mediaQueries).forEach((currentBreakPoint: any) => { | ||
breakpoints[currentBreakPoint] = isValidBreakpoint( | ||
theme?.config, | ||
mediaQueries[currentBreakPoint], | ||
width | ||
); | ||
}); | ||
|
||
return breakpoints; | ||
}; |