项目作者: retyui

项目描述 :
Better styling for react-native
高级语言: TypeScript
项目地址: git://github.com/retyui/react-native-stylex.git
创建时间: 2020-01-10T19:52:19Z
项目社区:https://github.com/retyui/react-native-stylex

开源协议:MIT License

下载


react-native-stylex

Better styling for react-native

react-native-stylex on npm
react-native-stylex downloads
react-native-stylex install size
CI status

Module features:

Integrations

Install

react-native-stylex requires react-native 0.59.0 or later.

1️⃣ Add module

  1. yarn add react-native-stylex
  2. # or npm install react-native-stylex

2️⃣ Add theme <ThemeProvider></ThemeProvider>

Stylex provides component, which makes the theme available to the rest of your app:

  1. import { ThemeProvider } from "react-native-stylex";
  2. import {
  3. createBreakpointsMatcher,
  4. createBreakpoints,
  5. maxWidth,
  6. } from "react-native-stylex/media-query";
  7. const breakpoints = {
  8. xs: 360,
  9. sm: 600,
  10. md: 960,
  11. lg: 1280,
  12. xl: 1920,
  13. };
  14. const { up, down, only, between } = createBreakpoints(breakpoints);
  15. const applyBreakpoints = createBreakpointsMatcher(breakpoints, maxWidth);
  16. const theme = {
  17. palette: { textColor: "black" },
  18. breakpoints: { up, down, only, between, apply: applyBreakpoints },
  19. utils: {
  20. fade: (color, value) => {},
  21. },
  22. };
  23. const Root = () => (
  24. <ThemeProvider value={theme}>
  25. <App ></App>
  26. </ThemeProvider>
  27. );
  28. export default Root;

3️⃣ Create styles makeUseStyles(...)

Stylex provides a helper function to inject styles to your component.

Normally, you’ll use it in this way:

  1. import { makeUseStyles } from "react-native-stylex";
  2. import { maxWidth } from "react-native-stylex/media-query";
  3. export const useStyles = makeUseStyles(({ palette, utils, breakpoints }) => ({
  4. root: {
  5. color: utils.fade(palette.textColor, 0.5),
  6. height: 100,
  7. // On screens that are 320 or less, set the height to 69
  8. ...maxWidth(320, { height: 69 }),
  9. },
  10. text: {
  11. fontSize: 16, // default value
  12. ...breakpoints.down("lg", { fontSize: 18 }), // if window width 0..1280
  13. ...breakpoints.down("sm", { fontSize: 20 }), // if window width 0..600
  14. },
  15. // The same example that you see above but unsing a 'applyBreakpoints'
  16. title: {
  17. fontSize: breakpoints.apply({
  18. sm: 20, // if window width 0....600
  19. lg: 18, // if window width 601..1280
  20. default: 16, // if window width 1281...
  21. }),
  22. },
  23. }));

4️⃣ Inject styles useStyles(...) & withStyles(...)

And finally just use in component:

  1. import React, { Component } from "react";
  2. import useStyles from "./styles";
  3. // Functional component (hooks variant)
  4. const Root = () => {
  5. const styles = useStyles();
  6. return <View style={styles.root} ></View>;
  7. };
  8. export default Root;
  9. //--------------------------------
  10. // Class component (HOC variant)
  11. class Root extends Component {
  12. render() {
  13. const { styles } = this.props;
  14. return <View style={styles.root} ></View>;
  15. }
  16. }
  17. export default withStyles(useStyles)(Root);

5️⃣ Do you use a Typescript ?

Alternative solutions

  • @emotion/native"">@emotion/native @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc"">react-native-stylex downloads
  • nativewind @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc">react-native-stylex downloads
  • @shopify/restyle @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc"">react-native-stylex downloads
  • tamagui @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc">react-native-stylex downloads
  • twrnc @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc">react-native-stylex downloads
  • tailwind-rn @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc">react-native-stylex downloads
  • dripsy @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc">react-native-stylex downloads
  • react-native-unistyles @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc">react-native-stylex downloads
  • react-native-zephyr @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc">react-native-stylex downloads
  • @fast-styles/react @emotion/native-vs-@fast-styles/react-vs-@shopify/restyle-vs-dripsy-vs-nativewind-vs-react-native-unistyles-vs-react-native-zephyr-vs-tailwind-rn-vs-tamagui-vs-twrnc"">react-native-stylex downloads