Skip to content

Instantly share code, notes, and snippets.

View donaldpipowitch's full-sized avatar

Donald Pipowitch donaldpipowitch

View GitHub Profile
@donaldpipowitch
donaldpipowitch / useScrollBottom.ts
Created May 29, 2019 08:53
React Hook to detect if a scrollable element reached the end.
function useScrollBottom<T extends HTMLElement>(offset?: number) {
const [atBottom, setAtBottom] = useState(false);
const scrollEl = useRef<T>(null);
useEffect(() => {
const { current } = scrollEl;
if (current) {
// by default we take the clientHeight into account, so "bottom" begins as soon as the last
// visible piece appears in the scrollable area
const withOffset = offset === undefined ? current.clientHeight : offset;
@donaldpipowitch
donaldpipowitch / usePrevious.ts
Created June 14, 2019 12:04
React Hook to use previous value
import { useEffect, useRef } from 'react';
function usePrevious<T>(value: T) {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
@donaldpipowitch
donaldpipowitch / example.ts
Created July 25, 2019 06:21
Share useState with useContext
import React, { createContext, useState, useContext, FC } from 'react';
type ContextValue = [boolean, (value: boolean) => void] | undefined;
const FullwidthContext = createContext<ContextValue>(undefined);
const defaultState = false;
export const FullwidthProvider: FC = ({ children }) => {
const state = useState(defaultState);
return (
@donaldpipowitch
donaldpipowitch / interceptor.ts
Created August 21, 2019 07:59
Try to refresh token once on 401 with RxJS and Angular Http Interceptor
import { Injectable } from '@angular/core';
import {
HttpEvent,
HttpInterceptor,
HttpHandler,
HttpRequest,
HttpErrorResponse,
HttpClient
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
@donaldpipowitch
donaldpipowitch / file.ts
Created August 21, 2019 13:13
Subsequent requests with RxJS (and Angular), but using the first response
this.http.get<Data1>(url1).pipe(
mergeMap((data1) =>
this.http.get<Data2>(url2).pipe(map((_data2) => data1))
)
);
@donaldpipowitch
donaldpipowitch / axe.js
Created November 26, 2018 11:44
aXe based a11y checks in your CI for Storybook
const puppeteer = require('puppeteer');
const chalk = require('chalk');
const { green, red, cyan, grey, bold } = chalk;
// we assume storybook can visited at http://localhost:9001
const url = 'http://localhost:9001/iframe.html';
const runAxe = () =>
new Promise((resolve, reject) =>
@donaldpipowitch
donaldpipowitch / click-outside.directive.ts
Created September 5, 2019 07:58
Angular directive to (conditionally) track clicks outside of an element
import {
Directive,
Input,
Output,
EventEmitter,
ElementRef,
OnDestroy
} from '@angular/core';
@Directive({
@donaldpipowitch
donaldpipowitch / example.html
Last active September 5, 2019 09:05
Angular directive to (conditionally) track focusout outside of an element (children are ignored)
div [my-focusout-outside]="shouldTrack" (focusoutOutside)="doSomething()">Hello</div>
@donaldpipowitch
donaldpipowitch / README.md
Created September 6, 2019 07:18
Small example to showcase differences a CSS-in-JS solution in React can offer vs. styling in Angular

React

index.tsx:

import React from 'react';
import styled from 'styled-components';
 
enum Area {
  header = 'header',
@donaldpipowitch
donaldpipowitch / use-breakpoints.tsx
Created September 11, 2019 12:52
useBreakpoints hook for React
import React, { createContext, useContext, useMemo, FC } from 'react';
import { useMedia } from 'use-media';
enum Breakpoints {
small = 'min-width: 768px',
medium = 'min-width: 992px',
large = 'min-width: 1200px'
}
type BreakpointsContextValue = {