Skip to content

Commit 81ec271

Browse files
author
streamich
committed
added media queries
1 parent 91235f4 commit 81ec271

File tree

8 files changed

+154
-27
lines changed

8 files changed

+154
-27
lines changed

README.md

+36-14
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,43 @@ console.log(css.atoms);
193193

194194
Output:
195195

196-
{ d: 'display',
196+
{ d: 'display',
197197
mar: 'margin',
198-
pad: 'padding'
199-
bd: 'border',
200-
col: 'color',
198+
pad: 'padding',
199+
bd: 'border',
200+
col: 'color',
201201
op: 'opacity',
202-
bg: 'backgroun
203-
fz: 'font-size
204-
fs: 'font-styl
205-
bxz: 'box-sizi
202+
bg: 'background',
203+
fz: 'font-size',
204+
fs: 'font-style',
205+
lh: 'line-height',
206+
bxz: 'box-sizing',
206207
cur: 'cursor',
207-
ov: 'overflow'
208-
pos: 'position
209-
ls: 'list-styl
210-
td: 'text-deco
208+
ov: 'overflow',
209+
pos: 'position',
210+
ls: 'list-style',
211+
ta: 'text-align',
212+
td: 'text-decoration',
211213
fl: 'float',
212-
w: 'width',
213-
h: 'height' }
214+
w: 'width',
215+
h: 'height' }
216+
217+
218+
## Media Queries
219+
220+
```js
221+
// ## Media queries
222+
console.log(css.css({
223+
'@media (max-width: 600px)': {
224+
'.facet_sidebar': {
225+
display: 'none'
226+
}
227+
}
228+
}));
229+
```
230+
231+
Output:
232+
233+
@media (max-width: 600px){
234+
.facet_sidebar{display:none}
235+
}

index.js

