@viewfly/scoped-css 0.0.16 → 0.0.17

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.
@@ -1,10 +1,2 @@
1
1
  import { JSXInternal } from '@viewfly/core';
2
- declare module '@viewfly/core' {
3
- namespace JSXInternal {
4
- interface Attributes {
5
- css?: JSXInternal.ClassNames;
6
- }
7
- }
8
- }
9
- export declare function getClassNames(config: JSXInternal.ClassNames, cssRecord: Record<string, string>): string;
10
- export declare function withScopedCSS(css: Record<string, string>, render: () => JSXInternal.Element): () => JSXInternal.JSXChildNode;
2
+ export declare function withScopedCSS(cssNamespace: string | string[], render: () => JSXInternal.Element): () => JSXInternal.JSXChildNode;
@@ -1,74 +1,29 @@
1
1
  import { JSXElement, JSXComponent } from '@viewfly/core';
2
2
 
3
- function cssNamesToArray(config) {
4
- const classes = [];
5
- if (!config) {
6
- return classes;
7
- }
8
- if (typeof config === 'string') {
9
- const items = config.match(/\S+/g);
10
- return items || classes;
11
- }
12
- else if (Array.isArray(config)) {
13
- for (const i of config) {
14
- classes.push(...cssNamesToArray(i));
15
- }
16
- }
17
- else if (typeof config === 'object') {
18
- if (config.toString !== Object.prototype.toString && !config.toString.toString().includes('[native code]')) {
19
- classes.push(config.toString());
20
- return classes;
21
- }
22
- for (const key in config) {
23
- if ({}.hasOwnProperty.call(config, key) && config[key]) {
24
- classes.push(key);
25
- }
26
- }
27
- }
28
- return classes;
29
- }
30
- function replaceCSSClass(template, cssMap) {
3
+ function replaceCSSClass(template, cssNamespace) {
31
4
  if (template instanceof JSXElement || template instanceof JSXComponent) {
32
- let { class: className, children } = template.props;
33
- const css = template.props.css;
34
- Reflect.deleteProperty(template.props, 'css');
35
- const c = getClassNames(css, cssMap);
36
- if (c) {
37
- if (className) {
38
- className += ' ' + c;
5
+ const children = template.props.children;
6
+ const nameSpaces = Array.isArray(cssNamespace) ? cssNamespace : [cssNamespace];
7
+ nameSpaces.forEach(i => {
8
+ if (typeof i === 'string' && i) {
9
+ template.props[i] = '';
39
10
  }
40
- else {
41
- className = c;
42
- }
43
- }
44
- if (className) {
45
- template.props.class = className;
46
- }
11
+ });
47
12
  if (Array.isArray(children)) {
48
13
  children.forEach(child => {
49
- replaceCSSClass(child, cssMap);
14
+ replaceCSSClass(child, cssNamespace);
50
15
  });
51
16
  }
52
17
  else {
53
- replaceCSSClass(children, cssMap);
18
+ replaceCSSClass(children, cssNamespace);
54
19
  }
55
20
  }
56
21
  return template;
57
22
  }
58
- function getClassNames(config, cssRecord) {
59
- const scopedClasses = [];
60
- cssNamesToArray(config).forEach(i => {
61
- const klass = cssRecord[i];
62
- if (klass) {
63
- scopedClasses.push(klass);
64
- }
65
- });
66
- return scopedClasses.join(' ');
67
- }
68
- function withScopedCSS(css, render) {
23
+ function withScopedCSS(cssNamespace, render) {
69
24
  return function scopedCSS() {
70
- return replaceCSSClass(render(), css);
25
+ return replaceCSSClass(render(), cssNamespace);
71
26
  };
72
27
  }
73
28
 
74
- export { getClassNames, withScopedCSS };
29
+ export { withScopedCSS };
package/bundles/index.js CHANGED
@@ -2,76 +2,30 @@
2
2
 
3
3
  var core = require('@viewfly/core');
4
4
 
5
- function cssNamesToArray(config) {
6
- const classes = [];
7
- if (!config) {
8
- return classes;
9
- }
10
- if (typeof config === 'string') {
11
- const items = config.match(/\S+/g);
12
- return items || classes;
13
- }
14
- else if (Array.isArray(config)) {
15
- for (const i of config) {
16
- classes.push(...cssNamesToArray(i));
17
- }
18
- }
19
- else if (typeof config === 'object') {
20
- if (config.toString !== Object.prototype.toString && !config.toString.toString().includes('[native code]')) {
21
- classes.push(config.toString());
22
- return classes;
23
- }
24
- for (const key in config) {
25
- if ({}.hasOwnProperty.call(config, key) && config[key]) {
26
- classes.push(key);
27
- }
28
- }
29
- }
30
- return classes;
31
- }
32
- function replaceCSSClass(template, cssMap) {
5
+ function replaceCSSClass(template, cssNamespace) {
33
6
  if (template instanceof core.JSXElement || template instanceof core.JSXComponent) {
34
- let { class: className, children } = template.props;
35
- const css = template.props.css;
36
- Reflect.deleteProperty(template.props, 'css');
37
- const c = getClassNames(css, cssMap);
38
- if (c) {
39
- if (className) {
40
- className += ' ' + c;
7
+ const children = template.props.children;
8
+ const nameSpaces = Array.isArray(cssNamespace) ? cssNamespace : [cssNamespace];
9
+ nameSpaces.forEach(i => {
10
+ if (typeof i === 'string' && i) {
11
+ template.props[i] = '';
41
12
  }
42
- else {
43
- className = c;
44
- }
45
- }
46
- if (className) {
47
- template.props.class = className;
48
- }
13
+ });
49
14
  if (Array.isArray(children)) {
50
15
  children.forEach(child => {
51
- replaceCSSClass(child, cssMap);
16
+ replaceCSSClass(child, cssNamespace);
52
17
  });
53
18
  }
54
19
  else {
55
- replaceCSSClass(children, cssMap);
20
+ replaceCSSClass(children, cssNamespace);
56
21
  }
57
22
  }
58
23
  return template;
59
24
  }
60
- function getClassNames(config, cssRecord) {
61
- const scopedClasses = [];
62
- cssNamesToArray(config).forEach(i => {
63
- const klass = cssRecord[i];
64
- if (klass) {
65
- scopedClasses.push(klass);
66
- }
67
- });
68
- return scopedClasses.join(' ');
69
- }
70
- function withScopedCSS(css, render) {
25
+ function withScopedCSS(cssNamespace, render) {
71
26
  return function scopedCSS() {
72
- return replaceCSSClass(render(), css);
27
+ return replaceCSSClass(render(), cssNamespace);
73
28
  };
74
29
  }
75
30
 
76
- exports.getClassNames = getClassNames;
77
31
  exports.withScopedCSS = withScopedCSS;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viewfly/scoped-css",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "A library for supporting scoped style sheets in the Viewfly framework。",
5
5
  "main": "./bundles/index.js",
6
6
  "module": "./bundles/index.esm.js",
@@ -12,7 +12,7 @@
12
12
  "license": "MIT",
13
13
  "keywords": [],
14
14
  "dependencies": {
15
- "@viewfly/core": "^0.0.16"
15
+ "@viewfly/core": "^0.0.17"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@rollup/plugin-commonjs": "^25.0.3",
@@ -32,5 +32,5 @@
32
32
  "bugs": {
33
33
  "url": "https://github.com/viewfly/viewfly.git/issues"
34
34
  },
35
- "gitHead": "424aee30ec55010f16a8e055222d42b07fae4b38"
35
+ "gitHead": "7a61edbca87f411d2ddbb390eb0b2e0416d80551"
36
36
  }