Commit 85697891 authored by Arana Jhonny's avatar Arana Jhonny Committed by Guillermo Rauch

Add cxs example. (#613)

* add cxs example

* fix example location
parent ef06aeee
...@@ -95,7 +95,7 @@ export default () => ( ...@@ -95,7 +95,7 @@ export default () => (
<p><details> <p><details>
<summary><b>Examples</b></summary> <summary><b>Examples</b></summary>
<ul><li><a href="./examples/with-styled-components">Styled components</a></li><li><a href="./examples/with-styletron">Styletron</a></li><li><a href="./examples/with-glamor">Glamor</a></li></ul> <ul><li><a href="./examples/with-styled-components">Styled components</a></li><li><a href="./examples/with-styletron">Styletron</a></li><li><a href="./examples/with-glamor">Glamor</a></li><li><a href="./examples/with-cxs">Cxs</a></li></ul>
</details></p> </details></p>
It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles: It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles:
......
# Example app with cxs
## How to use
Download the example (or clone the repo)[https://github.com/zeit/next.js.git]:
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-cxs
cd with-cxs
```
Install it and run:
```bash
npm install
npm run dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
This example features how yo use a different styling solution than [styled-jsx](https://github.com/zeit/styled-jsx) that also supports universal styles. That means we can serve the required styles for the first render within the HTML and then load the rest in the client. In this case we are using [cxs](https://github.com/jxnblk/cxs/).
For this purpose we are extending the `<Document />` and injecting the server side rendered styles into the `<head>`.
{
"name": "with-cxs",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"cxs": "^3.0.0",
"next": "^2.0.0-beta"
},
"author": "",
"license": "ISC"
}
import Document, { Head, Main, NextScript } from 'next/document'
import cxs from 'cxs'
export default class MyDocument extends Document {
static async getInitialProps ({ renderPage }) {
const page = renderPage()
let style = cxs.getCss()
return { ...page, style }
}
render () {
return (
<html>
<Head>
<title>My page</title>
<style dangerouslySetInnerHTML={{ __html: this.props.style }} />
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
)
}
}
import React from 'react'
import cxs from 'cxs'
export default () => (
<div className={cx.root}>
<h1 className={cx.title}>My page</h1>
</div>
)
const cx = {
root: cxs({
width: 80,
height: 60,
background: 'white',
':hover': {
background: 'black'
}
}),
title: cxs({
marginLeft: 5,
color: 'black',
fontSize: 22,
':hover': {
color: 'white'
}
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment