@symbo.ls/emotion 3.2.3 → 3.2.8

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/classList.js ADDED
@@ -0,0 +1,61 @@
1
+ 'use strict'
2
+
3
+ import { exec, isObject, isString } from '@domql/utils'
4
+
5
+ const RE_MULTI_SPACE = /\s+/g
6
+
7
+ export const assignKeyAsClassname = element => {
8
+ const { key } = element
9
+ if (element.classlist === true) element.classlist = key
10
+ else if (
11
+ !element.classlist &&
12
+ typeof key === 'string' &&
13
+ key.charCodeAt(0) === 95 &&
14
+ key.charCodeAt(1) !== 95
15
+ ) {
16
+ element.classlist = key.slice(1)
17
+ }
18
+ }
19
+
20
+ // stringifies class object
21
+ export const classify = (obj, element) => {
22
+ let className = ''
23
+ for (const item in obj) {
24
+ const param = obj[item]
25
+ if (typeof param === 'boolean' && param) className += ` ${item}`
26
+ else if (typeof param === 'string') className += ` ${param}`
27
+ else if (typeof param === 'function') {
28
+ className += ` ${exec(param, element)}`
29
+ }
30
+ }
31
+ return className
32
+ }
33
+
34
+ export const classList = (params, element) => {
35
+ if (!params) return
36
+ const { key } = element
37
+ if (params === true) params = element.classlist = { key }
38
+ if (isString(params)) params = element.classlist = { default: params }
39
+ if (isObject(params)) params = classify(params, element)
40
+ // TODO: fails on string
41
+ const className = params.replace(RE_MULTI_SPACE, ' ').trim()
42
+ return className
43
+ }
44
+
45
+ // LEGACY (still needed in old domql)
46
+ export const applyClassListOnNode = (params, element, node) => {
47
+ const className = classList(params, element)
48
+ // Use setAttribute for universal SVG/HTML compatibility
49
+ if (node && typeof node.setAttribute === 'function') {
50
+ node.setAttribute('class', className)
51
+ } else if (node) {
52
+ try { node.className = className } catch (e) { /* SVG element */ }
53
+ }
54
+ return className
55
+ }
56
+
57
+ export function applyClasslist (params, element, node) {
58
+ applyClassListOnNode(params, element, node)
59
+ }
60
+
61
+ export default applyClasslist
package/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  'use strict'
2
2
 
3
3
  import createInstance from '@emotion/css/create-instance'
4
+ import { isObjectLike, isString, isNumber, isBoolean, exec } from '@domql/utils'
5
+ import { applyClassListOnNode } from './classList.js'
6
+
7
+ export * from './classList.js'
4
8
 
