aboutsummaryrefslogtreecommitdiffstats
path: root/src/bem/_block.scss
blob: 49af04b5bbc1f50ed3d772e791c5f1598d5b3c1f (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
////
/// @group BEM
///
/// @access public
////

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

/// 
/// Generate a new block.
///
/// This mixin simply creates a new block with the name {namespace}_{name},
/// where {namespace} is the prefix assigned to $type and {name} is the
/// block's name.
///
/// @param {string} $name        - Block name
/// @param {string} $type [null] - BEMIT namespace of the block
///
/// @content
///
/// @throw If $type is invalid
/// @throw If the block is preceded by another block, element, modifier or suffix
///
/// @example scss - Creating a new block
///   @include block('something', 'component') {
///     /* some definitions */
///   }
///   
///   // Generates:
///   
///   .c-something {
///     /* some definitions */
///   }
///
@mixin block($name, $type: null) {
    $result:   block($name, $type);
    $selector: nth($result, 1);
    $context:  nth($result, 2);

    @include validators.validate(
        'block',
        (name: $name, type: $type),
        $selector,
        $context
    );

    @if $type != null {
        vars.$blocks: append(vars.$blocks, $name + '_' + $type);
    } @else {
        vars.$blocks: append(vars.$blocks, $name);
    }

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

///
/// Generate a new block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} block
///
@function block($name, $type: null) {
    //
    // Possible outcomes:
    //   - ({b,e,m,s}) block
    //

    $noop: contexts.assert-stack-count(vars.$context-id, vars.$max-depth);

    $selector:      null;
    $base-selector: null;

    @if $type != null {
        $namespace: map-get(vars.$namespaces, $type);

        @if not $namespace {
            @error '"#{$type}" is not a valid type.';
        }

        $base-selector: selector-parse('.' + $namespace + '-' + $name);

        @if $type != 'theme' or & {
            $selector: $base-selector;
        } @else if not & {
            $selector: bemfunctions.theme-selector($name);
        }
    } @else {
        $base-selector: selector-parse('.' + $name);
        $selector:      $base-selector;
    }

    @if & {
        $selector: selector-nest(&, $selector);
    }

    $context: 'block', (
        'name':          $name,
        'type':          $type,
        'selector':      $selector,
        'base-selector': $base-selector
    );

    @return $selector $context;
}

/// 
/// Generate a new object block. It's a shorthand for block($name, 'object').
///
/// @param {string} $name - Object block name
///
/// @content
///
@mixin object($name) {
    @include block($name, 'object') {
        @content;
    }
}

///
/// Generate a new object block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} object
///
@function object($name) {
    @return block($name, 'object');
}

/// 
/// Generate a new component block. It's a shorthand for block($name, 'component').
///
/// @param {string} $name - Component block name
///
/// @content
///
@mixin component($name) {
    @include block($name, 'component') {
        @content;
    }
}

///
/// Generate a new component block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} component
///
@function component($name) {
    @return block($name, 'component');
}

/// 
/// Generate a new layout block. It's a shorthand for block($name, 'layout').
///
/// @param {string} $name - Layout block name
///
/// @content
///
@mixin layout($name) {
    @include block($name, 'layout') {
        @content;
    }
}

///
/// Generate a new layout block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} layout
///
@function layout($name) {
    @return block($name, 'layout');
}

/// 
/// Generate a new utility block. It's a shorthand for block($name, 'utility').
///
/// @param {string} $name - Utility block name
///
/// @content
///
@mixin utility($name) {
    @include block($name, 'utility') {
        @content;
    }
}

///
/// Generate a new utility block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} utility
///
@function utility($name) {
    @return block($name, 'utility');
}

/// 
/// Generate a new scope block. It's a shorthand for block($name, 'scope').
///
/// @param {string} $name - Scope block name
///
/// @content
///
@mixin scope($name) {
    @include block($name, 'scope') {
        @content;
    }
}

///
/// Generate a new scope block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} scope
///
@function scope($name) {
    @return block($name, 'scope');
}

/// 
/// Generate a new theme block. It's a shorthand for block($name, 'theme').
///
/// @param {string} $name - Theme block name
///
/// @content
///
@mixin theme($name) {
    @include block($name, 'theme') {
        @content;
    }
}

///
/// Generate a new theme block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} theme
///
@function theme($name) {
    @return block($name, 'theme');
}

/// 
/// Generate a new JS block. It's a shorthand for block($name, 'js').
///
/// @param {string} $name - JS block name
///
/// @content
///
@mixin js($name) {
    @include block($name, 'js') {
        @content;
    }
}

///
/// Generate a new JS block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} js
///
@function js($name) {
    @return block($name, 'js');
}

/// 
/// Generate a new QA block. It's a shorthand for block($name, 'qa').
///
/// @param {string} $name - QA block name
///
/// @content
///
@mixin qa($name) {
    @include block($name, 'qa') {
        @content;
    }
}

///
/// Generate a new QA block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} qa
///
@function qa($name) {
    @return block($name, 'qa');
}

/// 
/// Generate a new hack block. It's a shorthand for block($name, 'hack').
///
/// @param {string} $name - Hack block name
///
/// @content
///
@mixin hack($name) {
    @include block($name, 'hack') {
        @content;
    }
}

///
/// Generate a new hack block. Check the respective mixin documentation for more information.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} hack
///
@function hack($name) {
    @return block($name, 'hack');
}

/// 
/// Assert that a block or element is composed of another block. In BEM, such a relationship is referred to
/// as a "mix": https://en.bem.info/methodology/key-concepts/#mix
///
/// Compilation will fail if the foreign block doesn't exist. This way, you can ensure that blocks are
/// defined in the right order so that composed blocks/elements will actually override the foreign
/// declarations without having to artificially increase the specificity.
///
/// @param {string | list} $block  - Either first block name, or list with two items: 1. block name, 2. block type
/// @param {string | list} $blocks - Either other block names, or list with two items: 1. block name, 2. block type
///
/// @throw If a block type is invalid 
/// @throw If a block doesn't exist
///
/// @example scss - Successful assertion
///   @include component('someBlock') {
///     /* some definitions */
///   }
///   
///   @include component('anotherBlock') {
///     /* some definitions */
///     
///     @include elem('elem') {
///       @include composed-of('someBlock' 'component');
///       
///       /* some definitions */
///     }
///   }
///
///   // Intended use: <div class="c-anotherBlock__elem c-someBlock">...</div>
///
/// @example scss - Failing assertion
///   @include component('anotherBlock') {
///     /* some definitions */
///     
///     @include elem('elem') {
///       @include composed-of('someBlock' 'component');
///       
///       /* some definitions */
///     }
///   }
///   
///   @include component('someBlock') {
///     /* some definitions */
///   }
///   
///   // Compilation will fail because c-someBlock is defined after c-anotherBlock__elem
///
@mixin composed-of($block, $blocks...) {
    @each $block in functions.list-prepend($blocks, $block) {
        @if type-of($block) == string {
            @if not index(vars.$blocks, $block) {
                @error 'Block "#{$block}" does not exist.';
            }
        } @else {
            $name: nth($block, 1);
            $type: nth($block, 2);

            @if not map-get(vars.$namespaces, $type) {
                @error '"#{$type}" is not a valid type.';
            }

            @if not index(vars.$blocks, $name + '_' + $type) {
                @error 'Block "#{$name}" does not exist.';
            }
        }
    }
}