Steps for Using Single Directory Components
Now that we’ve covered what Single Directory Components are and their benefits, let’s dig into how exactly you can implement this into your development environment.
Step 1: Enable the Single Directory Component Module
First things first, you need to enable the SDC module in Drupal. This module is part of the core experimental modules of Drupal 10.Â
Step 2: Create the Directory Structure
Establish a single directory for the SDC. This should include subdirectories for each essential part: assets, configuration, and any additional resources.
Step 3: Structure & Organize Your Directory Folders
Inside your directory, create individual folders for each component. Each folder should contain the necessary files:
Twig Template (.html.twig): The markup for the component.
Example:
CSS/SCSS (.css or .scss): Styles specific to the component.
Example:
JavaScript (.js): Any behavior or interaction logic.
YAML File (.libraries.yml): To declare styles and scripts for the component.
Example:
Here's an example of what the overall file structure looks like for a Single Directory Component:
Step 4: Register Your Component
To make your component accessible to Drupal, you’ll need to register it. In your theme’s .info.yml file, add the path to your components directory.
Step 5: Add Component-specific CSS and JavaScript
In your .libraries.yml file, register the styles and scripts for each component. In addition, make sure to attach the library to your component using Twig.Â
Step 6: Test & Iterate
Once you have set up your components, test them across the site. Since everything is modular and organized, iterating on design or functionality will be a smoother process.