@kirz/react-native-toolkit
Version:
Toolkit to speed up React Native development
178 lines • 6.01 kB
JavaScript
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); }
import MaskedView from '@react-native-masked-view/masked-view';
import React, { useMemo } from 'react';
import { easeGradient } from 'react-native-easing-gradient';
import LinearGradient from 'react-native-linear-gradient';
import { View } from './View';
import { scaleY, useTheme } from '../index';
const DEFAULT_FADE = scaleY(10);
function InnerFadeView(_ref) {
let {
style,
sides,
fadeDistance,
fadeStart,
fadeEnd,
direction,
fadeColor,
colors,
locations,
...props
} = _ref;
if (direction === 'vertical' && !(sides !== null && sides !== void 0 && sides.bottom) && !(sides !== null && sides !== void 0 && sides.top)) {
return props.children;
}
if (direction === 'horizontal' && !(sides !== null && sides !== void 0 && sides.left) && !(sides !== null && sides !== void 0 && sides.right)) {
return props.children;
}
const maskElement = direction === 'vertical' ? /*#__PURE__*/React.createElement(View, {
style: [style, {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
top: 0,
pointerEvents: 'none'
}]
}, ((sides === null || sides === void 0 ? void 0 : sides.top) ?? false) && /*#__PURE__*/React.createElement(LinearGradient, {
start: {
x: 0.5,
y: (typeof fadeStart === 'number' ? 0 : fadeStart === null || fadeStart === void 0 ? void 0 : fadeStart.top) ?? 0
},
end: {
x: 0.5,
y: (typeof fadeEnd === 'number' ? 0 : fadeEnd === null || fadeEnd === void 0 ? void 0 : fadeEnd.top) ?? 1
},
style: {
height: (typeof fadeDistance === 'number' ? fadeDistance : fadeDistance === null || fadeDistance === void 0 ? void 0 : fadeDistance.top) ?? DEFAULT_FADE
},
colors: colors,
locations: [...locations].reverse()
}), /*#__PURE__*/React.createElement(View, {
style: {
flex: 1,
...(!fadeColor && {
backgroundColor: 'white'
}),
marginVertical: -2
}
}), ((sides === null || sides === void 0 ? void 0 : sides.bottom) ?? true) && /*#__PURE__*/React.createElement(LinearGradient, {
start: {
x: 0.5,
y: (typeof fadeStart === 'number' ? 0 : fadeStart === null || fadeStart === void 0 ? void 0 : fadeStart.bottom) ?? 0
},
end: {
x: 0.5,
y: (typeof fadeEnd === 'number' ? 0 : fadeEnd === null || fadeEnd === void 0 ? void 0 : fadeEnd.bottom) ?? 1
},
style: {
height: (typeof fadeDistance === 'number' ? fadeDistance : fadeDistance === null || fadeDistance === void 0 ? void 0 : fadeDistance.bottom) ?? DEFAULT_FADE
},
colors: colors,
locations: locations
})) : /*#__PURE__*/React.createElement(View, {
style: [style, {
flexDirection: 'row',
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
top: 0,
pointerEvents: 'none'
}]
}, ((sides === null || sides === void 0 ? void 0 : sides.left) ?? false) && /*#__PURE__*/React.createElement(LinearGradient, {
start: {
x: (typeof fadeStart === 'number' ? 0 : fadeStart === null || fadeStart === void 0 ? void 0 : fadeStart.left) ?? 0,
y: 0.5
},
end: {
x: (typeof fadeEnd === 'number' ? 0 : fadeEnd === null || fadeEnd === void 0 ? void 0 : fadeEnd.left) ?? 1,
y: 0.5
},
style: {
width: (typeof fadeDistance === 'number' ? fadeDistance : fadeDistance === null || fadeDistance === void 0 ? void 0 : fadeDistance.left) ?? DEFAULT_FADE
},
colors: colors,
locations: [...locations].reverse()
}), /*#__PURE__*/React.createElement(View, {
style: {
flex: 1,
backgroundColor: 'white',
marginHorizontal: -2
}
}), ((sides === null || sides === void 0 ? void 0 : sides.right) ?? false) && /*#__PURE__*/React.createElement(LinearGradient, {
start: {
x: (typeof fadeStart === 'number' ? 0 : fadeStart === null || fadeStart === void 0 ? void 0 : fadeStart.right) ?? 0,
y: 0.5
},
end: {
x: (typeof fadeEnd === 'number' ? 0 : fadeEnd === null || fadeEnd === void 0 ? void 0 : fadeEnd.right) ?? 1,
y: 0.5
},
style: {
width: (typeof fadeDistance === 'number' ? fadeDistance : fadeDistance === null || fadeDistance === void 0 ? void 0 : fadeDistance.right) ?? DEFAULT_FADE
},
colors: colors,
locations: locations
}));
if (fadeColor) {
return /*#__PURE__*/React.createElement(View, _extends({}, props, {
style: style
}), props.children, maskElement);
}
return /*#__PURE__*/React.createElement(MaskedView, _extends({}, props, {
style: style,
maskElement: maskElement
}));
}
export function FadeView(_ref2) {
let {
sides = {
bottom: true
},
fadeDistance,
fadeColor,
...props
} = _ref2;
const theme = useTheme();
const {
colors,
locations
} = useMemo(() => {
const {
colors,
locations
} = easeGradient({
colorStops: {
0: {
color: fadeColor ? theme.parseColor(fadeColor).fade(1).hexa() : 'white'
},
1: {
color: fadeColor ?? 'transparent'
}
}
});
return {
colors,
locations
};
}, [fadeColor]);
return /*#__PURE__*/React.createElement(InnerFadeView, _extends({
sides: sides,
fadeDistance: fadeDistance
}, props, {
direction: "vertical",
fadeColor: fadeColor,
colors: colors,
locations: locations
}), /*#__PURE__*/React.createElement(InnerFadeView, _extends({
sides: sides,
fadeDistance: fadeDistance
}, props, {
direction: "horizontal",
fadeColor: fadeColor,
colors: colors,
locations: locations
})));
}
//# sourceMappingURL=FadeView.js.map