|
| 1 | +# Naming conventions |
| 2 | + |
| 3 | +This page documents the naming patterns already used across the Rad UI repo. |
| 4 | + |
| 5 | +Use these conventions for new work unless you are extending an older area that already has a different local pattern. In that case, prefer consistency inside that component or package over doing a partial rename in an unrelated PR. |
| 6 | + |
| 7 | +## General rules |
| 8 | + |
| 9 | +- Prefer descriptive names over short names. |
| 10 | +- Use `PascalCase` for component folders, component files, context files, providers, and exported React types. |
| 11 | +- Use `camelCase` for hooks, helper functions, and utility folders that are not React components. |
| 12 | +- Use `kebab-case` for docs route folders under `docs/app/docs`. |
| 13 | +- Keep related files together inside the component or primitive folder instead of scattering them across the repo. |
| 14 | + |
| 15 | +## UI component folders |
| 16 | + |
| 17 | +Most components under `src/components/ui` follow this shape: |
| 18 | + |
| 19 | +```text |
| 20 | +src/components/ui/Accordion/ |
| 21 | + Accordion.tsx |
| 22 | + contexts/ |
| 23 | + fragments/ |
| 24 | + stories/ |
| 25 | + tests/ |
| 26 | +``` |
| 27 | + |
| 28 | +Follow these naming rules for new UI components: |
| 29 | + |
| 30 | +- Name the folder after the exported component: `Accordion`, `Combobox`, `NumberField`. |
| 31 | +- Name the top-level entry file after the component: `Accordion.tsx`, `Combobox.tsx`, `NumberField.tsx`. |
| 32 | +- Put compound parts in `fragments/` with names like `AccordionRoot.tsx`, `AccordionItem.tsx`, `AccordionTrigger.tsx`. |
| 33 | +- Put tests in `tests/` and stories in `stories/`. |
| 34 | +- Use lowercase stylesheet filenames when they represent style packages, for example `accordion.clarity.scss` and `accordion.baremetal.scss`. |
| 35 | + |
| 36 | +## Contexts, providers, and hooks |
| 37 | + |
| 38 | +Context-related files should make their role obvious from the filename. |
| 39 | + |
| 40 | +- Use `*Context.tsx` for React context modules, for example `AccordionContext.tsx` or `NumberFieldContext.tsx`. |
| 41 | +- Use more specific names when there is more than one context, for example `AccordionItemContext.tsx` or `ComboboxRootContext.tsx`. |
| 42 | +- Use `*Provider` in the symbol name when a standalone provider component exists. |
| 43 | +- Use `use*` for hooks, for example `useComponentClass.ts` or `useControllableState/index.tsx`. |
| 44 | + |
| 45 | +For new work, prefer a `contexts/` directory name. The repo still has older `context/` folders, so do not rename those as drive-by cleanup in an unrelated issue. |
| 46 | + |
| 47 | +## Primitives vs UI components |
| 48 | + |
| 49 | +The repo distinguishes between headless primitives in `src/core/primitives` and composed components in `src/components/ui`. |
| 50 | + |
| 51 | +- Primitive folders still use `PascalCase`: `Button`, `Dialog`, `Combobox`. |
| 52 | +- Primitive implementation files may be explicit, such as `ComboboxPrimitive.tsx`, or use `index.tsx` in older folders. |
| 53 | +- UI component entry files should stay explicit: `Button.tsx`, `Dialog.tsx`, `Theme.tsx`. |
| 54 | +- When naming primitive parts, keep the `Primitive` suffix in the symbol or file when it clarifies the layer, such as `ButtonPrimitive`, `ComboboxPrimitiveRoot`, or `MenuPrimitive.tsx`. |
| 55 | + |
| 56 | +For new files, prefer explicit filenames when the file represents a concrete exported component or context. Keep `index.ts` or `index.tsx` for small entrypoints and for folders that already use that pattern. |
| 57 | + |
| 58 | +## Tests and stories |
| 59 | + |
| 60 | +Use the component or primitive name as the base filename. |
| 61 | + |
| 62 | +- Stories: `Accordion.stories.tsx`, `Badge.stories.tsx` |
| 63 | +- Visual-story variants: `AccordionItemVisualTests.stories.tsx` |
| 64 | +- Main test file: `Accordion.test.tsx` |
| 65 | +- Focused test files: `Accordion.a11y.test.tsx`, `Accordion.focus.test.tsx`, `Combobox.full.test.tsx`, `Combobox.simple.test.tsx` |
| 66 | + |
| 67 | +When adding a specialized test, keep the qualifier before `.test.tsx`. That keeps related files grouped together in editors and search. |
| 68 | + |
| 69 | +## Docs and examples |
| 70 | + |
| 71 | +Docs pages under `docs/app/docs` use route-oriented naming. |
| 72 | + |
| 73 | +- Route folders use `kebab-case`, for example `before-you-start`, `setting-up-dev-environment`, `radio-group`. |
| 74 | +- A docs page folder usually contains `page.tsx`, `seo.ts`, and `content.mdx`. |
| 75 | +- Supporting docs assets usually live in a local `docs/` folder with descriptive names such as `anatomy.tsx`, `component_api/root.tsx`, `codeUsage.js`, or `example_1.tsx`. |
| 76 | +- Standalone example components use `PascalCase`, for example `AvatarExample.tsx` or `SwitchExample.tsx`. |
| 77 | + |
| 78 | +## When to keep the local pattern |
| 79 | + |
| 80 | +Some areas are not perfectly uniform yet. Follow these rules when the repo already differs: |
| 81 | + |
| 82 | +- Match the surrounding folder if you are making a small change inside an existing component. |
| 83 | +- Prefer the newer convention only when creating a new area or when a full rename is already in scope. |
| 84 | +- Do not mix `context/` and `contexts/` inside the same component folder. |
| 85 | +- Do not introduce a second naming style for the same kind of file in the same directory. |
| 86 | + |
| 87 | +## Quick reference |
| 88 | + |
| 89 | +- UI component folder: `PascalCase` |
| 90 | +- UI component entry file: `PascalCase.tsx` |
| 91 | +- Fragment file: `PascalCasePart.tsx` |
| 92 | +- Context file: `PascalCaseContext.tsx` |
| 93 | +- Hook file: `useSomething.ts` |
| 94 | +- Stories file: `PascalCase.stories.tsx` |
| 95 | +- Test file: `PascalCase.test.tsx` |
| 96 | +- Docs route folder: `kebab-case` |
| 97 | +- Docs page support files: descriptive names, usually `page.tsx`, `seo.ts`, `content.mdx` |
0 commit comments