Skip to content

Instantly share code, notes, and snippets.

@JLarky
JLarky / IsolateCSS.tsx
Last active May 16, 2023 14:12
How to use shadow dom to isolate CSS of React component https://twitter.com/JLarky/status/1657989891526123520
View IsolateCSS.tsx
export function IsolateCSS(props: { children: React.ReactNode }) {
const onceRef = useRef(false);
const [shadowRoot, setShadowRoot] = useState<ShadowRoot>();
const ref = useCallback((ref: HTMLDivElement | null) => {
if (ref && onceRef.current === false) {
onceRef.current = true;
setShadowRoot(ref.attachShadow({ mode: 'open' }));
}
}, []);
View page.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script>
window.startClerk = async () => {
function formRedirect(){
const form = '<form method="get" action="" name="redirect"></form>';
@JLarky
JLarky / 01_t3Env.ts
Last active May 3, 2023 03:41
This is @t3-oss/env-core but if you don't want to send zod to client bundle (be careful, it's for Astro, not Next.js) https://twitter.com/JLarky/status/1653595995744395264
View 01_t3Env.ts
// src/t3Env.ts
import { createEnv, LooseOptions, StrictOptions } from '@t3-oss/env-core';
import { z, ZodType, ZodString } from 'zod';
function validateEnv<
TPrefix extends string,
TServer extends Record<string, ZodType> = NonNullable<unknown>,
TClient extends Record<string, ZodType> = NonNullable<unknown>
>(opts: LooseOptions<TPrefix, TServer, TClient> | StrictOptions<TPrefix, TServer, TClient>) {
return opts;
@JLarky
JLarky / 01_ExampleUsage.astro
Created April 11, 2023 13:08
Playing with youtube facade based on vb/lazyframe
View 01_ExampleUsage.astro
<LazyFrame
loading="lazy"
style={{
position: "absolute",
top: "0",
left: "0",
width: "100%",
height: "100%",
borderRadius: "10px",
}}
@JLarky
JLarky / 00_README.md
Last active March 26, 2023 18:08
Asking the same question different AI models
@JLarky
JLarky / index.astro
Created March 6, 2023 01:49
How to detect if the page is crawled by bot (Google Bot, Search Bot) in Astro (astro.build)
View index.astro
---
import isbot from "isbot";
const isBot = isbot(Astro.request.headers.get("User-Agent"));
console.log("isBot", isBot);
---
<h1>{isBot ? "bot" : "user"}</h1>
@JLarky
JLarky / README.md
Last active March 2, 2023 23:08
Quick and dirty GPT Tokenizer in one command
View README.md

dev

deno run --watch --allow-net main.ts
curl -d 'test world :)' http://localhost:8085

prod

deno run --allow-net=0.0.0.0:8085 https://gist.githubusercontent.com/JLarky/6a77d9d483b7f0067fd3eea1b785e628/raw/3cf707ae447f8309564b94613a014f9ddf3f972d/main.ts

curl -d 'test world :)' http://localhost:8085

@JLarky
JLarky / 10kb.txt
Created February 20, 2023 01:06
both files when gzipped produce 1.5kb files https://twitter.com/alpinelogic1/status/1627465760334401538
View 10kb.txt
bbbbbbaaaaaaabbaabaaabaabaaaaabbbbbabaaabaaababaaaabbbaabaababbaaaaabaabbababbaabaaaabaabbbaababbaabbaaabbaabaaabbbbbbaaababaabaaabbbabbbaababbaaaaaaaaabaabaaaaaaaaaaaabbbaaababaabaaabaaaaababaabaaaaabaaaaabaababababaaaababbbabbaababaaabbaaaaaabaaabaaaabaaaaabaabaaaaaababbabbaaaababaabbbaababaabaabaaaaabbbbaabbababaaaaabaabaabbaaabababaaaaabbbbbabaabaabbabaabaabaaaabaaaaabbaaaaabbbaabaabaaabaaabbbaabbbabbaaaaaaaabababbaaaabbaaabaaabababaaaaaabababaabaabbabbbaaabaaaaaabbaabbbabaabaaaaaaaaaaaaaaaabbaaaabaaaabaaaaaabbbaaabbabaaaaaaabbaaababaabaabaabaaaaaabbbaabbaabbaababaaabbbababaaaabbaaaaabbaaabaaaaaaaaabaabbbabaaaabbabaaaababbaaabbabaababbaabaababaabaaabbbbaaabaaaabbbbbaabbbaababbbaaababababbbabbbababbbaaabaaaabaababaaaabaabbabaaabbbaaabaabaababababaaaaaabbaabbaaaaabaaababaabaaababbaabbbaabbbbaaaaababaaaaaabaaaabaaababababaaaaaaababaaabaaababbaaaaaaabababbbabaabaabbbaababababaabbaabaaababaabbabbbaaaababbaaabbbabaaaaaabbabbababaaaaaabaaaaabaaaabaaaabaaaabaaabaaaaaaabbbababaaaabaaaaabaabaabbbabaaababaaabaaaaaba
@JLarky
JLarky / list files node.js
Last active February 6, 2023 18:26
how to list files in a folder in node.js https://sharegpt.com/c/RW0fYB3
View list files node.js
async function listFiles(dir: string) {
try {
const { readdir } = await import('fs/promises');
const files = await readdir(dir);
console.log(files);
} catch (err) {
throw err;
}
}
console.log(await listFiles('.'));
View keybindings.json
// ~/Library/Application\ Support/Code/User/keybindings.json
// https://sharegpt.com/c/jZsUO6C
{
"key": "shift+z shift+z",
"command": "workbench.action.files.save",
"when": "editorTextFocus && amVim.mode == 'NORMAL'"
},
{