Skip to content
Merged
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
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<script type="importmap">
{
"imports": {
"@std/assert/": "https://esm.sh/jsr/@std/assert/",
"@f-stack/functorial": "./build/packages/functorial/src/reactive.js",
"@f-stack/reflow": "./packages/reflow/src/html.js"
"@f-stack/reflow": "./build/packages/reflow/src/mod.js"
}
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion playground/components/Article.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { html } from "@f-stack/reflow";
import { Counter } from "../pages/reactivity/1-state-and-derived/Counter.ts";
import { Counter } from "./Counter.ts";

export const Article = (props: { title: string }) => {
return html`
Expand Down
7 changes: 0 additions & 7 deletions playground/components/Hi.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { derived, reactive } from "@f-stack/functorial";
import { reactive } from "@f-stack/functorial";
import { html, on } from "@f-stack/reflow";

const state = reactive({
count: 0,
const count = reactive({
value: 0,
});

export const Counter = () => {
return html`
<button ${on({ click: () => state.count++ })}>
Click ${derived(() => state.count)}
<button ${on({ click: () => count.value++ })}>
Click ${count}
</button>
`;
};
Expand Down
77 changes: 30 additions & 47 deletions playground/pages/sinks/Attach.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,48 @@
import { listen, reactive } from "@f-stack/functorial";
import { attach, attr, html, on, show } from "@f-stack/reflow";
import { reactive } from "@f-stack/functorial";
import { attach, attr, component, html, on, show } from "@f-stack/reflow";

export const AttachPage = () => {
const form = reactive({ value: "Bob" });
const color = reactive({ value: "#40E0D0" });
const display = reactive({ value: true });
const state = reactive({ color: "#40E0D0", show: true });

return html`
<form>
<label>username:
<input type="text" ${attach((i: HTMLInputElement) => {
i.defaultValue = form.value;
})} ${on<HTMLInputElement>({
input: function () {
form.value = this.value;
},
})}>
</label>
<button type="reset">Reset</button>
</form>

<div>
<input type="color" ${attr({
get value() {
return color.value;
},
value: state.color,
})} ${on<HTMLInputElement>({
input: function () {
color.value = this.value;
state.color = this.value;
},
})}>
<button ${on({
click: () => {
display.value = !display.value;
},
})}>Show</button>
<button ${on({ click: () => state.show = !state.show })}>Toggle</button>
</div>

${show(() => display.value, () =>
html`
<canvas width="300" height="300" ${attach(
(canvas: HTMLCanvasElement) => {
const context = canvas.getContext("2d");
${show(
() => state.show,
component(function () {
return html`
<canvas width="300" height="300" ${attach(
(canvas: HTMLCanvasElement) => {
const context = canvas.getContext("2d");

if (context) {
const draw = (color: string) => {
context.fillStyle = color;
context.fillRect(0, 0, 200, 200);
};
if (context) {
const draw = (color: string) => {
context.fillStyle = color;
context.fillRect(0, 0, 200, 200);
};

draw(color.value);
draw(state.color);

listen(color, (e) => {
console.log(e);
if (e.type !== "update") return;
draw(e.newValue);
});
}
},
)}>Enable JS</canvas>
`)}
this.listen(state, (e) => {
console.log(e);
if (e.type !== "update") return;
draw(e.newValue);
});
}
},
)}>Enable JS</canvas>
`;
}),
)}

<style>
html { scrollbar-gutter: stable; }
Expand Down
36 changes: 14 additions & 22 deletions playground/pages/sinks/Attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const Attr = () => {
return disabled.value;
},
});
const input = reactive({ text: "hello", number: 10 });
const user = reactive({ name: "Bob" });

return html`
<p>
Expand Down Expand Up @@ -48,26 +48,18 @@ export const Attr = () => {
},
})}>Remove disabled attribute</button>
</p>
<h2>Two-way bindings</h2>
<div>
<input type="text" ${attr({
get value() {
return input.text;
},
})} ${on<HTMLInputElement>({
input: function () {
input.text = this.value;
},
})}>
<input type="number" ${attr({
get value() {
return input.number;
},
})} ${on<HTMLInputElement>({
input: function () {
input.number = this.valueAsNumber;
},
})}>
</div>
<h2>Reset values</h2>
<form>
<label>username:
<input type="text" ${attr({
value: user.name,
})} ${on<HTMLInputElement>({
input: function () {
user.name = this.value;
},
})}>
</label>
<button type="reset">Reset</button>
</form>
`;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@scope ([scope="1"]) to ([scope]) {
@scope ([scope]) to ([scope]) {
p {
color: goldenrod;
font-family: "Comic Sans MS", cursive;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { attr, html } from "@f-stack/reflow";
// @ts-ignore works in Chrome
// import styles from "./hello.css" with { type: "css" };
import styles from "./hello.css" with { type: "css" };

document.adoptedStyleSheets.push(styles);

Expand All @@ -11,10 +11,9 @@ const Nested = () => {
};

/**
* Showcase simple interpolation
* Primitive interpolation
*/
export const Hello = () => {
// Add data
export const HtmlPage = () => {
const name = "World";
const src =
"https://images.dog.ceo/breeds/terrier-patterdale/dog-1268559_640.jpg";
Expand Down
2 changes: 1 addition & 1 deletion playground/render.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Counter } from "./pages/reactivity/1-state-and-derived/Counter.ts";
import { Counter } from "./components/Counter.ts";

document.body.replaceChildren(Counter().fragment);