Skip to content

Instantly share code, notes, and snippets.

View asvny's full-sized avatar
🎯
Focusing

Annamalai Saravanan asvny

🎯
Focusing
View GitHub Profile
@asvny
asvny / esbuild-relay.js
Created February 25, 2021 01:31 — forked from sciyoshi/esbuild-relay.js
Esbuild plugin for compiling relay queries
import { promises } from "fs";
import crypto from "crypto";
import path from "path";
import { print, parse } from "graphql";
const plugin = {
name: "relay",
setup: build => {
build.onLoad({ filter: /\.tsx$/, namespace: "" }, async args => {
let contents = await promises.readFile(args.path, "utf8");
@asvny
asvny / RouteParams.ts
Created November 4, 2020 19:30 — forked from natemoo-re/RouteParams.ts
Typed parameter object from path
type RestParam<S extends string> = S extends `...${infer A}` ? A : never;
type StandardParam<S extends string> = S extends `...${infer A}` ? never : S;
type ExtractParams<S extends string> = S extends `[${infer A}]` ? A : never;
type TupleToUnion<T extends any[]> = T[number];
type Split<S extends string> =
string extends S ? string[] :
S extends '' ? [] :
S extends `${infer T}/${infer U}` ? [T, ...Split<U>] :
[S];
@asvny
asvny / eval.js
Created November 4, 2020 10:22 — forked from natemoo-re/eval.js
Safe(ish) Eval
// Adopted from Alpine.js
// https://github.com/alpinejs/alpine/blob/b6e07b2775de444c5f9bdc4d94accb7b720fd6a7/src/utils.js#L59
function saferEval(expression, context, additionalHelperVariables = {}) {
return (new Function(['$data', ...Object.keys(additionalHelperVariables)], `var result; with($data) { result = ${expression} }; return result`))(
context, ...Object.values(additionalHelperVariables)
)
}
export default function sandboxedEval(expression, context = {}) {
@asvny
asvny / modern-reset.css
Created October 11, 2019 00:03
Modern reset
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Remove default padding */
ul[class],
ol[class] {
import SwiftUI
import Combine
class MyDatabase: BindableObject {
let didChange = PassthroughSubject<MyDatabase, Never>()
var contacts: [Contact] = [
Contact(id: 1, name: "Anna"), Contact(id: 2, name: "Beto"),
Contact(id: 3, name: "Jack"), Contact(id: 4, name: "Sam")
] {
@asvny
asvny / example.jsx
Created May 21, 2019 11:29 — forked from bvaughn/LICENSE.md
Advanced example for manually managing subscriptions in an async-safe way using hooks
import React, { useMemo } from "react";
import useSubscription from "./useSubscription";
// In this example, "source" is an event dispatcher (e.g. an HTMLInputElement)
// but it could be anything that emits an event and has a readable current value.
function Example({ source }) {
// In order to avoid removing and re-adding subscriptions each time this hook is called,
// the parameters passed to this hook should be memoized.
const subscription = useMemo(
() => ({
@asvny
asvny / App.js
Created November 20, 2018 20:53 — forked from ryanflorence/App.js
import React, { Suspense, useState } from "react";
import { unstable_createResource as createResource } from "react-cache";
import {
Combobox,
ComboboxInput,
ComboboxList,
ComboboxOption
} from "./Combobox2.js";
function App({ tabIndex, navigate }) {
// used to retrieve template content
const templates = new Map;
// used to retrieve node updates
const updates = new WeakMap;
// hyperHTML, the nitty gritty
function hyperHTML(chunks, ...interpolations) {
// if the static chunks are unknown
@asvny
asvny / FocusTrap.js
Created October 27, 2018 06:26
FocusTrap custom component using LitElement
import {
html,
LitElement
} from "https://unpkg.com/@polymer/lit-element@0.6.2/lit-element.js?module";
const KEYCODE_TAB = 9;
class FocusTrap extends LitElement {
constructor() {
super();
@asvny
asvny / useFocusTrap.jsx
Created October 26, 2018 04:55
FocusTrap - React Hook
// Based on https://hiddedevries.nl/en/blog/2017-01-29-using-javascript-to-trap-focus-in-an-element
import React, { useRef, useEffect } from 'react';
const KEYCODE_TAB = 9;
function useFocusTrap() {
const elRef = useRef(null);
function handleFocus(e) {