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

DynamicColorsIOS page #1851

Merged
merged 8 commits into from
Apr 21, 2020
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
20 changes: 4 additions & 16 deletions docs/colors.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,12 @@ 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
## Color APIs

```jsx
DynamicColorIOS({light: color, dark: color});
```
React Native has several color APIs designed to allow you to take full advantage of your platform's design and user preferences.

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)
- [PlatformColor](platformcolor) lets you reference the platform's color system.
- [DynamicColorIOS](dynamiccolorios) is iOS specific and allows you to specify which colors should be used in light or dark mode.

## Named colors

Expand Down
45 changes: 45 additions & 0 deletions docs/dynamiccolorios.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
id: dynamiccolorios
title: DynamicColorIOS
---

The `DynamicColorIOS` function is a platform color type specific to iOS.

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

`DynamicColorIOS` takes a single argument as an object with two keys: `dark` and `light`. These correspond to the colors you want to use for "light mode" and "dark mode" on iOS.

> In the future, more keys might become available for different user preferences, like high contrast.

At runtime, the system will choose 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.

<div class="toggler">
<span>Developer Notes</span>
<span role="tablist" class="toggle-devNotes">
<button role="tab" class="button-webNote" onclick="displayTabs('devNotes', 'webNote')">Web</button>
<button role="tab" class="button-iosNote" onclick="displayTabs('devNotes', 'iosNote')">iOS</button>
</span>
</div>

<block class="webNote devNotes" />

> If you’re familiar with `@media (prefers-color-scheme: dark)` in CSS, this is similar! Only instead of defining all the colors in a media query, you define which color to use under what circumstances right there where you're using it. Neat!

<block class="iosNote devNotes" />

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

<block class="endBlock devNotes" />

## Example

```jsx
import { DynamicColorIOS } from 'react-native';

const customDynamicTextColor = DynamicColorIOS({
dark: 'lightskyblue',
light: 'midnightblue',
});
```
51 changes: 31 additions & 20 deletions docs/platformcolor.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,57 @@ id: platformcolor
title: PlatformColor
---

```
```js
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.
You can use the `PlatformColor` function to access native colors on the target platform by 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.

<div class="toggler">
<span>Developer Notes</span>
<span role="tablist" class="toggle-devNotes">
<button role="tab" class="button-webNote" onclick="displayTabs('devNotes', 'webNote')">Web</button>
</span>
</div>

<block class="webNote devNotes" />

> If you’re familiar with design systems, another way of thinking about this is that `PlatformColor` lets you tap into the local design system's color tokens so your app can blend right in!

<block class="endBlock devNotes" />

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/)
- [iOS Color](https://developer.apple.com/documentation/uikit/uicolor/ui_element_colors?language=objc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Esemesek With facebook/react-native@25793ea landed, I think it would be preferable to link to the old link instead, which is a guide that describes the color system much more extensive and agnostic of APIs.

Copy link
Collaborator

@Simek Simek Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alloy Thank you for the comment. There is already a PR for that #1865.


## Example

```
```jsx
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>
);
}
import { Platform, PlatformColor, StyleSheet, Text, View } from 'react-native';

export default App = () => (
<View>
<Text style={styles.labelCell}>
I am a special label color!
</Text>
</View>
);

const styles = StyleSheet.create({
labelCell: {
labelCell: {
flex: 1,
alignItems: 'stretch',
...Platform.select({
ios: {color: PlatformColor('labelColor')},
android: {color: PlatformColor('?attr/colorControlNormal')},
default: {color: 'black'},
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).
> **Note:** 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 @@ -104,6 +104,9 @@
"drawerlayoutandroid": {
"title": "DrawerLayoutAndroid"
},
"dynamiccolorios": {
"title": "DynamicColorIOS"
},
"easing": {
"title": "Easing"
},
Expand Down
2 changes: 1 addition & 1 deletion website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
{
"type": "subcategory",
"label": "iOS APIs",
"ids": ["actionsheetios", "settings"]
"ids": ["actionsheetios", "dynamiccolorios", "settings"]
}
]
}
Expand Down