@symbo.ls/scratch 0.5.0 → 0.6.0

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@symbo.ls/scratch",
3
3
  "description": "Φ / CSS framework and methodology.",
4
4
  "author": "symbo.ls",
5
- "version": "0.5.0",
5
+ "version": "0.6.0",
6
6
  "files": [
7
7
  "src"
8
8
  ],
@@ -60,7 +60,7 @@ export const getColor = (value, key) => {
60
60
  } else rgb = val[tone].rgb
61
61
  }
62
62
  if (alpha) return `rgba(${rgb}, ${alpha})`
63
- return `rgb(${rgb})`
63
+ return CONFIG.useVariable ? `var(${val.var})` : `rgb(${rgb})`
64
64
  } else return CONFIG.useVariable ? `var(${val.var})` : val.value
65
65
  }
66
66
 
@@ -94,7 +94,7 @@ export const getMediaColor = (value, param) => {
94
94
  }
95
95
 
96
96
  export const setColor = (val, key, suffix) => {
97
- if (val.slice(0, 2) === '--') val = getColor(val.slice(2))
97
+ if (isString(val) && val.slice(0, 2) === '--') val = getColor(val.slice(2))
98
98
 
99
99
  if (isArray(val)) {
100
100
  return {
@@ -126,6 +126,8 @@ export const setColor = (val, key, suffix) => {
126
126
  }
127
127
 
128
128
  export const setGradient = (val, key, suffix) => {
129
+ if (isString(val) && val.slice(0, 2) === '--') val = getColor(val.slice(2))
130
+
129
131
  if (isArray(val)) {
130
132
  return {
131
133
  '@light': setGradient(val[0], key, 'light'),
@@ -1,19 +1,21 @@
1
1
  'use strict'
2
2
 
3
- import { getColor } from './color'
3
+ import { getColor, getMediaColor } from './color'
4
4
  import { CONFIG, CSS_VARS } from '../factory' // eslint-disable-line
5
5
 
6
6
  import {
7
7
  isObject,
8
8
  isString,
9
- isObjectLike
9
+ isObjectLike,
10
+ merge,
11
+ isArray
10
12
  } from '../utils'
11
13
 
12
14
  const ENV = process.env.NODE_ENV
13
15
 
14
16
  const setThemeValue = theme => {
15
17
  const value = {}
16
- const { state, variants, helpers, ...rest } = theme
18
+ const { state, media, helpers, ...rest } = theme
17
19
  const keys = Object.keys(rest)
18
20
  keys.map(key => {
19
21
  const conditions = ['color', 'Color', 'background', 'border']
@@ -30,6 +32,7 @@ const getThemeValue = theme => {
30
32
  }
31
33
 
32
34
  export const getTheme = (value, modifier) => {
35
+ if (CONFIG.useVariable) return getMediaTheme(value, modifier)
33
36
  const { THEME } = CONFIG
34
37
 
35
38
  if (isString(value)) {
@@ -44,9 +47,9 @@ export const getTheme = (value, modifier) => {
44
47
  if (isObjectLike(value) && value[1]) {
45
48
  const themeName = value[0]
46
49
  const subThemeName = value[1]
47
- const { helpers, variants, state } = THEME[themeName]
50
+ const { helpers, media, state } = THEME[themeName]
48
51
 
49
- if (variants && variants[subThemeName]) return getThemeValue(variants[subThemeName])
52
+ if (media && media[subThemeName]) return getThemeValue(media[subThemeName])
50
53
  if (helpers && helpers[subThemeName]) return getThemeValue(helpers[subThemeName])
51
54
  if (state && state[subThemeName]) return getThemeValue(state[subThemeName])
52
55
  } else if (isObject(value)) return setThemeValue(value)
@@ -74,7 +77,7 @@ const setPseudo = (theme, key, variant, themeValue) => {
74
77
  if (isObject(variant) && !variant.value) variant.value = result
75
78
  }
76
79
 
77
- const goThroughInteractiveStates = (theme, value) => {
80
+ const setSelectors = (theme, value) => {
78
81
  const { state } = theme
79
82
  if (!state) return
80
83
  const keys = Object.keys(state)
@@ -92,12 +95,12 @@ const setPrefersScheme = (theme, key, variant, themeValue) => {
92
95
  if (isObject(variant) && !variant.value) variant.value = result
93
96
  }
94
97
 
95
- const goThroughVariants = (theme, value) => {
96
- const { variants } = theme
97
- if (!variants) return
98
- const keys = Object.keys(variants)
98
+ const setMedia = (theme, value) => {
99
+ const { media } = theme
100
+ if (!media) return
101
+ const keys = Object.keys(media)
99
102
  keys.map(key => {
100
- const variant = variants[key]
103
+ const variant = media[key]
101
104
  if (key === 'dark' || key === 'light') setPrefersScheme(theme, key, variant, value)
102
105
  if (key === 'inverse') setInverseTheme(theme, variant, value)
103
106
  return theme
@@ -105,7 +108,7 @@ const goThroughVariants = (theme, value) => {
105
108
  return theme
106
109
  }
107
110
 
108
- const goThroughHelpers = (theme, value) => {
111
+ const setHelpers = (theme, value) => {
109
112
  const { helpers } = theme
110
113
  if (!helpers) return
111
114
  const keys = Object.keys(helpers)
@@ -119,13 +122,77 @@ const goThroughHelpers = (theme, value) => {
119
122
  }
120
123
 
121
124
  export const setTheme = (val, key) => {
122
- const { state, variants, helpers } = val
125
+ if (CONFIG.useVariable) return setMediaTheme(val, key)
126
+
127
+ const { state, media, helpers } = val
123
128
  const value = setThemeValue(val, key)
124
129
  const CSSvar = `--theme-${key}`
125
130
 
126
- goThroughInteractiveStates(val, value)
127
- goThroughVariants(val, value)
128
- goThroughHelpers(val, value)
131
+ setSelectors(val, value)
132
+ setMedia(val, value)
133
+ setHelpers(val, value)
134
+
135
+ return { var: CSSvar, value, state, media, helpers }
136
+ }
137
+
138
+ const keySetters = { // eslint-disable-line
139
+ '@': (theme, value) => setMedia(theme, value),
140
+ ':': (theme, value) => setSelectors(theme, value),
141
+ '.': (theme, value) => setHelpers(theme, value)
142
+ }
143
+
144
+ export const setMediaTheme = (val, key, suffix, prefers) => {
145
+ const theme = {}
146
+
147
+ if (isObject(val)) {
148
+ for (const param in val) {
149
+ const symb = param.slice(0, 1)
150
+ const value = val[param]
151
+ if (symb === '@' || symb === ':' || symb === '.') {
152
+ const hasPrefers = symb === '@' && param
153
+ theme[param] = setMediaTheme(value, key, param, prefers || hasPrefers)
154
+ } else {
155
+ const color = getColor(value, prefers)
156
+ const metaSuffixes = [...new Set([prefers, suffix].filter(v => v).map(v => v.slice(1)))]
157
+ const varmetaSuffixName = metaSuffixes.length ? '-' + metaSuffixes.join('-') : ''
158
+ const CSSVar = `--theme-${key}${varmetaSuffixName}-${param}`
159
+ if (CONFIG.useVariable) {
160
+ CSS_VARS[CSSVar] = color
161
+ theme[param] = `var(${CSSVar})`
162
+ } else {
163
+ theme[param] = color
164
+ }
165
+ }
166
+ }
167
+ }
129
168
 
130
- return { var: CSSvar, value, state, variants, helpers }
169
+ return theme
170
+ }
171
+
172
+ const recursiveTheme = val => {
173
+ const obj = {}
174
+ for (const param in val) {
175
+ const symb = param.slice(0, 1)
176
+ if (isObject(val[param])) {
177
+ if (symb === '@') {
178
+ const query = CONFIG.MEDIA[param.slice(1)]
179
+ const media = `@media screen and ${query}`
180
+ obj[media] = recursiveTheme(val[param])
181
+ } else if (symb === ':') {
182
+ obj[`&${param}`] = recursiveTheme(val[param])
183
+ }
184
+ } else obj[param] = val[param]
185
+ }
186
+ return obj
187
+ }
188
+
189
+ export const getMediaTheme = (val, key, themeObj) => {
190
+ if (!isString(val)) {
191
+ if ((ENV === 'test' || ENV === 'development') && CONFIG.verbose) console.warn(val, '- type for color is not valid')
192
+ return
193
+ }
194
+ const [name, modifier] = isArray(val) ? val : val.split(' ')
195
+ let value = CONFIG.THEME[name]
196
+ if (modifier && value[modifier]) value = value[modifier]
197
+ return recursiveTheme(value)
131
198
  }