ICX4 CSS/SCSS standards


// Definitions:
.selector {
  property: value;
}

// SELECTORS

// All selectors should be written on a separate line:
.selector,
.another-selector,
.one-more-selector {
  font-size: 1rem;
}

  // Apart from where a selector is an extension of the previous:
  .selector,
  .another-selector, .another-selector p, .another-selector span,
  .one-more-selector, .one-more-selector * {
    font-size: 1rem;
  }

// PROPERTIES
// All properties should be in alphabetical order:
.selector {
  font-size: 1rem;
  display: block;
  pointer-events: none;
}

  // Apart from positional properties, which should be listed under the position property:
  .selector {
    font-size: 1rem;
    position: relative;
      left: 1rem;
  }

  // And all margins, paddings and positional properties should be written in clockwise order:
  .selector {
    margin: {
      right: 1rem;
      left: 1rem;
    }
    padding: {
      top: 1rem;
      right: 2rem;
      bottom: 3rem;
      left: 4rem;
    }
    position: relative;
      top: 1rem;
      left: 1rem;
      z-index: 100;
  }

// Properties should only be written for the specific rules you wish to apply:
.selector {
  // WRONG:
  background: white;
  margin: 0 auto;
  
  // CORRECT
  background-color: white;
  margin: {
    right: auto;
    left: auto;
  }
}

// VALUES

// All values, including hex codes, should be written in lowercase:
.selector {
  background-color: lightgoldenrodyellow;
  color: #ba0bab;
}

// All decimals less than 1 should start with a leading 0
.selector {
  background-color: rgba(black, 0.2);
  opacity: 0.4;
}

// Commas and colons should always be followed by a space:
.selector {
  background-color: rgba(0, 0, 0, 0.2);
}

// Multiple comma-separated values for the same property should all be given their own line with an extra indentation:
.selector {
  box-shadow:
    0 0 0 0.5rem black,
    0 0 0 0.75rem white;
  margin: 0.5rem 0.75rem;
}

// All font-family declarations should always finish with a generic family:
.selector {
  font-family:
    Helvetica,
    Calibri,
    sans-serif;
}

// SASS/SCSS

// All variables, functions and mixins should be written in CSS notation
// (all lower case and hyphenated)
// (opinionated)
$background-color: white;

@function px-to-rem($value) {
  // ...
}

@mixin block-element {
  // ...
}

// All variables should be declared first and followed by a blank line:
.selector {
  $var: rgba($var, 0.2);
  
  background-color: $var;
}

// All mixins should appear before any other properties (but after variables):
.selector {
  $var: rgba($var, 0.2);
  
  @include my-mixin();
  @include my-second-mixin();
  background-color: $var;
}

// All nested declarations should start after a line-break and be indented:
.selector {
  position: relative;
  
  nested-selector {
    position: absolute;
  }
}

// Components should be structured in the following way:
// 1. Local variables
// 2. Extend and mixins
// 3. Parent selector styles
// 4. Pseudo classes
// 5. Cascading modifiers
// 6. Modifiers
// 7. States
// 8. Children
.component {
  $var: rgba($var, 0.2); // 1
  
  @extend .foo !optional; // 2
  @include my-mixin(); // 2
  background-color: $var; // 3
  
  &::before {} // 4
  
  &::after {} // 4
  
  .parent-scope & {} // 5
  
  &.-modifier {} // 6
  
  &:active {} // 7
  
  &:hover {} // 7
    
  &--child {} // 8
  
  &--child-two {} // 8
}
// NOTE: Naming conventions in this example are opinionated.

// All mixins and functions should be annotated using SASS Doc annotations (http://sassdoc.com/annotations/):

/// GET MAP VALUE
/// helper function to retrieve value from a map

/// @access public
/// @group helpers
/// @since 1.2 MarsBar
/// @author aepicos

/// @param {map} $map - the map to be searched
/// @param {string} $key - the key to retrieve
/// @returns value | 'ERROR'

/// @example SASS
///   .foo {
///     background-color: get-map-value($app-colours, alt-background);
///   }

/// @example CSS - output
///   .foo {
///     backgound-color: #fefef9;
///   }

@function get-map-value($map, $key) {
  @if map-has-key($map, to-lower-case($key)) {
    @return map-get($map, to-lower-case($key));
  }
  @else {
    @return 'ERROR';
  }
}