From 98e976933bf30457f0b5b0e9e72fc3fc31548b9e Mon Sep 17 00:00:00 2001 From: zombiej Date: Tue, 14 Sep 2021 16:31:25 +0800 Subject: [PATCH 1/2] feat: Add style checker --- .gitignore | 3 ++- examples/styleChecker.tsx | 18 ++++++++++++++++++ now.json | 11 +++++++++++ package.json | 3 ++- src/Dom/styleChecker.ts | 22 ++++++++++++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 examples/styleChecker.tsx create mode 100644 now.json create mode 100644 src/Dom/styleChecker.ts diff --git a/.gitignore b/.gitignore index 8ca35990..498bf6bf 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ lib coverage yarn.lock /es/ -package-lock.json \ No newline at end of file +package-lock.json +.doc \ No newline at end of file diff --git a/examples/styleChecker.tsx b/examples/styleChecker.tsx new file mode 100644 index 00000000..549f31b9 --- /dev/null +++ b/examples/styleChecker.tsx @@ -0,0 +1,18 @@ +import React from 'react'; +import { isStyleSupport, isStyleValueSupport } from '../src/Dom/styleChecker'; + +export default () => { + const supportFlex = isStyleSupport('flex'); + const supportSticky = isStyleValueSupport('position', 'sticky'); + const supportNotExistValue = isStyleValueSupport('position', 'sticky2'); + + return ( + + ); +}; diff --git a/now.json b/now.json new file mode 100644 index 00000000..8eab5ca7 --- /dev/null +++ b/now.json @@ -0,0 +1,11 @@ +{ + "version": 2, + "name": "rc-util", + "builds": [ + { + "src": "package.json", + "use": "@now/static-build", + "config": { "distDir": ".doc" } + } + ] +} diff --git a/package.json b/package.json index 082aea87..71e4ba14 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "compile": "father build", "prepublishOnly": "npm run compile && np --yolo --no-publish", "test": "father test", - "coverage": "father test --coverage && cat ./coverage/lcov.info | coveralls" + "coverage": "father test --coverage && cat ./coverage/lcov.info | coveralls", + "now-build": "father doc build --storybook" }, "devDependencies": { "@types/enzyme": "^3.10.5", diff --git a/src/Dom/styleChecker.ts b/src/Dom/styleChecker.ts new file mode 100644 index 00000000..ab63f4fd --- /dev/null +++ b/src/Dom/styleChecker.ts @@ -0,0 +1,22 @@ +import canUseDom from './canUseDom'; + +export const isStyleSupport = (styleName: string | string[]): boolean => { + if (canUseDom() && window.document.documentElement) { + const styleNameList = Array.isArray(styleName) ? styleName : [styleName]; + const { documentElement } = window.document; + + return styleNameList.some(name => name in documentElement.style); + } + return false; +}; + +export const isStyleValueSupport = (styleName: string, value: any) => { + if (!isStyleSupport(styleName)) { + return false; + } + + const ele = document.createElement('div'); + const origin = ele.style[styleName]; + ele.style[styleName] = value; + return ele.style[styleName] !== origin; +}; From 2e5c8439e6c4cbd2b8ec69352d49f4a4d3f0d088 Mon Sep 17 00:00:00 2001 From: zombiej Date: Tue, 14 Sep 2021 16:42:00 +0800 Subject: [PATCH 2/2] test: Add test case --- tests/style.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tests/style.test.js diff --git a/tests/style.test.js b/tests/style.test.js new file mode 100644 index 00000000..ba345325 --- /dev/null +++ b/tests/style.test.js @@ -0,0 +1,8 @@ +import { isStyleSupport } from '../src/Dom/styleChecker'; + +describe('StyleChecker', () => { + it('isStyleSupport', () => { + expect(isStyleSupport('position')).toBeTruthy(); + expect(isStyleSupport('not-exist')).toBeFalsy(); + }); +});