ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Sass Utility Framework: Spacing Helpers

SASS

Goal -- WPM

Ready
Exercise Algorithm Area
1/*
2* Sass Spacing Utility Framework Mixin
3* Generates margin and padding utility classes.
4*/
5
6// Define a map of spacing values
7$spacing-values:
8'0': 0,
9'1': 0.25rem,
10'2': 0.5rem,
11'3': 1rem,
12'4': 1.5rem,
13'5': 2rem,
14'auto': auto
15;
16
17// Mixin to generate spacing utilities
18@mixin generate-spacing-utilities($property, $sides, $values: $spacing-values)
19// Iterate over each spacing value
20@each $key, $value in $values
21// Generate classes for each side
22@each $side in $sides
23// Construct the class name (e.g., .m-1, .pt-2, .mb-auto)
24$class-name: "#{$property}-#{$side}-#{$key}"
25
26// Handle 'all' sides case
27@if $side == 'all'
28$class-name: "#{$property}-#{$key}"
29
30// Apply the style
31.#{$class-name}
32#{$property}-#{$side}: $value
33
34/*
35* Helper mixin for margin utilities
36*/
37@mixin margin-utilities($values: $spacing-values)
38@include generate-spacing-utilities('margin', ('t', 'b', 'l', 'r', 'all'), $values)
39
40/*
41* Helper mixin for padding utilities
42*/
43@mixin padding-utilities($values: $spacing-values)
44@include generate-spacing-utilities('padding', ('t', 'b', 'l', 'r', 'all'), $values)
45
46/*
47* Example Usage:
48* @include margin-utilities()
49* @include padding-utilities()
50*/
Algorithm description viewbox

Sass Utility Framework: Spacing Helpers

Algorithm description:

This Sass mixin set, `generate-spacing-utilities`, along with its helpers `margin-utilities` and `padding-utilities`, creates a system of utility classes for applying consistent spacing (margins and paddings) throughout a project. It uses a map `$spacing-values` to define common spacing increments (e.g., '0', '1', '2') and a helper function to generate classes for different sides (top, bottom, left, right, all) and properties.

Algorithm explanation:

The `generate-spacing-utilities` mixin takes the CSS property ('margin' or 'padding'), a list of sides ('t', 'b', 'l', 'r', 'all'), and a map of spacing values. It iterates through each key-value pair in the `$values` map. For each spacing value, it then iterates through the specified `$sides`. It constructs a class name based on the property, side, and spacing key (e.g., `.m-t-1` for margin-top-1). If the side is 'all', it simplifies the class name (e.g., `.m-1`). Finally, it applies the corresponding CSS property and value to the generated class. The helper mixins `margin-utilities` and `padding-utilities` simply call the main mixin with predefined sides. The time complexity is O(S * V), where S is the number of sides and V is the number of spacing values, due to nested iteration. Space complexity is proportional to the number of generated utility classes.

Pseudocode:

Define spacing_values map (key: string, value: CSS unit).

Mixin generate-spacing-utilities(property, sides, values):
  For each key, value in values:
    For each side in sides:
      If side is 'all':
        class_name = property + '-' + key
      Else:
        class_name = property + '-' + side + '-' + key
      Define class .class_name with property + '-' + side: value.

Mixin margin-utilities(values):
  Call generate-spacing-utilities('margin', ('t', 'b', 'l', 'r', 'all'), values).

Mixin padding-utilities(values):
  Call generate-spacing-utilities('padding', ('t', 'b', 'l', 'r', 'all'), values).