Skip to content

Instantly share code, notes, and snippets.

import * as React from 'react';
interface Props {
value: string | number;
name?: string;
onChange(name: string, value: number): void;
}
interface State {
value: string;
@everdimension
everdimension / FSA-handle-error.js
Created February 12, 2018 23:34
A way to describe response error while conforming to Flux Standard Action and passing request info
/**
* see redux-actions lib to see how `createAction` works
* https://redux-actions.js.org/docs/api/createAction.html
*/
import { createAction } from 'redux-act(ions)';
class ResponseError extends Error { /* add a `this.request` prop */ }
/** add a `meta` field when payload in an Error */
const receiveEntity = createAction('SOME_TYPE', identity, payload => {
@everdimension
everdimension / isBrowserIE.js
Last active April 6, 2018 19:21
check for ie11
function isBrowserIE() {
const ie11Re = /Trident.*rv:11/;
const edgeRe = /Edge\//;
const ua = navigator.userAgent;
return ie11Re.test(ua) || edgeRe.test(ua);
}
@everdimension
everdimension / on_scroll_visible.js
Created August 14, 2015 12:57
Add event listener to the `scroll` event to check if the element is fully visible within the window viewport and do something when it is. Use debounce function to not abuse the cpu.
function debounce (fn, wait) {
var timeout;
return function() {
var cntx = this;
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
@everdimension
everdimension / modal-dialog-a11y.html
Last active August 21, 2018 20:49
A proper way to create an accessible modal dialog element (a11y). This gist is only about the wrapper element, the links in the comment describe all necessary steps in detail.
<!--
A tabindex="-1" allows an element to receive programmatic focus.
This is useful a modal dialog window: when opened, focus should be set to the dialog so a screen reader
will begin reading and the keyboard will be able to navigate within the dialog.
Because the dialog (probably a <div> element) is not focusable by default, assigning it tabindex="-1"
allows scripting to set focus to it when it is presented
Resources:
https://www.w3.org/WAI/GL/wiki/Using_ARIA_role%3Ddialog_to_implement_a_modal_dialog_box#Note_on_focus_management
https://webaim.org/techniques/keyboard/tabindex
@everdimension
everdimension / Toaster.js
Last active October 17, 2018 09:25
Sample code showing a "toaster" architecture
class Toaster {
constructor() {
this.listeners = [];
this.id = 0;
}
subscribe(listener) {
this.listeners.push(listener);
return () => {
this.listeners = this.listeners.filter(l => l !== listener);
@everdimension
everdimension / normalizedIncludes.ts
Created October 18, 2018 16:45
Detect occurrence of a string in another string by normalizing both strings and ignoring diacritical marks
function normalizedIncludes(str1: string, str2: string) {
/**
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
*
* https://stackoverflow.com/a/37511463/3523645
*/
const diacriticsRange = /[\u0300-\u036f]/g;
const str1Normalized = str1.normalize('NFD').replace(diacriticsRange, '');
const str2Normalized = str2.normalize('NFD').replace(diacriticsRange, '');
return str1Normalized.includes(str2Normalized);
@everdimension
everdimension / native-font-stack.css
Last active February 16, 2019 18:12
Css rule for setting the "Native Font stack" or "system font".
body {
font-family:
/* Safari for OS X and iOS (San Francisco) */
-apple-system,
/* Chrome >= 56 for OS X (San Francisco), Windows, Linux and Android */
system-ui,
/* Chrome < 56 for OS X (San Francisco) */
BlinkMacSystemFont,
/* Windows */
"Segoe UI",
@everdimension
everdimension / sequential-polling.ts
Last active April 19, 2019 13:39
Polling: "make request — wait for response, but no less than 1 second — make another request"
export function sequentialPolling({
taskFn,
interval = 1000,
}: {
taskFn: () => Promise<any>;
interval?: number;
}) {
let isActive = true;
let timeoutId: number | undefined;
let rejector: Function | undefined;
class MyForm extends React.Component {
constructor() {
super();
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);