Skip to content

Instantly share code, notes, and snippets.

View BrianHung's full-sized avatar
💻
LLMs + local-first apps = autocomplete

Brian Hung BrianHung

💻
LLMs + local-first apps = autocomplete
View GitHub Profile
@BrianHung
BrianHung / GridList.tsx
Last active August 29, 2024 08:15
transition border on scroll
const [scrollPos, onScroll] = useScrollPos('top');
return (
<GridList
items={list.items}
className={cn(
'grow overflow-y-auto flex flex-col gap-px my-2',
'border-y border-border data-[scroll-pos="top"]:border-t-transparent data-[scroll-pos="bottom"]:border-b-transparent transition duration-100'
)}
aria-label="FileList sidebar"
@BrianHung
BrianHung / DurableObjectStorageAdapter.ts
Created July 26, 2024 08:46
automerge durable object storage
/**
* This module provides a storage adapter for Durable Objects.
*
* https://github.com/partykit/partykit/blame/main/packages/y-partykit/src/storage.ts
* @packageDocumentation
*/
import {
Chunk,
StorageAdapterInterface,
@BrianHung
BrianHung / debounceAutocompletion.ts
Created July 12, 2024 08:00
debounced codemirror autocomplete
export function debounceAutocompletion(
language: Language,
source: CompletionSource,
wait: number = 500,
) {
let currContext: CompletionContext;
let cancel = () => {}; // no-op
return [
language.data.of({
autocomplete: (context: CompletionContext) => {
@BrianHung
BrianHung / Highlight.tsx
Created July 4, 2024 00:14
CodeMirror 6 RunMode
// Copy of https://github.com/craftzdog/react-codemirror-runmode
// with support for custom languages
import React from "react";
import { Parser } from "@lezer/common";
import { Highlighter } from "@lezer/highlight";
import { StyleModule } from "style-mod";
import highlight from "./highlight";
@BrianHung
BrianHung / scrollbar.css
Last active May 30, 2024 06:08
scrollbar.css
::-webkit-scrollbar {
height: 1rem;
width: 0.5rem;
}
::-webkit-scrollbar:horizontal {
height: 0.5rem;
width: 1rem;
}
@BrianHung
BrianHung / Resizable.tsx
Created May 23, 2024 06:50
Horizontal Resizable React Component
import React from 'react';
const ResizableDiv = function ResizableDiv(props: { onChange: (dx: number) => void }) {
const { onChange = () => null, ...otherProps } = props;
const resizeRef = React.useRef<HTMLElement | null>(null);
const [resizing, setResizing] = React.useState(false);
const handlePointerDown = React.useCallback(
(event: React.PointerEvent) => {
@BrianHung
BrianHung / setGlobalRootColor.js
Created May 22, 2024 05:00
Prefers Color Scheme JavaScript React
export function setGlobalRootColor() {
try {
const rootElement = document.documentElement;
const classList = rootElement.classList;
classList.remove('light', 'dark');
const themePreference = localStorage.getItem('theme');
if (themePreference === 'system' || (!themePreference && true)) {
const prefersDark = '(prefers-color-scheme: dark)';
const darkModeMediaQuery = window.matchMedia(prefersDark);
@BrianHung
BrianHung / CodeMirror.ts
Last active March 10, 2024 23:25
CodeMirror NodeView for ProseMirror
import { autocompletion, closeBrackets, closeBracketsKeymap, completionKeymap } from '@codemirror/autocomplete';
import { defaultKeymap, history, historyKeymap, indentWithTab } from '@codemirror/commands';
import {
bracketMatching,
defaultHighlightStyle,
foldGutter,
foldKeymap,
indentOnInput,
LanguageDescription,
syntaxHighlighting,
@BrianHung
BrianHung / figma-dots.css
Created February 12, 2024 07:56
figma-dots.css
body {
background-image: radial-gradient(rgba(0, 0, 0, 0.1) 1px, transparent 0);
background-size: 16px 16px;
}
@BrianHung
BrianHung / prosemirror-history-combine-commands.ts
Last active February 6, 2024 08:08 — forked from anarchang/prosemirror-history-combine-commands.ts
Combine commands on the ProseMirror undo stack
/**
* Given two commands (commandA and commandB), returns a new command that when applied
* to a ProseMirror state that uses the prosemirror-history plugin applies commandA and
* then commandB so that both commands are undone with a single undo action.
**/
const combineCommands =
(commandA: Command, commandB: Command) =>
(state: EditorState, dispatch?: (tr: Transaction) => void): boolean => {
return commandA(state, (transactionA: Transaction) => {
const { state: stateA } = state.applyTransaction(transactionA)