iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🏗

Lessons learned from directory structures for smartly managing shared parts in BCD Design

に公開
2025/03/12

BCD Design is a component classification method for front-end development proposed in the following article.

In BCD Design, components are systematically classified based on three concepts—"Base, Case, Domain"—and two granularity axes—"atoms, molecules."
As a result, it maintains exceptionally high scalability in component classification, which often tends to become cluttered.

In BCD Design, in addition to the directory structure classified by the two axes mentioned above, you can also incorporate the "directory structure for smartly managing common parts" introduced in the following article.

In this article, I would like to focus on this "directory structure for smartly managing common parts" and summarize what I have organized in my mind.

Overview

First, I will explain the overview of the "directory structure for smartly managing common parts."
However, since many parts may be easier to understand by reading the original article, I will only explain the basic directory structure here.

Abstract Components and Concrete Components

src/
└── components/
    └── base/
        └── molecules/
            └── Field/
                ├── _abstract/
                │   ├── Label/
                │   ├── MessageList/
                │   ├── Field.hooks.tsx
                │   ├── Field.module.css
                │   ├── Field.stories.tsx
                │   ├── Field.tsx
                │   └── index.ts
                ├── CheckBoxField/
                │   ├── CheckBoxField.module.css
                │   ├── CheckBoxField.stories.tsx
                │   ├── CheckBoxField.tsx
                │   └── index.ts
                ├── SelectBoxField/
                ├── TextBoxField/
                └── index.ts

As in the original article, I will explain using the Field component as an example.

First, the Field component is a UI for input fields used in forms and such. It consists of a set of UIs for the label, the message list for validation errors, and the input section.

Now, if we consider the input UI, we can see that there are various types, such as TextBox, CheckBox, and SelectBox.

However, parts like labels and message lists are common to any Field regardless of the input section. From this, we can see that TextBoxField or CheckBoxField are not independent components, but rather concrete components of the abstract component Field.

Therefore, these concrete components can be grouped within the directory of the abstract component.
And since they are grouped together, we would want to standardize common parts like labels and message lists so they can be reused across each concrete component.

However, if we place common parts at the same level as the concrete component directories, the directory granularity that was previously consistent will become mismatched.

This is where the _abstract directory becomes effective. By placing common parts in the _abstract directory, you can treat abstracts (common parts) as siblings to concrete components. This allows you to maintain consistent directory granularity.

_base directory

In addition to the _abstract directory, you can also use a _base directory. The _base directory is used when the _base directory itself is also in a state where it can be used as a component as a base.

It might be easier to remember it as _base if it is exported from the parent directory, and _abstract if it is not.

variant

Next, consider the case where Field has variants such as primary or secondary.

Since variants for the input section UI, such as TextBox or CheckBox, are the responsibility of each concrete component, we can see that the variants held by Field are confined within _abstract.

src/
└── components/
    └── base/
        └── molecules/
            └── Field/
                ├── _abstract/
                │   ├── _variant/ # Define variants here
                │   │   ├── _abstract/
                │   │   │   ├── Field.tsx
                │   │   │   └── index.ts
                │   │   ├── Primary/
                │   │   ├── Secondary/
                │   │   └── index.ts
                │   ├── Label/
                │   ├── MessageList/
                │   ├── Field.hooks.tsx
                │   ├── Field.module.css
                │   ├── Field.stories.tsx
                │   ├── Field.tsx
                │   └── index.ts
                ├── CheckBoxField/
                │   ├── CheckBoxField.module.css
                │   ├── CheckBoxField.stories.tsx
                │   ├── CheckBoxField.tsx
                │   └── index.ts
                ├── SelectBoxField/
                ├── TextBoxField/
                └── index.ts

Variants are grouped in a directory called _variant. Detailed implementations of each variant are defined within variant directories like Primary/ or Secondary/. Additionally, common parts for each variant are placed in _abstract within _variant.

So, what is the difference between Field/_abstract/Field.tsx and Field/_abstract/_variant/_abstract/Field.tsx?

The difference lies in their responsibilities. While basic markup is performed in Field/_abstract/_variant/_abstract/Field.tsx, Field/_abstract/Field.tsx only handles switching between Primary and Secondary based on the variant value passed through props.

In other words, the component at the lowest level is the most primitive one.

By following this rule, dependencies become very clean.

First, concrete components such as TextBoxField and CheckBoxField depend only on their sibling abstract component, Field/_abstract.

Next, if variants exist, Field/_abstract depends only on its sibling elements like Label and MessageList, and the _variant directory.

