aboutsummaryrefslogtreecommitdiffstats
path: root/README.md
blob: 7c04922ac08f4a34be6066be76ea61f4ac6f8d38 (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
# iro-sass

iro-sass is a multi-purpose [Sass](http://sass-lang.com/) library for designing websites.
Its main feature is a BEM system, but it also includes some other useful features:

- Easing background gradients
- Context stacks: A temporary data storage
- Property trees: A persistent data storage
- Responsive properties: A generalization of responsive typography
- Modular scales

## Contents

- [iro-sass](#iro-sass)
  - [Contents](#contents)
  - [Getting started](#getting-started)
  - [Development](#development)
  - [Features](#features)
    - [BEM system](#bem-system)
    - [Easing background gradients](#easing-background-gradients)
    - [Context stacks](#context-stacks)
    - [Property trees](#property-trees)
    - [Responsive properties](#responsive-properties)
    - [Modular scales](#modular-scales)

## Getting started

Include iro-sass in your Sass stylesheet:

```scss
@import 'iro-sass/src/main';
```

iro-sass uses function, mixin and variable names that are prefixed with 'iro-' to avoid clashes with other libraries.
There are, however, shorter versions of many mixins and functions available (referred to as "shortcodes").
Just import one of these files to use a specific set of shortcodes:

- `iro-sass/src/bem-shortcodes`: BEM
- `iro-sass/src/responsive-shortcodes`: Responsive properties
- `iro-sass/src/harmony-shortcodes`: Modular scales

## Development

Clone the repository, then run `npm install` or `yarn` to install all dependencies.

The following npm scripts are available:

- `lint`: Lint the source code.
- `livelint`: Lint automatically whenever the code changes.
- `doc`: Generate the SassDoc documentation.
- `test`: Run unit tests.

## Features

### BEM system

iro-sass' main feature is its BEM system which was developed over the course of two years. Features include:

- **Full [BEM](https://en.bem.info/) and [BEMIT](https://csswizardry.com/2015/08/bemit-taking-the-bem-naming-convention-a-step-further/) support:** Namespaced blocks, suffixes, states, and so on.
- **Robustness:** Most selector-related operations use Sass' native selector functions instead of manual parsing and assembling.
- **Quality:** All mixins generate optimal selectors with a minimal degree of specificity.
- **Safety:** All mixins perform checks if they are used correctly.
- **Flexibility:** Mix BEM selectors and other selectors however you like — the BEM system will adapt.
- **Strictness:** The BEM system allows you to define rules that control how the BEM mixins may or may not be used.

Below is a basic example showing how the BEM system can be used:

```scss
@include iro-bem-object('media') {
    display:         flex;
    align-items:     flex-start;
    justify-content: flex-start;

    @include iro-bem-element('image') {
        display:  block;
        flex:     0 0 auto;
        order:    1;
        overflow: hidden;
    }

    @include iro-bem-element('body') {
        order: 2;
    }

    @include iro-bem-modifier('rtl') {
        justify-content: flex-end;

        @include iro-bem-element('image') {
            order: 2;
        }

        @include iro-bem-element('body') {
            order: 1;
        }
    }
}
```

The result is this CSS:

```css
.o-media {
    display:         flex;
    align-items:     flex-start;
    justify-content: flex-start;
}

.o-media__image {
    display:  block;
    flex:     0 0 auto;
    order:    1;
    overflow: hidden;
}

.o-media__body {
    order: 2;
}

.o-media--rtl {
    justify-content: flex-end;
}

.o-media--rtl .o-media__image {
    order: 2;
}

.o-media--rtl .o-media__body {
    order: 1;
}
```

### Easing background gradients

The background gradients generated by browsers usually have a pretty hard transition from one color to another. In some situations, this results in a clearly visible edge where the transition ends.

[Andreas Larsen wrote an article on CSS-Tricks](https://css-tricks.com/easing-linear-gradients/) where this whole problem is explained in detail.
He also developed a solution, a [PostCSS plugin](https://github.com/larsenwork/postcss-easing-gradients) which automatically converts linear gradients using an easing function into regular linear-gradients.

This solution works for simple use cases, but unfortunately it wasn't suitable for me.
First, I wanted to use easing radial-gradients as well, which aren't supported by the PostCSS plugin.
And second, I wanted to freely position the color stops and not be locked to 0% for the start and 100% for the end.

The easing gradients provided by iro-sass address the above problems.
Moreover, they have one more major feature: You can use multiple color stops with varying easing functions.

The syntax is kept as close to the [new CSSWG proposal](https://github.com/w3c/csswg-drafts/issues/1332) as possible to make the transition to the native easing gradients easier later on.

Example usage:

```scss
.test {
    background-image: iro-easing-linear-gradient(
        to right,
        #000 2em,
        #f00,
        ease-in-out-sine,
        transparent 10em
    );
}
```

This will generate a linear-gradient where black normally fades into red, from 2em to 6em.
Then, red *smoothly* fades into transparent, from 6em to 10em.
After that, the gradient remains transparent.

### Context stacks

Context stacks are a temporary data storage and, as the name suggests, are used like a conventional stack data structure.
This means: Whenever you want to store a context -- which is an identifier and any kind of data, such as a map, a list, a string, etc... -- you push it to the stack.
From then on, this context is publicly accessible.
In order to remove it, you pop the stack.

This feature becomes extremely useful when paired with mixins and their `@content` directive: Pushing a context to the stack before `@content` and popping the stack afterwards gives it the role of a call stack.
Thats how the BEM system, for example, attaches metadata to the selectors it generates.
These information are used to generate optimal selectors without much parsing.

Below is an example of how context stacks can be used:

```scss
$context-id: 'some-context-stack';

@mixin anything($p) {
    @include iro-context-push($context-id, 'anything', (
        --this: 1,
        --is:   true,
        --the:  'test',
        --data: $p
    ));

    @content;

    @include iro-context-pop($context-id);
}

// Usage:

.test {
    @include anything('hello') {
        $context-data: nth(iro-context-get($context-id, 'anything'), 2);
        $this: map-get($context-data, --this); // 1
        $is:   map-get($context-data, --is);   // true
        $the:  map-get($context-data, --the);  // 'test'
        $data: map-get($context-data, --data); // 'hello'
    }
}
```

### Property trees

Property trees are basically global maps that are immutable as long as you just use the intended functions.

It's a very simple feature, but it makes managing large sets of structured data much easier.

Example usage:

```scss
@include iro-props-save((
    --accent:      #f00,
    --accent-text: #fff,

    --background:     #fff,
    --text:           #222,
    
    --link: (
        --idle: (
            --text:      #000,
            --underline: #f00
        ),
        --hover: (
            --text:      #f00,
            --underline: #f00
        )
    )
), 'light');

// Usage:

p {
    color:            iro-props-get(--text, 'light');       // #222
    background-color: iro-props-get(--background, 'light'); // #fff
}

a {
    color:            iro-props-get(--link --idle --text, 'light');                // #000
    border-bottom:    1px solid iro-props-get(--link --idle --underline, 'light'); // #f00
    text-decoration:  none;

    &:hover {
        color:               iro-props-get(--link --hover --text, 'light');      // #f00
        border-bottom-color: iro-props-get(--link --hover --underline, 'light'); // #f00
    }
}
```

### Responsive properties

Responsive properties allow you to assign values to properties depending on the current viewport width.
iro-sass provides a large number of mixins for this task to cover many use cases.
The most simple one is the following:

```scss
.title {
    @include iro-responsive-property(padding, ( 20rem: 2.1rem, 40rem: 2.6rem, 60rem: 3.5rem ));
}
```

The padding will be 2.1rem if the viewport is 20rem wide, 2.6rem if it's 40rem wide, and 3.5rem if it's 60rem wide.
If the viewport is narrower than 20rem, the padding will stick with 2.1rem.
If the viewport is wider than 60rem, the padding will stick with 3.5rem.

By default, iro-sass will dynamically scale the property value between viewport widths, which is technique known from [fluid typography](https://css-tricks.com/snippets/css/fluid-typography/).
This behavior can be switched off if it's undesired.

If you use [include-media](https://include-media.com/), all responsive mixins also support named viewports.
The example above could then be written like this:

```scss
.title {
    @include iro-responsive-property(padding, ( phone: 2.1rem, tablet: 2.6rem, desktop: 3.5rem ));
}
```

### Modular scales

From the description of [modularscale-sass](https://github.com/modularscale/modularscale-sass):

> A modular scale is a list of values that share the same relationship. These values are often used to size type and create a sense of harmony in a design. Proportions within modular scales are all around us from the spacing of the joints on our fingers to branches on trees. These natural proportions have been used since the time of the ancient Greeks in architecture and design and can be a tremendously helpful tool to leverage for web designers.

iro-sass provides a mixin to create basic and multi-stranded modular scales.
It's a lightweight alternative to modularscale-sass.

Example with a multi-stranded modular scale:

```scss
$mod-scale: 1em 2em, 1.1;

h1 {
   font-size: iro-harmony-modular-scale(3, $mod-scale...); // Will be: 1.128em
}
h2 {
   font-size: iro-harmony-modular-scale(2, $mod-scale...); // Will be: 1.1em
}
h3 {
   font-size: iro-harmony-modular-scale(1, $mod-scale...); // Will be: 1.026em
}
```

Combined with iro-sass' responsive properties:

```scss
$responsive-mod-scale: (
    320px: (1rem 2rem, 1.1),
    640px: (1rem 2rem, 1.2)
);

h1 {
    @include iro-responsive-modular-scale(font-size, 3, $responsive-mod-scale);
}
h2 {
    @include iro-responsive-modular-scale(font-size, 2, $responsive-mod-scale);
}
h3 {
    @include iro-responsive-modular-scale(font-size, 1, $responsive-mod-scale);
}
```