Skip to content

Instantly share code, notes, and snippets.

View itsMapleLeaf's full-sized avatar

itsMapleLeaf

View GitHub Profile
@itsMapleLeaf
itsMapleLeaf / example.tsx
Last active October 29, 2019 20:38
root store hook example
// in RootStore.ts
export class RootStore {
userStore = new UserStore()
chatStore = new ChatStore()
appNavigationStore = new AppNavigationStore()
}
export const RootStoreContext = React.createContext<RootStore>()
export function useRootStore() {
@itsMapleLeaf
itsMapleLeaf / Pixiv Public API.yaml
Created August 5, 2019 02:34 — forked from ZipFile/Pixiv Public API.yaml
Unofficial API specification extracted from Pixiv Android App
swagger: "2.0"
info:
title: "Pixiv Public API"
description: "Unofficial API specification extracted from Pixiv Android App v4.8.2"
version: "1.0"
host: public-api.secure.pixiv.net
schemes:
- https
basePath: /v1
produces:
@itsMapleLeaf
itsMapleLeaf / example.tsx
Created August 13, 2019 12:20
geolocation hook example
function SomeComponent() {
const { location, error } = useGeolocation()
useEffect(() => {
if (error) {
Analytics.trackEvent("LocationService: ", error.message, error.code)
handleLocationError(error)
}
}, [error])
@itsMapleLeaf
itsMapleLeaf / co.ts
Last active August 29, 2019 15:22
theoretical typed co function
type CoGen<R> = {
next(): IteratorYieldResult<undefined>
next<T>(value: T): IteratorResult<Promise<T>, R>
}
async function co<R>(genfn: () => CoGen<R>): Promise<R> {
const it = genfn()
let result: IteratorResult<unknown, R> = it.next()
while (true) {
if (result.done) {
@itsMapleLeaf
itsMapleLeaf / example.tsx
Last active September 3, 2019 20:22
useWindowIntersection
function Example() {
const elementRef = useRef<HTMLDivElement>(null) // ref for the element you want to watch
const isInsideWindow = useWindowIntersection(elementRef)
return <div ref={elementRef}>
is inside window: {isInWindow}
</div>
}
@itsMapleLeaf
itsMapleLeaf / example.ts
Created September 26, 2019 00:47
TypeScript - filter type keys by value
type Thing = {
a: number
b: number
c: string
d: boolean
}
type Values<O> = O[keyof O]
type KeysWithType<O, T> = Values<{ [K in keyof O]: O[K] extends T ? K : never }>
@itsMapleLeaf
itsMapleLeaf / Portal.tsx
Last active August 16, 2021 02:08
portal
import { ReactNode, useEffect, useRef } from "react"
import { createPortal } from "react-dom"
export function Portal({ children }: { children: ReactNode }) {
const containerRef = useRef<Element>()
if (!containerRef.current) {
containerRef.current = document.createElement("react-portal")
document.body.append(containerRef.current)
}
@itsMapleLeaf
itsMapleLeaf / useInstanceValue.ts
Created November 7, 2019 02:16
useInstanceValue - convenience for creating a one-time value for the lifetime of a component
import { useRef } from "react"
const noValue = Symbol()
export default function useInstanceValue<T>(createValue: () => T) {
const ref = useRef<T | typeof noValue>(noValue)
if (ref.current === noValue) {
ref.current = createValue()
}
return ref.current
@itsMapleLeaf
itsMapleLeaf / helpers.ts
Created February 6, 2020 03:05
functional emotion lol
import { Interpolation } from "@emotion/core"
import { css } from "./styled"
type SizeUnit =
| 0
| 1
| 2
| 3
| 4
| 5
@itsMapleLeaf
itsMapleLeaf / atom.ts
Created March 8, 2020 00:47
state atom
import { useEffect, useState } from "react"
export type Atom<T> = {
set(newValue: T): void
get(): T
listen(listener: (value: T) => void): () => void
}
export function createAtom<T>(initialValue: T): Atom<T> {
let currentValue = initialValue