Skip to content

Commit

Permalink
feat: add support for platform colors
Browse files Browse the repository at this point in the history
  • Loading branch information
jpudysz committed Nov 10, 2023
1 parent e1e6377 commit f726518
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
43 changes: 43 additions & 0 deletions examples/expo/src/examples/PlatformColors.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react'
import { Text, View, PlatformColor, Platform } from 'react-native'
import { createStyleSheet, useStyles } from '../styles'

export const PlatformColors: React.FunctionComponent = () => {
const { styles } = useStyles(stylesheet)

return (
<View style={styles.container}>
<Text>
Unistyles supports also PlatformColor!
</Text>
</View>
)
}

const stylesheet = createStyleSheet({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
...Platform.select({
ios: {
color: PlatformColor('label'),
backgroundColor: PlatformColor('systemTealColor')
},
android: {
color: PlatformColor('?android:attr/textColor'),
backgroundColor: PlatformColor('@android:color/holo_blue_bright')
},
default: {
color: 'black',
backgroundColor: 'orange'
}
})
},
text: {
color: {
sm: PlatformColor('label'),
md: PlatformColor('systemTealColor')
}
}
})
1 change: 1 addition & 0 deletions examples/expo/src/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { Breakpoints } from './Breakpoints'
export { MediaQueries } from './MediaQueries'
export { Extreme } from './Extreme'
export { Memoization } from './Memoization'
export { PlatformColors } from './PlatformColors'
9 changes: 6 additions & 3 deletions src/types/breakpoints.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { OpaqueColorValue } from 'react-native'
import type { MediaQueries } from './mediaQueries'

export type Breakpoints = Record<string, number>
Expand Down Expand Up @@ -27,7 +28,9 @@ export type ExtractBreakpoints<T, B extends Breakpoints> = T extends Partial<Rec
export type RemoveKeysWithPrefix<T, B extends Breakpoints> = T extends (...args: Array<any>) => infer R
? (...args: Parameters<T>) => RemoveKeysWithPrefix<R, B>
: T extends object
? T extends Record<string, infer _V>
? { [K in keyof T as K extends MediaQueries ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B> }
: { [K in keyof T]: RemoveKeysWithPrefix<T[K], B> }
? T extends OpaqueColorValue
? string
: T extends Record<string, infer _V>
? { [K in keyof T as K extends MediaQueries ? keyof B & string : K]: RemoveKeysWithPrefix<T[K], B> }
: { [K in keyof T]: RemoveKeysWithPrefix<T[K], B> }
: T
2 changes: 2 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export const warn = (message: string) => {
}

export const isWeb = Platform.OS === 'web'
export const isIOS = Platform.OS === 'ios'
export const isAndroid = Platform.OS === 'android'
export const isServer = typeof window === 'undefined'
12 changes: 10 additions & 2 deletions src/utils/styles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Breakpoints, CustomNamedStyles, ScreenSize, SortedBreakpointEntries } from '../types'
import { getValueForBreakpoint } from './breakpoints'
import { normalizeStyles } from './normalizeStyles'
import { isWeb } from './common'
import { isAndroid, isIOS, isWeb } from './common'

/**
* Proxies a function to parse its return value for custom media queries or breakpoints.
Expand Down Expand Up @@ -33,6 +33,14 @@ export const proxifyFunction = <B extends Breakpoints>(
parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, breakpointPairs)
})

export const isPlatformColor = <T extends {}>(value: T): boolean => {
if (isIOS) {
return 'semantic' in value
}

return isAndroid && 'resource_paths' in value
}

/**
* Parses a style object to resolve custom media queries or breakpoints based on the provided screen size and breakpoints.
*
Expand Down Expand Up @@ -91,7 +99,7 @@ export const parseStyle = <T, B extends Breakpoints>(
}

const isDynamicFunction = typeof value === 'function'
const isValidStyle = typeof value !== 'object'
const isValidStyle = typeof value !== 'object' || isPlatformColor(value)

if (isDynamicFunction || isValidStyle) {
return [key, value]
Expand Down

0 comments on commit f726518

Please sign in to comment.