Skip to content

Commit

Permalink
feat: StyleManager support inline-style transform to CSSText
Browse files Browse the repository at this point in the history
  • Loading branch information
myxvisual committed Aug 8, 2017
1 parent 5b9c51b commit 40d3a45
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"react-dom": "^15.4.0"
},
"devDependencies": {
"@types/murmurhash-js": "^1.0.2",
"@types/react-router": "^3.0.11",
"babel-eslint": "^7.2.3",
"babel-preset-es2015": "^6.24.1",
Expand Down Expand Up @@ -76,8 +77,11 @@
"@types/react-transition-group": "^1.1.0",
"@types/tinycolor2": "^1.1.1",
"inline-style-prefixer": "^3.0.5",
"jss": "^8.1.0",
"jss-preset-default": "^3.0.0",
"keycode": "^2.1.8",
"marked": "^0.3.6",
"murmurhash-js": "^1.0.0",
"prismjs": "^1.6.0",
"prop-types": "^15.5.8",
"react-lazyload": "^2.2.7",
Expand Down
47 changes: 47 additions & 0 deletions src/common/react/isUnitlessNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export const isUnitlessNumber = {
animationIterationCount: true,
borderImageOutset: true,
borderImageSlice: true,
borderImageWidth: true,
boxFlex: true,
boxFlexGroup: true,
boxOrdinalGroup: true,
columnCount: true,
columns: true,
flex: true,
flexGrow: true,
flexPositive: true,
flexShrink: true,
flexNegative: true,
flexOrder: true,
gridRow: true,
gridRowEnd: true,
gridRowSpan: true,
gridRowStart: true,
gridColumn: true,
gridColumnEnd: true,
gridColumnSpan: true,
gridColumnStart: true,
fontWeight: true,
lineClamp: true,
lineHeight: true,
opacity: true,
order: true,
orphans: true,
tabSize: true,
widows: true,
zIndex: true,
zoom: true,

// SVG-related properties
fillOpacity: true,
floodOpacity: true,
stopOpacity: true,
strokeDasharray: true,
strokeDashoffset: true,
strokeMiterlimit: true,
strokeOpacity: true,
strokeWidth: true
};

export default isUnitlessNumber;
26 changes: 24 additions & 2 deletions src/styles/StyleManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import * as createHash from "murmurhash-js/murmurhash3_gc";
import isUnitlessNumber from "../common/react/isUnitlessNumber";

const replace2Dashes = (key: string) => key.replace(/[A-Z]/g, $1 => `-${$1.toLowerCase()}`);
const getStyleValue = (key: string, value: string) => ((typeof value === "number" && !(isUnitlessNumber as any)[key]) ? `${value}px` : value);

export class StyleManager {
theme: ReactUWP.ThemeType;
themeId: number;
sheets: any[] = [];

constructor(theme: ReactUWP.ThemeType) {
this.setupTheme(theme);
this.addSheet({}, "");
}

setupTheme = (theme: ReactUWP.ThemeType) => {
this.theme = theme;
this.themeId = createHash(JSON.stringify(theme));
}

renderSheets = () => {};
Expand All @@ -12,11 +25,20 @@ export class StyleManager {

updateTheme = () => {};

addSheet = () => {};
addSheet = (style: React.CSSProperties, className = "") => {
style = {
borderTop: 2,
fontSize: 12,
lineHeight: 2
};
let CSSText = Object.keys(style).map(key => ` ${replace2Dashes(key)}: ${getStyleValue(key, style[key])};`).join("\n");
const id = createHash(CSSText);
CSSText = `.${className}-${id} {\n${CSSText}\n}`;
}

updateSheetByID = () => {};

updateAllSheet = () => {};
updateAllSheets = () => {};

removeSheetByID = () => {};
}
Expand Down

0 comments on commit 40d3a45

Please sign in to comment.