@magic/css 0.7.43 → 0.7.44

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.
package/README.md CHANGED
@@ -539,6 +539,10 @@ update dependencies
539
539
  * use single quotes in output css
540
540
  * add cli
541
541
 
542
- #### 0.7.44 - unreleased
542
+ #### 0.7.44
543
+ * css props can be arrays to provide css overloads `{ color: ['green', 'red'] }` turns into `color: green; color: red;`
544
+ * update dependencies
545
+
546
+ #### 0.7.45 - unreleased
543
547
  ...
544
548
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic/css",
3
- "version": "0.7.43",
3
+ "version": "0.7.44",
4
4
  "author": "Wizards & Witches",
5
5
  "description": "css in js",
6
6
  "license": "AGPL-3.0",
@@ -29,14 +29,12 @@ const recurseParse = (mod, opts) => {
29
29
  let children = []
30
30
  const props = {}
31
31
 
32
- // media and keyframes are special, they provide a full body of css rules
33
32
  if (parent.startsWith('@keyframes') || parent.startsWith('@media')) {
33
+ // media and keyframes are special, they provide a full body of css rules
34
34
  const i = parse(items)
35
35
  return [parent, i]
36
- }
37
-
38
- // handle font face declarations that are expected to expand
39
- if (parent.startsWith('@font-face')) {
36
+ } else if (parent.startsWith('@font-face')) {
37
+ // handle font face declarations that are expected to expand
40
38
  let i = items
41
39
  if (is.string(items.url)) {
42
40
  i = [items]
@@ -47,14 +45,18 @@ const recurseParse = (mod, opts) => {
47
45
  }
48
46
  }
49
47
 
50
- Object.entries(items).forEach(([name, item]) => {
51
- if (is.object(item)) {
52
- name = getSelector(parent, name)
53
- children = deep.merge(children, recurseParse([name, item], opts))
54
- } else {
55
- props[name] = item
56
- }
57
- })
48
+ if (is.objectNative(items)) {
49
+ Object.entries(items).forEach(([name, item]) => {
50
+ if (is.array(item)) {
51
+ props[name] = item
52
+ } else if (is.objectNative(item)) {
53
+ name = getSelector(parent, name)
54
+ children = deep.merge(children, recurseParse([name, item], opts))
55
+ } else {
56
+ props[name] = item
57
+ }
58
+ })
59
+ }
58
60
 
59
61
  if (is.empty(props)) {
60
62
  if (!is.empty(children)) {
@@ -102,12 +104,14 @@ const parse = (styles, opts = {}) => {
102
104
  styles = styles(opts)
103
105
  }
104
106
 
105
- // this might trigger additionally to the is.function if statement above
106
107
  if (is.array(styles)) {
108
+ // if styles are an array, map over the items
107
109
  styles = styles.map(s => recurseParse(s, opts))
108
- } else if (!is.array(styles) && is.objectNative(styles)) {
110
+ } else if (is.objectNative(styles)) {
111
+ // styles are an object
109
112
  styles = Object.entries(styles).map(s => recurseParse(s, opts))
110
113
  } else {
114
+ // unknown styles
111
115
  log.error('invalid styles received', styles)
112
116
  }
113
117
 
@@ -25,7 +25,7 @@ const stringify = async (styles, opts = {}) => {
25
25
 
26
26
  const parsed = parse(styles, opts)
27
27
 
28
- const stringified = recurseStringify(parsed, plugins.stringify)
28
+ const stringified = recurseStringify(parsed, plugins.stringify, opts)
29
29
 
30
30
  /*
31
31
  * test the resulting css using postcss and autoprefix vendors where needed.
@@ -1,8 +1,18 @@
1
1
  import cases from '@magic/cases'
2
+ import is from '@magic/types'
2
3
 
3
4
  export const stringifyProps = props =>
4
5
  Object.entries(props)
5
- .map(([k, v]) => `${cases.kebab(k)}: ${v};`)
6
+ .map(([k, v]) => {
7
+ const kebab = cases.kebab(k)
8
+
9
+ // handle css overloads (color: ['green', 'red'] turns into "color: green; color: red;")
10
+ if (is.array(v)) {
11
+ return v.map(o => `${kebab}: ${o};`).join(' ')
12
+ }
13
+
14
+ return `${kebab}: ${v};`
15
+ })
6
16
  .join(' ')
7
17
 
8
18
  export default stringifyProps
@@ -2,9 +2,10 @@ import is from '@magic/types'
2
2
 
3
3
  import stringifyProps from './props.mjs'
4
4
 
5
- export const recurseStringify = (res, plugins = []) => {
5
+ export const recurseStringify = (res, plugins = [], opts = {}) => {
6
6
  if (is.array(res)) {
7
7
  const [name, items] = res
8
+
8
9
  if (is.string(name)) {
9
10
  let result = ''
10
11
  Object.entries(plugins).forEach(([lookup, fn]) => {
@@ -12,14 +13,16 @@ export const recurseStringify = (res, plugins = []) => {
12
13
  result = fn({ name, items, plugins })
13
14
  }
14
15
  })
16
+
15
17
  if (result) {
16
18
  return result
17
19
  }
18
20
  }
19
21
 
20
- return res.map(r => recurseStringify(r, plugins)).join(' ')
22
+ return res.map(r => recurseStringify(r, plugins, opts)).join(' ')
21
23
  } else if (is.object(res)) {
22
- return `{ ${stringifyProps(res)} }\n`
24
+ const stringified = stringifyProps(res)
25
+ return `{ ${stringified} }\n`
23
26
  }
24
27
 
25
28
  return res