-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PlatformColor implementations for iOS and Android (#27908)
Summary: This Pull Request implements the PlatformColor proposal discussed at react-native-community/discussions-and-proposals#126. The changes include implementations for iOS and Android as well as a PlatformColorExample page in RNTester. Every native platform has the concept of system defined colors. Instead of specifying a concrete color value the app developer can choose a system color that varies in appearance depending on a system theme settings such Light or Dark mode, accessibility settings such as a High Contrast mode, and even its context within the app such as the traits of a containing view or window. The proposal is to add true platform color support to react-native by extending the Flow type `ColorValue` with platform specific color type information for each platform and to provide a convenience function, `PlatformColor()`, for instantiating platform specific ColorValue objects. `PlatformColor(name [, name ...])` where `name` is a system color name on a given platform. If `name` does not resolve to a color for any reason, the next `name` in the argument list will be resolved and so on. If none of the names resolve, a RedBox error occurs. This allows a latest platform color to be used, but if running on an older platform it will fallback to a previous version. The function returns a `ColorValue`. On iOS the values of `name` is one of the iOS [UI Element](https://developer.apple.com/documentation/uikit/uicolor/ui_element_colors) or [Standard Color](https://developer.apple.com/documentation/uikit/uicolor/standard_colors) names such as `labelColor` or `systemFillColor`. On Android the `name` values are the same [app resource](https://developer.android.com/guide/topics/resources/providing-resources) path strings that can be expressed in XML: XML Resource: `@ [<package_name>:]<resource_type>/<resource_name>` Style reference from current theme: `?[<package_name>:][<resource_type>/]<resource_name>` For example: - `?android:colorError` - `?android:attr/colorError` - `?attr/colorPrimary` - `?colorPrimaryDark` - `android:color/holo_purple` - `color/catalyst_redbox_background` On iOS another type of system dynamic color can be created using the `IOSDynamicColor({dark: <color>, light:<color>})` method. The arguments are a tuple containing custom colors for light and dark themes. Such dynamic colors are useful for branding colors or other app specific colors that still respond automatically to system setting changes. Example: `<View style={{ backgroundColor: IOSDynamicColor({light: 'black', dark: 'white'}) }}/>` Other platforms could create platform specific functions similar to `IOSDynamicColor` per the needs of those platforms. For example, macOS has a similar dynamic color type that could be implemented via a `MacDynamicColor`. On Windows custom brushes that tint or otherwise modify a system brush could be created using a platform specific method. ## Changelog [General] [Added] - Added PlatformColor implementations for iOS and Android Pull Request resolved: #27908 Test Plan: The changes have been tested using the RNTester test app for iOS and Android. On iOS a set of XCTestCase's were added to the Unit Tests. <img width="924" alt="PlatformColor-ios-android" src="https://user-images.githubusercontent.com/30053638/73472497-ff183a80-433f-11ea-90d8-2b04338bbe79.png"> In addition `PlatformColor` support has been added to other out-of-tree platforms such as macOS and Windows has been implemented using these changes: react-native for macOS branch: microsoft/react-native-macos@master...tom-un:tomun/platformcolors react-native for Windows branch: microsoft/react-native-windows@master...tom-un:tomun/platformcolors iOS |Light|Dark| |{F229354502}|{F229354515}| Android |Light|Dark| |{F230114392}|{F230114490}| {F230122700} Reviewed By: hramos Differential Revision: D19837753 Pulled By: TheSavior fbshipit-source-id: 82ca70d40802f3b24591bfd4b94b61f3c38ba829
- Loading branch information
1 parent
5166856
commit f4de458
Showing
52 changed files
with
1,621 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {ColorValue} from './StyleSheetTypes'; | ||
import type {ProcessedColorValue} from './processColor'; | ||
|
||
export opaque type NativeColorValue = { | ||
resource_paths?: Array<string>, | ||
}; | ||
|
||
export const PlatformColor = (...names: Array<string>): ColorValue => { | ||
return {resource_paths: names}; | ||
}; | ||
|
||
export const ColorAndroidPrivate = (color: string): ColorValue => { | ||
return {resource_paths: [color]}; | ||
}; | ||
|
||
export const normalizeColorObject = ( | ||
color: NativeColorValue, | ||
): ?ProcessedColorValue => { | ||
if ('resource_paths' in color) { | ||
return color; | ||
} | ||
return null; | ||
}; | ||
|
||
export const processColorObject = ( | ||
color: NativeColorValue, | ||
): ?NativeColorValue => { | ||
return color; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {ColorValue} from './StyleSheetTypes'; | ||
import type {ProcessedColorValue} from './processColor'; | ||
|
||
export opaque type NativeColorValue = { | ||
semantic?: Array<string>, | ||
dynamic?: { | ||
light: ?(ColorValue | ProcessedColorValue), | ||
dark: ?(ColorValue | ProcessedColorValue), | ||
}, | ||
}; | ||
|
||
export const PlatformColor = (...names: Array<string>): ColorValue => { | ||
return {semantic: names}; | ||
}; | ||
|
||
export type DynamicColorIOSTuplePrivate = { | ||
light: ColorValue, | ||
dark: ColorValue, | ||
}; | ||
|
||
export const DynamicColorIOSPrivate = ( | ||
tuple: DynamicColorIOSTuplePrivate, | ||
): ColorValue => { | ||
return {dynamic: {light: tuple.light, dark: tuple.dark}}; | ||
}; | ||
|
||
export const normalizeColorObject = ( | ||
color: NativeColorValue, | ||
): ?ProcessedColorValue => { | ||
if ('semantic' in color) { | ||
// an ios semantic color | ||
return color; | ||
} else if ('dynamic' in color && color.dynamic !== undefined) { | ||
const normalizeColor = require('./normalizeColor'); | ||
|
||
// a dynamic, appearance aware color | ||
const dynamic = color.dynamic; | ||
const dynamicColor: NativeColorValue = { | ||
dynamic: { | ||
light: normalizeColor(dynamic.light), | ||
dark: normalizeColor(dynamic.dark), | ||
}, | ||
}; | ||
return dynamicColor; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const processColorObject = ( | ||
color: NativeColorValue, | ||
): ?NativeColorValue => { | ||
if ('dynamic' in color && color.dynamic != null) { | ||
const processColor = require('./processColor'); | ||
const dynamic = color.dynamic; | ||
const dynamicColor: NativeColorValue = { | ||
dynamic: { | ||
light: processColor(dynamic.light), | ||
dark: processColor(dynamic.dark), | ||
}, | ||
}; | ||
return dynamicColor; | ||
} | ||
return color; | ||
}; |
18 changes: 18 additions & 0 deletions
18
Libraries/StyleSheet/PlatformColorValueTypesAndroid.android.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow strict-local | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {ColorValue} from './StyleSheetTypes'; | ||
import {ColorAndroidPrivate} from './PlatformColorValueTypes'; | ||
|
||
export const ColorAndroid = (color: string): ColorValue => { | ||
return ColorAndroidPrivate(color); | ||
}; |
Oops, something went wrong.