Then, Field/_abstract/_variant depends only on its sibling element Field/_abstract/_variant/_abstract.

In this way, in the "directory structure for smartly managing common parts," components only depend on their upward sibling elements. This enables an easy-to-understand component design.

Directory Types

In the "directory structure for smartly managing common parts," you can assign types to each directory and file. There are five types:

  • File
    • index
    • Component
  • Directory
    • ComponentDirectory
    • VariantDirectory
    • ListDirectory<T extends ComponentDirectory | VariantDirectory>

index

This file is responsible for exporting the directories and files within that level. While not specifically noted hereafter, it can exist in all Directory types.

Component

This is the file where the component is implemented. Not only tsx but also hooks, style, story, etc., are included in the Component type.

ComponentDirectory

This directory contains Component type files. Directories such as _abstract and _base are also included in the ComponentDirectory type. Besides the Component type, it can contain the following types:

  • ComponentDirectory
  • ListDirectory<ComponentDirectory>
  • ListDirectory<VariantDirectory>

VariantDirectory

This directory contains component variations such as primary or secondary. The VariantDirectory type has the following characteristics:

  • A VariantDirectory type can only exist directly under a ListDirectory<VariantDirectory> type.
  • A VariantDirectory type can only contain Component types.

ListDirectory<T extends ComponentDirectory | VariantDirectory>

This is a directory that contains directories of type T. It is characterized by its inability to contain types other than T types.

ListDirectory<VariantDirectory>

This is a directory that contains directories of the VariantDirectory type. It is named _variant. As mentioned earlier, it cannot contain types other than the VariantDirectory type, but as an exception, it can contain _abstract or _base directories.


So far, I have explained the five types. Now, let's map the previous directory structure against these types.

src/
└── components/
    └── base/
        └── molecules/
            └── Field/ -> ListDirectory<ComponentDirectory>
                ├── _abstract/ -> ComponentDirectory
                │   ├── _variant/ -> ListDirectory<VariantDirectory>
                │   │   ├── _abstract/ -> ComponentDirectory
                │   │   │   ├── Field.tsx -> Component
                │   │   │   └── index.ts -> index
                │   │   ├── Primary/ -> VariantDirectory
                │   │   ├── Secondary/ -> VariantDirectory
                │   │   └── index.ts -> index
                │   ├── Label/ -> ComponentDirectory | ListDirectory<ComponentDirectory>
                │   ├── MessageList/ -> ComponentDirectory | ListDirectory<ComponentDirectory>
                │   ├── Field.hooks.tsx -> Component
                │   ├── Field.module.css -> Component
                │   ├── Field.stories.tsx -> Component
                │   ├── Field.tsx -> Component
                │   └── index.ts -> index
                ├── CheckBoxField/ -> ComponentDirectory
                │   ├── CheckBoxField.module.css -> Component
                │   ├── CheckBoxField.stories.tsx -> Component
                │   ├── CheckBoxField.tsx -> Component
                │   └── index.ts -> index
                ├── SelectBoxField/ -> ComponentDirectory
                ├── TextBoxField/ -> ComponentDirectory
                └── index.ts -> index

Organizing it this way reveals that a ComponentDirectory type directly under another ComponentDirectory type cannot be distinguished from a ListDirectory<ComponentDirectory> type without checking its internal contents. However, this versatility allows the directory structure to be applied recursively, which contributes to high scalability.

Dependencies and Prohibitions

By distinguishing between ListDirectory and ComponentDirectory, dependencies are contained within sibling directories or sibling elements.

"Sibling directories" refers to the relationship between directories that share the same parent directory. For example, CheckBoxField/CheckBoxField.tsx depends only on its sibling directory _abstract (strictly speaking, _abstract/index.ts).

And "sibling elements" refers to the relationship between directories and files at the same level. For example, _abstract/Field.tsx depends only on sibling elements such as _abstract/_variant, _abstract/Label, _abstract/MessageList, _abstract/Field.hooks.tsx, and so on.

The advantage of placing _abstract or _base directories is that child elements no longer refer to their parents.

For example, in the following directory structure, CheckBoxField.tsx would depend on Field.tsx (an element of the parent directory) rather than a sibling directory.

src/
└── components/
    └── base/
        └── molecules/
            └── Field/
                ├── Label/
                ├── MessageList/
                ├── Field.tsx
                ├── CheckBoxField/
                │   ├── CheckBoxField.tsx
                │   └── index.ts
                ├── SelectBoxField/
                ├── TextBoxField/
                └── index.ts

