Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding gatsby-plugin-styletron and an example site for it. #2365

Merged
merged 1 commit into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions examples/using-styletron/.eslintrc
@@ -0,0 +1,8 @@
{
"env": {
"browser": true
},
"globals": {
"graphql": false
}
}
4 changes: 4 additions & 0 deletions examples/using-styletron/README.md
@@ -0,0 +1,4 @@
# Using Styletron CSS-in-JS engine

Read more on how to style components
with [Styletron](http://styletron.js.org/).
15 changes: 15 additions & 0 deletions examples/using-styletron/gatsby-config.js
@@ -0,0 +1,15 @@
module.exports = {
siteMetadata: {
title: `Gatsby with Styletron`,
},
plugins: [
`gatsby-plugin-styletron`,
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: `UA-93349937-2`,
},
},
`gatsby-plugin-offline`,
],
}
29 changes: 29 additions & 0 deletions examples/using-styletron/package.json
@@ -0,0 +1,29 @@
{
"name": "gatsby-example-using-styletron",
"private": true,
"description": "Gatsby example site using the styletron plugin",
"version": "1.0.0",
"author": "Nadiia Dmytrenko <nadiia.dmytrenko@gmail.com>",
"dependencies": {
"gatsby": "latest",
"gatsby-link": "latest",
"gatsby-plugin-google-analytics": "latest",
"gatsby-plugin-offline": "latest",
"gatsby-plugin-styletron": "latest",
"lodash": "^4.16.4",
"react-typography": "^0.15.0",
"slash": "^1.0.0",
"styletron-react": "^3.0.0-rc.2",
"typography": "^0.15.8",
"typography-breakpoint-constants": "^0.14.0"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"main": "n/a",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build"
}
}
64 changes: 64 additions & 0 deletions examples/using-styletron/src/html.js
@@ -0,0 +1,64 @@
import React, { Component } from "react"
import * as PropTypes from "prop-types"
import { TypographyStyle } from "react-typography"
import typography from "./utils/typography"

let stylesStr
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
} catch (e) {
console.log(e)
}
}

const propTypes = {
headComponents: PropTypes.node.isRequired,
body: PropTypes.node.isRequired,
postBodyComponents: PropTypes.node.isRequired,
}

class Html extends Component {
render() {
let css
if (process.env.NODE_ENV === `production`) {
css = (
<style
id="gatsby-inlined-css"
dangerouslySetInnerHTML={{ __html: stylesStr }}
/>
)
}

return (
<html op="news" lang="en">
<head>
{this.props.headComponents}

<meta name="referrer" content="origin" />
<meta charSet="utf-8" />
<meta name="description" content="Gatsby example site using Glamor" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be updated to say Styletron instead of Glamor.

<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Gatsby Glamor</title>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

<TypographyStyle typography={typography} />
{css}
</head>
<body>
<div
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
{this.props.postBodyComponents}
</body>
</html>
)
}
}

Html.propTypes = propTypes

module.exports = Html
43 changes: 43 additions & 0 deletions examples/using-styletron/src/pages/index.js
@@ -0,0 +1,43 @@
import React from "react"
import {styled} from "styletron-react"

// Create a Container element that'll render a <div> tag with some styles
const Container = styled('div', {
margin: `0 auto`,
marginTop: `3rem`,
padding: `1.5rem`,
maxWidth: 800,
color: `red`,
})

// Create a Title element that'll render an <h1> tag with some styles
const Title = styled('h1', {
fontSize: `1.5em`,
textAlign: `center`,
color: `#744d9e`,
})

// Create a Wrapper element that'll render a <section> tag with some styles
const Wrapper = styled('div', {
padding: `4em`,
background: `#f5f3f7`,
})

class IndexPage extends React.Component {
render() {
return (
<Container>
<Wrapper>
<Title>Hello World, this is my first component styled with Styletron!</Title>
<p>
<a href="https://www.gatsbyjs.org/packages/gatsby-plugin-styletron/">
gatsby-plugin-styletron docs
</a>
</p>
</Wrapper>
</Container>
)
}
}

