@manyducks.co/dolla 2.0.0-alpha.5 → 2.0.0-alpha.51

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.
Files changed (83) hide show
  1. package/README.md +81 -933
  2. package/dist/core/context.d.ts +65 -0
  3. package/dist/{modules → core}/dolla.d.ts +43 -26
  4. package/dist/core/markup.d.ts +102 -0
  5. package/dist/core/nodes/dom.d.ts +13 -0
  6. package/dist/core/nodes/dynamic.d.ts +30 -0
  7. package/dist/core/nodes/fragment.d.ts +19 -0
  8. package/dist/core/nodes/html.d.ts +34 -0
  9. package/dist/core/nodes/outlet.d.ts +20 -0
  10. package/dist/core/nodes/portal.d.ts +22 -0
  11. package/dist/core/nodes/repeat.d.ts +28 -0
  12. package/dist/core/nodes/view.d.ts +97 -0
  13. package/dist/core/signals-api.d.ts +42 -0
  14. package/dist/core/signals.d.ts +22 -0
  15. package/dist/core/store.d.ts +52 -0
  16. package/dist/core/symbols.d.ts +4 -0
  17. package/dist/{views → core/views}/default-crash-view.d.ts +1 -1
  18. package/dist/core/views/fragment.d.ts +8 -0
  19. package/dist/{views → core/views}/passthrough.d.ts +2 -2
  20. package/dist/fragment-Bvuvw3ue.js +8 -0
  21. package/dist/fragment-Bvuvw3ue.js.map +1 -0
  22. package/dist/{modules/http.d.ts → http/index.d.ts} +3 -5
  23. package/dist/index.d.ts +15 -10
  24. package/dist/index.js +1056 -1216
  25. package/dist/index.js.map +1 -1
  26. package/dist/jsx-dev-runtime.d.ts +2 -2
  27. package/dist/jsx-dev-runtime.js +8 -8
  28. package/dist/jsx-dev-runtime.js.map +1 -1
  29. package/dist/jsx-runtime.d.ts +3 -3
  30. package/dist/jsx-runtime.js +10 -10
  31. package/dist/jsx-runtime.js.map +1 -1
  32. package/dist/markup-QqAGIoYP.js +1501 -0
  33. package/dist/markup-QqAGIoYP.js.map +1 -0
  34. package/dist/{modules/router.d.ts → router/index.d.ts} +53 -60
  35. package/dist/{routing.d.ts → router/router.utils.d.ts} +17 -3
  36. package/dist/router/router.utils.test.d.ts +1 -0
  37. package/dist/translate/index.d.ts +133 -0
  38. package/dist/typeChecking.d.ts +2 -98
  39. package/dist/typeChecking.test.d.ts +1 -0
  40. package/dist/types.d.ts +17 -7
  41. package/dist/utils.d.ts +18 -3
  42. package/docs/http.md +29 -0
  43. package/docs/i18n.md +38 -0
  44. package/docs/index.md +10 -0
  45. package/docs/router.md +80 -0
  46. package/docs/setup.md +31 -0
  47. package/docs/signals.md +166 -0
  48. package/docs/state.md +141 -0
  49. package/docs/stores.md +62 -0
  50. package/docs/views.md +208 -0
  51. package/examples/webcomponent/index.html +14 -0
  52. package/examples/webcomponent/main.js +165 -0
  53. package/index.d.ts +2 -2
  54. package/notes/TODO.md +6 -0
  55. package/notes/atomic.md +452 -0
  56. package/notes/context-routes.md +61 -0
  57. package/notes/custom-nodes.md +17 -0
  58. package/notes/elimination.md +33 -0
  59. package/notes/molecule.md +35 -0
  60. package/notes/readme-scratch.md +260 -0
  61. package/notes/route-middleware.md +42 -0
  62. package/notes/scratch.md +330 -7
  63. package/notes/splitting.md +5 -0
  64. package/notes/stores.md +53 -0
  65. package/package.json +13 -10
  66. package/vite.config.js +5 -10
  67. package/build.js +0 -34
  68. package/dist/markup.d.ts +0 -100
  69. package/dist/modules/language.d.ts +0 -41
  70. package/dist/modules/render.d.ts +0 -17
  71. package/dist/nodes/cond.d.ts +0 -26
  72. package/dist/nodes/html.d.ts +0 -31
  73. package/dist/nodes/observer.d.ts +0 -29
  74. package/dist/nodes/outlet.d.ts +0 -22
  75. package/dist/nodes/portal.d.ts +0 -19
  76. package/dist/nodes/repeat.d.ts +0 -34
  77. package/dist/nodes/text.d.ts +0 -19
  78. package/dist/passthrough-CtoBcpag.js +0 -1245
  79. package/dist/passthrough-CtoBcpag.js.map +0 -1
  80. package/dist/signals.d.ts +0 -101
  81. package/dist/view.d.ts +0 -50
  82. package/tests/signals.test.js +0 -135
  83. /package/dist/{routing.test.d.ts → core/signals-api.test.d.ts} +0 -0
