Skip to content

Commit

Permalink
PlatformColor API (#1813)
Browse files Browse the repository at this point in the history
* PlatformColor API

Added api documentation for the PlatformColor API function.

* Incorrect quotation marks typo fix

* removed platformcolor from versioned docs, moved it out of hooks

* Update en.json

* Added DynamicColorIOS section to colors.md

* Updates to colors.md and platformcolor.md

* Update colors.md

example for dynamiccolorios added to it's section.

* simplifier example for dynamiccolorios

* simplified the dynamiccolorios jsx code example

Co-authored-by: Kiki Saintonge <[email protected]>
  • Loading branch information
kikisaints and Kiki Saintonge authored Apr 14, 2020
1 parent bb36afa commit b45d54a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/colors.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ React Native supports also colors as an `int` values (in RGB color mode):

> **_Note:_** This might appear similar to the Android [Color](https://developer.android.com/reference/android/graphics/Color) ints representation but on Android values are stored in SRGB color mode (0xaarrggbb).
## DynamicColorIOS

```jsx
DynamicColorIOS({light: color, dark: color});
```

The `DynamicColorIOS` function is a platform color type specific to iOS. `DynamicColorIOS` takes a single argument as an object with two keys: light and dark.

At runtime, the system will chose which of the two colors to display depending on the current system appearance settings. Dynamic colors are useful for branding colors or other app specific colors that still respond automatically to system setting changes.

```
const headerTextColor = DynamicColorIOS({
dark: PlatformColor('labelColor'),
light: PlatformColor('secondaryLabelColor')
});
```

The `DynamicColorIOS` function is similar to the iOS native methods [`UIColor colorWithDynamicProvider:`](https://developer.apple.com/documentation/uikit/uicolor/3238040-colorwithdynamicprovider)

## Named colors

In React Native you can also use color name strings as values.
Expand Down
48 changes: 48 additions & 0 deletions docs/platformcolor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
id: platformcolor
title: PlatformColor
---

```
PlatformColor(color1, [color2, ...colorN])
```

Use the `PlatformColor` function to access native colors on the target platform via supplying the native color’s corresponding string value. You pass a string to the `PlatformColor` function, and provided it exists on that platform, that native color will be applied to the control or Javascript component specified in your style. All native color logic also translates if applicable, meaning if the native color specified is themes and/or high contrast sensitive, that logic will also transfer to the JavaScript component being colored.

For a full list of the types of system colors supported, see:

- [Android R.attr](https://developer.android.com/reference/android/R.attr)
- [iOS Color](https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/color/)

## Example

```
import React from 'react';
import { Text, View, StyleSheet, PlatformColor, Platform } from 'react-native';
export default function App() {
return (
<View>
<Text style={styles.labelCell}>
I am a special label color!
</Text>
</View>
);
}
const styles = StyleSheet.create({
labelCell: {
flex: 1,
alignItems: 'stretch',
...Platform.select({
ios: {color: PlatformColor('labelColor')},
android: {color: PlatformColor('?attr/colorControlNormal')},
default: {color: 'black'},
}),
},
});
```

The string value provided to the `PlatformColor` function must match and agree with the same string as it exists on the native platform the app is being run on. This means to avoid runtime errors the function should be wrapped in a platform check, either through a `Platform.OS === 'platform'` or a `Platform.Select()`.

You can find a complete example that demonstrates proper, intended use of PlatformColor in [PlatformColorExample.js](https://github.com/facebook/react-native/blob/master/RNTester/js/examples/PlatformColor/PlatformColorExample.js).
3 changes: 3 additions & 0 deletions website/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@
"platform-specific-code": {
"title": "Platform Specific Code"
},
"platformcolor": {
"title": "PlatformColor"
},
"profiling": {
"title": "Profiling"
},
Expand Down
1 change: 1 addition & 0 deletions website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"linking",
"panresponder",
"pixelratio",
"platformcolor",
"share",
"stylesheet",
"systrace",
Expand Down

0 comments on commit b45d54a

Please sign in to comment.