Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a secondary layout argument in createStyleSheet #155

Merged
merged 6 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/content/docs/other/sponsors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ Do you want to become a sponsor? Read a guide [for sponsors](/other/for-sponsors
size="40"
link="https://github.com/efstathiosntonas"
/>
<Sponsor
url="https://avatars.githubusercontent.com/u/6288237?v=4"
name="justblender"
size="40"
link="https://github.com/justblender"
/>
</div>

</Seo>
16 changes: 16 additions & 0 deletions docs/src/content/docs/reference/create-stylesheet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ const stylesheet = createStyleSheet(theme => ({
}))
```

In addition to the `theme` argument, you can also accept the `runtime` argument when passing a function. This can be useful for accessing `UnistylesRuntime` and its properties (such as screen orientation, dimensions, insets, and more) without having to import this class directly.

```ts /runtime/
const stylesheet = createStyleSheet((theme, runtime) => ({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
paddingTop: runtime.insets.top,
backgroundColor: runtime.orientation === 'portrait'
? theme.colors.accent
: theme.colors.oak
},
}))
```

Importantly, you'll receive the same TypeScript hints as with `StyleSheet.create`!

</Seo>
10 changes: 5 additions & 5 deletions examples/expo/src/examples/RuntimeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export const RuntimeScreen: React.FunctionComponent = () => {
)
}

const stylesheet = createStyleSheet(theme => ({
const stylesheet = createStyleSheet((theme, runtime) => ({
container: {
flex: 1,
paddingTop: 50,
Expand Down Expand Up @@ -228,15 +228,15 @@ const stylesheet = createStyleSheet(theme => ({
},
topInset: {
position: 'absolute',
top: UnistylesRuntime.insets.top,
top: runtime.insets.top,
left: 0,
right: 0,
height: 1,
backgroundColor: theme.colors.accent
},
bottomInset: {
position: 'absolute',
bottom: UnistylesRuntime.insets.bottom,
bottom: runtime.insets.bottom,
left: 0,
right: 0,
height: 1,
Expand All @@ -246,15 +246,15 @@ const stylesheet = createStyleSheet(theme => ({
position: 'absolute',
top: 0,
bottom: 0,
left: UnistylesRuntime.insets.left,
left: runtime.insets.left,
width: 1,
backgroundColor: theme.colors.accent
},
rightInset: {
position: 'absolute',
top: 0,
bottom: 0,
right: UnistylesRuntime.insets.right,
right: runtime.insets.right,
width: 1,
backgroundColor: theme.colors.accent
}
Expand Down
10 changes: 5 additions & 5 deletions examples/expo/src/examples/RuntimeWithStyleSheetScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { View, Text } from 'react-native'
import { createStyleSheet, useStyles, UnistylesRuntime } from 'react-native-unistyles'
import { createStyleSheet, useStyles } from 'react-native-unistyles'
import { DemoScreen } from '../components'

export const RuntimeWithStyleSheetScreen: React.FunctionComponent = () => {
Expand All @@ -25,7 +25,7 @@ export const RuntimeWithStyleSheetScreen: React.FunctionComponent = () => {
)
}

const stylesheet = createStyleSheet(theme => ({
const stylesheet = createStyleSheet((theme, runtime) => ({
container: {
flex: 1,
paddingTop: 50,
Expand All @@ -41,9 +41,9 @@ const stylesheet = createStyleSheet(theme => ({
alignItems: 'center',
padding: 20,
marginTop: 50,
width: UnistylesRuntime.screen.width / 2,
height: UnistylesRuntime.screen.height / 2,
backgroundColor: UnistylesRuntime.orientation === 'portrait'
width: runtime.screen.width / 2,
height: runtime.screen.height / 2,
backgroundColor: runtime.orientation === 'portrait'
? theme.colors.accent
: theme.colors.oak
}
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useUnistyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const useUnistyles = () => {
const [layout, setLayout] = useState({
breakpoint: unistyles.runtime.breakpoint,
orientation: unistyles.runtime.orientation,
screenSize: {
screen: {
width: unistyles.runtime.screen.width,
height: unistyles.runtime.screen.height
},
Expand Down Expand Up @@ -49,7 +49,7 @@ export const useUnistyles = () => {
return setLayout({
breakpoint: layoutEvent.payload.breakpoint,
orientation: layoutEvent.payload.orientation,
screenSize: layoutEvent.payload.screen,
screen: layoutEvent.payload.screen,
statusBar: layoutEvent.payload.statusBar,
insets: layoutEvent.payload.insets,
navigationBar: layoutEvent.payload.navigationBar
Expand Down
3 changes: 2 additions & 1 deletion src/types/breakpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ColorValue, OpaqueColorValue } from 'react-native'
import type { UnistylesTheme } from '../types'
import type { BreakpointsOrMediaQueries, ToDeepUnistyles } from './stylesheet'
import type { TransformStyles } from './core'
import type { UnistylesRuntime } from '../core'

type ExtractTransformArray<T> = T extends object
? { [K in keyof T]: ExtractBreakpoints<T[K]> }
Expand Down Expand Up @@ -56,6 +57,6 @@ type ParseStyleKeys<T> = T extends object
? { [K in keyof T]: ParseNestedObject<T[K]> }
: never

export type ReactNativeStyleSheet<T> = T extends (theme: UnistylesTheme) => infer R
export type ReactNativeStyleSheet<T> = T extends (theme: UnistylesTheme, runtime: UnistylesRuntime) => infer R
? ParseStyleKeys<R>
: ParseStyleKeys<T>
3 changes: 2 additions & 1 deletion src/types/stylesheet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
import type { ShadowOffset, TransformStyles, UnistylesTheme } from './core'
import type { UnistylesBreakpoints } from '../global'
import type { UnistylesRuntime } from '../core'

// these props are treated differently to nest breakpoints and media queries
type NestedKeys = 'shadowOffset' | 'transform' | 'textShadowOffset'
Expand Down Expand Up @@ -46,4 +47,4 @@ export type StyleSheet = {
[styleName: string]: UnistylesValues | ((...args: any) => UnistylesValues)
}

export type StyleSheetWithSuperPowers = ((theme: UnistylesTheme) => StyleSheet) | StyleSheet
export type StyleSheetWithSuperPowers = ((theme: UnistylesTheme, runtime: UnistylesRuntime) => StyleSheet) | StyleSheet
2 changes: 1 addition & 1 deletion src/useStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const useStyles = <ST extends StyleSheetWithSuperPowers>(
const { theme, layout, plugins } = useUnistyles()
const variants = useVariants(variantsMap)
const parsedStyles = useMemo(() => typeof stylesheet === 'function'
? stylesheet(theme)
? stylesheet(theme, unistyles.runtime)
: stylesheet, [theme, stylesheet, layout])

const dynamicStyleSheet = useMemo(() => Object
Expand Down
Loading