Skip to content

Instantly share code, notes, and snippets.

@madflanderz
madflanderz / .deps...npm...@openzeppelin...contracts...access...Ownable.sol
Created January 24, 2022 23:03
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
// type Todo = {id: string, title: string}
// MyPick<Todo, 'title'>
type MyPick<T, K extends keyof T> = {
[S in K]: T[S]
}
// type R = MyReadonly<{id: string}>
type MyReadonly<T> = {
readonly [S in keyof T]: T[S]
}
@madflanderz
madflanderz / useTimeout.ts
Created January 13, 2020 09:37
Effect to call a function after timeout
import { useEffect, useRef } from "react"
/**
* Runs a timeout after delay ms.
*
* @param callback function to execute
* @param delay delay time in ms
*
* Usage:
useTimeout(
@madflanderz
madflanderz / useKeyPress.ts
Last active December 20, 2019 15:27
Hook for responding to keyboard events.
import { useState, useEffect } from "react"
type HookProps = {
key?: string
keyCode: number
onKeyPress?: () => void
enabled?: boolean
}
/**
@madflanderz
madflanderz / i18n.tsx
Created December 9, 2019 16:36
Simple i18n solution
import React, { useCallback, useMemo } from "react"
import LanguageKeysDe from "./de"
import LanguageKeys, { LangProps } from "./en"
/**
Full example here:
https://codesandbox.io/s/simple-react-typescript-i18n-w0ut6
**/
@madflanderz
madflanderz / useOnScreen.tsx
Last active December 2, 2019 14:32
Hooks - React Intersection
import { useState, useEffect } from "react"
/**
Usage:
// Ref for the element that we want to detect whether on screen
const ref = useRef<HTMLDivElement>(null)
// Call the hook passing in ref and root margin
// In this case it would only be considered onScreen if more ...
@madflanderz
madflanderz / useInterval.ts
Created November 26, 2019 10:22
useInterval hook
import { useEffect, useRef } from "react"
function useInterval(callback: () => void, delay: number) {
const savedCallback = useRef<() => void | undefined>()
useEffect(() => {
savedCallback.current = callback
})
useEffect(() => {
@madflanderz
madflanderz / order-of-elements.test.tsx
Last active November 26, 2019 08:58
react-testing-library order of elements
/**
To check the order of elements you can use the underlying dom testing lib to search in specific elements. The following example gets all table rows and checks then for specific text in each row.
**/
import { getByText as domGetByText } from "@testing-library/react"
describe("StockTable", () => {
it("renders countries in correct order by country code", async () => {
const { getAllByTestId } = render(<StockTable offers={offers} />)
@madflanderz
madflanderz / extractTypeFromArray.ts
Last active October 25, 2019 13:11
Extract Type from Array
const food = ['apple', 'banana', 'cookie'] as const
// 'apple', 'banana', 'cookie'
type Food = typeof food[number]
// create a bookmark and use this code as the URL, you can now toggle the css on/off
// thanks+credit: https://dev.to/gajus/my-favorite-css-hack-32g3
javascript: (function() {
var elements = document.body.getElementsByTagName('*');
var items = [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].innerHTML.indexOf('* { background:#000!important;color:#0f0!important;outline:solid #f00 1px!important; background-color: rgba(255,0,0,.2) !important; }') != -1) {
items.push(elements[i]);
}
}