Skip to content

Instantly share code, notes, and snippets.

View davidkpiano's full-sized avatar
🎹
Working on XState Dev Tools

David Khourshid davidkpiano

🎹
Working on XState Dev Tools
View GitHub Profile
@davidkpiano
davidkpiano / app.js
Created September 4, 2015 14:41
Simple way to namespace React components
import React from 'react';
import * as My from './components/my-components.js';
export default class App extends React.Component {
render() {
return (
<div>
<My.Foo />
<My.Bar />
@davidkpiano
davidkpiano / css-state-machines.md
Last active June 15, 2023 15:26
Article for creating CSS State Machines

As the number of different possible states and transitions between states in a user interface grows, managing styles and animations can quickly become complicated. Even a simple login form has many different "user flows":

https://codepen.io/davidkpiano/pen/WKvPBP

State machines are an excellent pattern for managing state transitions in user interfaces in an intuitive, declarative way. We've been using them a lot on the Keyframers as a way to simplify otherwise complex animations and user flows, like the one above.

So, what is a state machine? Sounds technical, right? It’s actually more simple and intuitive than you might think. (Don’t look at Wikipedia just yet… trust me.)

Let’s approach this from an animation perspective. Suppose you’re creating a loading animation, which can be in only one of four states at any given time:

@davidkpiano
davidkpiano / machine.js
Last active October 3, 2022 16:21
Generated by XState Viz: https://xstate.js.org/viz
const updatePosition = assign({
position: (_, event) => event.position,
})
function geoService(context, event) {
return cb => {
if (!navigator.geolocation) {
cb({
type: 'error',
error: new Error('Geolocation is not supported'),
@davidkpiano
davidkpiano / machine.js
Last active July 20, 2022 20:23
Generated by XState Viz: https://xstate.js.org/viz
Machine({
initial: 'idle',
context: {
// the position of the box
x: 0,
y: 0,
// where you clicked
pointerx: 0,
pointery: 0,
// how far from where you clicked
@davidkpiano
davidkpiano / machine.js
Created September 6, 2019 16:03
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
initial: "start",
context: {
selected: [],
x1: 0,
y1: 0,
x2: 0,
y2: 0,
drag: {
x1: 0,
@davidkpiano
davidkpiano / SassMeister-input.scss
Last active February 12, 2022 02:29
Generated by SassMeister.com.
// ----
// Sass (v3.4.11)
// Compass (v1.0.3)
// ----
@function math-pow($number, $exp) {
@if (round($exp) != $exp) {
@return math-exp($exp * math-ln($number));
}
@davidkpiano
davidkpiano / poc.js
Last active October 2, 2021 12:55
xstate query ideas
function Example() {
const [{ isLoading, error, data }] = useMachine(
createQuery('repoData', () => fetch('...').then((res) => res.json()))
);
}
@davidkpiano
davidkpiano / reducer-with-effects.js
Last active August 20, 2021 18:58
An idea for actor-model-based effects with reducers
import {
useReducerWithEffects,
// hand-wavey, but meant to be used by React to create something
// external will produce async values sent to components
createEffect
} from 'react';
function fetchUserEffect(id, parentRef) {
const controller = new AbortController();
const signal = controller.signal;
const machine = Machine({
id: 'ATM',
initial: 'idle',
states: {
idle: {
on: {
INSERT_CARD: 'authenticating',
}
},
authenticating: {
@davidkpiano
davidkpiano / SassMeister-input.scss
Last active May 27, 2021 14:20
Generated by SassMeister.com.
// ----
// Sass (v3.4.12)
// Compass (v1.0.3)
// ----
$scotch-color-key: 'base' !default;
$scotch-colors: (
'primary': (
'base': #8e3329,