@symbo.ls/scratch 0.3.35 → 0.4.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.3.35",
5
+ "version": "0.4.2",
6
6
  "files": [
7
7
  "src"
8
8
  ],
@@ -9,5 +9,5 @@ export const applyDocument = () => merge(DOCUMENT, {
9
9
  theme: THEME.document,
10
10
  fontFamily: getDefaultOrFirstKey(FONT_FAMILY),
11
11
  fontSize: TYPOGRAPHY.base,
12
- lineHeight: TYPOGRAPHY.styles.lineHeight
12
+ lineHeight: TYPOGRAPHY.lineHeight
13
13
  })
@@ -1,5 +1,7 @@
1
1
  'use strict'
2
2
 
3
+ import { CSS_VARS } from '../factory'
4
+
3
5
  export * from './sequence'
4
6
  export * from './unit'
5
7
  export * from './typography'
@@ -13,4 +15,5 @@ export * from './icons'
13
15
  export * from './timing'
14
16
  export * from './document'
15
17
  export * from './cases'
18
+
16
19
  export const RESET = {}
@@ -1,6 +1,8 @@
1
1
  'use strict'
2
2
 
3
3
  export const MEDIA = {
4
+ tv: '(min-width: 2780px)',
5
+
4
6
  screenL: '(max-width: 1920px)',
5
7
  screenM: '(max-width: 1680px)',
6
8
  screenS: '(max-width: 1440px)',
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  import { SEQUENCE, TYPOGRAPHY } from '.'
4
- import { Arrayize, fallBack, generateSequence } from '../utils'
4
+ import { Arrayize, getSequenceValue, generateSequence, applySequenceVars, merge } from '../utils'
5
5
 
6
6
  const defaultProps = {
7
7
  base: TYPOGRAPHY.base,
@@ -9,12 +9,44 @@ const defaultProps = {
9
9
  ratio: SEQUENCE.phi,
10
10
  range: [-5, +15],
11
11
  subSequence: true,
12
+ unit: 'em',
12
13
  sequence: {},
13
- scales: {}
14
+ scales: {},
15
+ vars: {}
16
+ }
17
+
18
+ const runThroughMedia = props => {
19
+ for (const prop in props) {
20
+ const mediaProps = props[prop]
21
+ if (prop.slice(0, 1) === '@') {
22
+ const { type, base, ratio, range, subSequence, h1Matches, unit } = props
23
+
24
+ merge(mediaProps, {
25
+ type,
26
+ base,
27
+ ratio,
28
+ range,
29
+ subSequence,
30
+ h1Matches,
31
+ unit,
32
+ sequence: {},
33
+ scales: {},
34
+ styles: {},
35
+ vars: {}
36
+ })
37
+
38
+ generateSequence(mediaProps)
39
+
40
+ const mediaName = prop.slice(1)
41
+ applySequenceVars(mediaProps, mediaName)
42
+ }
43
+ }
14
44
  }
15
45
 
16
46
  export const applySpacingSequence = () => {
17
47
  generateSequence(defaultProps)
48
+ applySequenceVars(defaultProps)
49
+ runThroughMedia(defaultProps)
18
50
  }
19
51
 
20
52
  const getSequence = (props) => {
@@ -23,7 +55,7 @@ const getSequence = (props) => {
23
55
  return hasGenerated ? props : generateSequence(props)
24
56
  }
25
57
 
26
- export const mapSpacing = (val, property = 'padding', props, unit) => {
58
+ export const getSpacingByKey = (val, property = 'padding', props, unit) => {
27
59
  const prefix = '--spacing-'
28
60
 
29
61
  const generatedSequence = getSequence(props)
@@ -34,7 +66,7 @@ export const mapSpacing = (val, property = 'padding', props, unit) => {
34
66
 
35
67
  const length = stack.length
36
68
 
37
- const wrapFallBack = (prop, i) => fallBack({
69
+ const wrapSequenceItem = (prop, i) => getSequenceValue({
38
70
  type,
39
71
  prop,
40
72
  val: stack[i],
@@ -50,26 +82,26 @@ export const mapSpacing = (val, property = 'padding', props, unit) => {
50
82
 
51
83
  if (length === 2) {
52
84
  return [
53
- wrapFallBack(property + 'Block' + suffix, 0),
54
- wrapFallBack(property + 'Inline' + suffix, 1)
85
+ wrapSequenceItem(property + 'Block' + suffix, 0),
86
+ wrapSequenceItem(property + 'Inline' + suffix, 1)
55
87
  ]
56
88
  }
57
89
  if (length === 3) {
58
90
  return [
59
- wrapFallBack(property + 'BlockStart' + suffix, 0),
60
- wrapFallBack(property + 'Inline' + suffix, 1),
61
- wrapFallBack(property + 'BlockEnd' + suffix, 2)
91
+ wrapSequenceItem(property + 'BlockStart' + suffix, 0),
92
+ wrapSequenceItem(property + 'Inline' + suffix, 1),
93
+ wrapSequenceItem(property + 'BlockEnd' + suffix, 2)
62
94
  ]
63
95
  } else if (length === 4) {
64
96
  return [
65
- wrapFallBack(property + 'BlockStart' + suffix, 0),
66
- wrapFallBack(property + 'InlineStart' + suffix, 3),
67
- wrapFallBack(property + 'BlockEnd' + suffix, 2),
68
- wrapFallBack(property + 'InlineEnd' + suffix, 1)
97
+ wrapSequenceItem(property + 'BlockStart' + suffix, 0),
98
+ wrapSequenceItem(property + 'InlineStart' + suffix, 3),
99
+ wrapSequenceItem(property + 'BlockEnd' + suffix, 2),
100
+ wrapSequenceItem(property + 'InlineEnd' + suffix, 1)
69
101
  ]
70
102
  }
71
103
 
72
- return fallBack({ type, prop: property, val, prefix })
104
+ return getSequenceValue({ type, prop: property, val, prefix, unit })
73
105
  }
74
106
 
75
107
  export const SPACING = defaultProps
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  import { SEQUENCE } from '.'
4
- import { fallBack, generateSequence } from '../utils'
4
+ import { applySequenceVars, generateSequence, getSequenceValue } from '../utils'
5
5
 
6
6
  const defaultProps = {
7
7
  default: 150,
@@ -9,15 +9,18 @@ const defaultProps = {
9
9
  type: 'duration',
10
10
  ratio: SEQUENCE['perfect-fourth'],
11
11
  range: [-3, +12],
12
+ unit: 'ms',
12
13
  sequence: {},
13
- scales: {}
14
+ scales: {},
15
+ vars: {}
14
16
  }
15
17
 
16
18
  export const applyTimingSequence = () => {
17
19
  generateSequence(defaultProps)
20
+ applySequenceVars(defaultProps)
18
21
  }
19
22
 
20
- export const mapTiming = val => fallBack({
23
+ export const getTimingByKey = val => getSequenceValue({
21
24
  type: defaultProps.sequence,
22
25
  prop: 'duration',
23
26
  val,
@@ -1,40 +1,82 @@
1
1
  'use strict'
2
2
 
3
- import { SEQUENCE } from '.'
4
- import { fallBack, generateSequence, findHeadings } from '../utils'
3
+ import { CONFIG } from '../factory'
4
+ import { SEQUENCE, MEDIA } from '.'
5
+ import { getSequenceValue, generateSequence, findHeadings, merge, applySequenceVars } from '../utils'
5
6
 
6
7
  const defaultProps = {
7
- default: 16,
8
+ browserDefault: 16,
8
9
  base: 16,
9
10
  type: 'font-size',
10
11
  ratio: SEQUENCE['minor-third'],
11
12
  range: [-3, +12],
12
13
  h1Matches: +6,
13
- styles: {
14
- lineHeight: 1.5
15
- },
14
+ lineHeight: 1.5,
15
+ unit: 'em',
16
+ styles: {},
16
17
  sequence: {},
17
- scales: {}
18
+ scales: {},
19
+ vars: {}
18
20
  }
19
21
 
20
- export const applyTypographySequence = () => {
21
- generateSequence(defaultProps)
22
+ const runThroughMedia = props => {
23
+ for (const prop in props) {
24
+ const mediaProps = props[prop]
25
+ if (prop.slice(0, 1) === '@') {
26
+ const { type, base, ratio, range, subSequence, h1Matches, unit } = props
27
+
28
+ merge(mediaProps, {
29
+ type,
30
+ base,
31
+ ratio,
32
+ range,
33
+ subSequence,
34
+ h1Matches,
35
+ unit,
36
+ sequence: {},
37
+ scales: {},
38
+ styles: {},
39
+ vars: {}
40
+ })
41
+
42
+ generateSequence(mediaProps)
43
+
44
+ const mediaName = prop.slice(1)
45
+ applySequenceVars(mediaProps, mediaName)
46
+
47
+ const query = MEDIA[mediaName]
48
+ defaultProps.styles[`@media screen and ${query}`] = {
49
+ fontSize: mediaProps.base / defaultProps.browserDefault + unit
50
+ }
51
+ }
52
+ }
53
+ }
22
54
 
23
- if (defaultProps.h1Matches) {
24
- const HEADINGS = findHeadings(defaultProps)
25
- const { styles } = defaultProps
55
+ const applyHeadings = (props) => {
56
+ if (props.h1Matches) {
57
+ const unit = props.unit
58
+ const HEADINGS = findHeadings(props)
59
+ const { styles } = props
26
60
  for (const k in HEADINGS) {
27
61
  styles[`h${parseInt(k) + 1}`] = {
28
- fontSize: `${HEADINGS[k].scaling}em`
62
+ fontSize: CONFIG.useVariable ? `var(${HEADINGS[k].variable})` : `${HEADINGS[k].scaling}${unit}`
29
63
  }
30
64
  }
31
65
  }
32
66
  }
33
67
 
34
- export const mapFontSize = val => fallBack({
68
+ export const applyTypographySequence = () => {
69
+ generateSequence(defaultProps)
70
+ applyHeadings(defaultProps)
71
+ applySequenceVars(defaultProps)
72
+ runThroughMedia(defaultProps)
73
+ }
74
+
75
+ export const getFontSizeByKey = val => getSequenceValue({
35
76
  type: defaultProps.sequence,
36
77
  prop: 'fontSize',
37
78
  val,
79
+ unit: defaultProps.unit,
38
80
  prefix: '--font-size-'
39
81
  })
40
82
 
package/src/factory.js CHANGED
@@ -5,10 +5,8 @@ import * as CONF from './config'
5
5
  export const CSS_VARS = {}
6
6
  export const CONFIG = {
7
7
  verbose: false,
8
- useVariable: false,
8
+ useVariable: true,
9
9
  useReset: true,
10
10
  VARS: CSS_VARS,
11
11
  ...CONF
12
12
  }
13
-
14
-
package/src/reset.js CHANGED
@@ -1,11 +1,14 @@
1
1
  'use strict'
2
2
 
3
3
  import * as CONFIG from './config'
4
+ import { CSS_VARS } from './factory'
4
5
  import { getTheme } from './system'
5
6
  import { deepMerge, merge } from './utils'
6
7
 
7
8
  export const applyReset = (reset = {}) => {
8
9
  return deepMerge(merge(CONFIG.RESET, reset), {
10
+ ':root': CSS_VARS,
11
+
9
12
  html: {
10
13
  position: 'absolute',
11
14
  overflow: 'hidden',
@@ -18,8 +21,9 @@ export const applyReset = (reset = {}) => {
18
21
  transform: 'translate3d(0, 0, 1px)',
19
22
  scrollBehavior: 'smooth',
20
23
 
24
+ fontSize: CONFIG.TYPOGRAPHY.browserDefault + 'px',
25
+
21
26
  fontFamily: CONFIG.DOCUMENT.fontFamily,
22
- fontSize: CONFIG.DOCUMENT.fontSize / CONFIG.TYPOGRAPHY.default + CONFIG.UNIT.default,
23
27
  lineHeight: CONFIG.DOCUMENT.lineHeight
24
28
  },
25
29
 
@@ -29,10 +33,12 @@ export const applyReset = (reset = {}) => {
29
33
  margin: 0,
30
34
  fontFamily: CONFIG.DOCUMENT.fontFamily,
31
35
 
32
- ...getTheme('document')
33
- },
36
+ ...getTheme('document'),
34
37
 
35
- ...CONFIG.TYPOGRAPHY.styles,
38
+ fontSize: CONFIG.TYPOGRAPHY.base / CONFIG.TYPOGRAPHY.browserDefault + CONFIG.UNIT.default,
39
+
40
+ ...CONFIG.TYPOGRAPHY.styles
41
+ },
36
42
 
37
43
  // form elements
38
44
  fieldset: {
package/src/set.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  import { applyDocument, applySpacingSequence, applyTimingSequence, applyTypographySequence } from './config'
4
- import { CONFIG, CSS_VARS } from './factory'
4
+ import { CONFIG, CSS_VARS } from './factory' // eslint-disable-line no-unused-vars
5
5
  import { applyReset } from './reset'
6
6
  import { setColor, setGradient, setFont, setFontFamily, setTheme } from './system'
7
7
  import {
@@ -30,7 +30,8 @@ export const SETTERS = {
30
30
  media: setSameValue,
31
31
  timing: setSameValue,
32
32
  icons: setSameValue,
33
- reset: setSameValue
33
+ reset: setSameValue,
34
+ unit: setSameValue
34
35
  }
35
36
 
36
37
  /**
@@ -15,8 +15,9 @@ export const setFont = (val, key) => {
15
15
  return { var: CSSvar, value: val, fontFace }
16
16
  }
17
17
 
18
- export const getFontFamily = (LIBRARY, key) => {
19
- return getDefaultOrFirstKey(LIBRARY, key)
18
+ export const getFontFamily = key => {
19
+ const { FONT_FAMILY } = CONFIG
20
+ return getDefaultOrFirstKey(FONT_FAMILY, key)
20
21
  }
21
22
 
22
23
  export const setFontFamily = (val, key) => {
@@ -88,7 +88,6 @@ const goThroughInteractiveStates = (theme, value) => {
88
88
 
89
89
  const setPrefersScheme = (theme, key, variant, themeValue) => {
90
90
  const result = getTheme(variant)
91
- // console.log(variant)
92
91
  themeValue[`@media (prefers-color-scheme: ${key})`] = result
93
92
  if (isObject(variant) && !variant.value) variant.value = result
94
93
  }
@@ -99,8 +98,6 @@ const goThroughVariants = (theme, value) => {
99
98
  const keys = Object.keys(variants)
100
99
  keys.map(key => {
101
100
  const variant = variants[key]
102
- // console.log('=========')
103
- // console.log(theme, key, variant, value)
104
101
  if (key === 'dark' || key === 'light') setPrefersScheme(theme, key, variant, value)
105
102
  if (key === 'inverse') setInverseTheme(theme, variant, value)
106
103
  return theme
@@ -4,4 +4,4 @@ export * from './object'
4
4
  export * from './color'
5
5
  export * from './font'
6
6
  export * from './sequence'
7
- export * from './setVariables'
7
+ export * from './var'
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  import { UNIT } from '../config'
4
+ import { CONFIG } from '../factory'
4
5
 
5
6
  export const numToLetterMap = {
6
7
  '-6': 'U',
@@ -32,14 +33,65 @@ export const numToLetterMap = {
32
33
  }
33
34
 
34
35
  const setSequenceValue = ({ key, variable, value, scaling, state, index }) => {
35
- state.sequence[variable] = {
36
+ state.sequence[key] = {
36
37
  key,
37
38
  decimal: Math.round(value * 100) / 100,
38
39
  val: Math.round(value),
39
40
  scaling,
40
- index
41
+ index,
42
+ variable
41
43
  }
42
- state.scales[variable] = scaling
44
+ state.scales[key] = scaling
45
+ state.vars[variable] = scaling + state.unit
46
+ }
47
+
48
+ export const getSequenceValue = ({ type, prop, val = 'A', prefix = '--font-size-', unit = UNIT.default }) => {
49
+ if (typeof val !== 'string') console.warn(prop, val, 'is not a string')
50
+
51
+ if (val === '-' || val === '') return ({ })
52
+ if (
53
+ val === 'none' ||
54
+ val === 'auto' ||
55
+ val === 'fit-content' ||
56
+ val === 'min-content' ||
57
+ val === 'max-content'
58
+ ) return ({ [prop]: val })
59
+
60
+ const startsWithLetterRegex = /^-?[a-zA-Z]/i
61
+ const startsWithLetter = startsWithLetterRegex.test(val)
62
+ if (!startsWithLetter) return ({ [prop]: val })
63
+
64
+ const letterVal = val.toUpperCase()
65
+ const isNegative = letterVal.slice(0, 1) === '-' ? '-' : ''
66
+ let pureVal = isNegative ? letterVal.slice(1) : letterVal
67
+
68
+ let mediaName = ''
69
+ if (pureVal.includes('-')) {
70
+ console.log(pureVal)
71
+ mediaName = '-' + pureVal.split('-')[1].toLowerCase()
72
+ pureVal = pureVal.split('-')[0]
73
+ }
74
+
75
+ const value = type ? type[pureVal] : null
76
+ if (!value) return console.warn('can\'t find', type, pureVal)
77
+
78
+ if (CONFIG.useVariable) {
79
+ const varVal = `var(${prefix}${pureVal}${mediaName})`
80
+ return isNegative ? {
81
+ [prop]: `calc(${varVal} * -1)`
82
+ } : {
83
+ [prop]: varVal
84
+ }
85
+ }
86
+
87
+ if (unit === 'ms' || unit === 's') {
88
+ return ({ [prop]: isNegative + value.val + unit })
89
+ }
90
+
91
+ return ({
92
+ [prop]: isNegative + value.val + value.unit,
93
+ [prop]: isNegative + value.scaling + unit
94
+ })
43
95
  }
44
96
 
45
97
  export const generateSubSequence = ({ key, base, value, ratio, variable, state, index }) => {
@@ -56,8 +108,6 @@ export const generateSubSequence = ({ key, base, value, ratio, variable, state,
56
108
  const middle = (first + second) / 2
57
109
  if (diffRounded > 100) arr = [first, middle, second]
58
110
  else arr = [first, second]
59
- // else if (diffRounded > 2) arr = [first, second]
60
- // else if (diffRounded > 1) arr = [middle]
61
111
 
62
112
  arr.map((v, k) => {
63
113
  const scaling = Math.round(v / base * 1000) / 1000
@@ -70,6 +120,7 @@ export const generateSubSequence = ({ key, base, value, ratio, variable, state,
70
120
  export const generateSequence = ({ type, base, ratio, range, subSequence, ...state }) => {
71
121
  const n = Math.abs(range[0]) + Math.abs(range[1])
72
122
  const prefix = '--' + type + '-'
123
+
73
124
  for (let i = 0; i <= n; i++) {
74
125
  const key = range[1] - i
75
126
  const letterKey = numToLetterMap[key]
@@ -84,38 +135,10 @@ export const generateSequence = ({ type, base, ratio, range, subSequence, ...sta
84
135
  return state
85
136
  }
86
137
 
87
- export const fallBack = ({ type, prop, val = 'A', prefix = '--font-size-', unit = UNIT.default }) => {
88
- if (typeof val !== 'string') console.warn(prop, val, 'is not a string')
89
-
90
- if (val === '-' || val === '') return ({ })
91
- if (val === 'none' || val === 'auto' || val === 'fit-content' || val === 'min-content' || val === 'max-content') return ({ [prop]: val })
92
-
93
- // const startsWithLetterRegex = /^[a-zA-Z]/i
94
- const startsWithLetterRegex = /^-?[a-zA-Z]/i
95
- // const hasLetter = /[A-Za-z]+/.test(val)
96
- const startsWithLetter = startsWithLetterRegex.test(val)
97
- if (!startsWithLetter) return ({ [prop]: val })
98
-
99
- const letterVal = val.toUpperCase()
100
- const isNegative = letterVal.slice(0, 1) === '-' ? '-' : ''
101
- const simplyLetterVal = isNegative ? letterVal.slice(1) : letterVal
102
-
103
- const value = type ? type[prefix + simplyLetterVal] : null
104
- if (!value) return console.warn('can\'t find', type, prefix + simplyLetterVal, simplyLetterVal)
105
-
106
- if (unit === 'ms' || unit === 's') {
107
- return ({ [prop]: isNegative + value.val + unit })
108
- }
109
- return ({
110
- [prop]: isNegative + value.val + value.unit,
111
- [prop]: isNegative + value.scaling + 'em'
112
- })
113
- }
114
-
115
- export const findHeadings = (TYPOGRAPHY) => {
116
- const { h1Matches, type, sequence } = TYPOGRAPHY
138
+ export const findHeadings = props => {
139
+ const { h1Matches, sequence } = props
117
140
  return new Array(6).fill(null).map((_, i) => {
118
141
  const findLetter = numToLetterMap[h1Matches - i]
119
- return sequence[`--${type}-${findLetter}`]
142
+ return sequence[findLetter]
120
143
  })
121
144
  }
@@ -0,0 +1,40 @@
1
+ 'use strict'
2
+
3
+ import { MEDIA } from '../config'
4
+ import { CONFIG, CSS_VARS } from '../factory'
5
+ import { isObjectLike } from './object'
6
+
7
+ export const setVariables = (result, key) => {
8
+ // CSS_VARS[result.var] =
9
+ if (isObjectLike(result.value)) {
10
+ // console.group(key)
11
+ // for (const key in result.value) {
12
+ // console.log(key, result.value[key])
13
+ // }
14
+ // console.log(result)
15
+ // console.groupEnd(key)
16
+ } else {
17
+ CSS_VARS[result.var] = result.value
18
+ }
19
+ }
20
+
21
+ export const applySequenceVars = (props, mediaName) => {
22
+ const unit = props.unit || CONFIG.UNIT.default
23
+ const { sequence, scales } = props
24
+
25
+ for (const key in sequence) {
26
+ const item = sequence[key]
27
+ const value = (props.type === 'duration' ? sequence[key].val : scales[key]) + unit
28
+ const query = MEDIA[mediaName]
29
+
30
+ if (mediaName) {
31
+ let underMediaQuery = CSS_VARS[`@media ${query}`]
32
+ if (!underMediaQuery) underMediaQuery = CSS_VARS[`@media ${query}`] = {}
33
+ underMediaQuery[item.variable] = `var(${item.variable + '-' + mediaName})`
34
+ CSS_VARS[item.variable + '-' + mediaName] = value
35
+ } else {
36
+ CSS_VARS[item.variable + '-default'] = value
37
+ CSS_VARS[item.variable] = `var(${item.variable + '-default'})`
38
+ }
39
+ }
40
+ }
@@ -1,18 +0,0 @@
1
- 'use strict'
2
-
3
- import { CSS_VARS } from '../factory'
4
- import { isObjectLike } from './object'
5
-
6
- export const setVariables = (result, key) => {
7
- // CSS_VARS[result.var] =
8
- if (isObjectLike(result.value)) {
9
- // console.group(key)
10
- // for (const key in result.value) {
11
- // console.log(key, result.value[key])
12
- // }
13
- // console.log(result)
14
- // console.groupEnd(key)
15
- } else {
16
- CSS_VARS[result.var] = result.value
17
- }
18
- }