@viewfly/scoped-css 0.0.1-alpha.8 → 0.0.1

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/README.md CHANGED
@@ -1,4 +1,39 @@
1
1
  Viewfly
2
2
  ================================
3
3
 
4
- Viewfly 是一个简单、数据驱动的前端视图库,完整文档请参考官方网站:[viewfly.org](https://viewfly.org)
4
+ Viewfly 是一个简单、数据驱动的前端框架。此项目为 Viewfly 组件提供了 css 模块化的能力。
5
+
6
+ 要使用 css 模块化能力,还需要在 webpack 配置中 css loader 的模块化支持,具体可参考相关文档。
7
+
8
+ ## 安装
9
+ ```
10
+ npm install @viewfly/scoped-css
11
+ ```
12
+
13
+ ## API
14
+
15
+ ### scopedCSS()
16
+
17
+ ```css
18
+ /* app.module.css */
19
+ .app {
20
+ color: blue
21
+ }
22
+ ```
23
+
24
+ ```jsx
25
+ import { scopedCSS } from '@viewfly/scoped-css'
26
+
27
+ import css from './app.module.css'
28
+
29
+ const App = scopedCSS(css, () => {
30
+ return () => {
31
+ return <div css="app">App</div>
32
+ }
33
+ })
34
+ ```
35
+
36
+ scopedCSS 通过会识别标签的 css 属性,并替换为模拟化的 css class 类名。css 不仅可以支持字符串,还可以支持对象、数组、或数组内对象的组合。
37
+
38
+
39
+ 完整文档请参考官方网站:[viewfly.org](https://viewfly.org)
@@ -1,2 +1,9 @@
1
1
  import { ComponentSetup } from '@viewfly/core';
2
- export declare function scopedCss<T extends ComponentSetup>(css: Record<string, string>, factory: T): T;
2
+ declare module '@viewfly/core' {
3
+ namespace JSX {
4
+ interface Attributes<T extends object> {
5
+ css?: string | Record<string, unknown> | Array<string | Record<string, unknown>>;
6
+ }
7
+ }
8
+ }
9
+ export declare function scopedCSS<T extends ComponentSetup>(css: Record<string, string>, factory: T): T;
@@ -1,33 +1,67 @@
1
- import { JSXElement, Props } from '@viewfly/core';
1
+ import { JSXElement } from '@viewfly/core';
2
2
 
3
- function replaceCSSClass(template, css) {
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) {
4
31
  if (template instanceof JSXElement) {
5
- let { attrs, classes, children } = template.props;
6
- const cssNames = attrs.get('css');
7
- attrs.delete('css');
32
+ let { class: className, children } = template.props;
33
+ const css = template.props.css;
34
+ Reflect.deleteProperty(template.props, 'css');
8
35
  const scopedClasses = [];
9
- Props.classToArray(cssNames).forEach(i => {
10
- const klass = css[i];
36
+ cssNamesToArray(css).forEach(i => {
37
+ const klass = cssMap[i];
11
38
  if (klass) {
12
39
  scopedClasses.push(klass);
13
40
  }
14
41
  });
15
42
  const c = scopedClasses.join(' ');
16
43
  if (c) {
17
- if (classes) {
18
- classes += ' ' + c;
44
+ if (className) {
45
+ className += ' ' + c;
19
46
  }
20
47
  else {
21
- classes = c;
48
+ className = c;
22
49
  }
23
50
  }
24
- template.props.classes = classes;
25
- children.forEach(child => {
26
- replaceCSSClass(child, css);
27
- });
51
+ if (className) {
52
+ template.props.class = className;
53
+ }
54
+ if (Array.isArray(children)) {
55
+ children.forEach(child => {
56
+ replaceCSSClass(child, cssMap);
57
+ });
58
+ }
59
+ else {
60
+ replaceCSSClass(children, cssMap);
61
+ }
28
62
  }
29
63
  }
30
- function scopedCss(css, factory) {
64
+ function scopedCSS(css, factory) {
31
65
  return function (props) {
32
66
  const componentRender = factory(props);
33
67
  return function () {
@@ -38,4 +72,4 @@ function scopedCss(css, factory) {
38
72
  };
39
73
  }
40
74
 
41
- export { scopedCss };
75
+ export { scopedCSS };
package/bundles/index.js CHANGED
@@ -2,34 +2,68 @@
2
2
 
3
3
  var core = require('@viewfly/core');
4
4
 
5
- function replaceCSSClass(template, css) {
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) {
6
33
  if (template instanceof core.JSXElement) {
7
- let { attrs, classes, children } = template.props;
8
- const cssNames = attrs.get('css');
9
- attrs.delete('css');
34
+ let { class: className, children } = template.props;
35
+ const css = template.props.css;
36
+ Reflect.deleteProperty(template.props, 'css');
10
37
  const scopedClasses = [];
11
- core.Props.classToArray(cssNames).forEach(i => {
12
- const klass = css[i];
38
+ cssNamesToArray(css).forEach(i => {
39
+ const klass = cssMap[i];
13
40
  if (klass) {
14
41
  scopedClasses.push(klass);
15
42
  }
16
43
  });
17
44
  const c = scopedClasses.join(' ');
18
45
  if (c) {
19
- if (classes) {
20
- classes += ' ' + c;
46
+ if (className) {
47
+ className += ' ' + c;
21
48
  }
22
49
  else {
23
- classes = c;
50
+ className = c;
24
51
  }
25
52
  }
26
- template.props.classes = classes;
27
- children.forEach(child => {
28
- replaceCSSClass(child, css);
29
- });
53
+ if (className) {
54
+ template.props.class = className;
55
+ }
56
+ if (Array.isArray(children)) {
57
+ children.forEach(child => {
58
+ replaceCSSClass(child, cssMap);
59
+ });
60
+ }
61
+ else {
62
+ replaceCSSClass(children, cssMap);
63
+ }
30
64
  }
31
65
  }
32
- function scopedCss(css, factory) {
66
+ function scopedCSS(css, factory) {
33
67
  return function (props) {
34
68
  const componentRender = factory(props);
35
69
  return function () {
@@ -40,4 +74,4 @@ function scopedCss(css, factory) {
40
74
  };
41
75
  }
42
76
 
43
- exports.scopedCss = scopedCss;
77
+ exports.scopedCSS = scopedCSS;
package/package.json CHANGED
@@ -1,21 +1,18 @@
1
1
  {
2
2
  "name": "@viewfly/scoped-css",
3
- "version": "0.0.1-alpha.8",
4
- "description": "Viewfly is a simple and easy-to-use JavaScript framework with an intuitive development experience",
3
+ "version": "0.0.1",
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",
7
7
  "typings": "./bundles/public-api.d.ts",
8
8
  "scripts": {
9
- "start": "webpack-dev-server",
10
- "test": "cross-env env=test jest",
11
- "test-c": "cross-env env=test jest --coverage",
12
9
  "build:lib": "rimraf bundles && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
13
10
  "publish:lib": "npm run build:lib && npm publish --access=public"
14
11
  },
15
12
  "license": "MIT",
16
13
  "keywords": [],
17
14
  "dependencies": {
18
- "@viewfly/core": "^0.0.1-alpha.8"
15
+ "@viewfly/core": "^0.0.1"
19
16
  },
20
17
  "devDependencies": {
21
18
  "@rollup/plugin-commonjs": "^23.0.2",
@@ -35,5 +32,5 @@
35
32
  "bugs": {
36
33
  "url": "https://github.com/viewfly/viewfly.git/issues"
37
34
  },
38
- "gitHead": "462b51e8623348002ba7c6ba2caf7df131534506"
35
+ "gitHead": "13f2af4215a1cd85af544af55744b2635e5cbf1b"
39
36
  }
package/rollup.config.ts DELETED
@@ -1,24 +0,0 @@
1
- import commonjs from '@rollup/plugin-commonjs'
2
- import typescript from '@rollup/plugin-typescript'
3
-
4
- export default {
5
- input: 'src/public-api.ts',
6
- output: [
7
- {
8
- file: './bundles/index.js',
9
- format: 'cjs'
10
- },
11
- {
12
- file: './bundles/index.esm.js',
13
- format: 'esm'
14
- }
15
- ],
16
- plugins: [
17
- commonjs(),
18
- typescript({
19
- compilerOptions: {
20
- paths: {}
21
- }
22
- })
23
- ]
24
- }
@@ -1,39 +0,0 @@
1
- import { JSXElement, ComponentSetup, Props } from '@viewfly/core'
2
-
3
- function replaceCSSClass(template, css: Record<string, string>) {
4
- if (template instanceof JSXElement) {
5
- let { attrs, classes, children } = template.props
6
- const cssNames = attrs.get('css')
7
- attrs.delete('css')
8
- const scopedClasses: string[] = []
9
- Props.classToArray(cssNames).forEach(i => {
10
- const klass = css[i]
11
- if (klass) {
12
- scopedClasses.push(klass)
13
- }
14
- })
15
- const c = scopedClasses.join(' ')
16
- if (c) {
17
- if (classes) {
18
- classes += ' ' + c
19
- } else {
20
- classes = c
21
- }
22
- }
23
- template.props.classes = classes
24
- children.forEach(child => {
25
- replaceCSSClass(child, css)
26
- })
27
- }
28
- }
29
-
30
- export function scopedCss<T extends ComponentSetup>(css: Record<string, string>, factory: T): T {
31
- return function (props: any) {
32
- const componentRender = factory(props)
33
- return function () {
34
- const template = componentRender()
35
- replaceCSSClass(template, css)
36
- return template
37
- }
38
- } as T
39
- }
package/src/public-api.ts DELETED
@@ -1 +0,0 @@
1
- export * from './css-modules'
package/tsconfig.json DELETED
@@ -1,37 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "declaration": true,
4
- "useDefineForClassFields": false,
5
- "emitDecoratorMetadata": true,
6
- "experimentalDecorators": true,
7
- "allowSyntheticDefaultImports": true,
8
- "lib": [
9
- "esnext",
10
- "dom"
11
- ],
12
- "target": "es6",
13
- "strict": true,
14
- "module": "es2020",
15
- "moduleResolution": "node",
16
- "inlineSourceMap": true,
17
- "inlineSources": true,
18
- "noImplicitAny": false,
19
- "ignoreDeprecations": "5.0",
20
- "suppressImplicitAnyIndexErrors": true,
21
- "outDir": "bundles/",
22
- "downlevelIteration": true,
23
- "paths": {
24
- "@viewfly/core": [
25
- "../core/src/public-api.ts"
26
- ]
27
- }
28
- },
29
- "include": [
30
- "src"
31
- ],
32
- "ts-node": {
33
- "compilerOptions": {
34
- "module": "CommonJS"
35
- }
36
- }
37
- }