Skip to content

Instantly share code, notes, and snippets.

View jacobrask's full-sized avatar

Jacob Rask jacobrask

  • Gothenburg, Sweden
View GitHub Profile
import invariant from "tiny-invariant";
class AmalgoBox extends HTMLElement {
get input() {
return this.querySelector("input") as HTMLInputElement;
}
get button() {
return this.querySelector("button") as HTMLButtonElement;
}

assert() (sometimes called invariant())

Instead of checks like:

if (value === null) {
  throw new Error("missing value")
}
doSomethingThatNeedsValue(value)
@WebReflection
WebReflection / custom-elements-pattern.md
Last active March 26, 2025 23:15
Handy Custom Elements' Patterns

Handy Custom Elements' Patterns

Ricardo Gomez Angel Photo by Ricardo Gomez Angel on Unsplash

This gist is a collection of common patterns I've personally used here and there with Custom Elements.

These patterns are all basic suggestions that could be improved, enriched, readapted, accordingly with your needs.

@tim-field
tim-field / useRouter.js
Last active June 23, 2023 01:20
Hooks Router
import { useEffect, useState } from "react"
import { createBrowserHistory } from "history"
const history = createBrowserHistory()
const toLocation = path => new URL(path, window.location.href)
// without this react won't re-render as location is the same object
// @see https://reactjs.org/docs/hooks-reference.html#bailing-out-of-a-state-update
const cloneLocation = () => Object.assign({}, window.location)
import React from 'react'
import { findDOMNode } from 'react-dom'
if (typeof window !== 'undefined' && 'scrollRestoration' in window.history) {
history.scrollRestoration = 'manual'
}
export const useRestoreScroll = () => ({
renderRootContainer: (props) => (
<RestoreScrollContainer {...props}/>
@HenrikJoreteg
HenrikJoreteg / README.md
Last active September 20, 2021 01:36
Minimalist routing in Redux

Why would you want to do this? Because you often don't need more. It's nice to not have to think about your "router" as this big special thing.

Instead, with this approch, your app's current pathname is just another piece of state, just like anything else.

This also means that when doing server-side rendering of a redux app, you can just do:

var app = require('your/redux/app')
var React = require('react')
@Dr-Nikson
Dr-Nikson / README.md
Last active December 30, 2024 11:14
Auth example (react + redux + react-router)
@sebmarkbage
sebmarkbage / Enhance.js
Last active February 10, 2025 06:23
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
@BinaryMuse
BinaryMuse / gist:3b0a4ba166ac81840cab
Created November 3, 2014 16:20
Async fetches initiated by the store when necessary
// Component
var AnswerDisplay = React.createClass({
mixins: [FluxMixin, StoreWatchMixin("answer")],
getStateFromFlux() {
var flux = this.getFlux(),
answerStore = flux.store("answer");
return {
@CGaskell
CGaskell / EnumList
Created July 31, 2014 09:48
Sitecore Enum Driven Dropdownlist. Uses [Description] attribute if defined.
using System;
using System.ComponentModel;
using Sitecore.Web.UI.HtmlControls;
namespace DetangledDigital.Sc.FieldTypes
{
/*
*
* @CGaskell
* Custom field type. Renders a dropdown list based on a specified enum.