Something went wrong. Try again.
Testing building apps with atproto
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import { atom, derived, isAtom } from "./atoms";import { ref, html, setup, render } from "./core";
export const Show = (showIfTrue, { when, fallback = (key) => html`<template ${key}></template>` }) => { const key = ref() const initialRender = when.value ? showIfTrue(key) : fallback(key)
setup(() => { when.subscribe((val) => { if (!val) { key.element.insertAdjacentHTML('afterend', render(() => { return fallback(key) })) } else { key.element.insertAdjacentHTML('afterend', render(() => { return showIfTrue(key) })) } key.element.remove() }).until(() => !key.element.parentElement.isConnected) })
return initialRender}
export const Repeat = (template, { list }) => { const isReactive = isAtom(list); const items = isReactive ? list.value : list;
if (!isReactive) { return items.map((item) => { return template(item); }).join(''); }
const startAnchor = ref(); const endAnchor = ref();
setup(() => { list.subscribe((newItems) => { const startElement = startAnchor.element; const endElement = endAnchor.element; if (!startElement || !endElement) return;
let node = startElement.nextSibling; while (node && node !== endElement) { const next = node.nextSibling; node.remove(); node = next; }
endElement.insertAdjacentHTML('beforebegin', render(() => { return newItems.map((item) => { return template(item); }).join(''); })); }, { lazy: true }) .until(() => !startAnchor.element?.isConnected); });
return html` <template ${startAnchor}></template> ${items.map((item, i) => template(item)).join('')} <template ${endAnchor}></template> `;};
export { atom, derived, isAtom }