Skip to content

Instantly share code, notes, and snippets.

@just-boris
Last active January 22, 2019 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save just-boris/76856b7d51772a1af5b978196155cab3 to your computer and use it in GitHub Desktop.
Save just-boris/76856b7d51772a1af5b978196155cab3 to your computer and use it in GitHub Desktop.
React scoped styling options
.cache
dist
node_modules
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Emotion-based styles</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
</style>
</head>
<body>
<div id="app"></div>
<script src="fps.js"></script>
<script src="emotion.js"></script>
</body>
</html>
import React from "react";
import { render } from "react-dom";
import styled from "@emotion/styled";
const Item = styled.div({
color: "steelblue",
padding: "10px",
borderBottom: "1px solid spacegray"
});
class Demo extends React.Component {
constructor() {
super();
this.state = {
showList: false
};
}
toggle() {
this.setState({
showList: !this.state.showList
});
}
render() {
const { showList } = this.state;
return (
<React.Fragment>
<h1>Long list demo</h1>
<button onClick={() => this.toggle()}>
{showList ? "Hide list" : "Show list"}
</button>
{showList &&
Array.from({ length: 500 }, (_, i) => (
<Item key={i}>{`Item ${i}`}</Item>
))}
</React.Fragment>
);
}
}
render(<Demo />, document.getElementById("app"));
import Stats from "stats.js";
const stats = new Stats();
document.body.appendChild(stats.dom);
stats.dom.style.left = "";
stats.dom.style.right = "0";
function loop() {
stats.update();
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
{
"name": "react-styled-options",
"version": "1.0.0",
"module": "dist/index.js",
"scripts": {
"build": "parcel build *.html",
"start": "parcel serve *.html"
},
"devDependencies": {
"parcel-bundler": "^1.11.0"
},
"dependencies": {
"@emotion/core": "^10.0.6",
"@emotion/styled": "^10.0.6",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"react-shade": "^0.3.0",
"stats.js": "^0.17.0"
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Shadow dom based styles</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
</style>
</head>
<body>
<div id="app"></div>
<script src="fps.js"></script>
<script src="react-shade.js"></script>
</body>
</html>
import React from "react";
import { render } from "react-dom";
import { styled } from "react-shade";
const Item = styled({
":host": {
color: "steelblue",
padding: "10px",
borderBottom: "1px solid spacegray"
}
});
class Demo extends React.Component {
constructor() {
super();
this.state = {
showList: false
};
}
toggle() {
this.setState({
showList: !this.state.showList
});
}
render() {
const { showList } = this.state;
return (
<React.Fragment>
<h1>Long list demo</h1>
<button onClick={() => this.toggle()}>
{showList ? "Hide list" : "Show list"}
</button>
{showList &&
Array.from({ length: 500 }, (_, i) => (
<Item key={i}>{`Item ${i}`}</Item>
))}
</React.Fragment>
);
}
}
render(<Demo />, document.getElementById("app"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment