@curiousmedia/breakpoint 1.4.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,175 @@
1
+ $breakpoints: () !default;
2
+
3
+ @function breakpointPresets($queries) {
4
+ $output: ();
5
+
6
+ @each $query in $queries {
7
+ $output: append($output, breakpointPreset($query))
8
+ }
9
+
10
+ @return $output;
11
+ }
12
+
13
+ @function breakpointPreset($query) {
14
+ $preset: map-get($breakpoints, $query);
15
+
16
+ @if ($preset == null) {
17
+ @return $query;
18
+ } @else {
19
+ @return $preset;
20
+ }
21
+ }
22
+
23
+ @function breakpointParses($queries) {
24
+ $output: ();
25
+
26
+ @each $query in $queries {
27
+ $output: append($output, breakpointParse($query))
28
+ }
29
+
30
+ @return $output;
31
+ }
32
+
33
+ @function breakpointParse($query) {
34
+ $ruleIndex: str-index($query, ' ');
35
+
36
+ @if ($ruleIndex == null) {
37
+ @return(
38
+ rule: $query,
39
+ condition: null
40
+ );
41
+ } @else {
42
+ @return (
43
+ rule: str-slice($query, 1, $ruleIndex - 1),
44
+ condition: str-slice($query, $ruleIndex + 1)
45
+ );
46
+ }
47
+ }
48
+
49
+ // Source: https://kittygiraudel.com/2013/08/08/advanced-sass-list-functions/
50
+ // @author Kitty Giraudel
51
+ @function breakpointRemoveNth($list, $index) {
52
+ $result: null;
53
+
54
+ @if type-of($index) != number {
55
+ @warn "$index: #{quote($index)} is not a number for `remove-nth`.";
56
+ } @else if $index == 0 {
57
+ @warn "List index 0 must be a non-zero integer for `remove-nth`.";
58
+ } @else if abs($index) > length($list) {
59
+ @warn "List index is #{$index} but list is only #{length($list)} item long for `remove-nth`.";
60
+ } @else {
61
+ $result: ();
62
+ $index: if($index < 0, length($list) + $index + 1, $index);
63
+
64
+ @for $i from 1 through length($list) {
65
+ @if $i != $index {
66
+ $result: append($result, nth($list, $i));
67
+ }
68
+ }
69
+ }
70
+
71
+ @return $result;
72
+ }
73
+
74
+ @function breakpointMatchingRules($queries) {
75
+ $rule: (map-get(nth($queries, 1), 'rule'));
76
+
77
+ @each $query in $queries {
78
+ @if($rule != map-get($query, 'rule')) {
79
+ @return false;
80
+ }
81
+ }
82
+
83
+ @return true;
84
+ }
85
+
86
+ @function breakpointJoinQueries($queries) {
87
+ $condition: "";
88
+ $rule: null;
89
+
90
+ @for $i from 1 through length($queries) {
91
+ $query: nth($queries, $i);
92
+
93
+ @if($i == 1) {
94
+ $rule: map-get($query, 'rule');
95
+ }
96
+
97
+ @if(map-get($query, 'condition') != null) {
98
+ @if($i > 1) {
99
+ $condition: $condition + ", ";
100
+ }
101
+
102
+ $condition: $condition + map-get($query, "condition");
103
+ }
104
+ }
105
+
106
+ @return (rule: $rule, condition: $condition);
107
+ }
108
+
109
+ @mixin breakpointBuild($query) {
110
+ $rule: map-get($query, 'rule');
111
+ $condition: map-get($query, 'condition');
112
+
113
+ @if($rule == '@media') {
114
+ @media #{$condition} {
115
+ @content;
116
+ }
117
+ } @else if ($rule == '@supports') {
118
+ @supports #{$condition} {
119
+ @content;
120
+ }
121
+ } @else if ($rule == '@page') {
122
+ @if($condition == null) {
123
+ @page {
124
+ @content;
125
+ }
126
+ } @else {
127
+ @page #{$condition} {
128
+ @content;
129
+ }
130
+ }
131
+ } @else {
132
+ @error "Unknown rule.";
133
+ }
134
+ }
135
+
136
+ @mixin breakpointAny($queries...) {
137
+ $queries: breakpointParses(breakpointPresets($queries));
138
+
139
+ @if(breakpointMatchingRules($queries)) {
140
+ @include breakpointBuild(breakpointJoinQueries($queries)) {
141
+ @content;
142
+ }
143
+ } @else {
144
+ @each $query in $queries {
145
+ @include breakpointBuild($query) {
146
+ @content;
147
+ }
148
+ }
149
+ }
150
+ }
151
+
152
+ @mixin breakpointAll($queries...) {
153
+ @if (length($queries) >= 1) {
154
+ $query: nth($queries, 1);
155
+ $queries: breakpointRemoveNth($queries, 1);
156
+
157
+ $query: breakpointParse(breakpointPreset($query));
158
+
159
+ @include breakpointAll($queries...) {
160
+ @include breakpointBuild($query) {
161
+ @content;
162
+ }
163
+ }
164
+ } @else {
165
+ @content;
166
+ }
167
+ }
168
+
169
+ @mixin breakpointProp($property, $values) {
170
+ @each $bp, $value in $values {
171
+ @include breakpointAny($bp) {
172
+ #{$property}: $value;
173
+ }
174
+ }
175
+ }
@@ -1,81 +1,175 @@
1
- // Breakpoint default
2
- $breakpoint: () !default;
3
-
4
- // Breakpoint any
5
- // @param {List} ids - List of breakpoints
6
- // @content - Styles will be applied to breakpoint
7
- @mixin breakpointAny($ids...) {
8
- @include _breakpoint($ids, ', ', false) {
9
- @content;
10
- }
1
+ $breakpoints: () !default;
2
+
3
+ @function presets($queries) {
4
+ $output: ();
5
+
6
+ @each $query in $queries {
7
+ $output: append($output, preset($query))
8
+ }
9
+
10
+ @return $output;
11
+ }
12
+
13
+ @function preset($query) {
14
+ $preset: map-get($breakpoints, $query);
15
+
16
+ @if ($preset == null) {
17
+ @return $query;
18
+ } @else {
19
+ @return $preset;
20
+ }
21
+ }
22
+
23
+ @function parses($queries) {
24
+ $output: ();
25
+
26
+ @each $query in $queries {
27
+ $output: append($output, parse($query))
28
+ }
29
+
30
+ @return $output;
31
+ }
32
+
33
+ @function parse($query) {
34
+ $ruleIndex: str-index($query, ' ');
35
+
36
+ @if ($ruleIndex == null) {
37
+ @return(
38
+ rule: $query,
39
+ condition: null
40
+ );
41
+ } @else {
42
+ @return (
43
+ rule: str-slice($query, 1, $ruleIndex - 1),
44
+ condition: str-slice($query, $ruleIndex + 1)
45
+ );
46
+ }
47
+ }
48
+
49
+ // Source: https://kittygiraudel.com/2013/08/08/advanced-sass-list-functions/
50
+ // @author Kitty Giraudel
51
+ @function removeNth($list, $index) {
52
+ $result: null;
53
+
54
+ @if type-of($index) != number {
55
+ @warn "$index: #{quote($index)} is not a number for `remove-nth`.";
56
+ } @else if $index == 0 {
57
+ @warn "List index 0 must be a non-zero integer for `remove-nth`.";
58
+ } @else if abs($index) > length($list) {
59
+ @warn "List index is #{$index} but list is only #{length($list)} item long for `remove-nth`.";
60
+ } @else {
61
+ $result: ();
62
+ $index: if($index < 0, length($list) + $index + 1, $index);
63
+
64
+ @for $i from 1 through length($list) {
65
+ @if $i != $index {
66
+ $result: append($result, nth($list, $i));
67
+ }
68
+ }
69
+ }
70
+
71
+ @return $result;
11
72
  }
12
73
 
13
- // Breakpoint all
14
- // @param {List} ids - List of breakpoints
15
- // @content - Styles will be applied to breakpoint
16
- @mixin breakpointAll($ids...) {
17
- @include _breakpoint($ids, ' and ', true) {
18
- @content;
19
- }
74
+ @function matchingRules($queries) {
75
+ $rule: (map-get(nth($queries, 1), 'rule'));
76
+
77
+ @each $query in $queries {
78
+ @if($rule != map-get($query, 'rule')) {
79
+ @return false;
80
+ }
81
+ }
82
+
83
+ @return true;
20
84
  }
21
85
 
22
- // Breakpoint prop
23
- // @param {String} property - Targeted CSS property
24
- // @param {List|Map} values - List of values or map of breakpoint IDs with values.
25
- @mixin breakpointProp($property, $values) {
26
- $i: 1;
27
-
28
- @if(type-of($values) == 'list') {
29
- @each $value in $values {
30
- $ids: map-keys($breakpoints);
31
- $bp: nth($ids, $i);
32
- // content: "#{$bp}";
33
- @include breakpointAny($bp) {
34
- #{$property}: nth($values, $i);
35
- }
36
-
37
- $i: $i + 1;
38
- }
39
- } @else if(type-of($values) == 'map') {
40
- @each $bp, $value in $values {
41
- @include breakpointAny($bp) {
42
- #{$property}: $value;
43
- }
44
- }
45
- }
86
+ @function joinQueries($queries) {
87
+ $condition: "";
88
+ $rule: null;
89
+
90
+ @for $i from 1 through length($queries) {
91
+ $query: nth($queries, $i);
92
+
93
+ @if($i == 1) {
94
+ $rule: map-get($query, 'rule');
95
+ }
96
+
97
+ @if(map-get($query, 'condition') != null) {
98
+ @if($i > 1) {
99
+ $condition: $condition + ", ";
100
+ }
101
+
102
+ $condition: $condition + map-get($query, "condition");
103
+ }
104
+ }
105
+
106
+ @return (rule: $rule, condition: $condition);
46
107
  }
47
108
 
48
- // _Breakpoint
49
- // @param {List} ids - List of breakpoints
50
- // @param {String} glue - Glue to join together multiple breakpoints
51
- // @param {Boolean} stripAppendedMediaTypes - Remove media types from all but first breakpoint
52
- // @content - Styles will be applied to breakpoint
53
- @mixin _breakpoint($ids, $glue, $stripAppendedMediaTypes) {
54
- $output: "";
55
- $count: 0;
109
+ @mixin build($query) {
110
+ $rule: map-get($query, 'rule');
111
+ $condition: map-get($query, 'condition');
56
112
 
57
- @each $id in $ids {
58
- @if $count > 0 {
59
- $output: $output + $glue;
60
- }
113
+ @if($rule == '@media') {
114
+ @media #{$condition} {
115
+ @content;
116
+ }
117
+ } @else if ($rule == '@supports') {
118
+ @supports #{$condition} {
119
+ @content;
120
+ }
121
+ } @else if ($rule == '@page') {
122
+ @if($condition == null) {
123
+ @page {
124
+ @content;
125
+ }
126
+ } @else {
127
+ @page #{$condition} {
128
+ @content;
129
+ }
130
+ }
131
+ } @else {
132
+ @error "Unknown rule.";
133
+ }
134
+ }
61
135
 
62
- $query: map-get($breakpoints, $id);
136
+ @mixin any($queries...) {
137
+ $queries: parses(presets($queries));
63
138
 
64
- @if $query == null {
65
- $query: $id
66
- }
139
+ @if(matchingRules($queries)) {
140
+ @include build(joinQueries($queries)) {
141
+ @content;
142
+ }
143
+ } @else {
144
+ @each $query in $queries {
145
+ @include build($query) {
146
+ @content;
147
+ }
148
+ }
149
+ }
150
+ }
67
151
 
68
- @if $count > 0 and $stripAppendedMediaTypes {
69
- $index: str-index($query, '(');
70
- $query: str-slice($query, $index);
71
- }
152
+ @mixin all($queries...) {
153
+ @if (length($queries) >= 1) {
154
+ $query: nth($queries, 1);
155
+ $queries: removeNth($queries, 1);
72
156
 
157
+ $query: parse(preset($query));
73
158
 
74
- $output: $output + $query;
75
- $count: $count + 1;
76
- }
159
+ @include all($queries...) {
160
+ @include build($query) {
161
+ @content;
162
+ }
163
+ }
164
+ } @else {
165
+ @content;
166
+ }
167
+ }
77
168
 
78
- @media #{$output} {
79
- @content;
80
- }
169
+ @mixin prop($property, $values) {
170
+ @each $bp, $value in $values {
171
+ @include any($bp) {
172
+ #{$property}: $value;
173
+ }
174
+ }
81
175
  }
package/src/breakpoint.js CHANGED
@@ -1,122 +1,85 @@
1
- module.exports = class Breakpoint {
2
- /**
3
- * @constructor
4
- * @param {Array.<{id: String, query: String}>} breakpoints
5
- */
6
- constructor(breakpoints) {
7
- this.breakpoints = breakpoints;
8
- }
9
-
10
- /**
11
- * Any
12
- * @param {...String} ids
13
- * @return {Boolean|Null}
14
- */
15
- any(...ids) {
16
- let result;
17
-
18
- if(Array.isArray(ids)) {
19
- result = this._queryMultiple(ids);
20
- return (result.indexOf(true) > -1);
21
- } else if(typeof ids === 'string') {
22
- return !!this._findQuery(ids);
23
- } else {
24
- Breakpoint._warn('Unexpected "ids" format. Expecting a breakpoint id or array of ids.');
25
- return null;
26
- }
27
- }
28
-
29
- /**
30
- * All
31
- * @param {...String} ids
32
- * @return {Boolean|Null}
33
- */
34
- all(...ids) {
35
- let result;
36
-
37
- if(Array.isArray(ids)) {
38
- result = this._queryMultiple(ids, true);
39
-
40
- return (result.length && result.indexOf(false) === -1);
41
- } else if(typeof ids === 'string') {
42
- return !!this._findQuery(ids);
43
- } else {
44
- Breakpoint._warn('Unexpected "ids" format. Expecting a breakpoint id or array of ids.');
45
- return null;
46
- }
47
- }
48
-
49
- /**
50
- * Match multiple
51
- * @param {Array.<String>|String} ids
52
- * @param {Boolean} [singleQuery=false] singleQuery
53
- * @return {Array}
54
- */
55
- _queryMultiple(ids, singleQuery=false) {
56
- let results = [];
57
- let queryString = "";
58
-
59
- //Loop through array
60
- for(let i=0, len = ids.length; i<len; i++) {
61
- let query = this._findQuery(ids[i]);
62
-
63
- if(singleQuery) {
64
- if(i>0) {
65
- query = query.replace(/^[^(]+/, '');
66
- }
67
-
68
- if(query) {
69
- results.push(query);
70
- }
71
- } else {
72
- let result = Breakpoint.query(query);
73
-
74
- if(typeof result === 'boolean') {
75
- results.push(result);
76
- }
77
- }
78
- }
79
-
80
- if(singleQuery) {
81
- results = [Breakpoint.query(results.join(' and '))];
82
- }
83
-
84
- return results;
85
- }
86
-
87
- /**
88
- * Match
89
- * @param {Array.<String>|String} id
90
- * @return {String}
91
- */
92
- _findQuery(id) {
93
- let breakpoint;
94
-
95
- if(typeof this.breakpoints[id] === 'string') {
96
- return this.breakpoints[id];
97
- } else {
98
- return id;
99
- }
100
- }
101
-
102
- /**
103
- * Warn
104
- * @param {String} message
105
- */
106
- static _warn(message) {
107
- if(window.console === Object(window.console) && typeof window.console.warn === 'function') {
108
- window.console.warn(message);
109
- }
110
- }
111
-
112
- /**
113
- * Query
114
- * @param query
115
- * @return {Boolean}
116
- */
117
- static query(query) {
118
- if(typeof query === 'string') {
119
- return window.matchMedia(query).matches;
120
- }
121
- }
1
+ export default class Breakpoint {
2
+ constructor(breakpoints) {
3
+ this.breakpoints = breakpoints;
4
+ }
5
+
6
+ presets(queries) {
7
+ return queries.map((query) => {
8
+ return this.preset(query);
9
+ });
10
+ }
11
+
12
+ preset(query) {
13
+ if(typeof this.breakpoints[query] === 'string') {
14
+ return this.breakpoints[query];
15
+ } else {
16
+ return query;
17
+ }
18
+ }
19
+
20
+ parses(queries) {
21
+ return queries.map((query) => {
22
+ return this.parse(query);
23
+ });
24
+ }
25
+
26
+ parse(query) {
27
+ let ruleIndex = query.search(' ');
28
+
29
+ if(ruleIndex < 0) {
30
+ return {
31
+ rule: query,
32
+ condition: null
33
+ }
34
+ } else {
35
+ return {
36
+ rule: query.substring(0, ruleIndex),
37
+ condition: query.substring(ruleIndex + 1)
38
+ };
39
+ }
40
+ }
41
+
42
+ tests(queries) {
43
+ return queries.map((query) => {
44
+ return this.test(query);
45
+ })
46
+ }
47
+
48
+ test(query) {
49
+ switch(query.rule) {
50
+ case "@media":
51
+ if(window.matchMedia) {
52
+ return window.matchMedia(query.condition).matches
53
+ } else {
54
+ return false;
55
+ }
56
+ break;
57
+ case "@page":
58
+ // Javascript is never run on printed documents; @page always returns true
59
+ return true;
60
+ break;
61
+ case "@supports":
62
+ if(window.CSS && window.CSS.supports) {
63
+ return window.CSS.supports(query.condition);
64
+ } else {
65
+ return false;
66
+ }
67
+ break;
68
+ default:
69
+ throw new Error('Unknown rule');
70
+ break;
71
+ }
72
+ }
73
+
74
+ any(...queries) {
75
+ queries = this.parses(this.presets(queries));
76
+
77
+ return (this.tests(queries).indexOf(true) >= 0);
78
+ }
79
+
80
+ all(...queries) {
81
+ queries = this.parses(this.presets(queries));
82
+
83
+ return !(this.tests(queries).indexOf(false) >= 0);
84
+ }
122
85
  }