Skip to content

Instantly share code, notes, and snippets.

View DylanPiercey's full-sized avatar

Dylan Piercey DylanPiercey

View GitHub Profile
@DylanPiercey
DylanPiercey / global-state.js
Created December 7, 2023 18:11
marko global state
const valuesById = new Map();
const listenersById = new Map();
export function withGlobalState(component, out, id, state = {}) {
if (typeof document === "undefined") {
if (Array.isArray(id)) {
for (const item of id) {
withGlobalState(component, out, item, state);
}
} else {
@DylanPiercey
DylanPiercey / case.js
Created July 20, 2023 17:25
Switch casing
(() => {
let activeMode = "default";
const modes = {
default: (_) => _,
camel: (_, c) => c.toUpperCase(),
snake: (_, c) => `_${c}`,
kebab: (_, c) => `-${c}`,
smoosh: (_, c) => c.toLowerCase(),
};
const root = document.createElement("div");
@DylanPiercey
DylanPiercey / index.marko
Last active July 28, 2021 22:32
Local storage Tags API example
<local/count=0 storageKey="count"/>
<p>You clicked ${count} times</p>
<button onClick() { count++ }>
Click me
</button>
<div/getEl>
<h1>Test</h1>
</div>
<return={
get div_width() {
return getEl().clientWidth
}
} />
@DylanPiercey
DylanPiercey / index.marko
Created July 28, 2021 15:23
A counter component example using the Marko "Tags API"
<let/count = 0/>
<p>You clicked ${count} times</p>
<button onClick() { count++ }>
Click me
</button>
<let/x="Hello"/>
<input value:=x/>
<div>${x}</div>
@DylanPiercey
DylanPiercey / stream.js
Last active October 1, 2022 09:22
Progressive HTML with fetch
const getWritableDOM = (() => {
const testDoc = document.implementation.createHTMLDocument();
testDoc.write("<script>");
// Safari and potentially other browsers strip script tags from detached documents.
// If that's the case we'll fallback to an iframe implementation.
if (testDoc.scripts.length) {
return target =>
toWritable(target, document.implementation.createHTMLDocument());
} else {
@DylanPiercey
DylanPiercey / index.marko
Created February 10, 2021 15:57
Tween Tag implementation for Marko 6 / FLUURT
<let/distance=0/>
<let/startTime=0/>
<let/curValue=input.default/>
<const/ease=input.ease || (v) => v < 0.5 ? 2 * v * v : -1 + (4 - 2 * v) * v/>
<yield=curValue onnext=(nextValue) => {
startTime = performance.now();
distance = curValue - nextValue;
}/>
@DylanPiercey
DylanPiercey / index.marko
Last active December 12, 2020 16:56
Marko tag variables (as syntax)
static function createBuilder(template) {
const parts = template.replace(/\\n/, "\n").replace(/</g, "&lt;").split(" ");
const partEntries = Object.entries(parts.reduce((map, str, i) => {
["TAG", "VAR", "PARAMS", "ATTRS"].forEach(name => {
if (str.indexOf(name) >= 0) {
map[name] = i;
}
});
return map;
}, {}));
@DylanPiercey
DylanPiercey / ValidatedForm.marko
Created August 19, 2020 15:51
Form validation with context example
class {
onCreate() {
this.state = {
errors: {},
values: {}
};
}
async validate(path, value) {
try {