Skip to content

Instantly share code, notes, and snippets.

View alexreardon's full-sized avatar

Alex Reardon alexreardon

View GitHub Profile
@alexreardon
alexreardon / fix-post-drop-pointer-bug.ts
Last active April 12, 2024 16:03
Fix Chrome + Safari bug where the element under where the user started dragging (on the viewport) is entered into by the browser
import { bindAll } from 'bind-event-listener';
import type { DragLocation } from '../internal-types';
type CleanupFn = () => void;
/** Set a `style` property on a `HTMLElement`
*
* @returns a `cleanup` function to restore the `style` property to it's original state
*/
@alexreardon
alexreardon / how-this-works.md
Created August 7, 2022 00:29
How `this` works in javascript

this binding

  • this is the runtime context of a function.
  • this is determined by the call site
  • the same function can be executed with different this runtime contexts. You can think of this as another arguement to the function
  • Comparison: scopes are generally defined at compile time (exception: eval)
const person = {
 name: 'Alex',
@alexreardon
alexreardon / alternative-flex-naming.md
Last active May 23, 2024 05:53
Alternative flex naming

flex container

  • flex-directionmain-axis (row [horizontal] → or column [vertical] ↓)
  • justify-contentmain-axis-align
  • align-itemscross-axis-align
  • align-contentcross-axis-align-wrapped

flex child

  • align-selfcross-axis-align-override
@alexreardon
alexreardon / entering-and-leaving-the-window.ts
Last active October 31, 2024 21:39
Detecting when entering or leaving a window
import { bindAll } from 'bind-event-listener';
// *Usually* to detect if you are entering / leaving a window you can
// use the `event.relatedTarget` property:
// "dragenter":
// - `event.relatedTarget` should point to the element that you are coming from
// - Scenario: A -> B
// - `event.target`: `B` (entering B)
// - `event.relatedTarget`: `A`: (leaving A - where you are coming from)
@alexreardon
alexreardon / drag-and-drop-notes.md
Last active August 22, 2025 12:44
An explanation of the timing of drag and drop events

Drag and drop

This is a collection of knowledge I have built up regarding browser powered drag and drop functionality

Events

dragstart

  • timing: once as drag is starting
  • event.target: draggable Element
@alexreardon
alexreardon / typescript.md
Last active July 13, 2022 00:56
TypeScript notes

Learning typescript

This work is not exclusively my own. There are sections copy pasted from elsewhere

Resources

Docs 📖

@alexreardon
alexreardon / machine.js
Last active May 31, 2021 03:57
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
id: 'save-button',
initial: 'hiding',
states: {
hiding: {
on: {
SHOW: 'waiting',
},
},
waiting: {
@alexreardon
alexreardon / machine.js
Last active February 2, 2021 03:26
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'simple-promise',
initial: 'pending',
states: {
pending: {
on: {
RESOLVE: 'resolved',
REJECT: 'rejected',
}
import React, { createContext, ReactNode, useEffect, useMemo, useRef, useState } from 'react';
import { interpret } from 'xstate';
import useRequiredContext from '../shared/use-required-context';
import { Nullable } from '../types';
import isShallowEqual from './is-shallow-equal';
import machine from './machine';
import { MachineEvents, MachineStateType, ServiceType } from './machine-types';
const ServiceContext = createContext<Nullable<ServiceType>>(null);
function Parent({ children }) {
const [state, send, service] = useMachine(toggleMachine);
return <ServiceContext.Provider value={service}>{children}</ServiceContext.Provider>;
}
function Child({ children }) {
const service = useContext(ServiceContext);
const [state, send] = useService(ServiceContext);