mirror of
https://github.com/zhigang1992/react-native-web.git
synced 2026-05-08 02:39:21 +08:00
Move the styling strategy into a separate module within react-web-style; consider supporting strategy injection.
39 lines
1009 B
JavaScript
39 lines
1009 B
JavaScript
import autoprefix from './autoprefix';
|
|
import styleMap from './styleMap';
|
|
|
|
/**
|
|
* Find declarations that correspond to single purpose classes
|
|
*/
|
|
function getSinglePurposeClassName(prop, style) {
|
|
const uniqueClassName = `${prop}-${style[prop]}`;
|
|
if (
|
|
style.hasOwnProperty(prop) &&
|
|
styleMap[uniqueClassName]
|
|
) {
|
|
return styleMap[uniqueClassName];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Replace inline styles with single purpose classes where possible
|
|
*/
|
|
export default function stylingStrategy(props) {
|
|
const { className: origClassName, style: origStyle } = props;
|
|
const classNames = [ origClassName ];
|
|
const style = {};
|
|
|
|
for (const prop in origStyle) {
|
|
const singlePurposeClassName = getSinglePurposeClassName(prop, origStyle);
|
|
if (singlePurposeClassName) {
|
|
classNames.push(singlePurposeClassName);
|
|
} else {
|
|
style[prop] = origStyle[prop];
|
|
}
|
|
}
|
|
|
|
const className = classNames.join(' ');
|
|
const prefixedStyle = autoprefix(style);
|
|
|
|
return { className, style: prefixedStyle };
|
|
}
|