@b9g/crank 0.4.4 → 0.5.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,55 +1,25 @@
1
1
  # Crank.js
2
- Write JSX-driven components with functions, promises and generators.
2
+ ### The most “Just JavaScript” web framework.
3
3
 
4
- Documentation is available at [crank.js.org](https://crank.js.org). Crank.js is in a beta phase, and some APIs may change. To read more about the motivations for this library, you can read the [introductory blog post](https://crank.js.org/blog/introducing-crank).
4
+ Crank.js is a framework where components are defined with *Plain Old JavaScript Functions*. But not just regular functions! Components can also be defined with async functions for working with promises, and generator functions for working with local state.
5
5
 
6
- ## Features
7
- ### Declarative
8
- Crank uses the same JSX syntax and diffing algorithm popularized by React, allowing you to write HTML-like code directly in JavaScript.
6
+ Its API aims to be minimal and transparent. By relying on standard JavaScript control flow operators and data structures, and by making the process of rendering explicit, Crank.js helps developers write durable, bug-free applications with the latest and greatest libraries and APIs.
9
7
 
10
- ### Just Functions
11
- All components in Crank are just functions or generator functions. No classes, hooks, proxies or template languages are needed.
8
+ Rather than forcing developers to work with increasingly convoluted reactive solutions and bespoke templating languages, Crank.js uses the platform. It is a dedication to the web, to the promise of an accessible and inclusive medium for self-expression and commerce, built on the thesis that simpler code is how we’ll push the frontier.
12
9
 
13
- ### Promise-friendly
14
- Crank provides first-class support for promises. You can define components as async functions and race renderings to display fallback UIs.
10
+ ## Get Started
15
11
 
16
- ### Lightweight
17
- Crank has no dependencies, and its core is a single file. It currently measures at [5KB minified and gzipped](https://bundlephobia.com/result?p=@b9g/crank).
18
-
19
- ### Performant
20
- [According to benchmarks](https://github.com/krausest/js-framework-benchmark), Crank beats React in terms of speed and memory usage, and is currently comparable to Preact or Vue.
21
-
22
- ### Extensible
23
- The core renderer can be extended to target alternative environments such as WebGL libraries, terminals, smartphones or smart TVs.
24
-
25
- ## Installation
26
- Crank is available on [NPM](https://npmjs.org/@b9g/crank) in the ESModule and CommonJS formats.
12
+ The documentation for Crank.js is available at [crank.js.org](https://crank.js.org). Crank.js is published on NPM under the `@b9g` organization (short for “b*ikeshavin*g”).
27
13
 
28
14
  ```shell
29
15
  $ npm install @b9g/crank
30
16
  ```
31
17
 
32
- ```jsx
33
- /** @jsx createElement */
34
- import {createElement} from "@b9g/crank";
35
- import {renderer} from "@b9g/crank/dom";
36
-
37
- renderer.render(<div id="hello">Hello world</div>, document.body);
38
- ```
18
+ ### Key Examples
39
19
 
40
- If your environment does not support ESModules (you may see a message like `SyntaxError: Unexpected token export`), you can import the CommonJS versions of the library under the `cjs` directory.
20
+ #### A Simple Component
41
21
 
42
- ```jsx
43
- /** @jsx createElement */
44
- import {createElement} from "@b9g/crank/cjs";
45
- import {renderer} from "@b9g/crank/cjs/dom";
46
-
47
- renderer.render(<div id="hello">Hello world</div>, document.body);
48
- ```
49
-
50
- ## Key Examples
51
- ### A Simple Component
52
- ```jsx
22
+ ```jsx live
53
23
  /** @jsx createElement */
54
24
  import {createElement} from "@b9g/crank";
55
25
  import {renderer} from "@b9g/crank/dom";
@@ -63,10 +33,9 @@ function Greeting({name = "World"}) {
63
33
  renderer.render(<Greeting />, document.body);
64
34
  ```
65
35
 
66
- [Try on CodeSandbox](https://codesandbox.io/s/a-simple-crank-component-mhciu)
36
+ #### A Stateful Component
67
37
 
68
- ### A Stateful Component
69
- ```jsx
38
+ ```jsx live
70
39
  /** @jsx createElement */
71
40
  import {createElement} from "@b9g/crank";
72
41
  import {renderer} from "@b9g/crank/dom";
@@ -89,10 +58,9 @@ function *Timer() {
89
58
  renderer.render(<Timer />, document.body);
90
59
  ```
91
60
 
92
- [Try on CodeSandbox](https://codesandbox.io/s/a-stateful-crank-component-hh8zx)
61
+ #### An Async Component
93
62
 
94
- ### An Async Component
95
- ```jsx
63
+ ```jsx live
96
64
  /** @jsx createElement */
97
65
  import {createElement} from "@b9g/crank";
98
66
  import {renderer} from "@b9g/crank/dom";
@@ -110,10 +78,9 @@ async function QuoteOfTheDay() {
110
78
  renderer.render(<QuoteOfTheDay />, document.body);
111
79
  ```
112
80
 
113
- [Try on CodeSandbox](https://codesandbox.io/s/an-async-crank-component-ru02q)
114
-
115
81
  ### A Loading Component
116
- ```jsx
82
+
83
+ ```jsx live
117
84
  /** @jsx createElement */
118
85
  import {createElement, Fragment} from "@b9g/crank";
119
86
  import {renderer} from "@b9g/crank/dom";
@@ -167,5 +134,3 @@ function *RandomDogApp() {
167
134
 
168
135
  renderer.render(<RandomDogApp />, document.body);
169
136
  ```
170
-
171
- [Try on CodeSandbox](https://codesandbox.io/s/a-loading-crank-component-pci9d)
package/crank.cjs CHANGED
@@ -108,6 +108,7 @@ class Element {
108
108
  this.static_ = static_;
109
109
  }
110
110
  }
111
+ // See Element interface
111
112
  Element.prototype.$$typeof = ElementSymbol;
112
113
  function isElement(value) {
113
114
  return value != null && value.$$typeof === ElementSymbol;
@@ -130,6 +131,7 @@ function createElement(tag, props, ...children) {
130
131
  switch (name) {
131
132
  case "crank-key":
132
133
  case "c-key":
134
+ case "$key":
133
135
  // We have to make sure we don’t assign null to the key because we
134
136
  // don’t check for null keys in the diffing functions.
135
137
  if (props[name] != null) {
@@ -138,12 +140,14 @@ function createElement(tag, props, ...children) {
138
140
  break;
139
141
  case "crank-ref":
140
142
  case "c-ref":
143
+ case "$ref":
141
144
  if (typeof props[name] === "function") {
142
145
  ref = props[name];
143
146
  }
144
147
  break;
145
148
  case "crank-static":
146
149
  case "c-static":
150
+ case "$static":
147
151
  static_ = !!props[name];
148
152
  break;
149
153
  default:
@@ -157,11 +161,25 @@ function createElement(tag, props, ...children) {
157
161
  else if (children.length === 1) {
158
162
  props1.children = children[0];
159
163
  }
164
+ // string aliases for the special tags
165
+ // TODO: Does this logic belong here, or in the Element constructor
166
+ switch (tag) {
167
+ case "$FRAGMENT":
168
+ tag = Fragment;
169
+ break;
170
+ case "$PORTAL":
171
+ tag = Portal;
172
+ break;
173
+ case "$COPY":
174
+ tag = Copy;
175
+ break;
176
+ case "$RAW":
177
+ tag = Raw;
178
+ break;
179
+ }
160
180
  return new Element(tag, props1, key, ref, static_);
161
181
  }
162
- /**
163
- * Clones a given element, shallowly copying the props object.
164
- */
182
+ /** Clones a given element, shallowly copying the props object. */
165
183
  function cloneElement(el) {
166
184
  if (!isElement(el)) {
167
185
  throw new TypeError("Cannot clone non-element");
@@ -735,9 +753,9 @@ const IsDone = 1 << 4;
735
753
  *
736
754
  * NOTE: This is mainly used to prevent some false positives in component
737
755
  * yields or returns undefined warnings. The reason we’re using this versus
738
- * IsUnmounted is a very troubling jest test (cascades sync generator parent
739
- * and sync generator child) where synchronous code causes a stack overflow
740
- * error in a non-deterministic way. Deeply disturbing stuff.
756
+ * IsUnmounted is a very troubling test (cascades sync generator parent and
757
+ * sync generator child) where synchronous code causes a stack overflow error
758
+ * in a non-deterministic way. Deeply disturbing stuff.
741
759
  */
742
760
  const IsErrored = 1 << 5;
743
761
  /**