@symbo.ls/scratch 0.5.0 → 0.6.2

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