Skip to content

Instantly share code, notes, and snippets.

type AnyFunction = (...args: any[]) => any
function useEvent<T extends AnyFunction>(callback?: T) {
const ref = useRef<AnyFunction | undefined>(() => {
throw new Error("Cannot call an event handler while rendering.")
})
// Or useInsertionEffect if it's React 18
useLayoutEffect(() => {
ref.current = callback
})
@s-cork
s-cork / codemirror.tsx
Last active September 15, 2021 07:11
Codemirror 6 with react
import React, { useEffect, useMemo, useCallback, useLayoutEffect, useState } from "react";
import { EditorView } from "@codemirror/view";
import { EditorState, Compartment, EditorStateConfig, StateEffect, Extension } from "@codemirror/state";
import { indentUnit } from "@codemirror/language";
/** creates an editor view from an initial state - destroys the view on cleanup */
export function useEditorView(initState: (() => EditorStateConfig) | EditorStateConfig = {}): EditorView {
const view = useMemo(
() => new EditorView({ state: EditorState.create(typeof initState === "function" ? initState() : initState) }),
[]
// by dave @beesandbombs
int[][] result;
float t, c;
float ease(float p) {
return 3*p*p - 2*p*p*p;
}
float ease(float p, float g) {

Reach UI Philosophy

Reach UI is an accessible foundation for React applications and design systems.

The three equally important goals are to be:

  • Accessible
  • Composable
  • Stylable
@jamiewinder
jamiewinder / detectexternalclick.tsx
Created November 22, 2018 16:05
Portal-friendly external event detection
import React from 'react';
import { DetectExternalEvents } from './detectexternalevents';
const EXTERNAL_REACT_EVENTS = ['onMouseDown', 'onTouchStart'];
const EXTERNAL_DOM_EVENTS = ['mousedown', 'touchstart'];
export interface IDetectExternalClickProps {
component?: string;
onExternalClick: (event: Event) => void;
@treyhunner
treyhunner / choice_enum.py
Created April 9, 2018 23:49
Enum for use with Django ChoiceField
from enum import Enum, EnumMeta
class ChoiceEnumMeta(EnumMeta):
def __iter__(self):
return ((tag, tag.value) for tag in super().__iter__())
class ChoiceEnum(Enum, metaclass=ChoiceEnumMeta):
"""
@samthor
samthor / safari-nomodule.js
Last active February 14, 2024 02:54
Safari 10.1 `nomodule` support
// UPDATE: In 2023, you should probably stop using this! The narrow version of Safari that
// does not support `nomodule` is probably not being used anywhere. The code below is left
// for posterity.
/**
* Safari 10.1 supports modules, but does not support the `nomodule` attribute - it will
* load <script nomodule> anyway. This snippet solve this problem, but only for script
* tags that load external code, e.g.: <script nomodule src="nomodule.js"></script>
*
* Again: this will **not** prevent inline script, e.g.:
@jevakallio
jevakallio / reactiveconf-slam-poetry.md
Last active July 7, 2021 19:57
#ReactiveConf 2017 Lightning Talk Submission: JavaScript Slam Poetry

TL;DR: If you want to see me perform a spoken word poem about JavaScript in front of 1000 people (and on video), please ⭐ star this gist. If you're on mobile, you'll need to request desktop site.

JavaScript Slam Poetry

Javascript! Slam! Poetry!

@nemtsov
nemtsov / remove-unused-imports.js
Last active December 20, 2022 04:02
A jscodeshift to remove unused imports
module.exports = (file, api, options) => {
const j = api.jscodeshift;
const printOptions = options.printOptions || {quote: 'single'};
const root = j(file.source);
const requires = {};
const filterAndTransformRequires = path => {
const varName = path.value.local.name;
const scopeNode = path.parentPath.scope.node;
from keras.models import Sequential
from keras.layers import Dense
x, y = ...
x_val, y_val = ...
# 1-dimensional MSE linear regression in Keras
model = Sequential()
model.add(Dense(1, input_dim=x.shape[1]))
model.compile(optimizer='rmsprop', loss='mse')