Skip to content

Commit 8baa4d3

Browse files
authored
Merge pull request #535 from filipekiss/nth-parameter-grid
Add `$nth` parameter for `column` and `span` mixins
2 parents 3a79f8f + 1d15182 commit 8baa4d3

File tree

10 files changed

+81
-27
lines changed

10 files changed

+81
-27
lines changed

docs/api.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# API
22

3-
### column($ratios: 1, $offset: 0, $cycle: 0, $gutter: map-get($jeet, 'gutter'))
3+
### column($ratios: 1, $offset: 0, $cycle: 0, $gutter: map-get($jeet, 'gutter'), $nth: map-get($jeet, 'nth'))
44

55
Specify an initial ratio, either as fractions or decimals, then pass the parent container's context ratio to maintain consistent gutters as you nest.
66

@@ -12,6 +12,9 @@ Want to change it up when you get down to mobile? Maybe just show 2 images per r
1212

1313
Need to adjust column gutters for a specific container? `col(1/4, $gutter: .5)`. Note the gutter isn't a fixed width.
1414

15+
The `nth` parameter allow you to switch betweet `nth-child` and `nth-of-type` when building the
16+
grid. `nth-child` is the default rule. The accepted values are `child` or `type`.
17+
1518
### column-width($ratios: 1, $gutter: map-get($jeet, 'gutter'))
1619

1720
A function to return strictly the column width with none of the styles.
@@ -20,7 +23,7 @@ A function to return strictly the column width with none of the styles.
2023

2124
A function that returns the gutter size.
2225

23-
### span($ratio: 1, $offset: 0, $cycle: 0)
26+
### span($ratio: 1, $offset: 0, $cycle: 0, $nth: map-get($jeet, 'nth'))
2427

2528
Need a grid without the gutters? For instance, for a horizontal navigation where you want buttons touching. Do so with `span(1/5)`. No need to pass more than one ratio since we don't need to worry about the math involved with gutters and all that.
2629

docs/settings.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ $jeet: (
77
gutter: 3,
88
max-width: 1440px,
99
layout-direction: LTR,
10-
parent-first: false
10+
parent-first: false,
11+
nth: "child"
1112
);
1213
```
1314

@@ -29,6 +30,11 @@ Support for left-to-right, or right-to-left (`RTL`) text/layouts.
2930

3031
When assigning multiple ratio contexts to a `col()`, do you want to reference the outer most container first? Example: Let's assume we have a column set to `col(1/4)` that is nested inside of another column that is `col(1/3)` which is nested inside of another column that is `col(1/2)`. By default, to maintain consistently sized gutters (even when nesting), our inner-most column would look like `col(1/4 1/3 1/2)`. If we adjust this global variable to be `true`, our inner-most column could be written from a top-down perspective like so: `col(1/2 1/3 1/4)`. This is entirely a preference thing. Do you like stepping up or down?
3132

33+
### nth: "child"
34+
35+
Switches between using `nth-child` or `nth-of-type` when rendering grid classes. The default is
36+
`nth-child`. Use `"type"` if you wish to use `nth-of-type`.
37+
3238
## Example Usage
3339

3440
```scss

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jeet",
3-
"version": "7.1.0",
3+
"version": "7.2.0",
44
"description": "A simple Sass and Stylus grid system. Built for humans.",
55
"homepage": "https://jeet.gs",
66
"license": "MIT",

