Skip to content

Instantly share code, notes, and snippets.

View chaance's full-sized avatar
🏄‍♂️
Out there

Chance Strickland chaance

🏄‍♂️
Out there
View GitHub Profile
@chaance
chaance / route-announcement.js
Created November 21, 2021 16:02
Remix route transition announcements
import * as React from "react";
import { useLocation, useTransition } from "remix";
function RouteAnnouncement({ getTitle = defaultGetTitle }) {
let location = useLocation();
let transition = useTransition();
let liveRegionRef = React.useRef(null);
React.useEffect(() => {
liveRegionRef.current = document.createElement("div");
@chaance
chaance / postcss-simple-scoper.js
Last active October 12, 2021 02:53
This was the result of a brainfart I had today. I wanted a PostCSS plugin that game me some *very simple* scoping capabilities, and nothing I found quite did what I wanted. Yet to thoroughly test this, but putting it here for now so I don't forget about it.
const DIRECTIVES = [
/**
* If there is currently a local scope or no scope, the :root directive will
* revert to using only the top-level scope if one is defined.
*
* @example
*
* @simple-scope;
*
@chaance
chaance / reset.css
Created October 5, 2021 22:38
Go-to resets for new projects
html {
box-sizing: border-box;
tab-size: 4;
font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial,
sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
line-height: 1.15;
-webkit-text-size-adjust: 100%;
}
*,
@chaance
chaance / composer.json
Created September 2, 2021 04:09
Example composer.json for a WordPress project using PHP Codesniffer
{
"name": "blah/blah",
"type": "wordpress-theme",
"authors": [
{
"name": "Chance Strickland",
"email": "hi@chance.dev"
}
],
"require": {
@chaance
chaance / example.ts
Created August 12, 2021 03:39
Merge discriminated union props for better easier destructuring
type SomeProps =
| { action: "START" }
| { action: "STOP"; time: number }
| { action: "PAUSE"; time: number; ref: { current: any } };
function useExample(props: SomeProps) {
let {
action, // "START" | "STOP" | "PAUSE"
time, // number | undefined,
ref // { current: any } | undefined
@chaance
chaance / useAccessibleRouting.js
Last active August 9, 2021 18:09
More accessible client-side routing with React Router. Experimental code!
function useAccessibleRouting(skipLinkRef) {
let location = useLocation();
let liveRegionRef = React.useRef();
React.useEffect(
/**
* Create a live region that will be used to announce route changes when the
* user navigates. This hook should be called from the root App component,
* so this should be created and appended to the DOM only once.
*/
@chaance
chaance / weird.tsx
Created May 27, 2021 22:18
Inferred link props with TS template literal types
type LinkProps<T extends { href: any }> = T extends { href: infer U }
// If our `href` prop starts with a given substring, we might
// determine that we have an external link and filter internal
// routing-related props. This could just as easily work in reverse!
? U extends `https://${string}` | `http://${string}`
? ExternalLinkProps
: InternalLinkProps
: never;
@chaance
chaance / phony-use-state.ts
Created April 13, 2021 13:57
Phony useState
// For Reference
import * as React from 'react'
import * as ReactDOM from 'react-dom'
const states: any[] = []
let calls = -1
function useState<S>(value: S) {
const call = ++calls
@chaance
chaance / some-action-types.ts
Created April 13, 2021 13:34
Weird type mapping thing
// With template literal types this kind of thing is a breeze!
type RootActionPrefix = 'auth';
type ActionType = 'LOGIN' | 'LOGOUT';
type RootActionTypes = `${RootActionPrefix}/${ActionType}`;
// Is there any sort of mapped-type magic that would let me
// turn this...
type AuthActions =
| { type: 'LOGIN'; user: UserNoPassword }
@chaance
chaance / test.ts
Created April 7, 2021 21:03
Focusing in tests is frustrating
describe("when navigating between focused buttons", () => {
let buttons: HTMLElement[];
beforeAll(() => {
let rendered = renderTestAccordion();
buttons = rendered.buttons;
});
it("should move focus to the next focusable button on `ArrowDown` press", () => {
// document.activeElement is the body here, cool
buttons[0].focus();