Files
react-native/RNTester/js/examples/Button/ButtonExample.js
T
Héctor Ramos a397d330a4 Support light and dark themes in RNTester
Summary:
Initial conversion of RNTester to support light and dark themes. Theming is implemented by providing the desired color theme via context. Example:

```
const ThemedContainer = props => (
  <RNTesterThemeContext.Consumer>
    {theme => {
      return (
        <View
          style={{
            paddingHorizontal: 8,
            paddingVertical: 16,
            backgroundColor: theme.SystemBackgroundColor,
          }}>
          {props.children}
        </View>
      );
    }}
  </RNTesterThemeContext.Consumer>
);
```

As RNTester's design follows the base iOS system appearance, I've chosen light and dark themes based on the actual iOS 13 semantic colors. The themes are RNTester-specific, however, and we'd expect individual apps to build their own color palettes.

## Examples

The new Appearance Examples screen demonstrates how context can be used to force a theme. It also displays the list of colors in each RNTester theme.

https://pxl.cl/HmzW (screenshot: Appearance Examples screen on RNTester with Dark Mode enabled. Displays useColorScheme hook, and context examples.)
https://pxl.cl/HmB3 (screenshot: Same screen, with light and dark RNTester themes visible)

Theming support in this diff mostly focused on the main screen and the Dark Mode examples screen. This required updating the components used by most of the examples, as you can see in this Image example:
https://pxl.cl/H0Hv (screenshot: Image Examples screen in Dark Mode theme)

Note that I have yet to go through every single example screen to update it. There's individual cases, such as the FlatList example screen, that are not fully converted to use a dark theme when appropriate. This can be taken care later as it's non-blocking.

Reviewed By: zackargyle

Differential Revision: D16681909

fbshipit-source-id: e47484d4b3f0963ef0cc3d8aff8ce3e9051ddbae
2019-08-31 10:05:06 -07:00

128 lines
3.6 KiB
JavaScript

/**
* 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';
const React = require('react');
const {Alert, Button, View, StyleSheet} = require('react-native');
import {RNTesterThemeContext} from '../../components/RNTesterTheme';
function onButtonPress(buttonName) {
Alert.alert(`${buttonName} has been pressed!`);
}
exports.displayName = 'ButtonExample';
exports.framework = 'React';
exports.title = '<Button>';
exports.description = 'Simple React Native button component.';
exports.examples = [
{
title: 'Simple Button',
description: ('The title and onPress handler are required. It is ' +
'recommended to set accessibilityLabel to help make your app usable by ' +
'everyone.': string),
render: function(): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<Button
onPress={() => onButtonPress('Simple')}
testID="simple_button"
color={theme.LinkColor}
title="Press Me"
accessibilityLabel="See an informative alert"
/>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Adjusted color',
description: ('Adjusts the color in a way that looks standard on each ' +
'platform. On iOS, the color prop controls the color of the text. On ' +
'Android, the color adjusts the background color of the button.': string),
render: function(): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<Button
onPress={() => onButtonPress('Purple')}
testID="purple_button"
color={theme.SystemPurpleColor}
title="Press Purple"
accessibilityLabel="Learn more about purple"
/>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Fit to text layout',
description: ('This layout strategy lets the title define the width of ' +
'the button': string),
render: function(): React.Node {
return (
<RNTesterThemeContext.Consumer>
{theme => {
return (
<View style={styles.container}>
<Button
onPress={() => onButtonPress('Left')}
testID="left_button"
color={theme.LinkColor}
title="This looks great!"
accessibilityLabel="This sounds great!"
/>
<Button
onPress={() => onButtonPress('Right')}
testID="right_button"
color={theme.SystemPurpleColor}
title="Ok!"
accessibilityLabel="Ok, Great!"
/>
</View>
);
}}
</RNTesterThemeContext.Consumer>
);
},
},
{
title: 'Disabled Button',
description: 'All interactions for the component are disabled.',
render: function(): React.Node {
return (
<Button
disabled
onPress={() => onButtonPress('Disabled')}
testID="disabled_button"
title="I Am Disabled"
accessibilityLabel="See an informative alert"
/>
);
},
},
];
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
justifyContent: 'space-between',
},
});