ampproject / amp-react-prototype

A scratch pad to experiment with React rendered AMP Components
Apache License 2.0
36 stars 9 forks source link

JSS extractor #56

Closed dvoytenko closed 4 years ago

dvoytenko commented 4 years ago

Related to https://github.com/ampproject/wg-bento/issues/7

This pull request highlights several important CSS interactions:

1. A CSS in JS example

See View1.css.js and View1.js.

It uses a popular JSS framework. It was picked for this demo because it's one of a more flexible frameworks that can be mapped to other approaches, such as import .css, styled-components, etc.

const styles = {
  myButton: {color: 'green', display: 'flex', ...}
}

const useStyles = createUseStyles(styles);

function MyComponent() {
  const classes = useStyles();
  return (<div className={classes.myButton}></div>);
}

2. How to extract CSS-in-JSS declarations into a stylesheet

See extract-css.js.

The main steps are (and simpler steps are likely possible):

  1. Use rollup to get style bundles for JSS files.
  2. Load the resulting JSS declarations as a single JSS object.
  3. Create a stylesheet using JSS API.