Fix handling of numeric gradient offsets, closes #827

This commit is contained in:
Mikael Sand
2018-12-10 05:36:09 +02:00
parent 77872d453d
commit e156844f98
+15 -9
View File
@@ -9,7 +9,10 @@ import units from "../units";
const percentReg = /^([+\-]?\d+(?:\.\d+)?(?:[eE][+\-]?\d+)?)(%?)$/;
function percentToFloat(percent) {
const matched = percent.toString().match(percentReg);
if (typeof percent === 'number') {
return percent;
}
const matched = percent.match(percentReg);
if (!matched) {
console.warn(
`\`${percent}\` is not a valid number or percentage string.`,
@@ -26,15 +29,18 @@ export default function(props) {
}
const stops = {};
Children.forEach(props.children, child => {
if (child.props.stopColor && child.props.offset) {
// convert percent to float.
const offset = percentToFloat(child.props.offset);
// add stop
Children.forEach(props.children, ({
props: {
offset,
stopColor,
stopOpacity,
}
}) => {
offset = percentToFloat(offset);
if (stopColor && !isNaN(offset)) {
//noinspection JSUnresolvedFunction
stops[offset] = Color(child.props.stopColor).alpha(
extractOpacity(child.props.stopOpacity),
stops[offset] = Color(stopColor).alpha(
extractOpacity(stopOpacity),
);
}
});