How do you structure folder components in your Sanity projects?

7 replies
Last updated: Mar 16, 2021
How do you guys structure your folder components? This is always a issue 😄 never happy about the outcome. I usually have a folder "components" and for instance a component Blog which is a folder and inside I have like BlogDetail, BlogOverview, BlogCard and a index.js where i export all the components. Wondering how you guys structure your projects?
AI Update

This is a great question and honestly, there's no "perfect" solution – it's one of those things that evolves with your project! Your current approach is actually pretty solid and commonly used.

Here are a few patterns I've seen work well in Sanity + React/Next.js projects:

Your current approach (domain-based grouping):

components/
  Blog/
    index.js
    BlogDetail.jsx
    BlogOverview.jsx
    BlogCard.jsx

This is totally valid! The index.js barrel export keeps imports clean. Some folks take this further by adding a Blog.module.css or co-locating tests here too.

Feature-based structure:

components/
  blog/
    BlogCard.jsx
    BlogDetail.jsx
  shared/
    Button.jsx
    Card.jsx
  layout/
    Header.jsx
    Footer.jsx

This groups by feature/domain but flattens it a bit. Works well when components don't have many sub-components.

Atomic design approach:

components/
  atoms/        # Button, Input, etc.
  molecules/    # BlogCard, SearchBar
  organisms/    # BlogOverview, Header
  templates/    # PageLayout

This can feel over-engineered for smaller projects, but scales nicely for design systems.

Sanity-specific consideration:

If you're using Portable Text for rich text, many folks create a dedicated structure:

components/
  portableText/
    serializers/
      ImageSerializer.jsx
      CodeSerializer.jsx
    PortableTextRenderer.jsx

My personal take: Your current structure is fine! The main thing is consistency. I'd maybe add:

  • A shared/ or common/ folder for truly reusable components (buttons, inputs, etc.)
  • Co-locate component-specific styles/tests with the component
  • Use barrel exports (index.js) sparingly – they can hurt tree-shaking

The "never happy about the outcome" feeling is universal 😄 – just pick a convention, document it in a README, and stick with it. You can always refactor later when pain points become obvious!

Show original thread
7 replies
Sounds a bit similar to the structure in Sara Vieira’s Opinionated React; which I do my best to follow.
Not because I think it’s entirely “correct”, but I was in need of conventions and it seemed as good as any!


https://opinionatedreact.com/
Ha, we’ve had this discussion at work recently. I’ve trained my eye to look at the folder name so I don’t mind it.
user L
How would you deal with this? Any suggestions?
😄
I like the
index.js
approach because I know what the “root” element is in a folder which has multiple components related to the “root”. But yeah you’ve got to look one step backwards to know where you are, occasionally.
user S
same here! I hate importing like this:
import BlogHeader from "components/BlogHeader/BlogHeader".
^^ I’ve seen this solved by having an
index.js
that just exports the root element. But then I don’t love having that file in every directory just for that purpose 😄

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?