export default IndexPage
40 changes: 40 additions & 0 deletions examples/using-styletron/src/utils/typography.js
@@ -0,0 +1,40 @@
import Typography from "typography"
import {
MOBILE_MEDIA_QUERY,
TABLET_MEDIA_QUERY,
} from "typography-breakpoint-constants"

const options = {
baseFontSize: `18px`,
baseLineHeight: 1.45,
blockMarginBottom: 0.75,
scaleRatio: 2.15,
overrideStyles: ({ rhythm, scale }, options) => {
return {
"h1,h2,h3,h4": {
lineHeight: 1.2,
},
[TABLET_MEDIA_QUERY]: {
// Make baseFontSize on mobile 17px.
html: {
fontSize: `${17 / 16 * 100}%`,
},
},
[MOBILE_MEDIA_QUERY]: {
// Make baseFontSize on mobile 16px.
html: {
fontSize: `${16 / 16 * 100}%`,
},
},
}
},
}

const typography = new Typography(options)

// Hot reload typography in development.
if (process.env.NODE_ENV !== `production`) {
typography.injectStyles()
}

export default typography
5 changes: 5 additions & 0 deletions packages/gatsby-plugin-styletron/.babelrc
@@ -0,0 +1,5 @@
{
"presets": [
["../../.babelrc.js", { "browser": true }]
]
}
5 changes: 5 additions & 0 deletions packages/gatsby-plugin-styletron/.eslintrc
@@ -0,0 +1,5 @@
{
"rules": {
"react/display-name": [0]
}
}
5 changes: 3 additions & 2 deletions packages/gatsby-plugin-styletron/.gitignore
@@ -1,2 +1,3 @@
/*.js
!index.js
/gatsby-node.js
/gatsby-ssr.js
/gatsby-browser.js
16 changes: 15 additions & 1 deletion packages/gatsby-plugin-styletron/README.md
@@ -1,3 +1,17 @@
# gatsby-plugin-styletron

Stub README
A [Gatsby](https://github.com/gatsbyjs/gatsby) plugin for [styletron](https://github.com/rtsao/styletron) with built-in server-side rendering support.

## Install

`npm install --dev gatsby-plugin-styletron`

## How to use

Edit `gatsby-config.js`

```javascript
plugins: [
`gatsby-plugin-styletron`,
]
```
7 changes: 5 additions & 2 deletions packages/gatsby-plugin-styletron/package.json
Expand Up @@ -11,13 +11,16 @@
"keywords": [
"gatsby"
],
"author": "Kyle Mathews &lt;mathews.kyle@gmail.com&gt;",
"author": "Nadiia Dmytrenko &lt;nadiia.dmytrenko@gmail.com&gt;",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.26.0",
"cross-env": "^5.0.5"
},
"dependencies": {
"babel-runtime": "^6.26.0"
"babel-runtime": "^6.26.0",
"styletron-client": "^3.0.0-rc.1",
"styletron-react": "^3.0.0-rc.2",
"styletron-server": "^3.0.0-rc.1"
}
}
Empty file.
11 changes: 11 additions & 0 deletions packages/gatsby-plugin-styletron/src/gatsby-browser.js
@@ -0,0 +1,11 @@
const React = require(`react`)
const Styletron = require(`styletron-client`)
const { StyletronProvider } = require(`styletron-react`)

const styletron = new Styletron()

exports.wrapRootComponent = ({ Root }) => () => (
<StyletronProvider styletron={styletron}>
<Root />
</StyletronProvider>
)
25 changes: 25 additions & 0 deletions packages/gatsby-plugin-styletron/src/gatsby-ssr.js
@@ -0,0 +1,25 @@
const React = require(`react`)
const Styletron = require(`styletron-server`)
const { StyletronProvider } = require(`styletron-react`)
const { renderToString } = require(`react-dom/server`)

exports.replaceRenderer = ({ bodyComponent, setHeadComponents, replaceBodyHTMLString }) => {
const styletron = new Styletron()

const app = (
<StyletronProvider styletron={styletron}>
{bodyComponent}
</StyletronProvider>
)

replaceBodyHTMLString(renderToString(app))

const stylesheets = styletron.getStylesheets()
const headComponents = stylesheets.map((sheet, index) => (
<style media={sheet.media || `all`} key={index}>
{sheet.css}
</style>
))

setHeadComponents(headComponents)
}
38 changes: 37 additions & 1 deletion yarn.lock
Expand Up @@ -6,6 +6,10 @@
version "6.0.88"
resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.88.tgz#f618f11a944f6a18d92b5c472028728a3e3d4b66"

"@types/react@*":
version "16.0.10"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.0.10.tgz#a24b630f5f1f170866a148a147d4fc8636ea88e0"

JSONStream@^1.0.3, JSONStream@^1.0.4:
version "1.3.1"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.1.tgz#707f761e01dae9e16f1bcf93703b78c70966579a"
Expand Down Expand Up @@ -5763,7 +5767,7 @@ ini@^1.3.2, ini@^1.3.3, ini@^1.3.4, ini@~1.3.0:
version "1.3.4"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e"

inline-style-prefixer@^3.0.6:
inline-style-prefixer@^3.0.3, inline-style-prefixer@^3.0.6:
version "3.0.8"
resolved "https://registry.yarnpkg.com/inline-style-prefixer/-/inline-style-prefixer-3.0.8.tgz#8551b8e5b4d573244e66a34b04f7d32076a2b534"
dependencies:
Expand Down Expand Up @@ -11652,6 +11656,38 @@ styled-jsx@^1.0.10:
string-hash "1.1.1"
stylis "3.2.18"

styletron-client@^3.0.0-rc.1:
version "3.0.0-rc.1"
resolved "https://registry.yarnpkg.com/styletron-client/-/styletron-client-3.0.0-rc.1.tgz#291a820a141bf212bef9a264ddf9790531470bda"
dependencies:
styletron-core "^3.0.0-rc.1"

styletron-core@^3.0.0-rc.1:
version "3.0.0-rc.1"
resolved "https://registry.yarnpkg.com/styletron-core/-/styletron-core-3.0.0-rc.1.tgz#3a6b0a2f82f4cd666de317af21b05006221394ef"

styletron-react@^3.0.0-rc.2:
version "3.0.0-rc.2"
resolved "https://registry.yarnpkg.com/styletron-react/-/styletron-react-3.0.0-rc.2.tgz#3be968d2bb917f6490dd849b73d996f1cffd24d7"
dependencies:
"@types/react" "*"
prop-types "^15.5.8"
styletron-client "^3.0.0-rc.1"
styletron-server "^3.0.0-rc.1"
styletron-utils "^3.0.0-rc.1"

styletron-server@^3.0.0-rc.1:
version "3.0.0-rc.1"
resolved "https://registry.yarnpkg.com/styletron-server/-/styletron-server-3.0.0-rc.1.tgz#998d22f5c1319dfd7b2f5eb7270819a0f0ac51b0"
dependencies:
styletron-core "^3.0.0-rc.1"

styletron-utils@^3.0.0-rc.1:
version "3.0.0-rc.1"
resolved "https://registry.yarnpkg.com/styletron-utils/-/styletron-utils-3.0.0-rc.1.tgz#0ae06bbc9009aa904add7dbc1f5b34702e153e75"
dependencies:
inline-style-prefixer "^3.0.3"

stylis@3.2.18:
version "3.2.18"
resolved "https://registry.yarnpkg.com/stylis/-/stylis-3.2.18.tgz#211661f13b636e9e451456a1aadcec31248edf0e"
Expand Down