Skip to content

Instantly share code, notes, and snippets.

@robdecker
Last active May 1, 2021 22:01
Show Gist options
  • Save robdecker/7366622 to your computer and use it in GitHub Desktop.
Save robdecker/7366622 to your computer and use it in GitHub Desktop.
[Sass folder structure best practices] #sass

Sass Structure

The Sass folder structure loosely follows the concepts discussed at https://medium.com/p/7fe19ab647fa.

Directories

/framework

This folder contains only partials and is reserved for project-wide CSS. Ideally, all of the CSS in here is as close to project-agnostic as possible. In other words, there are several files in here that could show up in any of your projects.

/framework/mixins

This folder contains only partials. Mixins relating to any partial in the /framework folder. Also included here are any completely generic mixins.

/components

Differs from modules (in the above article) since modules in Drupal have a very different meaning.

This folder also contains only partials and is reserved for reusable UI patterns. As a general rule of thumb, anytime you anticipate needing the same CSS more than once, you should modularize it. The more your project matures, the more CSS you should be able to compartmentalize into components. Preferably, if any component needs a mixin, a mixin should be created in /components/mixins using the same name as the component file.

Components within a component file should be created using silent placeholders:

%silent-identifier {}

Using silent placeholders dramatically reduces the duplication of styles within the compiled css file.

/components/mixins

This folder contains only partials. Mixins relating to any partial in the /components folder.

/layouts

This folder contains only partials. Includes the layout config, and the layout partials themselves.

/vendor

This folder contains only partials. Some files in here will be a sass partial, while others will simply be the original vendor CSS files with their extensions changed to .scss.

With that in mind, this folder also contains an override partial. Any time you need to override a vendor style, write that CSS there. This allows you to upgrade a plugin at any time without losing any custom changes that you've made.

/sections

This folder contains only partials. Section folders are where any CSS that has not (yet) been modularized lives. Depending on the project, you might have a dozen section folders or none. For many projects, these could be as simple as:

/sections/user
/sections/homepage
/sections/blog

If you really want to keep your files neat and tidy, you could even nest additional section folders inside of each other. Be creative.

Within each section folder/file, you should @extend the silent placeholders you created in the /framework and /component files. Example:

.some-selector {
  @extend %silent-identifier;
}

Very minimal actual css goes in here.

Best Practices

Most actual css code goes into /framework and /components partials. "Use" the partials in /sections partials. When in doubt, create something generic, and the use it with an @extend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment