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

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

/// 
/// Create a new state rule.
///
/// @param {string} $state  - First state name
/// @param {list}   $states - List of more state names
///
/// @content
///
/// @example scss - Using single is-state
///   @include object('menu') {
///     display: none;
///     
///     @include state('is', open') {
///       display: block;
///     }
///   }
///   
///   // Generates:
///   
///   .o-menu {
///     display: none;
///   }
///   
///   .o-menu.is-open {
///     display: block;
///   }
///
/// @example scss - Using multiple is-states
///   @include object('menu') {
///     display: none;
///     
///     @include state('is', open', 'visible') {
///       display: block;
///     }
///   }
///   
///   // Generates:
///   
///   .o-menu {
///     display: none;
///   }
///   
///   .o-menu.is-open,
///   .o-menu.is-visible {
///     display: block;
///   }
///
@mixin state($prefix, $state, $states...) {
    $result:   state($prefix, $state, $states...);
    $selector: nth($result, 1);
    $context:  nth($result, 2);

    @include validators.validate(
        'state',
        (prefix: $prefix, state: $state, states: $states),
        $selector,
        $context
    );

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

///
/// Generate a new state.
/// 
/// @return {list} A list with two items: 1. selector, 2. context or `null`
///
/// @see {mixin} has
///
@function state($prefix, $state, $states...) {
    $selector:   ();
    $parts-data: ();

    @each $state in join($state, $states) {
        $sel:        selector-parse('.#{$prefix}-#{$state}');
        @if & {
            $sel:    selector-append(&, $sel);
        }
        $selector:   join($selector, $sel, comma);
        $parts-data: append(
            $parts-data, (
                'name':     $state,
                'selector': $sel
            )
        );
    }

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

    @return $selector $context;
}

/// 
/// Create a new has-state modifier.
///
/// It's a shorthand for state('is', $state, $states...).
///
@mixin is($state, $states...) {
    @include state('is', $state, $states...) {
        @content;
    }
}

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

/// 
/// Create a new has-state modifier.
///
/// It's a shorthand for state('has', $state, $states...).
///
@mixin has($state, $states...) {
    @include state('has', $state, $states...) {
        @content;
    }
}

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