+19-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ function toBlocks(pojo) {
4040
for (var selector in pojo) {
4141
if (pojo.hasOwnProperty(selector)) {
4242
(function process_block(styles) {
43+
// `@media` query
44+
if (selector[0] === '@') {
45+
blocks.push([selector, toBlocks(styles)]);
46+
return;
47+
}
4348
if (!(styles instanceof Array))
4449
styles = [styles];
4550
var tmp = {};
@@ -93,14 +98,23 @@ function toBlocks(pojo) {
9398
return blocks;
9499
}
95100
exports.toBlocks = toBlocks;
96-
function css(pojo) {
97-
var blocks = toBlocks(pojo);
101+
function concat(blocks) {
98102
var blockstrs = [];
99103
for (var i = 0; i < blocks.length; i++) {
100-
if (blocks[i][1].length)
101-
blockstrs.push(blocks[i][0] + '{' + blocks[i][1].join(';') + '}');
104+
if (blocks[i][1].length) {
105+
if (typeof blocks[i][1][0] === 'string') {
106+
blockstrs.push(blocks[i][0] + '{' + blocks[i][1].join(';') + '}');
107+
}
108+
else {
109+
blockstrs.push(blocks[i][0] + '{' + concat(blocks[i][1]) + '}');
110+
}
111+
}
102112
}
103-
return blockstrs.join('\n');
113+
return blockstrs.join('');
114+
}
115+
function css(pojo) {
116+
var blocks = toBlocks(pojo);
117+
return concat(blocks);
104118
}
105119
exports.css = css;
106120
// Inject CSS into DOM

index.ts

+30-7
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,24 @@ export var atoms = {
3131
};
3232

3333

34-
export function toBlocks(pojo): [string, string[]][] {
34+
export type Tselector = string;
35+
export type Tstyles = string[];
36+
export type Tblock = [Tselector, Tstyles];
37+
export type Tquery = string;
38+
export type Tmediablock = [Tquery, Tblock[]];
39+
40+
41+
export function toBlocks(pojo): (Tblock|Tmediablock)[] {
3542
var blocks = [];
3643

3744
for(var selector in pojo) { if(pojo.hasOwnProperty(selector)) { (function process_block(styles) {
45+
46+
// `@media` query
47+
if(selector[0] === '@') {
48+
blocks.push([selector, toBlocks(styles)]);
49+
return;
50+
}
51+
3852
if(!(styles instanceof Array)) styles = [styles];
3953

4054
var tmp: any = {};
@@ -80,15 +94,24 @@ export function toBlocks(pojo): [string, string[]][] {
8094
}
8195

8296

83-
export function css(pojo): string {
84-
var blocks = toBlocks(pojo);
85-
97+
function concat(blocks) {
8698
var blockstrs = [];
8799
for(var i = 0; i < blocks.length; i++) {
88-
if(blocks[i][1].length)
89-
blockstrs.push(blocks[i][0] + '{' + blocks[i][1].join(';') + '}');
100+
if(blocks[i][1].length) {
101+
if(typeof blocks[i][1][0] === 'string') { // `Tblock`
102+
blockstrs.push(blocks[i][0] + '{' + blocks[i][1].join(';') + '}');
103+
} else { // `Tmediablock`
104+
blockstrs.push(blocks[i][0] + '{' + concat(blocks[i][1]) + '}');
105+
}
106+
}
90107
}
91-
return blockstrs.join('\n');
108+
return blockstrs.join('');
109+
}
110+
111+
112+
export function css(pojo): string {
113+
var blocks = toBlocks(pojo);
114+
return concat(blocks);
92115
}
93116

94117
// Inject CSS into DOM

media.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var css = require('./index');
2+
// ## Media queries
3+
var pojo = {
4+
h1: {
5+
td: 'none'
6+
},
7+
'@media (max-width: 600px)': {
8+
'.facet_sidebar': {
9+
display: 'none'
10+
},
11+
h1: {
12+
color: 'red',
13+
span: {
14+
color: 'green'
15+
}
16+
}
17+
}
18+
};
19+
var bl = css.toBlocks(pojo);
20+
console.log(bl);
21+
console.log(bl[0][1]);
22+
console.log(css.css(pojo));

media.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
declare var require: any;
2+
var css = require('./index');
3+
4+
5+
// ## Media queries
6+
var pojo = {
7+
h1: {
8+
td: 'none',
9+
},
10+
'@media (max-width: 600px)': {
11+
'.facet_sidebar': {
12+
display: 'none'
13+
},
14+
h1: {
15+
color: 'red',
16+
span: {
17+
color: 'green',
18+
}
19+
}
20+
}
21+
};
22+
var bl = css.toBlocks(pojo);
23+
24+
console.log(bl);
25+
console.log(bl[0][1]);
26+
console.log(css.css(pojo));
27+

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "css-light",
33
"description": "Write CSS in JavaScript",
4-
"version": "1.0.3",
4+
"version": "1.0.4",
55
"keywords": [
66
"css",
77
"javascript",

test.js

+9
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,12 @@ console.log(css.css({
121121
}));
122122
// div{padding:0 0 0 0;padding:0 0 0 0}
123123
console.log(css.atoms);
124+
// ## Media queries
125+
console.log(css.css({
126+
'@media (max-width: 600px)': {
127+
'.facet_sidebar': {
128+
display: 'none'
129+
}
130+
}
131+
}));
132+
// @media (max-width: 600px){.facet_sidebar{display:none}}

test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,13 @@ console.log(css.css({
141141
// div{padding:0 0 0 0;padding:0 0 0 0}
142142
console.log(css.atoms);
143143

144+
145+
// ## Media queries
146+
console.log(css.css({
147+
'@media (max-width: 600px)': {
148+
'.facet_sidebar': {
149+
display: 'none'
150+
}
151+
}
152+
}));
153+
// @media (max-width: 600px){.facet_sidebar{display:none}}

0 commit comments

Comments
 (0)