Skip to content

Instantly share code, notes, and snippets.

View sethdavis512's full-sized avatar
🤖

Seth Davis sethdavis512

🤖
View GitHub Profile
@sethdavis512
sethdavis512 / favicon.svg
Last active January 20, 2024 18:11
Set favicon to SVG
View favicon.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sethdavis512
sethdavis512 / ConditionalWrapper.tsx
Last active December 13, 2023 03:20
A component that wraps children with a tag based on the condition
View ConditionalWrapper.tsx
import type { ReactNode } from 'react';
interface ConditionalWrapperProps {
condition: boolean;
wrapper: (children: ReactNode) => ReactNode;
children: ReactNode;
}
export default function ConditionalWrapper({
condition,
@sethdavis512
sethdavis512 / mock-hook.ts
Created December 12, 2023 22:23
The Typescript way to mock custom hooks
View mock-hook.ts
jest.mock('../path/to/hooks/useMyHook', () => ({
__esModule: true,
default: jest.fn(() => ({
isLoading: false
}))
}));
(useMyHook as jest.MockedFunction<any>).mockReturnValue({ isLoading: true });
View .zshrc
# Aliases
# Commit handlers
alias ga="git commit --amend"
alias gam="git add . && git commit --amend"
alias gamp="gam && gpf"
alias gp="git push -u origin $(git symbolic-ref --short HEAD)"
alias gpu="git pull"
alias gr="git reset HEAD^"
alias gt="git add . && git commit -m \"TEMP\" --no-verify"
@sethdavis512
sethdavis512 / getUniqueId.ts
Last active October 4, 2023 20:05
Get unique ID function
View getUniqueId.ts
function getUniqueId(prefix: string, length: number = 8): string {
let result = `${prefix}-`;
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result = `${result}${characters.charAt(Math.floor(Math.random() * charactersLength))}`;
@sethdavis512
sethdavis512 / useD3.ts
Created June 23, 2023 13:50
React hook for D3 usage
View useD3.ts
const useD3 = (renderChartFn: (el: any) => void, dependencies: any[]) => {
const ref = useRef<HTMLElement>();
useEffect(() => {
if (!!ref.current) {
renderChartFn(d3.select(ref.current));
}
return () => {};
}, dependencies);
@sethdavis512
sethdavis512 / gist:8c1cea61ecdd12f55db06d581daa6025
Created March 16, 2023 21:59 — forked from BjornDCode/gist:5cb836a6b23638d6d02f5cb6ed59a04a
Tailwind - Fixed sidebar, scrollable content
View gist:8c1cea61ecdd12f55db06d581daa6025
// Source: https://twitter.com/calebporzio/status/1151876736931549185
<div class="flex">
<aside class="h-screen sticky top-0">
// Fixed Sidebar
</aside>
<main>
// Content
</main>
@sethdavis512
sethdavis512 / Switchboard.tsx
Last active December 9, 2022 03:28
A component that only shows one of its children and allows dynamic switching between children
View Switchboard.tsx
import React, {
useContext,
createContext,
ReactNode,
useMemo,
Children,
isValidElement,
useState
} from 'react';
View encode-object-params.js
const encodeGetParams = p =>
Object.entries(p).map(kv => kv.map(encodeURIComponent).join("=")).join("&");
const params = {
user: "María Rodríguez",
awesome: true,
awesomeness: 64,
"ZOMG+&=*(": "*^%*GMOZ"
};
@sethdavis512
sethdavis512 / custom-file-generator-cli-tutorial.md
Last active October 25, 2023 10:51
Custom File Generator CLI Tutorial
View custom-file-generator-cli-tutorial.md

As a developer who works on multiple React projects daily, I like having a tool that can help me quickly and efficiently write consistent code. One of the best ways I've found is writing a custom command line tool to rapidly scaffold out my most common code patterns.

My tool of choice is Plop.js. Plop is a powerful "micro-generator framework" built to help maintain patterns as well as speed up your project build time. From the documenation:

If you boil plop down to its core, it is basically glue code between inquirer prompts and handlebar templates.

In this tutorial, we'll build out a simple React component generator for your Typescript projects. By the end, you'll have a fully functioning CLI that is customized to your file generating needs. Let's get started.

Prerequisites