# Creating New Shadcn and React Components This guide explains how to create new Shadcn (UI) components and React components in this Rails application with React frontend. ## Overview This project uses: - **Shadcn/ui** for UI components (built on Radix UI and Tailwind CSS) - **React** for frontend components - **Rails** as the backend framework - **esbuild** for JavaScript bundling ## Directory Structure ``` app/ ├── javascript/ │ ├── components/ │ │ └── ui/ # Shadcn components │ └── controllers/ # React controllers ├── views/ │ └── components/ # Rails view components └── docs/ # Documentation ``` ## Creating Shadcn Components ### 1. Using the Shadcn CLI The easiest way to add new Shadcn components is using the CLI: ```bash # Navigate to the project root cd /home/acid/Documents/aperonight # Add a new component (example: adding a card) npx shadcn-ui@latest add card ``` This will: - Install the component to `app/javascript/components/ui/` - Update the components.json configuration - Create the necessary TypeScript/JavaScript files ### 2. Manual Component Creation If the CLI is not available, create components manually: #### Create the component file ```bash # Create a new component (example: button.jsx) touch app/javascript/components/ui/button.jsx ``` #### Basic component structure ```javascript // app/javascript/components/ui/button.jsx import * as React from "react" import { cn } from "@/lib/utils" const Button = React.forwardRef(({ className, ...props }, ref) => { return ( ) } export default HomePage ``` ## Configuration Updates ### 1. Update components.json ```json { "style": "default", "rsc": false, "tsx": false, "tailwind": { "config": "tailwind.config.js", "css": "app/assets/stylesheets/application.postcss.css", "baseColor": "slate", "cssVariables": true }, "aliases": { "components": "app/javascript/components", "utils": "app/javascript/lib/utils" } } ``` ### 2. Update JavaScript entry point ```javascript // app/javascript/application.js import "./components" import "./controllers" ``` ## Naming Conventions ### Shadcn Components - Use kebab-case for filenames: `button.jsx`, `card.jsx` - Use PascalCase for exports: `export { Button }` - Follow Radix UI naming patterns ### React Components - Use PascalCase for filenames: `MyComponent.jsx` - Use PascalCase for components: `const MyComponent = () => {}` - Use camelCase for props: `myProp`, `onClick` ## Testing Components ### 1. Create test file ```bash # Create test file touch test/components/my_component_test.rb ``` ### 2. Write component test ```javascript // test/components/my_component_test.jsx import { render, screen } from "@testing-library/react" import MyComponent from "../../app/javascript/components/MyComponent" test("renders component", () => { render() expect(screen.getByText("Test")).toBeInTheDocument() }) ``` ## Common Patterns ### 1. Props Pattern ```javascript // Pass Rails data as props const MyComponent = ({ user, config }) => { return
{user.name}
} ``` ### 2. Event Handling ```javascript // Handle events from Rails const MyComponent = ({ onAction }) => { return } ``` ### 3. Styling Integration ```javascript // Use Tailwind classes const MyComponent = () => { return
Content
} ``` ## Troubleshooting ### Common Issues 1. **Component not rendering**: Check controller connection 2. **Styling issues**: Verify Tailwind classes 3. **Props not passing**: Check data-controller attributes 4. **Import errors**: Verify alias paths in components.json ### Debug Steps 1. Check browser console for errors 2. Verify component file exists in correct location 3. Check import paths in application.js 4. Verify Rails view includes correct data attributes ## Example created for testing purpose ```html
```