@fun-land/fun-web 0.6.0 → 1.0.0
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/CHANGELOG.md +12 -0
- package/README.md +32 -3
- package/dist/esm/src/dom.d.ts +5 -5
- package/dist/esm/src/dom.js +5 -1
- package/dist/esm/src/dom.js.map +1 -1
- package/dist/esm/tsconfig.publish.tsbuildinfo +1 -1
- package/dist/src/dom.d.ts +5 -5
- package/dist/src/dom.js +5 -1
- package/dist/src/dom.js.map +1 -1
- package/dist/tsconfig.publish.tsbuildinfo +1 -1
- package/examples/todo-app/TodoApp.ts +4 -5
- package/package.json +12 -13
- package/src/dom.test.ts +93 -1
- package/src/dom.ts +16 -17
- package/src/mount.test.ts +2 -2
package/src/dom.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** DOM utilities for functional element creation and manipulation */
|
|
2
|
-
import { type FunState } from "@fun-land/fun-state";
|
|
2
|
+
import { type FunState, type FunRead } from "@fun-land/fun-state";
|
|
3
3
|
import type { Component, ElementChild } from "./types";
|
|
4
4
|
import { Accessor } from "@fun-land/accessor";
|
|
5
5
|
|
|
@@ -11,7 +11,8 @@ export type Enhancer<El extends Element> = (element: El) => El;
|
|
|
11
11
|
*/
|
|
12
12
|
const entries = <T extends Record<string, unknown>>(
|
|
13
13
|
obj: T
|
|
14
|
-
): Array<[keyof T, T[keyof T]]> =>
|
|
14
|
+
): Array<[keyof T, T[keyof T]]> =>
|
|
15
|
+
Object.entries(obj) as Array<[keyof T, T[keyof T]]>;
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Create an HTML element with attributes and children
|
|
@@ -30,6 +31,7 @@ export const h = <Tag extends keyof HTMLElementTagNameMap>(
|
|
|
30
31
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
32
|
attrs?: Record<string, any> | null,
|
|
32
33
|
children?: ElementChild | ElementChild[]
|
|
34
|
+
// eslint-disable-next-line complexity
|
|
33
35
|
): HTMLElementTagNameMap[Tag] => {
|
|
34
36
|
const element = document.createElement(tag);
|
|
35
37
|
|
|
@@ -82,7 +84,7 @@ type HxHandlers<El extends Element> = Partial<{
|
|
|
82
84
|
}>;
|
|
83
85
|
|
|
84
86
|
type HxBindings<El extends Element> = Partial<{
|
|
85
|
-
[K in WritableKeys<El> & string]:
|
|
87
|
+
[K in WritableKeys<El> & string]: FunRead<El[K]>;
|
|
86
88
|
}>;
|
|
87
89
|
|
|
88
90
|
type HxOptionsBase<El extends Element> = {
|
|
@@ -145,15 +147,13 @@ export function hx<Tag extends keyof HTMLElementTagNameMap>(
|
|
|
145
147
|
}
|
|
146
148
|
|
|
147
149
|
if (bind) {
|
|
148
|
-
const bindElementProperty = <
|
|
150
|
+
const bindElementProperty = <
|
|
151
|
+
K extends WritableKeys<HTMLElementTagNameMap[Tag]> & string,
|
|
152
|
+
>(
|
|
149
153
|
key: K,
|
|
150
|
-
state:
|
|
154
|
+
state: FunRead<HTMLElementTagNameMap[Tag][K]>
|
|
151
155
|
): void => {
|
|
152
|
-
bindProperty<HTMLElementTagNameMap[Tag], K>(
|
|
153
|
-
key,
|
|
154
|
-
state,
|
|
155
|
-
signal
|
|
156
|
-
)(element);
|
|
156
|
+
bindProperty<HTMLElementTagNameMap[Tag], K>(key, state, signal)(element);
|
|
157
157
|
};
|
|
158
158
|
|
|
159
159
|
for (const key of Object.keys(bind) as Array<
|
|
@@ -161,10 +161,7 @@ export function hx<Tag extends keyof HTMLElementTagNameMap>(
|
|
|
161
161
|
>) {
|
|
162
162
|
const state = bind[key];
|
|
163
163
|
if (!state) continue;
|
|
164
|
-
bindElementProperty(
|
|
165
|
-
key,
|
|
166
|
-
state
|
|
167
|
-
);
|
|
164
|
+
bindElementProperty(key, state);
|
|
168
165
|
}
|
|
169
166
|
}
|
|
170
167
|
|
|
@@ -240,7 +237,7 @@ export const attrs =
|
|
|
240
237
|
export const bindProperty =
|
|
241
238
|
<E extends Element, K extends keyof E & string>(
|
|
242
239
|
key: K,
|
|
243
|
-
state:
|
|
240
|
+
state: FunRead<E[K]>,
|
|
244
241
|
signal: AbortSignal
|
|
245
242
|
) =>
|
|
246
243
|
(el: E): E => {
|
|
@@ -386,6 +383,7 @@ export const bindListChildren =
|
|
|
386
383
|
rows.clear();
|
|
387
384
|
};
|
|
388
385
|
|
|
386
|
+
// eslint-disable-next-line complexity
|
|
389
387
|
const reconcile = (): void => {
|
|
390
388
|
const items = list.get();
|
|
391
389
|
|
|
@@ -468,7 +466,7 @@ export const bindListChildren =
|
|
|
468
466
|
* });
|
|
469
467
|
*/
|
|
470
468
|
export function renderWhen<State, Props>(options: {
|
|
471
|
-
state:
|
|
469
|
+
state: FunRead<State>;
|
|
472
470
|
predicate?: (value: State) => boolean;
|
|
473
471
|
component: Component<Props>;
|
|
474
472
|
props: Props;
|
|
@@ -487,6 +485,7 @@ export function renderWhen<State, Props>(options: {
|
|
|
487
485
|
let childCtrl: AbortController | null = null;
|
|
488
486
|
let childEl: Element | null = null;
|
|
489
487
|
|
|
488
|
+
// eslint-disable-next-line complexity
|
|
490
489
|
const reconcile = () => {
|
|
491
490
|
const shouldRender = predicate(state.get());
|
|
492
491
|
|
|
@@ -524,7 +523,7 @@ export function renderWhen<State, Props>(options: {
|
|
|
524
523
|
|
|
525
524
|
/** add passed class (idempotent) to element when state returns true */
|
|
526
525
|
export const bindClass =
|
|
527
|
-
(className: string, state:
|
|
526
|
+
(className: string, state: FunRead<boolean>, signal: AbortSignal) =>
|
|
528
527
|
<E extends Element>(el: E): E => {
|
|
529
528
|
state.watch(signal, (active) => el.classList.toggle(className, active));
|
|
530
529
|
return el;
|
package/src/mount.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { mount } from "./mount";
|
|
2
2
|
import { h } from "./dom";
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
3
|
+
import type { Component } from "./index";
|
|
4
|
+
import { funState, type FunState } from "@fun-land/fun-state";
|
|
5
5
|
|
|
6
6
|
describe("mount()", () => {
|
|
7
7
|
let container: HTMLDivElement;
|