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

[docs] Add example for using styled-components as styled-engine #22788

Merged
merged 36 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c2d31cc
add emotion peer dependencies
mnajdova Sep 25, 2020
5ae933f
fixed types & tests
mnajdova Sep 25, 2020
18b0668
prettier
mnajdova Sep 25, 2020
f0ef95c
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 28, 2020
583d557
added example project
mnajdova Sep 28, 2020
0bdbd9d
name fix
mnajdova Sep 28, 2020
c7bebb8
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 28, 2020
92b2d6e
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 28, 2020
22e0700
prettier
mnajdova Sep 28, 2020
1598adf
changed example to use create-react-app
mnajdova Sep 28, 2020
4205477
updated project name
mnajdova Sep 28, 2020
77d956d
prettier
mnajdova Sep 28, 2020
8c85d35
removed eslintConfig
mnajdova Sep 28, 2020
3fcb447
removed App test
mnajdova Sep 28, 2020
69f6f7b
updated create-react-app package
mnajdova Sep 28, 2020
9b58298
changed version in example
mnajdova Sep 28, 2020
e4467ba
Update examples/create-react-app-with-styled-components-engine/src/Ap…
mnajdova Sep 28, 2020
735d705
Update examples/create-react-app-with-styled-components-engine/src/Ap…
mnajdova Sep 28, 2020
499c0d6
removed index.css
mnajdova Sep 28, 2020
33c7424
Update examples/create-react-app/package.json
mnajdova Sep 28, 2020
d554f85
Update examples/create-react-app-with-styled-components-engine/packag…
mnajdova Sep 28, 2020
be1c594
Update examples/create-react-app-with-styled-components-engine/packag…
mnajdova Sep 28, 2020
3d4d4c4
Update examples/create-react-app-with-styled-components-engine/public…
mnajdova Sep 28, 2020
c6f4e5b
removed index.css import
mnajdova Sep 28, 2020
56876d8
removed unreferenced aria-labelledby
mnajdova Sep 29, 2020
1fa072d
Update docs/src/pages/getting-started/example-projects/example-projec…
mnajdova Sep 29, 2020
13da531
Merge branch 'next' of https://github.com/mui-org/material-ui into next
mnajdova Sep 29, 2020
398ccd2
Merge branch 'next' into feat/add-styled-components-example
mnajdova Sep 29, 2020
b79fb3e
prettier
mnajdova Sep 29, 2020
f679732
Merge branch 'feat/add-styled-components-example' of https://github.c…
mnajdova Sep 29, 2020
436f3a1
Update examples/create-react-app-with-styled-components-engine/packag…
mnajdova Sep 29, 2020
171efee
renamed example & improved description
mnajdova Sep 29, 2020
c97e82f
Merge branch 'feat/add-styled-components-example' of https://github.c…
mnajdova Sep 29, 2020
a3095e1
renamed project name
mnajdova Sep 29, 2020
a6fc69c
added styled-components link
mnajdova Sep 29, 2020
f314d6f
prettier
mnajdova Sep 29, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You can find some example projects in the [GitHub repository](https://github.com
- [Preact](https://github.com/mui-org/material-ui/tree/next/examples/preact)
- [CDN](https://github.com/mui-org/material-ui/tree/next/examples/cdn)
- [Plain server-side](https://github.com/mui-org/material-ui/tree/next/examples/ssr)
- [Use styled-components as style engine](https://github.com/mui-org/material-ui/tree/next/examples/create-react-app-with-styled-components)
- And more

Create React App is an awesome project for learning React.
Expand Down
23 changes: 23 additions & 0 deletions examples/create-react-app-with-styled-components/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
25 changes: 25 additions & 0 deletions examples/create-react-app-with-styled-components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Create React App example

## How to use

Download the example [or clone the repo](https://github.com/mui-org/material-ui):

```sh
curl https://codeload.github.com/mui-org/material-ui/tar.gz/next | tar -xz --strip=2 material-ui-next/examples/create-react-app-styled-components
cd create-react-app-styled-components
```

Install it and run:

```sh
npm install
npm start
```

or:

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/next/examples/create-react-app-styled-components)

## The idea behind the example

This example demonstrates how you can setup [Create React App](https://github.com/facebookincubator/create-react-app) with [styled-components](https://styled-components.com/) as a style engine for your application.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { addWebpackAlias, override } = require('customize-cra');

module.exports = override(
addWebpackAlias({
'@material-ui/styled-engine': '@material-ui/styled-engine-sc',
}),
);
39 changes: 39 additions & 0 deletions examples/create-react-app-with-styled-components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "create-react-app-with-styled-components",
"version": "5.0.0",
"private": true,
"dependencies": {
"@material-ui/core": "next",
"@material-ui/lab": "next",
"@material-ui/styled-engine-sc": "next",
"@testing-library/jest-dom": "latest",
"@testing-library/react": "latest",
"@testing-library/user-event": "latest",
"react": "latest",
"react-dom": "latest",
"react-scripts": "latest",
"styled-components": "latest"
},
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-app-rewired eject"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"customize-cra": "latest",
"react-app-rewired": "latest"
}
}
Binary file not shown.
45 changes: 45 additions & 0 deletions examples/create-react-app-with-styled-components/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<!-- Fonts to support Material Design -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
15 changes: 15 additions & 0 deletions examples/create-react-app-with-styled-components/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';
import Slider from '@material-ui/lab/SliderStyled';
import Typography from '@material-ui/core/Typography';

export default function App() {
return (
<div>
<Typography>Styled slider powered by styled-components</Typography>
<Slider defaultValue={30} color="primary" />
<Slider defaultValue={30} color="secondary" />
<Slider defaultValue={30} disabled />
<Slider defaultValue={30} valueLabelDisplay="auto" step={10} marks min={10} max={110} />
</div>
);
}
16 changes: 16 additions & 0 deletions examples/create-react-app-with-styled-components/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root'),
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
141 changes: 141 additions & 0 deletions examples/create-react-app-with-styled-components/src/serviceWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// This optional code is used to register a service worker.
// register() is not called by default.

// This lets the app load faster on subsequent visits in production, and gives
// it offline capabilities. However, it also means that developers (and users)
// will only see deployed updates on subsequent visits to a page, after all the
// existing tabs open on the page have been closed, since previously cached
// resources are updated in the background.

// To learn more about the benefits of this model and instructions on how to
// opt-in, read https://bit.ly/CRA-PWA

const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.0/8 are considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);

export function register(config) {
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
// The URL constructor is available in all browsers that support SW.
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
if (publicUrl.origin !== window.location.origin) {
// Our service worker won't work if PUBLIC_URL is on a different origin
// from what our page is served on. This might happen if a CDN is used to
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
return;
}

window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;

if (isLocalhost) {
// This is running on localhost. Let's check if a service worker still exists or not.
checkValidServiceWorker(swUrl, config);

// Add some additional logging to localhost, pointing developers to the
// service worker/PWA documentation.
navigator.serviceWorker.ready.then(() => {
console.log(
'This web app is being served cache-first by a service ' +
'worker. To learn more, visit https://bit.ly/CRA-PWA'
);
});
} else {
// Is not localhost. Just register service worker
registerValidSW(swUrl, config);
}
});
}
}