5
9
  export const createEmotion = (key = 'smbls', container) => {
6
10
  const cleanKey = key.replaceAll(/\./g, '-')
@@ -8,3 +12,69 @@ export const createEmotion = (key = 'smbls', container) => {
8
12
  }
9
13
 
10
14
  export const emotion = createEmotion()
15
+
16
+ export const transformEmotionStyle = emotion => {
17
+ return (params, element, state) => {
18
+ const execParams = exec(params, element)
19
+ if (params) {
20
+ const { __ref: ref } = element
21
+ ref.__class.style = execParams
22
+ }
23
+ transformEmotionClass(emotion)(
24
+ element.classlist,
25
+ element,
26
+ element.state,
27
+ true
28
+ )
29
+ }
30
+ }
31
+
32
+ export const transformEmotionClass = emotion => {
33
+ return (params, element, state, flag) => {
34
+ if (element.style && !flag) return
35
+ const { __ref } = element
36
+ const { __class, __classNames } = __ref
37
+
38
+ if (!isObjectLike(params)) return
39
+ if (element.props.class) {
40
+ __classNames.classProps = element.props.class
41
+ }
42
+ if (element.attr?.class) {
43
+ __classNames.class = element.attr.class
44
+ }
45
+
46
+ for (const key in __class) {
47
+ const prop = __class[key]
48
+ if (!prop) {
49
+ delete __classNames[key]
50
+ continue
51
+ }
52
+ let className
53
+ if (isString(prop) || isNumber(prop)) className = prop
54
+ else if (isBoolean(prop)) className = element.key
55
+ else className = emotion.css(prop)
56
+ __classNames[key] = className
57
+ }
58
+
59
+ // Remove stale classNames that no longer exist in __class
60
+ for (const key in __classNames) {
61
+ if (key === 'classProps' || key === 'class') continue
62
+ if (__class[key] === undefined) {
63
+ delete __classNames[key]
64
+ }
65
+ }
66
+
67
+ if (element.node) applyClassListOnNode(__classNames, element, element.node)
68
+ }
69
+ }
70
+
71
+ export const transformDOMQLEmotion = (emotion, options) => {
72
+ if (!emotion) emotion = createInstance(options || { key: 'smbls' })
73
+
74
+ return {
75
+ style: transformEmotionStyle(emotion),
76
+ classlist: transformEmotionClass(emotion)
77
+ }
78
+ }
79
+
80
+ export * from './initEmotion.js'
package/initEmotion.js ADDED
@@ -0,0 +1,45 @@
1
+ 'use strict'
2
+
3
+ import { transformDOMQLEmotion, emotion as defaultEmotion } from './index.js'
4
+ import { init, DEFAULT_CONTEXT } from 'smbls'
5
+ import { deepClone, deepMerge } from '@domql/utils'
6
+
7
+ import { DEFAULT_CONFIG } from '@symbo.ls/default-config'
8
+
9
+ const OPTION_KEYS = [
10
+ 'useReset', 'useVariable', 'useFontImport', 'useIconSprite',
11
+ 'useSvgSprite', 'useDocumentTheme', 'useDefaultIcons', 'useDefaultConfig', 'verbose',
12
+ 'globalTheme'
13
+ ]
14
+
15
+ export const initEmotion = (key, options = {}) => {
16
+ const doc = options.parent || options.document || document
17
+ const initOptions = options.initOptions || {}
18
+ const emotion = initOptions.emotion
19
+
20
+ if (!initOptions.emotion) initOptions.emotion = defaultEmotion
21
+
22
+ const registry =
23
+ options.registry || transformDOMQLEmotion(initOptions.emotion, options)
24
+ const designSystem =
25
+ initOptions.useDefaultConfig || options.useDefaultConfig || options.designSystem?.useDefaultConfig
26
+ ? deepMerge(options.designSystem, deepClone(DEFAULT_CONFIG))
27
+ : options.designSystem || deepClone(DEFAULT_CONFIG)
28
+
29
+ // Pick context-level overrides (from user's config.js spread into context)
30
+ const contextOverrides = {}
31
+ for (const k of OPTION_KEYS) {
32
+ if (options[k] !== undefined) contextOverrides[k] = options[k]
33
+ }
34
+
35
+ const scratchSystem = init(designSystem, {
36
+ key,
37
+ emotion,
38
+ document: doc,
39
+ ...DEFAULT_CONTEXT,
40
+ ...contextOverrides,
41
+ ...initOptions
42
+ })
43
+
44
+ return [scratchSystem, emotion, registry]
45
+ }
package/package.json CHANGED
@@ -1,15 +1,14 @@
1
1
  {
2
2
  "name": "@symbo.ls/emotion",
3
- "version": "3.2.3",
3
+ "version": "3.2.8",
4
4
  "license": "MIT",
5
- "source": "index.js",
6
- "main": "index.js",
7
5
  "type": "module",
8
- "targets": {},
9
- "files": [],
6
+ "main": "index.js",
7
+ "source": "index.js",
10
8
  "dependencies": {
11
- "@emotion/css": "^11.10.0"
12
- },
13
- "browserslist": "> 0.5%, last 2 versions, not dead",
14
- "gitHead": "9fc1b79b41cdc725ca6b24aec64920a599634681"
9
+ "@domql/utils": "^3.2.3",
10
+ "@emotion/css": "^11.10.0",
11
+ "@symbo.ls/default-config": "^3.2.3",
12
+ "smbls": "^3.2.3"
13
+ }
15
14
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 symbo.ls
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.