@cantoo/rn-svg
Version:
SVG library for react-native
175 lines (174 loc) • 5.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _reactNative = require("react-native");
var _extractResponder = _interopRequireDefault(require("../lib/extract/extractResponder"));
var _extractViewBox = _interopRequireDefault(require("../lib/extract/extractViewBox"));
var _extractColor = _interopRequireDefault(require("../lib/extract/extractColor"));
var _Shape = _interopRequireDefault(require("./Shape"));
var _G = _interopRequireDefault(require("./G"));
var _NativeComponents = require("./NativeComponents");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const RNSVGSvgViewManager = _reactNative.NativeModules.RNSVGSvgViewManager;
const styles = _reactNative.StyleSheet.create({
svg: {
backgroundColor: 'transparent',
borderWidth: 0
}
});
const defaultStyle = styles.svg;
class Svg extends _Shape.default {
static displayName = 'Svg';
static defaultProps = {
preserveAspectRatio: 'xMidYMid meet'
};
measureInWindow = callback => {
const {
root
} = this;
root && root.measureInWindow(callback);
};
measure = callback => {
const {
root
} = this;
root && root.measure(callback);
};
measureLayout = (relativeToNativeNode, onSuccess, onFail) => {
const {
root
} = this;
root && root.measureLayout(relativeToNativeNode, onSuccess, onFail);
};
setNativeProps = props => {
const {
width,
height
} = props;
if (width) {
props.bbWidth = width;
}
if (height) {
props.bbHeight = height;
}
const {
root
} = this;
root && root.setNativeProps(props);
};
toDataURL = (callback, options) => {
if (!callback) {
return;
}
const handle = (0, _reactNative.findNodeHandle)(this.root);
RNSVGSvgViewManager.toDataURL(handle, options, callback);
};
render() {
const {
style,
opacity,
viewBox,
children,
onLayout,
preserveAspectRatio,
...extracted
} = this.props;
const stylesAndProps = {
...(Array.isArray(style) ? Object.assign({}, ...style) : style),
...extracted
};
let {
color,
width,
height,
focusable,
// Inherited G properties
font,
transform,
fill,
fillOpacity,
fillRule,
stroke,
strokeWidth,
strokeOpacity,
strokeDasharray,
strokeDashoffset,
strokeLinecap,
strokeLinejoin,
strokeMiterlimit
} = stylesAndProps;
if (width === undefined && height === undefined) {
width = height = '100%';
}
const props = extracted;
props.focusable = Boolean(focusable) && focusable !== 'false';
const rootStyles = [defaultStyle];
if (style) {
rootStyles.push(style);
}
let override = false;
const overrideStyles = {};
const o = opacity != null ? +opacity : NaN;
if (!isNaN(o)) {
override = true;
overrideStyles.opacity = o;
}
if (width && height) {
override = true;
const w = parseInt(width, 10);
const h = parseInt(height, 10);
const doNotParseWidth = isNaN(w) || width[width.length - 1] === '%';
const doNotParseHeight = isNaN(h) || height[height.length - 1] === '%';
overrideStyles.width = doNotParseWidth ? width : w;
overrideStyles.height = doNotParseHeight ? height : h;
overrideStyles.flex = 0;
}
if (override) {
rootStyles.push(overrideStyles);
}
props.style = rootStyles.length > 1 ? rootStyles : defaultStyle;
if (width != null) {
props.bbWidth = width;
}
if (height != null) {
props.bbHeight = height;
}
(0, _extractResponder.default)(props, props, this);
const tint = (0, _extractColor.default)(color);
if (tint != null) {
props.color = tint;
props.tintColor = tint;
}
if (onLayout != null) {
props.onLayout = onLayout;
}
return /*#__PURE__*/_react.default.createElement(_NativeComponents.RNSVGSvg, _extends({}, props, {
ref: this.refMethod
}, (0, _extractViewBox.default)({
viewBox,
preserveAspectRatio
})), /*#__PURE__*/_react.default.createElement(_G.default, {
children,
style,
font,
transform,
fill,
fillOpacity,
fillRule,
stroke,
strokeWidth,
strokeOpacity,
strokeDasharray,
strokeDashoffset,
strokeLinecap,
strokeLinejoin,
strokeMiterlimit
}));
}
}
exports.default = Svg;
//# sourceMappingURL=Svg.js.map