Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Implementing CSS Modules for React Native

Implementing CSS Modules for React Native

Krister Kari's presentation about implementing CSS modules for React Native at ReactJS Community Spring 2018 meetup.

Finitec Meetups

April 10, 2018
Tweet

More Decks by Finitec Meetups

Other Decks in Technology

Transcript

  1. const styles = StyleSheet.create({ container: { flex: 1, justifyContent: "center",

    alignItems: "center", backgroundColor: "#F5FCFF", }, welcome: { fontSize: 20, textAlign: "center", margin: 10, }, instructions: { textAlign: "center", color: "#333333", marginBottom: 5, }, });
  2. const Container = styled.View` flex: 1; justify-content: center; align-items: center;

    background-color: #0f0; `; const Title = styled.Text` font-size: 30px; text-align: center; color: palevioletred; box-shadow: 1px 2px 3px red; elevation: 6; `; styled-components
  3. Wouldn’t it be nice if there would be an easier

    way to use all that CSS in React Native?
  4. .container { padding: 30px 10px; margin-top: 65px; align-items: center; border:

    2px dashed #f00; } <div className={styles.container}>Some text</div> import styles from "./styles.css";
  5. <style> ._20WEds96_Ee1ra54-24ePy { padding: 30px 10px; margin-top: 65px; align-items: center;

    border: 2px dashed #f00; } </style> <div class="_20WEds96_Ee1ra54-24ePy">Some text</div>
  6. What is included? React Native specific CSS parser Transformer to

    parse and live reload CSS Babel plugins to add className property to React Native to add platform specific extensions for CSS files
  7. .container { padding: 30px 10px; margin-top: 65px; align-items: center; border:

    2px dashed #f00; } { container: { paddingBottom: 30, paddingLeft: 10, paddingRight: 10, paddingTop: 30, marginTop: 65, alignItems: "center", borderColor: "#f00", borderStyle: "dashed", borderWidth: 2 } } ↓ ↓ ↓ ↓ ↓ ↓ CSS JS
  8. module.exports = { getTransformModulePath() { return require.resolve("./my-css-transformer.js"); }, getSourceExts() {

    return ["css"]; } } rn-cli.config.js Tell bundler to support additional file extensions Custom transformer
  9. module.exports.transform = function({ src, filename, options }) { if (filename.endsWith(".css"))

    { return upstreamTransformer.transform({ src: "module.exports = " + JSON.stringify(css2rn(src)), filename, options }); } return upstreamTransformer.transform({ src, filename, options }); }; my-css-transformer.js Call to CSS parser
  10. Node { type: 'JSXExpressionContainer', start: 67, end: 85, loc: SourceLocation

    { start: Position { line: 1, column: 67 }, end: Position { line: 1, column: 85 } }, expression: Node { type: 'ObjectExpression', start: 68, end: 84, loc: SourceLocation { start: [Position], end: [Position] }, properties: [ [Node] ] } } Abstract Syntax Tree (AST)
  11. import styles from "./styles.css"; styles.android.css - Android styles.ios.css - iOS

    styles.native.css - Android and iOS styles.css - Android, iOS and Web ↓
  12. import styles from "./styles.css"; import { Platform } from "react-native";

    var styles = Platform.OS === "ios" ? require("./styles.ios.css") : require("./styles.css"); ↓ ↓ ↓ ↓ ↓ ↓
  13. .container { background-color: red; } @media (orientation: landscape) { .container

    { background-color: blue; } } { container: { backgroundColor: "red" }, "@media (orientation: landscape)": { container: { backgroundColor: "blue" } } } CSS JS
  14. .container { background-color: red; } @media (orientation: landscape) { .container

    { background-color: blue; } } { __mediaQueries: { "@media (orientation: landscape)": [{ expressions: [ { feature: "orientation", modifier: undefined, value: "landscape" } ], inverse: false, type: "all" }] }, container: { backgroundColor: "red" }, "@media (orientation: landscape)": { container: { backgroundColor: "blue" } } } CSS JS
  15. import { Dimensions } from "react-native"; function getMatchObject() { const

    win = Dimensions.get("window"); return { width: win.width, height: win.height, orientation: win.width > win.height ? "landscape" : "portrait", "aspect-ratio": win.width / win.height, type: "screen" }; }
  16. .container { background-color: red; font-size: 20px; } @media (orientation: landscape)

    { .container { background-color: blue; } } { container: { backgroundColor: "blue", fontSize: 20 } } ↓ ↓ ↓ ↓ ↓ ↓ CSS JS
  17. To be implemented… CLI installer A better way to measure

    Media Queries runtime performance CSS Viewport units And more…