aboutsummaryrefslogtreecommitdiffstats
path: root/src/bem/_block.scss
blob: d0658912d6a9201674a1854f7f3c41caa742e61b (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
////
/// @group BEM
///
/// @access public
////

/// 
/// 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 iro-bem-block('something', 'component') {
///     /* some definitions */
///   }
///   
///   // Generates:
///   
///   .c-something {
///     /* some definitions */
///   }
///
@mixin iro-bem-block($name, $type: null) {
    $result:   iro-bem-block($name, $type);
    $selector: nth($result, 1);
    $context:  nth($result, 2);

    @include iro-bem-validate(
        'block',
        (name: $name, type: $type),
        $selector,
        $context
    );

    @if $type != null {
        $iro-bem-blocks: append($iro-bem-blocks, $name + '_' + $type) !global;
    } @else {
        $iro-bem-blocks: append($iro-bem-blocks, $name) !global;
    }

    @include iro-context-push($iro-bem-context-id, $context...);
    @at-root #{$selector} {
        @content;
    }
    @include iro-context-pop($iro-bem-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} iro-bem-block
///
@function iro-bem-block($name, $type: null) {
    //
    // Possible outcomes:
    //   - ({b,e,m,s}) block
    //

    $noop: iro-context-assert-stack-count($iro-bem-context-id, $iro-bem-max-depth);

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

    @if $type != null {
        $namespace: map-get($iro-bem-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: iro-bem-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 iro-bem-block($name, 'object').
///
/// @param {string} $name - Object block name
///
/// @content
///
@mixin iro-bem-object($name) {
    @include iro-bem-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} iro-bem-object
///
@function iro-bem-object($name) {
    @return iro-bem-block($name, 'object');
}

/// 
/// Generate a new component block. It's a shorthand for iro-bem-block($name, 'component').
///
/// @param {string} $name - Component block name
///
/// @content
///
@mixin iro-bem-component($name) {
    @include iro-bem-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} iro-bem-component
///
@function iro-bem-component($name) {
    @return iro-bem-block($name, 'component');
}

/// 
/// Generate a new layout block. It's a shorthand for iro-bem-block($name, 'layout').
///
/// @param {string} $name - Layout block name
///
/// @content
///
@mixin iro-bem-layout($name) {
    @include iro-bem-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} iro-bem-layout
///
@function iro-bem-layout($name) {
    @return iro-bem-block($name, 'layout');
}

/// 
/// Generate a new utility block. It's a shorthand for iro-bem-block($name, 'utility').
///
/// @param {string} $name - Utility block name
///
/// @content
///
@mixin iro-bem-utility($name) {
    @include iro-bem-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} iro-bem-utility
///
@function iro-bem-utility($name) {
    @return iro-bem-block($name, 'utility');
}

/// 
/// Generate a new scope block. It's a shorthand for iro-bem-block($name, 'scope').
///
/// @param {string} $name - Scope block name
///
/// @content
///
@mixin iro-bem-scope($name) {
    @include iro-bem-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} iro-bem-scope
///
@function iro-bem-scope($name) {
    @return iro-bem-block($name, 'scope');
}

/// 
/// Generate a new theme block. It's a shorthand for iro-bem-block($name, 'theme').
///
/// @param {string} $name - Theme block name
///
/// @content
///
@mixin iro-bem-theme($name) {
    @include iro-bem-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} iro-bem-theme
///
@function iro-bem-theme($name) {
    @return iro-bem-block($name, 'theme');
}

/// 
/// Generate a new JS block. It's a shorthand for iro-bem-block($name, 'js').
///
/// @param {string} $name - JS block name
///
/// @content
///
@mixin iro-bem-js($name) {
    @include iro-bem-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} iro-bem-js
///
@function iro-bem-js($name) {
    @return iro-bem-block($name, 'js');
}

/// 
/// Generate a new QA block. It's a shorthand for iro-bem-block($name, 'qa').
///
/// @param {string} $name - QA block name
///
/// @content
///
@mixin iro-bem-qa($name) {
    @include iro-bem-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} iro-bem-qa
///
@function iro-bem-qa($name) {
    @return iro-bem-block($name, 'qa');
}

/// 
/// Generate a new hack block. It's a shorthand for iro-bem-block($name, 'hack').
///
/// @param {string} $name - Hack block name
///
/// @content
///
@mixin iro-bem-hack($name) {
    @include iro-bem-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} iro-bem-hack
///
@function iro-bem-hack($name) {
    @return iro-bem-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 iro-bem-component('someBlock') {
///     /* some definitions */
///   }
///   
///   @include iro-bem-component('anotherBlock') {
///     /* some definitions */
///     
///     @include iro-bem-element('elem') {
///       @include iro-bem-composed-of('someBlock' 'component');
///       
///       /* some definitions */
///     }
///   }
///
///   // Intended use: <div class="c-anotherBlock__elem c-someBlock">...</div>
///
/// @example scss - Failing assertion
///   @include iro-bem-component('anotherBlock') {
///     /* some definitions */
///     
///     @include iro-bem-element('elem') {
///       @include iro-bem-composed-of('someBlock' 'component');
///       
///       /* some definitions */
///     }
///   }
///   
///   @include iro-bem-component('someBlock') {
///     /* some definitions */
///   }
///   
///   // Compilation will fail because c-someBlock is defined after c-anotherBlock__elem
///
@mixin iro-bem-composed-of($block, $blocks...) {
    @each $block in iro-list-prepend($blocks, $block) {
        @if type-of($block) == string {
            @if not index($iro-bem-blocks, $block) {
                @error 'Block "#{$block}" does not exist.';
            }
        } @else {
            $name: nth($block, 1);
            $type: nth($block, 2);

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

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