react-native-bpk-appearance
Backpack React Native appearance.
Installation
npm install react-native-bpk-appearance --save-dev
Because this package ships with native code, it is also necessary to add some native dependencies to your RN project:
Android
Append |uiMode
to the android:configChanges
prop of <activity>
in AndroidManifest.xml
. Example:
Add the following configurations to gradle:
- Define the
react-native-bpk-appearance
project in yoursettings.gradle
file:
include ':react-native-bpk-appearance' project(':react-native-bpk-appearance').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-bpk-appearance/src/android')
- Add
react-native-bpk-appearance
as a dependency in your app/modulebuild.gradle
file:
dependencies { implementation project(':react-native-bpk-appearance') }
If you have defined project-wide properties in your root build.gradle
, this library will detect the presence of the following properties:
ext { compileSdkVersion = 28 targetSdkVersion = 28 minSdkVersion = 21 buildToolsVersion = "28.0.3"}
If you haven't or are using the pre compiled version bellow, it will use the values shown above.
Pre compiled version
Alternatively, the pre compiled version is available on Skyscanner's internal Artifactory. Make sure you have the infrastructure-maven
registry configured and are logged in, then add the following dependency to your build.gradle
file:
dependencies { implementation 'net.skyscanner.backpack:react-native-bpk-appearance:<version>' }
Note: The version should be the same used for the npm package.
Importing the bridge package
After you have installed the lib, import the DialogPackage()
in your react application:
.... @Overrideprotected List<ReactPackage> { return Arrays.<ReactPackage>;}
iOS
Add a dependency to your Podfile using the path to the NPM package as follows:
pod 'ReactNativeDarkMode', path: '../node_modules/react-native-bpk-appearance/node_modules/react-native-dark-mode/ReactNativeDarkMode.podspec'
Usage
First wrap your app with BpkAppearanceProvider
; const App = <BpkAppearanceProvider> <App> children </App> </BpkAppearanceProvider>;
Now you can use the provided hooks to react to changes in the system appearance
;;;;; const style = BpkDynamicStyleSheet; const UserProfile = { const currentStyle = ; const image = ; return <View style=currentStyleview> <BpkImage style=currentStyleimage source= uri: image alt="user profile"> </View> ;};
For non-functional components use BpkAppearanceConsumer
or withBpkAppearance
HOC and the unpack*
functions. (See full in API section bellow).
BpkAppearanceConsumer usage
;;;;; const style = BpkDynamicStyleSheet; type Props = user: Object const defaultProps = user: guest: true ; <Props> { const user = thisprops; return <BpkAppearanceConsumer> { const currentStyle = ; return <View style=currentStyleview> <BpkText>username</BpkText> </View> } </BpkAppearanceConsumer> ; }; ;
withBpkAppearance usage
;;;;; const style = BpkDynamicStyleSheet; type Props = user: Object const defaultProps = user: guest: true ; <Props & WithBpkAppearanceInjectedProps> { const bpkAppearance user = thisprops; const currentStyle = ; return <View style=currentStyleview> <BpkText>username</BpkText> </View> ; }; type ComponentConfig = Config<Props typeof defaultProps>;<ComponentConfig>UserProfile;
API
Table of Contents
- useBpkAppearance
- useBpkColorScheme
- useBpkDynamicValue
- useBpkDynamicStyle
- useBpkDynamicStyleSheet
- BpkAppearanceConsumer
- BpkDynamicStyleSheet
- create
- withBpkAppearance
- isBpkDynamicValue
- unpackBpkDynamicValue
- unpackBpkDynamicStyle
useBpkAppearance
Fetch the current appearance as provided by the nearest [BpkAppearanceProvider]
Returns BpkAppearancePreferences the current appearance
useBpkColorScheme
Fetch the current color scheme as provided by the nearest [BpkAppearanceProvider]
Returns ColorSchemeName the current color scheme
useBpkDynamicValue
Takes in a BpkDynamicValue
and returns the correct value for the
current color scheme as provided by the nearest [BpkAppearanceProvider]
Parameters
value
Object a dynamic value.
Examples
const color =
Returns mixed the value for the current color scheme.
If value
is not a valid dynamic value it will be returned back
useBpkDynamicStyle
Takes in a style object and returns the correct value for all properties, based on the current color scheme as provided by the nearest [BpkAppearanceProvider]
Parameters
style
Object the style object
Examples
const color = ;
Returns Object object with mapped properties for the current color scheme
useBpkDynamicStyleSheet
Takes in a BpkDynamicStyleSheet
and returns the correct value for
the current color scheme as provided by the nearest [BpkAppearanceProvider]
Parameters
style
BpkDynamicStyle the dynamic stylesheet
Examples
const dynamicStyles = BpkDynamicStyleSheetconst styles = ;
Returns BpkDynamicStyleProp the current stylesheet
BpkAppearanceConsumer
A render prop component that provides the current BpkAppearance as provided by the nearest [BpkAppearanceProvider].
NOTE: This component should mainly be used in class components, for functional components we recommend using the provided hooks.
Parameters
children
Function Function that will receive the current appearance and should return a react Node.children.children
Examples
<BpkAppearanceConsumer> { const logo = ; return <BpkImage style=stylesimage alt="image title" source=uri: logo /> }</BpkAppearanceConsumer>
Returns Node a react Node.
BpkDynamicStyleSheet
create
Creates a new dynamic stylesheet that transforms all BpkDynamicValues
into
a plain StyleSheet
for each color scheme.
This should generally be used in conjunction with useBpkDynamicStyleSheet
hook.
Parameters
obj
Object a style containing dynamic values
Examples
BpkDynamicStyleSheet;
BpkDynamicStyleSheet;
Returns BpkDynamicStyle an object containing a plain stylesheet for each color scheme.
withBpkAppearance
This HOC wraps a component and provides the current BpkAppearancePreferences
as provided by the nearest BpkAppearanceProvider
.
NOTE: If you are using a functional component use one of the provided hooks instead.
Parameters
Component
Component the component to be wrapped
Examples
;; <Props & WithBpkAppearanceInjectedProps> { const bpkAppearance ...rest = thisprops; ... } <Config<Props DefaultProps>>MyComponent;
Returns Component the wrapped component with an extra bpkAppearance
prop.
isBpkDynamicValue
Check if a value is a BpkDynamicValue
Parameters
value
mixed the value to be checked
Returns boolean true if is a BpkDynamicValue
or false otherwise
unpackBpkDynamicValue
Takes in a BpkDynamicValue
and returns the correct value for provided appearance.
Parameters
value
mixed a dynamic value.appearance
Object the appearance preferences.
Examples
const color =
Returns mixed the value for the current color scheme.
If value
is not a valid dynamic value it will be returned back
unpackBpkDynamicStyle
Takes in a style object and returns the correct value for all properties, based on the current color scheme as provided by the nearest [BpkAppearanceProvider]
Parameters
Examples
const style = ;
Returns Object object with mapped properties for the current color scheme