cohost/html/common.tsx

218 lines
5.4 KiB
TypeScript
Raw Normal View History

2023-07-20 13:33:55 -04:00
import {
Attributes,
ComponentChild,
ComponentChildren,
JSX,
VNode,
toChildArray,
} from "preact";
2023-07-12 14:15:53 -04:00
import { render } from "preact-render-to-string";
import { writeText } from "copy-paste";
2023-07-10 14:52:20 -04:00
2023-07-12 14:15:53 -04:00
export function Main({
children,
...attributes
}: {
2023-07-20 14:31:12 -04:00
children?: ComponentChildren;
2023-08-19 20:30:42 -04:00
style?: JSX.CSSProperties;
attributes?: JSX.HTMLAttributes;
2023-07-12 14:15:53 -04:00
}) {
return (
<div {...attributes} id="main">
{...toChildArray(children)}
</div>
);
}
2023-07-12 07:05:48 -04:00
2023-07-12 14:15:53 -04:00
export const HCenter = ({
children,
...attrs
}: {
children: ComponentChildren;
attrs?: Attributes;
}) => (
2023-07-12 07:05:48 -04:00
<div
2023-07-12 14:15:53 -04:00
{...attrs}
2023-07-12 07:05:48 -04:00
class="hcenter"
style="display: flex; justify-content: center;"
>
2023-07-12 14:15:53 -04:00
{...toChildArray(children)}
2023-07-12 07:05:48 -04:00
</div>
);
export const eggbug_emotions = (attrs: JSX.HTMLAttributes<HTMLImageElement>) =>
({
smiling: (
<img
class="eggbug"
src="https://staging.cohostcdn.org/attachment/f33b4285-0455-4128-96b8-117054af40c3/eggbugSquare.png"
alt="eggbug, smiling"
{...attrs}
/>
),
frowning: (
<img
class="eggbug"
src="https://static.pyrope.net/eggbug-sad.png"
alt="eggbug, frowning"
{...attrs}
/>
),
revengeance: (
<img
class="eggbug"
src="https://static.pyrope.net/eggbug-revengeance.png"
alt="ULTRA EGGBUG REVENGEANCE, MEGA. THERE IS FIRE. THERE IS TRISCAR. EGGBUG REVENGEANCE. YOU ARE. DIE!"
{...attrs}
/>
),
jpeg_annihilation: (
<img
class="eggbug"
src="https://static.pyrope.net/eggbug-jpeg-annihilation.gif"
alt="eggbug dissolving as the image gets more and more jpeg"
{...attrs}
/>
),
golfball: (
<img
class="eggbug"
src="https://static.pyrope.net/eggbug-golfball.png"
alt="eggbug as a golfball. no legs"
{...attrs}
/>
),
} as const);
2023-07-12 14:15:53 -04:00
export type EggbugEmotion = keyof ReturnType<typeof eggbug_emotions>;
type bla = ({
type: EggbugEmotion;
} & JSX.HTMLAttributes<HTMLImageElement>)["type"];
2023-07-31 17:07:27 -04:00
export const EggbugImg = ({
type,
...attrs
}: { type: EggbugEmotion } & Omit<
JSX.HTMLAttributes<HTMLImageElement>,
"type"
>) => eggbug_emotions(attrs)[type];
2023-07-12 12:55:08 -04:00
2023-07-20 14:31:12 -04:00
export const render_and_copy = (elem: VNode, pretty = false) => {
const rendered = render(elem, null, { pretty });
2023-07-12 14:15:53 -04:00
writeText(rendered);
console.log(rendered);
};
2023-07-15 22:53:32 -04:00
export const mk_class_wrapper =
(klass: string) =>
2023-07-20 14:31:12 -04:00
({ children }: { children?: ComponentChildren }) =>
2023-07-16 00:36:39 -04:00
<div class={klass}>{...toChildArray(children)}</div>;
2023-07-20 13:33:55 -04:00
export const slidify = (
Slide: (_: {
slide: JSX.Element;
next?: JSX.Element;
n: number;
max: number;
}) => JSX.Element,
slides: JSX.Element[],
n = 1,
max = slides.length
): JSX.Element =>
slides[0] ? (
<Slide
slide={slides[0]}
next={slidify(Slide, slides.slice(1), n + 1, max)}
n={n}
max={max}
/>
) : (
<></>
);
// https://cohost.org/lexyeevee/post/2107474-css-for-css-baby-3 (wayyy down at the bottom)
export const DisappearOnClick = ({
children,
className,
}: {
className?: string;
children: ComponentChildren;
}) => (
<details
open
class={`disappearing ${className}`}
style="position: relative; cursor: pointer;"
>
<summary style="list-style: none; position: absolute; inset: 0;"></summary>
{children}
</details>
);
export function shuffle<T>(array: T[]): T[] {
let currentIndex = array.length,
randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex],
array[currentIndex],
];
}
return array;
}
// type NOf<N extends number, T, R extends any[] = []> = R["length"] extends N
// ? R
// : NOf<N, T, [T, ...R]>;
// export function n_of<N extends number, T, R extends any[]>(
// n: N,
// x: T
// ): NOf<N, T, R> {
// return [];
// }
export function n_of<T>(n: number, x: T): T[] {
return new Array(n).fill(x);
}
2023-07-25 01:41:07 -04:00
export const static_url = (res: string) => `https://static.pyrope.net/${res}`;
2023-08-20 13:33:54 -04:00
export const randirect = (...urls: string[]) =>
`https://pyrope.net/randirect#${urls.join("::")}`;
2023-09-02 21:08:44 -04:00
// could do some [T, ...T[]] shenanigans for totality but meh
// kinda bad name
export const pick_random = <T,>(xs: T[]): T =>
xs[Math.floor(Math.random() * xs.length)];
2023-09-04 23:22:46 -04:00
export const jitter = (n: number) => n * (Math.random() - 0.5);
export const svg_url = (svg: string) => `data:image/svg+xml,${encodeURI(svg)}`;
export const css = {
url(href: string) {
return `url('${href}')`;
},
px(n: number) {
return `${n}px`;
},
em(n: number) {
return `${n}em`;
},
rem(n: number) {
return `${n}rem`;
},
inline_block: "inline-block",
block: "block",
grid: "grid",
flex: "flex",
} as const;