Skip to content

Instantly share code, notes, and snippets.

View timc1's full-sized avatar
🖊️

Tim timc1

🖊️
View GitHub Profile
@timc1
timc1 / use-focus.js
Last active April 25, 2019 04:30
Simple hook using React Context API to give control of the flow of focus.
import React from 'react'
const FocusContext = React.createContext()
export function FocusProvider({ children }) {
const focusElement = React.useRef(null)
const cacheFocusElement = element => (focusElement.current = element)
const toggleFocus = () => {
@timc1
timc1 / focus-hook.js
Last active April 27, 2019 23:57
Simple hook to control of the flow of focus.
import React from "react"
export default function useFocus() {
const cachedElement = React.useRef(null)
const setFocusCache = () => (cachedElement.current = document.activeElement)
const setFocus = () =>
cachedElement.current ? cachedElement.current.focus() : {}
import React from "react";
function isMobile() {
if (typeof document !== `undefined`) {
return "ontouchstart" in document.documentElement === true;
}
return false;
}
const mobile = isMobile();
@timc1
timc1 / use-dropzone.tsx
Last active March 3, 2025 03:24
A React hook that returns whether a file is being dragged into the document from the operating system and not from within the browser.
import React from 'react'
type DropzoneContextValue = {
isDragging: boolean
}
const DropzoneContext = React.createContext<DropzoneContextValue | undefined>(
undefined
)
@timc1
timc1 / gist:f1547d1b37e69781e102c3dd88aca0d1
Created May 13, 2019 16:29
Yarn base eslint packages
yad @typescript-eslint/parser eslint-config-react-app eslint-plugin-jsx-a11y eslint-config-prettier eslint-plugin-react-hooks
@timc1
timc1 / use-auth.js
Created June 10, 2019 00:20
React Context + Hooks + Firebase Authentication
import React from 'react'
import firebaseConfig from '../path/to/firebase-config'
import firebase from 'firebase/app'
import 'firebase/auth'
import FullPageLoading from '../path/to/full-page-loading'
AuthProvider.actions = {
setUser: 'SET_USER',
toggleLoading: 'TOGGLE_LOADING',
}
import React from 'react'
import styled from '@emotion/styled'
type StatusType = 'error' | 'neutral' | 'success'
type State = {
isStatusShowing: boolean
status: string
type: StatusType
}

This emulate's vim's <C-e> and <C-y> for scrolling in VSCode using the macros extension.

scrolling vscode editor

  1. add the following to settings.json
"macros": {
  "scrollLineDownFaster": [
 "scrollLineDown",
@timc1
timc1 / useTheme.tsx
Created April 16, 2020 01:21
🌑☀️mode theming hook
import * as React from "react";
type Theme = "system" | "light" | "dark";
const STORAGE_KEY = "theme";
const VALID_THEMES: Theme[] = ["system", "light", "dark"];
const DARK_MODE_MEDIA_QUERY = "(prefers-color-scheme: dark)";
function getAppTheme(): Theme {
if (typeof window !== "undefined") {
@timc1
timc1 / useTheme2.tsx
Created April 18, 2020 20:10
🌑☀️core app system/light/dark mode theming + varying themes for nested components
import * as React from "react";
type ThemeConfig = "system" | "light" | "dark";
type ThemeName = "light" | "dark";
// Custom themes are keyed by a unique id.
type KeyedThemes = {
[k: string]: {
config: ThemeConfig;
themeName: ThemeName;
};