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

Async Unmounting #136

Closed
wants to merge 11 commits into from
Closed
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
5 changes: 5 additions & 0 deletions examples/playground/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"@babel/preset-react",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

<body>
<div id="app"></div>
<script src="index.js"></script>
<script src="src/index.jsx"></script>
</body>
</html>
27 changes: 27 additions & 0 deletions examples/playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "crank-playground",
"private": true,
"version": "1.0.0",
"main": "index.html",
"scripts": {
"start": "parcel index.html --open",
"build": "parcel build index.html"
},
"eslint": {
"settings": {
"react": {
"pragma": "createElement"
}
}
},
"dependencies": {
"@bikeshaving/crank": "0.2.1"
},
"devDependencies": {
"@babel/core": "^7.0.0-0",
"@babel/preset-react": "^7.10.4",
"parcel-bundler": "^1.6.1"
},
"keywords": [],
"browserslist": ["last 1 Chrome versions"]
}
File renamed without changes.
5,146 changes: 5,146 additions & 0 deletions examples/playground/yarn.lock

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions examples/simple/.babelrc

This file was deleted.

3 changes: 0 additions & 3 deletions examples/simple/index.js

This file was deleted.

5 changes: 5 additions & 0 deletions examples/transitions/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"@babel/preset-react",
]
}
12 changes: 12 additions & 0 deletions examples/transitions/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Crank.js Sandbox</title>
<meta charset="UTF-8" />
</head>

<body>
<div id="app"></div>
<script src="src/index.jsx"></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "a-simple-crank-component",
"name": "transitions",
"private": true,
"version": "1.0.0",
"main": "index.html",
Expand All @@ -15,12 +15,13 @@
}
},
"dependencies": {
"@bikeshaving/crank": "*"
"@bikeshaving/crank": "0.2.1"
},
"devDependencies": {
"@babel/core": "^7.0.0-0",
"@babel/preset-react": "^7.9.4",
"@babel/preset-react": "^7.10.4",
"parcel-bundler": "^1.6.1"
},
"keywords": []
"keywords": [],
"browserslist": ["last 1 Chrome versions"]
}
69 changes: 69 additions & 0 deletions examples/transitions/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/** @jsx createElement */
import {createElement} from "@bikeshaving/crank";
import {renderer} from "@bikeshaving/crank/dom";

const alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
function shuffle(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}

return arr;
}

function randomLetters() {
return shuffle(Array.from(alphabet))
.slice(0, Math.floor(Math.random() * 26))
.sort();
}

async function* Letter({letter, index}) {
let node;
try {
const style = {
position: "absolute",
top: "20px",
transition: "transform 750ms, opacity 750ms",
};

node = yield <span style={{...style, color: "green"}}>{letter}</span>;
node.style.transform = `translate(${index * 1.2}em, -20px)`;
node.style.opacity = 0;
requestAnimationFrame(() => {
node.style.transform = `translate(${index * 1.2}em, 0)`;
node.style.opacity = 1;
});

for await ({letter, index} of this) {
node = yield <span style={{...style, color: "black"}}>{letter}</span>;
requestAnimationFrame(() => {
node.style.transform = `translate(${index * 1.2}em, 0)`;
});
}
} finally {
requestAnimationFrame(() => {
node.style.color = "red";
node.style.transform = `translate(${index * 1.2}em, 20px)`;
node.style.opacity = 0;
});
await new Promise((resolve) => setTimeout(resolve, 750));
}
}

function* Letters() {
const interval = setInterval(() => this.refresh(), 1500);
try {
while (true) {
const letters = randomLetters().map((l, i) => (
<Letter crank-key={l} letter={l} index={i} />
));

yield <div style="height: 40px">{letters}</div>;
}
} finally {
clearInterval(interval);
}
}

renderer.render(<Letters />, document.body);
4 changes: 2 additions & 2 deletions examples/simple/yarn.lock → examples/transitions/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@
"@babel/types" "^7.4.4"
esutils "^2.0.2"

"@babel/preset-react@^7.9.4":
"@babel/preset-react@^7.10.4":
version "7.10.4"
resolved "https://registry.yarnpkg.com/@babel/preset-react/-/preset-react-7.10.4.tgz#92e8a66d816f9911d11d4cc935be67adfc82dbcf"
integrity sha512-BrHp4TgOIy4M19JAfO1LhycVXOPWdDbTRep7eVyatf174Hff+6Uk53sDyajqZPu8W1qXRBiYOfIamek6jA7YVw==
Expand Down Expand Up @@ -889,7 +889,7 @@
lodash "^4.17.19"
to-fast-properties "^2.0.0"

"@bikeshaving/crank@*":
"@bikeshaving/crank@0.2.1":
version "0.2.1"
resolved "https://registry.yarnpkg.com/@bikeshaving/crank/-/crank-0.2.1.tgz#b7de2eede6f295208f941c371f118dfc801360ec"
integrity sha512-yvlrHku3oN189Wd2a8Wmau8306ENlJ9QdOcHbvXo0vkUBI/g8Q8G8OT0QgaeSLvpDeKErgdrYU9+KSUpqWsBOw==
Expand Down
7 changes: 7 additions & 0 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ export class DOMRenderer extends Renderer<Node, string | undefined> {
}
}
}

remove(el: CrankElement<string | symbol>, parent: Node, child: Node): void {
// Attempting to remove a node which isn’t a direct child of the parent will throw an error, so we add this guard here.
if (child.parentNode === parent) {
parent.removeChild(child);
}
}
}

export const renderer = new DOMRenderer();
Loading