import { App } from "."; import { computed } from "./reactivity/computed"; import { reactive } from "./reactivity/reactive"; import { ref } from "./reactivity/ref"; import { html } from "./util"; // ------------------------------------------------ // Slots, multiple default and named, :if and :for // const card = { // template: html`
//

// //
`, // }; // const main = { // template: html` //
//

card below

// // card title // // //
// `, // }; // const app = new App(); // app.register("card", card); // app.mount(main, "#app"); // ------------------------------------------------ // Slots, multiple default and named, :if and :for const app = new App(); const parent = { template: html`

parent, bool: {{bool}}

showing 1-3 because bool is true
1
2
3
content 1 always shown
content 2, animals:
animal: {{animal}}
`, main() { const bool = ref(true); const animals = reactive(["dog", "cat", "bear"]); setInterval(() => { bool.value = !bool.value; }, 2000); return { bool, animals }; }, }; const card = { template: html`

card

`, }; app.register("card", card); const parentBlock = app.mount(parent, "body"); const cardBlock = parentBlock.context.blocks[0]; // ------------------------------------------------ // Component pros, mirror and spread, bind and no bind // const child = { // template: html`
Animal: {{animal}} {{index}}
`, // props: { animal: { default: "cat" }, index: { default: 0 } }, // main({ animal, index }) { // return { animal, index }; // }, // }; // const parent = { // template: html` //
// // mirror, no bind: // //
// mirror, bind: // //
// spread, no bind: // //
// spread, bind: // //
// regular prop: // //
// regular prop, bind: // //
//
div has "id" set to animal.value
//
//
div has "id" set and bound to animal.value
//
//
div has "animal" set to animal.value
//
//
div has "animal" set and bound to animal.value
//
//
div has "animal" spread
//
//
div has "animal" spread and bound
//
//
//
//
// if bool, mirror, no bind: // // if bool, mirror, bind: // //
// for list, mirror, no bind: // //
// for list, mirror, bind: // // if bool, for list, mirror, no bind: these have the value "DOG!" because by the time for :for directive is evaluated, animal.value is "DOG!", and no longer "dog". //
// //
//
// `, // main() { // const bool = ref(false); // const animal = ref("dog"); // const spread = reactive({ animal: "panther" }); // const list = reactive([1, 2, 3]); // setTimeout(() => { // spread.animal = "PANTHER!"; // animal.value = "DOG!"; // bool.value = true; // }, 500); // setTimeout(() => { // animal.value = "DOG!!!!!"; // }, 1000); // return { animal, spread, bool, list }; // }, // }; // const app = new App(); // app.register("child", child); // app.mount(parent, "#app"); // ------------------------------------------------ // Event directive // const counter = { // template: html` //
//
//
color is gray
//
color is white
//
{{style.color}}
//
//

Count: {{count}}{{count >= 2 ? '!!!' : ''}}

// // //
//
{{color}}
//
//
// `, // main() { // const count = ref(0); // const style = reactive({ color: "gray" }); // const increment = () => count.value++; // const decrement = () => count.value--; // setInterval(() => { // style.color = style.color === "gray" ? "white" : "gray"; // }, 500); // return { count, increment, decrement, style }; // }, // }; // const app = new App(); // app.mount(counter, "#app"); // ------------------------------------------------ // :if above :for // :if with :teleport // const main = { // template: html` //
//
//
//
{{item}}
//
//
//
//
if bool teleported! {{items}}
//
//
// `, // main() { // const items = reactive([1, 2, 3, 4, 5]); // const bool = ref(true); // setInterval(() => (bool.value = !bool.value), 2050); // return { items, bool }; // }, // }; // const app = new App(); // app.mount(main, "#app"); // ------------------------------------------------ // :html // const main = { // template: html`
`, // main() { // const html = ref("

hello

"); // setTimeout(() => { // if (html.value === "

hello

") { // html.value = "

world

"; // } // }, 1000); // return { html }; // }, // }; // const app = new App(); // app.mount(main, "#app"); // ------------------------------------------------ // Colors from css framework // const main = { // template: html` //
//

phase

//

Colors

// //
//
//
{{variant}}-{{rank}}
//
//
//
//
// `, // main() { // const ranks = reactive(["5", "10", "20", "30", "40", "50", "60", "70", "80", "90"]); // const basesReverse = computed(() => Array.from(ranks).reverse()); // const bg = (variant: string, rank: string, index: number) => ({ backgroundColor: `var(--${variant}-${rank})`, color: `var(--${variant}-${basesReverse.value[index]})` }); // // return { ranks, bg }; // }, // }; // // const app = new App(); // app.mount(main, "#app"); // ------------------------------------------------ // :scope // const child = { // template: html` //
// I am child and I have a cheeseburger: "{{food}}" (does not inherit) //
// //
//
// `, // main() { // // Comment this out to implicitly inherit the // // provided :scope value from the parent. // const food = ref("🍔"); // return { food }; // }, // }; // // const main = { // template: html` //
//
//
Parent has pizza: {{food}} and scoped drink: {{drink}}
// Child slot, food: {{food}} {{drink}} //
//
// `, // main() { // // return { food: ref("nothing") }; // }, // }; // // const app = new App(); // app.register("child", child); // app.mount(main, "#app"); // ------------------------------------------------ // Practical :scope demo // const main = { // template: html` //
//
ON
//
OFF
// //
visible: {{visible}} / foo: {{foo}}
//
// `, // main() { // const foo = ref("bar"); // return { foo }; // }, // }; // const app = new App(); // app.mount(main, "#app"); // -------- // weird issue // const child = { // template: html`
child{{thing}}
`, // props: { thing: { default: 1 } }, // main({ thing }) { // return { thing }; // }, // }; // // const counter = { // template: html` //
//
//
//
{{color}}
//
// //
//
// `, // main() { // const colors = reactive(["red", "orange"]); // return { colors }; // }, // }; // // const app = new App(); // app.register("child", child); // app.mount(counter, "#app"); // ------------------------------------------------ //