react-native-unistyles
Version:
Level up your React Native StyleSheet
88 lines (85 loc) • 2.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.preprocessor = exports.normalizeNumericValue = exports.normalizeColor = void 0;
var _normalizeColors = _interopRequireDefault(require("@react-native/normalize-colors"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// based on react-native-web normalizer
// https://github.com/necolas/react-native-web
const normalizeColor = (color, opacity = 1) => {
// If the opacity is 1 there's no need to normalize the color
if (opacity === 1) {
return color;
}
const integer = (0, _normalizeColors.default)(color);
// If the color is an unknown format, the return value is null
if (integer === null) {
return color;
}
const hex = integer.toString(16).padStart(8, '0');
if (hex.length === 8) {
const [r = 0, g = 0, b = 0, a = 1] = hex.split(/(?=(?:..)*$)/).map(x => parseInt(x, 16)).filter(num => !isNaN(num));
return `rgba(${r},${g},${b},${a / 255 * opacity})`;
}
return color;
};
exports.normalizeColor = normalizeColor;
const normalizeNumericValue = value => value ? `${value}px` : value;
exports.normalizeNumericValue = normalizeNumericValue;
const normalizeTransform = (key, value) => {
if (key.includes('scale')) {
return value;
}
if (typeof value === 'number') {
return normalizeNumericValue(value);
}
return value;
};
const createTextShadowValue = style => {
// at this point every prop is present
const {
textShadowColor,
textShadowOffset,
textShadowRadius
} = style;
const offsetX = normalizeNumericValue(textShadowOffset.width);
const offsetY = normalizeNumericValue(textShadowOffset.height);
const radius = normalizeNumericValue(textShadowRadius);
const color = normalizeColor(textShadowColor);
return `${offsetX} ${offsetY} ${radius} ${color}`;
};
const createBoxShadowValue = style => {
// at this point every prop is present
const {
shadowColor,
shadowOffset,
shadowOpacity,
shadowRadius
} = style;
const offsetX = normalizeNumericValue(shadowOffset.width);
const offsetY = normalizeNumericValue(shadowOffset.height);
const radius = normalizeNumericValue(shadowRadius);
const color = normalizeColor(shadowColor, shadowOpacity);
return `${offsetX} ${offsetY} ${radius} ${color}`;
};
const createTransformValue = transforms => transforms.map(transform => {
const [key] = Object.keys(transform);
if (!key) {
return undefined;
}
const value = transform[key];
switch (key) {
case 'matrix':
case 'matrix3d':
return `${key}(${value.join(',')})`;
default:
return `${key}(${normalizeTransform(key, value)})`;
}
}).filter(Boolean).join(' ');
const preprocessor = exports.preprocessor = {
createTextShadowValue,
createBoxShadowValue,
createTransformValue
};
//# sourceMappingURL=normalizer.js.map