aboutsummaryrefslogtreecommitdiffstats
path: root/src/bem/_modifier.scss
blob: 07267fed1cc9256b0d91d7e75de74a999bba5e8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
////
/// @group BEM
///
/// @access public
////

@use './validators';
@use './vars';
@use '../functions';
@use '../contexts';

/// 
/// Generate a new BEM modifier.
/// 
/// If the parent context is block or element, the modifier will modify said block or element according
/// to the BEM naming convention.
///
/// If the parent context is a modifier or suffix, then the modifier will depend on said modifier or suffix.
/// Depending on $extend, the meaning of this dependency (and the resulting selector) varies:
/// If it's false (default), you signalize that the modifier also exists by itself, but it changes its
/// behavior when the parent modifier or suffix is set.
/// If it's true, you signalize that the modifier extends the parent modifier or suffix and can only be
/// used in conjunction with it.
///
/// @param {string | list} $name  - First element name or list with two items: 1. first element name, 2. bool indicating if the modifier is extending
/// @param {string | list} $names - More element names or lists with two items: 1. element name, 2. bool indicating if the modifier is extending
///
/// @content
///
/// @throw If the element is not preceded by a block, element, modifier or suffix.
///
/// @example scss - Modifier that modifies a block or element
///   @include component('block') {
///     @include modifier('mod') {
///       background-color: #eee;
///     }
///     
///     @include elem('elem') {
///       @include modifier('mod') {
///         background-color: #222;
///       }
///     }
///   }
///   
///   // Generates:
///   
///   .c-block--mod {
///     background-color: #eee;
///   }
///   
///   .c-block__elem--mod {
///     background-color: #222;
///   }
///
/// @example scss - Modifier nested in another modifier, not extending
///   @include component('block') {
///     @include modifier('mod') {
///       background-color: #eee;
///     }
///     
///     @include modifier('dark') {
///       /* some definitions */
///       
///       @include modifier('mod') {
///         background-color: #222;
///       }
///     }
///   }
///   
///   // Generates:
///   
///   .c-block--mod {
///     background-color: #eee;
///   }
///   
///   .c-block--dark {
///     /* some definitions */
///   }
///   
///   .c-block--dark.c-block--mod {
///     background-color: #222;
///   }
///
/// @example scss - Modifier nested in another modifier, extending
///   @include component('block') {
///     @include modifier('mod') {
///       background-color: #eee;
///     }
///     
///     @include modifier('dark') {
///       /* some definitions */
///       
///       @include modifier('mod' true) {
///         background-color: #222;
///       }
///     }
///   }
///   
///   // Generates:
///   
///   .c-block--mod {
///     background-color: #eee;
///   }
///   
///   .c-block--dark {
///     /* some definitions */
///   }
///   
///   .c-block--dark--mod {
///     background-color: #222;
///   }
///
@mixin modifier($name, $names...) {
    $result:   modifier($name, $names...);
    $selector: nth($result, 1);
    $context:  nth($result, 2);

    @include validators.validate(
        'modifier',
        (name: $name, names: $names),
        $selector,
        $context
    );

    @include contexts.push(vars.$context-id, $context...);
    @at-root #{$selector} {
        @content;
    }
    @include contexts.pop(vars.$context-id);
}

///
/// Generate a new BEM modifier. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} modifier
///
@function modifier($name, $names...) {
    $noop: contexts.assert-stack-count(vars.$context-id, vars.$max-depth);
    $noop: contexts.assert-stack-must-contain(vars.$context-id, 'block');

    $parent-context:  contexts.get(vars.$context-id, 'block' 'element' 'modifier' 'suffix' 'state');
    $parent-selector: map-get(nth($parent-context, 2), 'selector');
    $selector:        ();
    $parts-data:      ();

    @if not functions.selector-suffix-match(&, $parent-selector) {
        //
        // The current selector doesn't match the parent selector.
        // The user manually added a selector between parent context and this modifier call.
        // This case is forbidden because any outcome semantically wouldn't make sense:
        //   - {b,e,m,s} [manual selector] {b,e,m,s}--modifier
        //   - {b,e,m,s}--modifier [manual selector]
        // The first case would make the modifier behave like an element.
        // The second case is unintuitive, the code would be more clear by nesting the manual
        // selector in the modifier instead.
        //

        @error 'A modifier must be an immediate child of the parent context';
    }

    @each $name in functions.list-prepend($names, $name) {
        $extend: false;
        @if type-of($name) == list {
            $extend: nth($name, 2);
            $name:   nth($name, 1);
        }

        @if index('block' 'element', nth($parent-context, 1)) or $extend == true {
            //
            // Either the parent context is block or element, or a modifier or suffix
            // is to be extended. The modifier part can simply be appended.
            // Possible outcomes:
            //   - {b,e,m,s}--modifier
            //

            $sel:        selector-append(&, vars.$modifier-separator + $name);
            $selector:   join($selector, $sel, comma);
            $parts-data: append(
                $parts-data, (
                    'name':     $name,
                    'selector': $sel
                )
            );
        } @else {
            //
            // Parent context is modifier, suffix or state and $extend is false.
            //

            $be-context: contexts.get(vars.$context-id, 'block' 'element');

            @if nth($be-context, 1) == 'element' {
                //
                // Latest context is element. Since element contexts can consist of multiple single
                // elements, inspect all elements and append its selector with the suffix "--$name".
                // This has to be done with string comparison since none of Sass selector functions
                // is of use here. 
                // Possible outcomes:
                //   - {m,s}.{e}--modifier
                //

                $nsel: ();

                @each $elem-part-data in map-get(nth($be-context, 2), 'parts') {
                    $elem-part-selector: map-get($elem-part-data, 'selector');

                    $sel: ();
                    @each $s in & {
                        @each $ps in $elem-part-selector {
                            @if str-index(inspect($s), inspect($ps) + vars.$modifier-separator) or str-index(inspect($s), inspect($ps) + vars.$suffix-separator) {
                                $sel: join($sel, selector-unify($s, selector-append($ps, vars.$modifier-separator + $name)), comma);
                            }
                        }
                    }
                    @if length($sel) == 0 {
                        @error 'Could not generate modifier selector.';
                    }

                    $nsel: join($nsel, $sel, comma);
                }

                $selector:   join($selector, $nsel, comma);
                $parts-data: append(
                    $parts-data, (
                        'name':     $name,
                        'selector': $nsel
                    )
                );
            } @else {
                //
                // Latest context is block. Just append the modifier part.
                // Possible outcomes:
                //   - {m,s}.{b}--modifier
                //

                $block-base-selector: map-get(nth($be-context, 2), 'base-selector');

                $sel:        selector-append(&, $block-base-selector, vars.$modifier-separator + $name);
                $selector:   join($selector, $sel, comma);
                $parts-data: append(
                    $parts-data, (
                        'name':     $name,
                        'selector': $sel
                    )
                );
            }
        }
    }

    $context: 'modifier', (
        'parts':    $parts-data,
        'selector': $selector
    );

    @return $selector $context;
}