Skip to content

Instantly share code, notes, and snippets.

View sethdavis512's full-sized avatar

Seth Davis sethdavis512

View GitHub Profile
sethdavis512 / as-child.tsx
Last active May 24, 2024 17:43
asChild example
import { Slot, type AsChildProps } from "./slot.tsx"
type ButtonProps = AsChildProps<
> & {
style?: React.CSSProperties
className?: string
function Button({ asChild, ...props }: ButtonProps) {
sethdavis512 / getUniqueId.ts
Last active May 13, 2024 15:33
Get unique ID function
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 / Grid.tsx
Created April 14, 2024 01:13
Grid component (Tailwind)
import { type ReactNode } from 'react';
interface GridProps {
children: ReactNode;
as?: keyof JSX.IntrinsicElements;
className?: string;
export default function Grid({ as = 'div', children, className }: GridProps) {
const Component = as;
const getRandomBool = (chance: number = 50): boolean =>
Math.round(Math.random() * 100) >= chance;
= Bootstrap 3 Media Queries =
/*========== Mobile First Method ==========*/
/* Custom, iPhone Retina */
sethdavis512 / favicon.svg
Last active March 18, 2024 19:20
Set favicon to SVG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# 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 / ConditionalWrapper.tsx
Last active December 13, 2023 03:20
A component that wraps children with a tag based on the condition
import type { ReactNode } from 'react';
interface ConditionalWrapperProps {
condition: boolean;
wrapper: (children: ReactNode) => ReactNode;
children: ReactNode;
export default function ConditionalWrapper({
sethdavis512 / mock-hook.ts
Created December 12, 2023 22:23
The Typescript way to mock custom hooks
jest.mock('../path/to/hooks/useMyHook', () => ({
__esModule: true,
default: jest.fn(() => ({
isLoading: false
(useMyHook as jest.MockedFunction<any>).mockReturnValue({ isLoading: true });
sethdavis512 /
Last active October 25, 2023 10:51
Custom File Generator CLI Tutorial

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.