Based on this, let's organize the rules for imports and exports. We can identify the following prohibitions:

  • Child elements depending on parent elements
  • Importing directly from anything other than index
  • Exporting anything other than sibling elements

Application of Variants

In the examples so far, variants had only one axis (color). What would the directory structure look like if there were two or more axes (for example, color and shape)?
Here, I will propose two possible directions.

Recursive Approach

src/
└── components/
    └── base/
        └── atoms/
            └── Button/
                ├── _base/
                │   ├── _variant/
                │   │   ├── _base/
                │   │   │   ├── _variant/
                │   │   │   │   ├── _base/
                │   │   │   │   │   ├── Button.tsx
                │   │   │   │   │   └── index.ts
                │   │   │   │   ├── Rounded/
                │   │   │   │   ├── Rect/
                │   │   │   │   └── index.ts
                │   │   │   ├── Button.tsx
                │   │   │   └── index.ts
                │   │   ├── Primary/
                │   │   ├── Secondary/
                │   │   └── index.ts
                │   ├── Button.tsx
                │   └── index.ts
                ├── ClearButton/
                ├── SearchButton/
                └── index.ts

This is an approach where you add another _variant inside the _abstract or _base within a _variant.

The advantage of this method is that Button/_base/_variant/_base/_variant/_base/Button.tsx only needs to consider whether it is Rounded or Rect, and similarly, Button/_base/Button.tsx only needs to handle selecting either Primary or Secondary.

However, this approach has two drawbacks.

First, the most primitive Button is pushed deeper into the hierarchy every time a variant is added.
In the example above, the most primitive Button was Button/_base/_variant/_base/Button.tsx until Rounded and Rect were added.
Once Rounded and Rect were added, the most primitive Button changed to Button/_base/_variant/_base/_variant/_base/Button.tsx.

In this way, as the number of variant axes increases, the hierarchy becomes deeper, requiring modifications to every Button.tsx, which sacrifices readability and maintainability.

Second, it becomes impossible to create exclusive variants.
With the current configuration, styles like Primary and Secondary can be freely combined with shapes like Rounded and Rect. However, if you add a Ghost variant in addition to Primary and Secondary, and Ghost does not define a button shape, Rounded or Rect cannot be applied.
When such cases occur, they cannot be managed appropriately with the current approach.

Managing via sibling directories and controlling with exports

This is a method where even if multiple axes exist, they are all managed within the same _variant directory. However, the axes are controlled through dependencies.

src/
└── components/
    └── base/
        └── atoms/
            └── Button/
                ├── _base/
                │   ├── _variant/
                │   │   ├── _abstract/
                │   │   │   ├── Button.tsx
                │   │   │   └── index.ts
                │   │   ├── Primary/
                │   │   ├── Secondary/
                │   │   ├── Rounded/
                │   │   ├── Rect/
                │   │   ├── Ghost/
                │   │   └── index.ts
                │   ├── Button.tsx
                │   └── index.ts
                ├── ClearButton/
                ├── SearchButton/
                └── index.ts

In the example above, Primary/ and Secondary/ depend on _abstract/. Additionally, Rounded/ and Rect/ depend on Primary/ and Secondary/, while Ghost/ depends on _base/. Finally, index.ts only exports Rounded/, Rect/, and Ghost/.

By doing this, it becomes possible to handle variants with two or more axes without increasing the hierarchy depth. However, it has the drawback that you must accurately keep track of what index.ts is exporting.

To make the export targets easier to identify, it might be a good idea to add an underscore to directories that are not intended for export, such as _Primary or _Secondary.

In this approach, variants of multiple axes are managed within the _variant directory at the same level. However, the axes are controlled by utilizing dependencies.

In the example above, Primary and Secondary depend on _abstract. Also, Rounded and Rect depend on Primary and Secondary, while Ghost depends on _base respectively.

Therefore, index.ts only exports Rounded, Rect, and Ghost.

By doing this, you can handle variants with multiple axes without deepening the directory hierarchy.

However, there is a drawback: you need to accurately understand which targets index.ts exports. One idea to clarify export targets is to add an underscore to directories that are not exported, such as _Primary or _Secondary.

Conclusion

In this article, I reflected on what I consider to be important points when actually putting into practice the "directory structure for smartly managing common parts." Since there are many cases in standard BCD Design where I want to manage a single component by dividing it into parts, I intend to try managing them using this directory structure.

GitHubで編集を提案

Discussion