scss/_grid.scss

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
@mixin column($ratios: 1, $offset: 0, $cycle: 0, $gutter: map-get($jeet, 'gutter'), $clearfix: true) {
1+
@mixin column($ratios: 1, $offset: 0, $cycle: 0, $gutter: map-get($jeet, 'gutter'), $clearfix: true, $nth: map-get($jeet, 'nth')) {
22
$side: _get-layout-direction();
33
$opposite-side: _opposite-direction($side);
44
$column-widths: _get-column($ratios, $gutter);
55
$margin-last: 0;
66
$margin-l: $margin-last;
77
$margin-r: nth($column-widths, 2);
8+
$nth-selector: "nth-child";
9+
@if $nth == "type" {
10+
$nth-selector: "nth-of-type";
11+
}
812

913
@if $offset != 0 {
1014
@if $offset < 0 {
@@ -32,18 +36,18 @@
3236
};
3337

3438
@if $cycle != 0 {
35-
&:nth-child(n) {
39+
&:#{$nth-selector}(n) {
3640
margin-#{_opposite-direction($side)}: $margin-r * 1%;
3741
float: $side;
3842
clear: none;
3943
}
4044

41-
&:nth-child(#{$cycle}n) {
45+
&:#{$nth-selector}(#{$cycle}n) {
4246
margin-#{_opposite-direction($side)}: $margin-last * 1%;
4347
float: _opposite-direction($side);
4448
}
4549

46-
&:nth-child(#{$cycle}n + 1) {
50+
&:#{$nth-selector}(#{$cycle}n + 1) {
4751
clear: both;
4852
}
4953
} @else {
@@ -84,12 +88,17 @@
8488
}
8589

8690

87-
@mixin span($ratio: 1, $offset: 0, $cycle: 0, $clearfix: true) {
91+
@mixin span($ratio: 1, $offset: 0, $cycle: 0, $clearfix: true, $nth: map-get($jeet, "nth")) {
8892
$side: _get-layout-direction();
8993
$opposite-side: _opposite-direction($side);
9094
$span-width: _get-span($ratio);
9195
$margin-r: 0;
9296
$margin-l: $margin-r;
97+
$nth-selector: "nth-child";
98+
@if $nth == "type" {
99+
$nth-selector: "nth-of-type";
100+
}
101+
93102
@if $offset != 0 {
94103
@if $offset < 0 {
95104
$offset: $offset * -1;
@@ -102,7 +111,7 @@
102111
@if $clearfix {
103112
@include clearfix;
104113
}
105-
114+
106115
float: $side;
107116
clear: none;
108117
text-align: inherit;
@@ -113,16 +122,16 @@
113122
};
114123

115124
@if $cycle != 0 {
116-
&:nth-child(n) {
125+
&:#{$nth-selector}(n) {
117126
float: $side;
118127
clear: none;
119128
}
120129

121-
&:nth-child(#{$cycle}n) {
130+
&:#{$nth-selector}(#{$cycle}n) {
122131
float: _opposite-direction($side);
123132
}
124133

125-
&:nth-child(#{$cycle}n + 1) {
134+
&:#{$nth-selector}(#{$cycle}n + 1) {
126135
clear: both;
127136
}
128137
}

scss/_settings.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ $jeet: (
22
gutter: 3,
33
max-width: 1440px,
44
layout-direction: LTR,
5-
parent-first: false
5+
parent-first: false,
6+
nth: child
67
);

styl/_grid.styl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
column($ratios = 1, $offset = 0, $cycle = 0, $gutter = $jeet.gutter, $clearfix = true)
1+
column($ratios = 1, $offset = 0, $cycle = 0, $gutter = $jeet.gutter, $clearfix = true, $nth = $jeet.nth)
22
side = _get-layout-direction()
33
opposite-side = opposite-position(side)
44
column-widths = _get-column($ratios, $gutter)
55
margin-last = 0
66
margin-l = margin-last
77
margin-r = column-widths[1]
8+
$nth-selector = "nth-child"
9+
if $nth is "type"
10+
$nth-selector = "nth-of-type"
811

912
unless $offset == 0
1013
if $offset < 0
@@ -26,16 +29,16 @@ column($ratios = 1, $offset = 0, $cycle = 0, $gutter = $jeet.gutter, $clearfix =
2629
margin-{opposite-side}: (margin-r)%
2730

2831
if $cycle != 0
29-
&:nth-child(n)
32+
&:{$nth-selector}(n)
3033
margin-{opposite-side}: (margin-r)%
3134
float: side
3235
clear: none
3336

34-
&:nth-child({$cycle}n)
37+
&:{$nth-selector}({$cycle}n)
3538
margin-{opposite-side}: (margin-last)%
3639
float: opposite-side
3740

38-
&:nth-child({$cycle}n + 1)
41+
&:{$nth-selector}({$cycle}n + 1)
3942
clear: both
4043
else
4144
&:last-child
@@ -62,12 +65,16 @@ column-gutter($ratios = 1, $gutter = $jeet.gutter)
6265
return $gutter + '%'
6366

6467

65-
span($ratio = 1, $offset = 0, $cycle = 0, $clearfix = true)
68+
span($ratio = 1, $offset = 0, $cycle = 0, $clearfix = true, $nth = $jeet.nth)
6669
side = _get-layout-direction()
6770
opposite-side = opposite-position(side)
6871
span-width = _get-span($ratio)
6972
margin-r = 0
7073
margin-l = margin-r
74+
$nth-selector = "nth-child"
75+
if $nth is "type"
76+
$nth-selector = "nth-of-type"
77+
7178

7279
unless $offset == 0
7380
if $offset < 0
@@ -87,14 +94,14 @@ span($ratio = 1, $offset = 0, $cycle = 0, $clearfix = true)
8794
margin-{opposite-side}: (margin-r)%
8895

8996
if $cycle != 0
90-
&:nth-child(n)
97+
&:{$nth-selector}(n)
9198
float: side
9299
clear: none
93100

94-
&:nth-child({$cycle}n)
101+
&:{$nth-selector}({$cycle}n)
95102
float: opposite-side
96103

97-
&:nth-child({$cycle}n + 1)
104+
&:{$nth-selector}({$cycle}n + 1)
98105
clear: both
99106

100107

styl/_settings.styl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ $jeet = {
22
gutter: 3,
33
max-width: 1440px,
44
layout-direction: LTR,
5-
parent-first: false
5+
parent-first: false,
6+
nth: child
67
}

test/node-stylus/style.styl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
div
22
column(1/3)
33

4+
section.child
5+
column(1/4, $cycle: 4)
6+
7+
section.type
8+
column(1/4, $cycle: 4, $nth: "type")
9+
410
span
5-
span(1/2, $clearfix: false)
11+
span(1/2, $clearfix: false)

test/playground/sass/style.css

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ div {
5858
float: left;
5959
clear: none;
6060
text-align: inherit;
61-
width: 33.33333%;
61+
width: 33.3333333333%;
6262
margin-left: 0%;
6363
margin-right: 0%; }
6464
div::after {
@@ -72,5 +72,22 @@ div {
7272
float: right; }
7373
div:nth-child(3n + 1) {
7474
clear: both; } }
75-
76-
/*# sourceMappingURL=style.css.map */
75+
@media (min-width: 1440px) {
76+
div {
77+
float: left;
78+
clear: none;
79+
text-align: inherit;
80+
width: 25%;
81+
margin-left: 0%;
82+
margin-right: 0%; }
83+
div::after {
84+
content: '';
85+
display: table;
86+
clear: both; }
87+
div:nth-of-type(n) {
88+
float: left;
89+
clear: none; }
90+
div:nth-of-type(4n) {
91+
float: right; }
92+
div:nth-of-type(4n + 1) {
93+
clear: both; } }

test/playground/sass/style.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ div {
2727
@media (min-width: 900px) {
2828
@include span(1/3, $cycle: 3);
2929
}
30+
31+
@media (min-width:1440px) {
32+
@include span(1/4, $cycle: 4, $nth: "type");
33+
}
3034
}

0 commit comments

Comments
 (0)