Skip to content

Instantly share code, notes, and snippets.

View mjackson's full-sized avatar
💿

Michael Jackson mjackson

💿
View GitHub Profile
import React from "react";
import pathToRegexp from "path-to-regexp";
function Home() {
return <h1>Home</h1>;
}
function About() {
return <h1>About</h1>;
}
@mjackson
mjackson / travis.yml
Last active May 25, 2022 13:56
Travis CI + google-cloud-sdk + updated kubectl + Docker
# The Google Cloud SDK on Travis is pretty old (2014). So if
# you want to use an up-to-date version, you have to install
# your own. This config is the bare minimum you'll need to
# get an updated version of the SDK + kubectl.
cache:
directories:
# We cache the SDK so we don't have to download it again on subsequent builds.
- $HOME/google-cloud-sdk
services:
# Include the docker service so you can roll your own images.
@mjackson
mjackson / Dockerfile
Created October 13, 2017 00:30
Running `gatsby develop` on a container in development
FROM node:8
WORKDIR /home/node/app
ADD https://github.com/Yelp/dumb-init/releases/download/v1.1.1/dumb-init_1.1.1_amd64 /usr/local/bin/dumb-init
RUN chmod +x /usr/local/bin/dumb-init
COPY package.json yarn.lock ./
RUN yarn --pure-lockfile
COPY . .
import React from "react";
import ReactDOM from "react-dom";
import PropTypes from "prop-types";
document.body.style.background = `
linear-gradient(135deg,
#1e5799 0%,
#2989d8 50%,
#207cca 51%,
#7db9e8 100%
import throttle from 'lodash/throttle'
import React, { PropTypes } from 'react'
const ThrottledInput = React.createClass({
propTypes: {
delay: PropTypes.number
},
getDefaultProps() {
return {
@mjackson
mjackson / nested-ternary-jsx.js
Created November 2, 2016 19:35
Nested ternaries work like if/else if/else for JSX
const element = (
<div>
{currentPage === 'signin' ? (
<SignInPage/>
) : currentPage === 'welcome' ? (
<WelcomePage/>
) : currentPage === 'about' ? (
<AboutPage/>
) : (
<DefaultPage/>
@mjackson
mjackson / useStorage.js
Last active November 4, 2021 06:36
A React hook for persisting state between page refreshes (also SSR compatible)
import { useEffect, useRef } from 'react';
function getItem(storage, key) {
const value = storage.getItem(key);
if (!value) return null;
try {
return JSON.parse(value);
} catch (error) {
import { useRef, useState } from 'react'
function useUndo([state, setState]) {
const history = useRef([state])
const [index, setIndex] = useState(0)
function undo() {
setIndex(Math.max(index - 1, 0))
}
function redo() {
import React from "react";
export type ColorScheme = "light" | "dark";
export default function useColorSchemePreference(
defaultColorScheme: ColorScheme = "light"
) {
let darkQuery = "(prefers-color-scheme: dark)";
let [colorScheme, setColorScheme] = React.useState<ColorScheme>(
typeof window === "object" && window.matchMedia
@mjackson
mjackson / createBinding.js
Last active September 18, 2021 09:19
A workaround for the lack of a promise cancelation API
/**
* Registers the given callback to be called in the node.js callback
* style, with error as the first argument, when the promise resolves.
*
* Also, returns a function that may be used to prevent the callback
* from ever being called. Calling the returned function is synonymous
* with saying "I no longer care about the resolution of this promise,
* even if it fails."
*
* Since there is no provision in the promise spec for cancel/abort