Skip to content
AI Skill Index

Scaffold a component to house style

Matches the conventions already in the codebase instead of importing new ones.

Author: Editorial
Sign in to save
Write a new component that looks like it was written by whoever wrote the rest of this codebase.

What it should do: [BEHAVIOUR]
Here are two existing components to match:

[PASTE TWO EXAMPLES]

Before writing, tell me what you observed: file layout, prop typing style, how state is handled, naming, how styles are applied, whether comments are used and for what.

Then write the component following those observations, not your defaults.

Do not introduce a library that is not already used in the examples. Do not "improve" a convention you disagree with — a component that is stylistically correct and slightly worse is more maintainable than one that is the odd file out.

Highlighted parts are placeholders. Replace them before you send.

Worked example

The input is invented for this page. The output shows what the prompt asks for with that input.

Example input (invented)

What it should do: A button that copies a given text to the clipboard and shows "Copied" for two seconds.

Two existing components

import styles from "./Badge.module.css"

type BadgeProps = {
  label: string
  tone?: "neutral" | "warning"
}

export function Badge({ label, tone = "neutral" }: BadgeProps) {
  return <span className={`${styles.badge} ${styles[tone]}`}>{label}</span>
}
import { useState } from "react"

import styles from "./Disclosure.module.css"

type DisclosureProps = {
  title: string
  children: React.ReactNode
}

/** Starts closed. Remembers nothing between page loads. */
export function Disclosure({ title, children }: DisclosureProps) {
  const [isOpen, setIsOpen] = useState(false)

  return (
    <div className={styles.disclosure}>
      <button type="button" className={styles.toggle} aria-expanded={isOpen} onClick={() => setIsOpen(!isOpen)}>
        {title}
      </button>
      {isOpen && <div className={styles.panel}>{children}</div>}
    </div>
  )
}

Example output

What I observed

  • File layout. One component per file, a named export, no default export. React imports first, a blank line, then the component's own CSS module.
  • Props. A type, not an interface, named after the component plus Props, declared above it. Props are destructured in the signature, with defaults there.
  • State. useState with a boolean named isX and a setter named setIsX.
  • Naming. Components are function declarations in PascalCase. A handler is an inline arrow on the element, even when the line runs long.
  • Styles. A CSS module imported as styles. Each element has a class named after its role. A variant is a second class looked up by prop value, as in styles[tone].
  • Comments. One JSDoc line above the component, only where the behaviour is not obvious from its name. Badge has none.
  • Other. type="button" on every button, double quotes, no semicolons.

The component

import { useEffect, useState } from "react"

import styles from "./CopyButton.module.css"

type CopyButtonProps = {
  text: string
  label?: string
}

/** Shows "Copied" for two seconds after a successful copy. */
export function CopyButton({ text, label = "Copy" }: CopyButtonProps) {
  const [isCopied, setIsCopied] = useState(false)

  useEffect(() => {
    if (!isCopied) return
    const timer = setTimeout(() => setIsCopied(false), 2000)
    return () => clearTimeout(timer)
  }, [isCopied])

  return (
    <button type="button" className={styles.copyButton} onClick={() => navigator.clipboard.writeText(text).then(() => setIsCopied(true))}>
      {isCopied ? "Copied" : label}
    </button>
  )
}

If the browser refuses the copy, the button keeps its label. Neither example handles errors, so this does not either. Say if you want a failure message, and where the house puts error text [VERIFY].

ShareXLinkedIn

3 of the 17 other prompts filed under Coding & data.

Generative code tools

2 tools are filed under Generative code, the same category as this prompt.

Tool

Bolt.new

Prompt a full-stack app into existence in the browser, then deploy it.

Freemium
Tool

v0

Vercel's generator for React and Tailwind interfaces.

Freemium

Generative code guides

1 guide is filed under Generative code, the same category as this prompt.

Generative code

Bolt.new alternatives: AI app builders compared

What Bolt.new does, which tools on this index build apps the same way in the browser, which work on your own code from the editor or terminal, and how to pick.

Beginner·6 min read

Cookies

We use only the cookies this site needs to work. Optional analytics helps us see which content is worth writing more of — nothing loads until you say yes.

Read the privacy policy