Skip to content

Instantly share code, notes, and snippets.

View thebuilder's full-sized avatar

Daniel Schmidt thebuilder

View GitHub Profile
@thebuilder
thebuilder / hookWithRef.js
Last active January 16, 2024 13:50
Hook with ref callback
import React, {useCallback, useRef} from 'react'
function useHookWithRefCallback() {
const ref = useRef(null)
const setRef = useCallback(node => {
if (ref.current) {
// Make sure to cleanup any events/references added to the last instance
}
if (node) {
@thebuilder
thebuilder / jest.config.js
Last active October 29, 2022 02:42
Use Jest Projects to run both JSDom and Node tests in the same project
module.exports = {
projects: [
{
displayName: 'dom',
testEnvironment: 'jsdom',
snapshotSerializers: ['enzyme-to-json/serializer'],
testMatch: ['**/__tests__/**/*.test.js?(x)']
},
{
displayName: 'node',
@thebuilder
thebuilder / arrayElement.ts
Created July 20, 2022 10:25
TypeScript: Pick type of item in an Array
/**
* Get an element from an array
* @example
* interface Item {
* id: number
* }
*
* interface BunchOfItems extends Array<Item> {}
*
* interface SingleItemAgain extends ArrayElement<BunchOfItems> {}
@thebuilder
thebuilder / stories.test.ts
Last active March 22, 2021 14:09
Test all Stories in a CSF Storybook
/* eslint-disable jest/valid-title */
import React from 'react';
import { render, RenderResult, waitFor } from '@testing-library/react';
import chalk from 'chalk';
import globby from 'globby';
import path from 'path';
import {
IncludeExcludeOptions,
isExportStory,
storyNameFromExport,
@thebuilder
thebuilder / Example.story.js
Last active November 5, 2020 10:34
Mock Fetch requests in Storybook
import React from 'react'
import { storiesOf } from '@storybook/react'
import FetchStory from './FetchStory'
storiesOf('Fetch Example', module)
.add('Plain', () => (
<FetchStory mocks={{
matcher: '/api/signup',
response: {
body: {
@thebuilder
thebuilder / useInView.tsx
Last active January 26, 2020 00:49
useInView
/* eslint-disable react-hooks/exhaustive-deps */
import * as React from 'react'
import { observe, unobserve } from './intersection'
import { InViewHookResponse, IntersectionOptions } from './index'
type State = {
inView: boolean
entry?: IntersectionObserverEntry
}
@thebuilder
thebuilder / gist:7248688
Created October 31, 2013 12:14
Events with correct scope in TypeScript
class Tracker {
count = 0;
constructor() {
window.addEventListener("mousedown", this.mouseDown);
window.addEventListener("mouseup", this.mouseUp);
}
mouseDown = (ev: MouseEvent) => {
window.addEventListener("mousemove", this.mouseMove);
}
mouseUp = (ev: MouseEvent) => {
@thebuilder
thebuilder / frameLoop.js
Created September 8, 2015 08:45
Call a function with a specific FPS. Exposes methods to pause, start and destroy the looper. Uses ES6.
/**
* @param fn {Function} Callback function to trigger on frame
* @param fps {int} Target FPS
* @returns {{pause: pause, start: start, destroy: destroy}}
*/
function frameLoop(fn, fps=60) {
let then = 0;
let interval = 1000 / fps;
let isRunning = true;
let currentFrameId = null;
@thebuilder
thebuilder / gist:5081990
Last active June 27, 2016 15:19
Get the root FrameLayout of an Android activity.
public FrameLayout getRootFramelayout() {
if (_root == null) _root = (FrameLayout) getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
return (FrameLayout) _root.getChildAt(0);
}
@thebuilder
thebuilder / Button-arrow-func.jsx
Last active April 5, 2016 02:44
Stateless Button with arrow binding - But this creates a new instance on every render?
import React from 'react';
function handleClick(label) {
alert('clicked ' + label);
}
function Button({label}) {
return (
<button onClick={(event) => handleClick(label)}>{label}</button>