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

Add codesandbox example to the project folder #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "react-wait-example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build"
},
"dependencies": {
"react": ">=16.7.0-alpha",
"react-dom": ">=16.7.0-alpha",
"react-scripts": "2.0.3",
"react-wait": "0.3.0"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
14 changes: 14 additions & 0 deletions example/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>react-wait example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>

<body>
<div id="root"></div>
</body>

</html>
89 changes: 89 additions & 0 deletions example/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";

import { useWait, Waiter } from "react-wait";

import "./styles.css";

function Spinner() {
return (
<img
className="spinner"
alt="spinner"
width="24"
height="24"
src="https://i.pinimg.com/originals/3e/f0/e6/3ef0e69f3c889c1307330c36a501eb12.gif"
/>
);
}

function Sidebar() {
const { Wait } = useWait();
return (
<div className="Sidebar">
<Wait on="sidebar" fallback={<Spinner />}>
This will go to loading state if <b>sidebar</b> loading is fired.
</Wait>
</div>
);
}

function Header() {
const { Wait } = useWait();
return (
<div className="Header">
<Wait on="header" fallback={<Spinner />}>
This will go to loading state if <b>header</b> loading is fired.
</Wait>
</div>
);
}

function Footer() {
const { Wait } = useWait();
return (
<div className="Footer">
<Wait on="footer" fallback={<Spinner />}>
This will go to loading state if <b>footer</b> loading is fired.
</Wait>
</div>
);
}

function Main(props) {
return <div className="Main">{props.children}</div>;
}

function App() {
const [message, setMessage] = useState("sidebar");
const { startWaiting, endWaiting, waiters } = useWait();

useEffect(() => {
startWaiting("sidebar");
}, []);

return (
<div className="App">
<Header />
<Sidebar />
<Main>
<p>Write "sidebar", "footer" or "header" to the message:</p>
<input value={message} onChange={e => setMessage(e.target.value)} />
<button onClick={() => startWaiting(message)}>Start Loading</button>
<button onClick={() => endWaiting(message)}>End Loading</button>
<hr />
<p>The active waiters:</p>
<pre>{JSON.stringify(waiters, null, 2)}</pre>
</Main>
<Footer />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(
<Waiter>
<App />
</Waiter>,
rootElement
);
42 changes: 42 additions & 0 deletions example/src/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.App {
font-family: sans-serif;
display: grid;
width: calc(100vw - 20px);
height: calc(100vh - 20px);
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 100px 1fr;
grid-template-rows: 80px 1fr 80px;
grid-gap: 10px;
}

.spinner {
margin: 20px auto;
display: block;
}

.Header {
grid-area: header;
background-color: #eee;
padding: 10px;
}

.Main {
grid-area: main;
background-color: #eee;
padding: 10px;
}

.Footer {
grid-area: footer;
background-color: #eee;
padding: 10px;
}

.Sidebar {
grid-area: sidebar;
background-color: #eee;
padding: 10px;
}
Loading