@@ -0,0 +1,166 @@
1
+ ## ⚡ Reactive Updates with `Signals`
2
+
3
+ Dolla sets out to solve the challenge of keeping your UI in sync with your data. All apps have state that changes at runtime, and your UI must update itself to stay in sync with that state as it changes. JavaScript frameworks all have their own ways of doing this, but there are two main ones; virtual DOM and signals. Dolla follows the Signals philosophy.
4
+
5
+ [React](https://react.dev) and similar frameworks make use of a [virtual DOM](https://svelte.dev/blog/virtual-dom-is-pure-overhead), in which every state change causes a "diff" of the real DOM nodes on the page against a lightweight representation of what those nodes _should_ look like, followed by a "patch" where the minimal updates are performed to bring the DOM in line with the ideal virtual DOM.
6
+
7
+ [Solid](https://www.solidjs.com) and similar frameworks make use of [signals](https://dev.to/this-is-learning/the-evolution-of-signals-in-javascript-8ob), which are containers for data that will change over time. Signal values are accessed through special getter functions that can be called inside of a "scope" to track their values. When the value of a tracked signal changes, any computations that happened in scopes that depend on those signals are re-run. In an app like this, all of your DOM updates are performed with pinpoint accuracy without diffing as signal values change.
8
+
9
+ The Signals API in Dolla has just four functions:
10
+
11
+ - `$` to create a new Source or derived Signal.
12
+ - `get` to unwrap a possible Signal value.
13
+ - `peek` to unwrap a possible Signal value without tracking it.
14
+ - `effect` to run side effects when tracked signals change.
15
+
16
+ ### Basic State API
17
+
18
+ ```js
19
+ import { $ } from "@manyducks.co/dolla";
20
+
21
+ const count = $(72);
22
+
23
+ // Get the current value.
24
+ count(): // 72
25
+
26
+ // Set a new value.
27
+ count(300);
28
+
29
+ // The State now reflects the latest value.
30
+ count(); // 300
31
+
32
+ // Data can also be updated by passing an update function.
33
+ // This function takes the current state and returns the next.
34
+ count((value) => value + 1);
35
+ count(); // 301
36
+ ```
37
+
38
+ ### Deriving States from other States
39
+
40
+ #### Example 1: Doubled
41
+
42
+ ```js
43
+ import { $ } from "@manyducks.co/dolla";
44
+
45
+ // Passing a value to $() results in a Source...
46
+ const count = $(1);
47
+
48
+ // ...while passing a function results in a Signal with a derived value.
49
+ const doubled = $(() => count() * 2);
50
+
51
+ count(10);
52
+ doubled(); // 20
53
+ ```
54
+
55
+ ##### A note on derived signals.
56
+
57
+ Because signals are simply functions that return a value, you can also derive state by simply defining a function that returns a value. Any `Source` called in this function will therefore be tracked when this function is called in a tracked scope.
58
+
59
+ The difference is that the value of the plain function is computed again each and every time that function is called. Wrapping it with `$()` will result in the computed value being cached until one of its dependencies changes. If you are coming from React then you may want to think of this like `useMemo`.
60
+
61
+ ```js
62
+ // Plain getter: OK
63
+ const plainCount = () => count() * 2;
64
+
65
+ // Signal: OK
66
+ const cachedCount = $(() => count() * 2);
67
+ ```
68
+
69
+ Using plain getters to derive values is perfectly fine. It may be a waste to cache a very simple getter, but if the value is accessed frequently or involves expensive computations then you can get better performance by wrapping it in a Signal.
70
+
71
+ #### Example 2: Selecting a User
72
+
73
+ ```js
74
+ import { $ } from "@manyducks.co/dolla";
75
+
76
+ const users = $([
77
+ { id: 1, name: "Audie" },
78
+ { id: 2, name: "Bob" },
79
+ { id: 3, name: "Cabel" },
80
+ ]);
81
+ const userId = $(1);
82
+
83
+ const selectedUser = $(() => users().find((user) => user.id === userId()));
84
+
85
+ selectedUser(); // { id: 1, name: "Audie" }
86
+
87
+ userId(3);
88
+
89
+ selectedUser(); // { id: 3, name: "Cabel" }
90
+ ```
91
+
92
+ That was a more realistic example you might actually use in real life. Here we are selecting a user from a list based on its `id` field. This is kind of similar to a `JOIN` operation in a SQL database. I use this kind of pattern constantly in my apps.
93
+
94
+ The strength of setting up a join like this is that the `$users` array can be updated (by API call, websockets, etc.) and your `$selectedUser` will always be pointing to the latest version of the user data.
95
+
96
+ #### Example 3: Narrowing Complex Data
97
+
98
+ ```jsx
99
+ import { $ } from "@manyducks.co/dolla";
100
+
101
+ const user = $({ id: 1, name: "Audie" });
102
+ const name = $(() => user().name);
103
+
104
+ name(); // "Audie"
105
+
106
+ // In a view:
107
+ <span class="user-name">{name}</span>;
108
+ ```
109
+
110
+ Another common pattern. In a real app, most data is stored as arrays of objects. But what you need in order to slot it into a view is just a string. In the example above we've selected the user's name and slotted it into a `span`. If the `$user` value ever changes, the name will stay in sync.
111
+
112
+ ### Converting to and from Signals
113
+
114
+ ```js
115
+ import { $, get } from "@manyducks.co/dolla";
116
+
117
+ const count = state(512);
118
+
119
+ // Unwrap the value of count. Returns 512.
120
+ const value = get(count);
121
+ // Passing a non-state value will simply return it.
122
+ const name = get("World");
123
+
124
+ // If you need to convert a static piece of data into a Signal you can simply wrap it in a getter function.
125
+ const value = () => "Hello";
126
+ ```
127
+
128
+ ### In Views
129
+
130
+ ```jsx
131
+ import { $ } from "@manyducks.co/dolla";
132
+
133
+ function UserNameView(props, ctx) {
134
+ const name = $(() => props.user().name);
135
+
136
+ // Passing an object to `class` results in keys with a truthy value being applied as classes.
137
+ // Those with falsy values will be ignored.
138
+ // Signals can be given as values and they will be tracked.
139
+ return (
140
+ <span
141
+ class={{
142
+ "user-name": true,
143
+ "is-selected": props.selected
144
+ }}>
145
+ {name}
146
+ </span>
147
+ );
148
+ })
149
+
150
+ // In parent view:
151
+
152
+ const selected = $(false);
153
+ const user = $({ id: 1, name: "Audie" });
154
+
155
+ <UserNameView selected={selected} user={user} />
156
+
157
+ // Changing signal values out here will now update the UserNameView internals.
158
+ ```
159
+
160
+ ---
161
+
162
+ End.
163
+
164
+ - [🗂️ Docs](./index.md)
165
+ - [🏠 README](../README.md)
166
+ - [🦆 That's a lot of ducks.](https://www.manyducks.co)
package/docs/state.md ADDED
@@ -0,0 +1,141 @@
1
+ ## ⚡ Reactive Updates with `State`
2
+
3
+ Dolla sets out to solve the challenge of keeping your UI in sync with your data. All apps have state that changes at runtime, and as those values change your UI must update itself to stay in sync with that state. JavaScript frameworks all have their own ways of meeting this challenge, but there are two main ones; virtual DOM and signals.
4
+
5
+ [React](https://react.dev) and similar frameworks make use of a [virtual DOM](https://svelte.dev/blog/virtual-dom-is-pure-overhead), in which every state change causes a "diff" of the real DOM nodes on the page against a lightweight representation of what those nodes _should_ look like, followed by a "patch" where the minimal updates are performed to bring the DOM in line with the ideal virtual DOM.
6
+
7
+ [Solid](https://www.solidjs.com) and similar frameworks make use of [signals](https://dev.to/this-is-learning/the-evolution-of-signals-in-javascript-8ob), which are containers for data that will change over time. Signal values are accessed through special getter functions that can be called inside of a "scope" to track their values. When the value of a tracked signal changes, any computations that happened in scopes that depend on those signals are re-run. In an app like this, all of your DOM updates are performed with pinpoint accuracy without diffing as signal values change.
8
+
9
+ Dolla uses a concept of a `State`, which is a signal-like container for values that change over time. Where `State` differs from signals, however, is that there is no magical scope tracking going on behind the scenes. All States that depend on others do so explicity, so your code is easier to read and understand.
10
+
11
+ The `State` API has just four functions:
12
+
13
+ - `createState` to create a new state and a linked setter function.
14
+ - `derive` to create a new state whose value depends on one or more other states.
15
+ - `toState` to ensure that a value is a state object.
16
+ - `toValue` to ensure that a value is a plain value.
17
+
18
+ ### Basic State API
19
+
20
+ ```js
21
+ import { createState } from "@manyducks.co/dolla";
22
+
23
+ // Equivalent to React's `useState` or Solid's `createSignal`.
24
+ // A new read-only State and linked Setter are created.
25
+ const [$count, setCount] = createState(72);
26
+
27
+ // Get the current value.
28
+ $count.get(): // 72
29
+
30
+ // Set a new value.
31
+ setCount(300);
32
+
33
+ // The State now reflects the latest value.
34
+ $count.get(); // 300
35
+
36
+ // Data can also be updated by passing a function.
37
+ // This function takes the current state and returns a new one.
38
+ setCount((current) => current + 1);
39
+ $count.get(); // 301
40
+ ```
41
+
42
+ ### Deriving States from other States
43
+
44
+ #### Example 1: Doubled
45
+
46
+ ```js
47
+ import { createState, derive } from "@manyducks.co/dolla";
48
+
49
+ const [$count, setCount] = createState(1);
50
+
51
+ const $doubled = derive([$count], (count) => count * 2);
52
+
53
+ setCount(10);
54
+ $doubled.get(); // 20
55
+ ```
56
+
57
+ That was a typical toy example where we create a `$doubled` state that always contains the value of `$count`... doubled! This is the essential basic example of computed properties, as written in Dolla.
58
+
59
+ #### Example 2: Selecting a User
60
+
61
+ ```js
62
+ import { createState, derive } from "@manyducks.co/dolla";
63
+
64
+ const [$users, setUsers] = createState([
65
+ { id: 1, name: "Audie" },
66
+ { id: 2, name: "Bob" },
67
+ { id: 3, name: "Cabel" },
68
+ ]);
69
+ const [$selectedUserId, setSelectedUserId] = createState(1);
70
+
71
+ const $selectedUser = derive([$users, $selectedUserId], (users, id) => {
72
+ return users.find((user) => user.id === id);
73
+ });
74
+
75
+ $selectedUser.get(); // { id: 1, name: "Audie" }
76
+
77
+ setSelectedId(3);
78
+
79
+ $selectedUser.get(); // { id: 3, name: "Cabel" }
80
+ ```
81
+
82
+ That was a more realistic example you might actually use in real life. Here we are selecting a user from a list based on its `id` field. This is kind of similar to a `JOIN` operation in a SQL database. I use this kind of pattern constantly in my apps.
83
+
84
+ The strength of setting up a join like this is that the `$users` array can be updated (by API call, websockets, etc.) and your `$selectedUser` will always be pointing to the latest version of the user data.
85
+
86
+ #### Example 3: Narrowing Complex Data
87
+
88
+ ```jsx
89
+ import { createState, derive } from "@manyducks.co/dolla";
90
+
91
+ const [$user, setUser] = createState({ id: 1, name: "Audie" });
92
+
93
+ const $name = derive([$user], (user) => user.name);
94
+
95
+ $name.get(); // "Audie"
96
+
97
+ // In a view:
98
+ <span class="user-name">{$name}</span>;
99
+ ```
100
+
101
+ Another common pattern. In a real app, most data is stored as arrays of objects. But what you need in order to slot it into a view is just a string. In the example above we've selected the user's name and slotted it into a `span`. If the `$user` value ever changes, the name will stay in sync.
102
+
103
+ ### Converting to and from States
104
+
105
+ ```js
106
+ import { createState, toState, toValue } from "@manyducks.co/dolla";
107
+
108
+ const [$count, setCount] = createState(512);
109
+
110
+ // Unwrap the value of $count. Returns 512.
111
+ const count = toValue($count);
112
+ // Passing a non-state value will simply return it.
113
+ const name = toValue("World");
114
+
115
+ // Wrap "Hello" into a State containing "Hello"
116
+ const $value = toState("Hello");
117
+ // Passing a state will simply return that same state.
118
+ const $number = toState($count);
119
+ ```
120
+
121
+ ### In Views
122
+
123
+ ```jsx
124
+ import { derive } from "@manyducks.co/dolla";
125
+
126
+ function UserNameView(props, ctx) {
127
+ const $name = derive([props.$user], (user) => user.name);
128
+
129
+ return <span class={{ "user-name": true, "is-selected": props.$selected }}>{$name}</span>;
130
+ });
131
+ ```
132
+
133
+ In the example above we've displayed the `name` field from a `$user` object inside of a span. We are also assigning an `is-selected` class dynamically based on whether the `$selected` prop contains a truthy or falsy value.
134
+
135
+ ---
136
+
137
+ End.
138
+
139
+ - [🗂️ Docs](./index.md)
140
+ - [🏠 README](../README.md)
141
+ - [🦆 That's a lot of ducks.](https://www.manyducks.co)
package/docs/stores.md ADDED
@@ -0,0 +1,62 @@
1
+ # Stores
2
+
3
+ > TODO: Write about stores
4
+
5
+ ```tsx
6
+ import Dolla, { createState } from "@manyducks.co/dolla";
7
+
8
+ function CounterStore (initialValue, ctx) {
9
+ const [$count, setCount] = createState(initialValue);
10
+
11
+ // Respond to context events which bubble up from views.
12
+ ctx.on("counter:increment", (e) => {
13
+ e.stop(); // call to stop events bubbling to parent contexts.
14
+ setCount((count) => count + 1);
15
+ });
16
+
17
+ ctx.on("counter:decrement", () => {
18
+ setCount((count) => count - 1);
19
+ });
20
+
21
+ ctx.on("counter:reset", () => {
22
+ setCount(0);
23
+ });
24
+
25
+ return $count;
26
+ });
27
+
28
+ // Stores can be provided by the app itself.
29
+ Dolla.provide(CounterStore, 0);
30
+
31
+ function CounterView(props, ctx) {
32
+ // Store instances can also be provided at the view level to provide them to the current scope and those of child views.
33
+ // Views that are not children of this CounterView will not be able to access this particular instance of CounterStore.
34
+ ctx.provide(CounterStore, 0);
35
+
36
+ // Store return values can be accessed with `use`.
37
+ // This method will check the current context for an instance, then recursively check up the view tree until it finds one.
38
+ // An error will be thrown if no instances of the store are provided.
39
+ const $count = ctx.use(CounterStore);
40
+
41
+ // The buttons increment the value inside the store by emitting events.
42
+ // Child views at any depth could also emit these events to update the store.
43
+ return (
44
+ <div>
45
+ <p>Clicks: {$count}</p>
46
+ <div>
47
+ <button onClick={() => ctx.emit("counter:decrement")}>-1</button>
48
+ <button onClick={() => ctx.emit("counter:reset")}>Reset</button>
49
+ <button onClick={() => ctx.emit("counter:increment")}>+1</button>
50
+ </div>
51
+ </div>
52
+ );
53
+ });
54
+ ```
55
+
56
+ ---
57
+
58
+ End.
59
+
60
+ - [🗂️ Docs](./index.md)
61
+ - [🏠 README](../README.md)
62
+ - [🦆 That's a lot of ducks.](https://www.manyducks.co)
package/docs/views.md ADDED
@@ -0,0 +1,208 @@
1
+ # Views
2
+
3
+ Views are one of two component types in Dolla. We call them views because they deal specifically with presenting visible things to the user. The other type of component, [Stores](./stores.md), deal with data and events.
4
+
5
+ At its most basic, a view is a function that returns markup.
6
+
7
+ ```jsx
8
+ function ExampleView() {
9
+ return <h1>Hello World!</h1>;
10
+ }
11
+ ```
12
+
13
+ ## View Props
14
+
15
+ A view function takes a `props` object as its first argument. This object contains all properties passed to the view when it's invoked.
16
+
17
+ ```jsx
18
+ function ListItemView(props) {
19
+ return <li>{props.label}</li>;
20
+ }
21
+
22
+ function ListView() {
23
+ return (
24
+ <ul>
25
+ <ListItemView label="Squirrel" />
26
+ <ListItemView label="Chipmunk" />
27
+ <ListItemView label="Groundhog" />
28
+ </ul>
29
+ );
30
+ }
31
+ ```
32
+
33
+ As you may have guessed, you can pass States as props and slot them in in exactly the same way. This is important because Views do not re-render the way you might expect from other frameworks. Whatever you pass as props is what the View gets for its entire lifecycle.
34
+
35
+ ## View Helpers
36
+
37
+ ### `cond($condition, whenTruthy, whenFalsy)`
38
+
39
+ The `cond` helper does conditional rendering. When `$condition` is truthy, the second argument is rendered. When `$condition` is falsy the third argument is rendered. Either case can be left null or undefined if you don't want to render something for that condition.
40
+
41
+ ```jsx
42
+ function ConditionalListView(props) {
43
+ return (
44
+ <div>
45
+ {cond(
46
+ props.$show,
47
+
48
+ // Visible when truthy
49
+ <ul>
50
+ <ListItemView label="Squirrel" />
51
+ <ListItemView label="Chipmunk" />
52
+ <ListItemView label="Groundhog" />
53
+ </ul>,
54
+
55
+ // Visible when falsy
56
+ <span>List is hidden</span>,
57
+ )}
58
+ </div>
59
+ );
60
+ }
61
+ ```
62
+
63
+ ### `repeat($items, keyFn, renderFn)`
64
+
65
+ The `repeat` helper repeats a render function for each item in a list. The `keyFn` takes an item's value and returns a number, string or Symbol that uniquely identifies that list item. If `$items` changes or gets reordered, all rendered items with matching keys will be reused, those no longer in the list will be removed and those that didn't previously have a matching key are created.
66
+
67
+ ```jsx
68
+ function RepeatedListView() {
69
+ const [$items, setItems] = createState(["Squirrel", "Chipmunk", "Groundhog"]);
70
+
71
+ return (
72
+ <ul>
73
+ {repeat(
74
+ $items,
75
+ (item, index) => item, // Using the string itself as the key
76
+ ($item, $index, context) => {
77
+ return <ListItemView label={$item} />;
78
+ },
79
+ )}
80
+ </ul>
81
+ );
82
+ }
83
+ ```
84
+
85
+ ### `portal(content, parentNode)`
86
+
87
+ The `portal` helper displays DOM elements from a view as children of a parent element elsewhere in the document. Portals are typically used to display modals and other content that needs to appear at the top level of a document.
88
+
89
+ ```jsx
90
+ function PortalView() {
91
+ const content = (
92
+ <div class="modal">
93
+ <p>This is a modal.</p>
94
+ </div>
95
+ );
96
+
97
+ // Content will be appended to `document.body` while this view is connected.
98
+ return portal(document.body, content);
99
+ }
100
+ ```
101
+
102
+ ## View Context
103
+
104
+ A view function takes a context object as its second argument. The context provides a set of functions you can use to respond to lifecycle events, observe dynamic data, print debug messages and display child elements among other things.
105
+
106
+ ```jsx
107
+ function ExampleView(props, ctx) {
108
+ ctx.onMount(() => {
109
+ ctx.log("HELLO!");
110
+ });
111
+
112
+ return <h1>Hello World!</h1>;
113
+ }
114
+ ```
115
+
116
+ ### Printing Debug Messages
117
+
118
+ ```jsx
119
+ function ExampleView(props, ctx) {
120
+ // Set the name of this view's context. Console messages are prefixed with name.
121
+ ctx.name = "CustomName";
122
+
123
+ // Print messages to the console. These are suppressed by default in the app's "production" mode.
124
+ // You can also change which of these are printed and filter messages from certain contexts in the `createApp` options object.
125
+ ctx.info("Verbose debugging info that might be useful to know");
126
+ ctx.log("Standard messages");
127
+ ctx.warn("Something bad might be happening");
128
+ ctx.error("Uh oh!");
129
+
130
+ // If you encounter a bad enough situation, you can halt and disconnect the entire app.
131
+ ctx.crash(new Error("BOOM"));
132
+
133
+ return <h1>Hello World!</h1>;
134
+ }
135
+ ```
136
+
137
+ ### Lifecycle Events
138
+
139
+ ```jsx
140
+ function ExampleView(props, ctx) {
141
+ ctx.beforeMount(() => {
142
+ // Do something before this view's DOM nodes are created.
143
+ });
144
+
145
+ ctx.onMount(() => {
146
+ // Do something immediately after this view is connected to the DOM.
147
+ });
148
+
149
+ ctx.beforeUnmount(() => {
150
+ // Do something before removing this view from the DOM.
151
+ });
152
+
153
+ ctx.onUnmount(() => {
154
+ // Do some cleanup after this view is disconnected from the DOM.
155
+ });
156
+
157
+ return <h1>Hello World!</h1>;
158
+ }
159
+ ```
160
+
161
+ ### Displaying Children
162
+
163
+ The context has an `outlet` function that can be used to display children at a location of your choosing.
164
+
165
+ ```js
166
+ function LayoutView(props, ctx) {
167
+ return (
168
+ <div className="layout">
169
+ <div className="content">{ctx.outlet()}</div>
170
+ </div>
171
+ );
172
+ }
173
+
174
+ function ExampleView() {
175
+ // <h1> and <p> are displayed inside LayoutView's outlet.
176
+ return (
177
+ <LayoutView>
178
+ <h1>Hello</h1>
179
+ <p>This is inside the box.</p>
180
+ </LayoutView>
181
+ );
182
+ }
183
+ ```
184
+
185
+ ### Watching States
186
+
187
+ The `watch` function starts observing when the view is connected and stops when disconnected. This takes care of cleaning up watchers so you don't have to worry about memory leaks.
188
+
189
+ ```jsx
190
+ function ExampleView(props, ctx) {
191
+ const [$count, setCount] = createState(0);
192
+
193
+ // This callback will run when any states in the dependency array receive new values.
194
+ ctx.watch([$count], (count) => {
195
+ ctx.log("count is now", count);
196
+ });
197
+
198
+ // ...
199
+ }
200
+ ```
201
+
202
+ ---
203
+
204
+ End.
205
+
206
+ - [🗂️ Docs](./index.md)
207
+ - [🏠 README](../README.md)
208
+ - [🦆 That's a lot of ducks.](https://www.manyducks.co)
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <title>Web Component Example</title>
6
+ </head>
7
+
8
+ <body>
9
+ <my-counter></my-counter>
10
+
11
+ <script type="module" src="./main.js"></script>
12
+ </body>
13
+
14
+ </html>