Skip to content

Commit

Permalink
Add stylus example (#1479)
Browse files Browse the repository at this point in the history
* Add stylus example

* Add CSS modules to example

* Update readme
  • Loading branch information
iansinnott authored and KyleAMathews committed Jul 13, 2017
1 parent f04809d commit 5e064c1
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/using-stylus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Using gatsby-plugin-stylus

https://using-stylus.gatsbyjs.org

Example site that demonstrates how to build Gatsby sites
using the Gatsby plugin `gatsby-plugin-stylus`
which sets up Webpack to run [Stylus][] code.

The example includes both "plain" Stylus and usage of CSS modules.

[Stylus]: https://github.com/stylus/stylus
32 changes: 32 additions & 0 deletions examples/using-stylus/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
plugins: [
`gatsby-plugin-stylus`, // <--- This is the important part
`gatsby-plugin-offline`,
{
resolve: `gatsby-plugin-google-analytics`,
options: {
trackingId: `UA-93349937-2`,
},
},
],
}

/**
* NOTE: You can use stylus plugins as well! Just import them as you normally
* would and pass them to the `use` option of the stylus plugin.
*
* --------------------------------------------------------------------------
*
* const rupture = require(`rupture`)
*
* module.exports = {
* plugins: [
* {
* resolve: `gatsby-plugin-stylus`,
* options: {
* use: [rupture()],
* },
* },
* ],
* }
*/
21 changes: 21 additions & 0 deletions examples/using-stylus/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "using-stylus",
"version": "1.0.0",
"description": "Example Gatsby website using the Gatsby plugin gatsby-plugin-stylus",
"main": "index.js",
"scripts": {
"develop": "gatsby develop",
"build": "gatsby build"
},
"author": "Ian Sinnott <ian@iansinnott.com>",
"license": "MIT",
"dependencies": {
"gatsby": "latest",
"gatsby-link": "latest",
"gatsby-plugin-google-analytics": "latest",
"gatsby-plugin-offline": "latest",
"gatsby-plugin-stylus": "latest",
"react-typography": "^0.15.10",
"typography": "^0.15.10"
}
}
69 changes: 69 additions & 0 deletions examples/using-stylus/src/html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React, { Component } from "react"
import * as PropTypes from "prop-types"
import Typography from "typography"
import { TypographyStyle } from "react-typography"

const typography = new 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 demoing stylus plugin"
/>
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>Using gatsby-plugin-stylus</title>
<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
45 changes: 45 additions & 0 deletions examples/using-stylus/src/pages/css-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from "react"
import Link from 'gatsby-link'
import s from "./css-modules.module.styl"

class CssModules extends React.Component {
render() {
return (
<div>
<header className={s.header}>
<h1 className={s["the-stylus-class"]}>Hi stylish friends</h1>
<p>
All the styles for this page are written using <a href="https://github.com/stylus/stylus">Stylus</a>
</p>
<p>Open the inspector and inspect the class names of these elements.</p>
<Link to="/" className={s.cta}>View the plain example</Link>
</header>
<section className={s.main}>
<div className={s["stylus-nav-example"]}>
<h2>Nav example</h2>
<ul>
<li>
<a href="#">Store</a>
</li>
<li>
<a href="#">Help</a>
</li>
<li>
<a href="#">Logout</a>
</li>
</ul>
</div>

{/* This is intentionally global. See the stylus file */}
<div className="grid">
<div>1</div>
<div>2</div>
<div>2</div>
</div>
</section>
</div>
)
}
}

export default CssModules
73 changes: 73 additions & 0 deletions examples/using-stylus/src/pages/css-modules.module.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Variables don't need a dollar sign but it makes them easy to identify later
// in code
$color = pink

borderRadius($radius = 5px)
-webkit-border-radius $radius
-moz-border-radius $radius
border-radius $radius

.header
padding 20px
background thistle

h1
text-decoration none

.cta
display inline-block
color white
text-transform uppercase
text-decoration none
font-family sans-serif
padding 20px
background darken(thistle, 20%)

a
font-weight bold

section
padding 20px

.stylus-nav-example
ul
margin 0
padding 0
list-style none


li
display inline-block


a
borderRadius(3px)

background $color
display block
margin-right 6px
padding 3px 9px
text-decoration none


.the-stylus-class
text-decoration underline


:global(.grid) div
border 1px solid black
padding 0.5rem
margin-bottom 1rem
background #fafafa
border 1px solid #ddd
background orange


@media (min-width: 400px)
:global(.grid) div
background transparentify(blue, 0.4)

@media (min-width: 900px)
:global(.grid) div
background transparentify(pink, 0.1)

42 changes: 42 additions & 0 deletions examples/using-stylus/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from "react"
import Link from 'gatsby-link'
import "./index.styl"

class CssModules extends React.Component {
render() {
return (
<div>
<header className="header">
<h1 className="the-stylus-class">Hi stylish friends</h1>
<p>
All the styles for this page are written using <a href="https://github.com/stylus/stylus">Stylus</a>.
</p>
<Link to="/css-modules/" className="cta">View the CSS Modules example</Link>
</header>
<section className="main">
<div className="stylus-nav-example">
<h2>Nav example</h2>
<ul>
<li>
<a href="#">Store</a>
</li>
<li>
<a href="#">Help</a>
</li>
<li>
<a href="#">Logout</a>
</li>
</ul>
</div>
<div className="grid">
<div>1</div>
<div>2</div>
<div>2</div>
</div>
</section>
</div>
)
}
}

export default CssModules
71 changes: 71 additions & 0 deletions examples/using-stylus/src/pages/index.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Variables don't need a dollar sign but it makes them easy to identify later
// in code
$color = pink

borderRadius($radius = 5px)
-webkit-border-radius $radius
-moz-border-radius $radius
border-radius $radius

.header
padding 20px
background thistle
h1
text-decoration none
.cta
display inline-block
color white
text-transform uppercase
text-decoration none
font-family sans-serif
padding 20px
background darken(thistle, 20%)

a
font-weight bold

section
padding 20px

.stylus-nav-example
ul
margin 0
padding 0
list-style none


li
display inline-block


a
borderRadius(3px)

background $color
display block
margin-right 6px
padding 3px 9px
text-decoration none



.the-stylus-class
text-decoration underline


.grid div
border 1px solid black
padding 0.5rem
margin-bottom 1rem
background #fafafa
border 1px solid #ddd


@media (min-width: 400px)
.grid div
background transparentify(blue, 0.4)

@media (min-width: 900px)
.grid div
background transparentify(pink, 0.1)

0 comments on commit 5e064c1

Please sign in to comment.