function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
);

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');

// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
};
})
.catch(error => {
console.error('Error during service worker registration:', error);
});
}

function checkValidServiceWorker(swUrl, config) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl, {
headers: { 'Service-Worker': 'script' },
})
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
const contentType = response.headers.get('content-type');
if (
response.status === 404 ||
(contentType != null && contentType.indexOf('javascript') === -1)
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl, config);
}
})
.catch(() => {
console.log(
'No internet connection found. App is running in offline mode.'
);
});
}

export function unregister() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready
.then(registration => {
registration.unregister();
})
.catch(error => {
console.error(error.message);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';
5 changes: 3 additions & 2 deletions examples/create-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
"eject": "react-scripts eject"
},
"dependencies": {
"@material-ui/core": "latest",
"clsx": "latest",
"@emotion/core": "latest",
"@emotion/styled": "latest",
"@material-ui/core": "next",
"react": "latest",
"react-dom": "latest",
"react-scripts": "latest"
Expand Down
2 changes: 1 addition & 1 deletion examples/create-react-app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function App() {
<Container maxWidth="sm">
<Box my={4}>
<Typography variant="h4" component="h1" gutterBottom>
Create React App v4-beta example
Create React App v5-beta example
</Typography>
<ProTip />
<Copyright />
Expand Down