Last active
June 17, 2021 20:07
-
-
Save fhugoduarte/60d3c898ee40944e99af57f53121ec90 to your computer and use it in GitHub Desktop.
styled-components integration
This file contains hidden or 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
Show hidden characters
{ | |
"useScreenRem": { | |
"prefix": "rem", | |
"body": [ | |
"${({ theme }) => theme.screen.rem(${1:})}px;", | |
], | |
"description": "Use rem helper function from responsive native" | |
} | |
} |
This file contains hidden or 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
import 'styled-components'; | |
import type { ResponsiveTheme } from '../ThemeProvider'; | |
import { YourThemeType } from '../theme'; | |
declare module 'styled-components' { | |
export interface DefaultTheme extends ResponsiveTheme, YourThemeType {} | |
} |
This file contains hidden or 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
import React, { useMemo } from 'react'; | |
import { | |
useScreen, | |
ScreenContextData, | |
MediaQuery, | |
BreakpointValues, | |
rem, | |
getNearestBreakpointValue, | |
validateMediaQuery | |
} from 'responsive-native'; | |
import { | |
ThemeProvider as StyledThemeProvider, | |
} from 'styled-components'; | |
type Query = Omit<MediaQuery, 'currentBreakpoint'>; | |
type Screen = Pick<ScreenContextData, 'breakpoint' | 'padding'> & { | |
breakpointValue<T = unknown>( | |
values: BreakpointValues, | |
): T | undefined; | |
mediaQuery(query: Query): boolean; | |
rem(size: number, shouldScale?: boolean): number; | |
}; | |
export interface ResponsiveTheme { | |
screen: Screen; | |
} | |
interface Props { | |
children?: React.ReactNode; | |
} | |
export function ThemeProvider ({children}: Props) { | |
const { breakpoint, padding, baseFontSize, fontScaleFactor } = useScreen(); | |
const theme = useMemo(() => { | |
return { | |
// ...yourTheme | |
screen: { | |
breakpoint, | |
padding, | |
rem: (size: number, shouldScale?: boolean) => { | |
return rem({ | |
size, | |
shouldScale, | |
baseFontSize, | |
fontScaleFactor | |
}) | |
}, | |
breakpointValue: (values: BreakpointValues) => { | |
return getNearestBreakpointValue({ | |
breakpoint: breakpoint.size, | |
values | |
}) | |
} , | |
mediaQuery: ({ minBreakpoint, maxBreakpoint, platform }: Query) => { | |
return validateMediaQuery({minBreakpoint, maxBreakpoint, platform, currentBreakpoint: breakpoint.size}) | |
} | |
} | |
} | |
}, [breakpoint, padding, baseFontSize, fontScaleFactor]) | |
return ( | |
<StyledThemeProvider theme={theme}> | |
{children} | |
</StyledThemeProvider> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment