react-native-credit-cards
Version:
A customizable credit card form component for React Native with card flip animation and validation
112 lines (111 loc) • 4.59 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomFlipCard = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = __importDefault(require("react"));
const react_native_reanimated_1 = __importStar(require("react-native-reanimated"));
const CustomFlipCard = ({ children, flip, style, duration = 300, friction = 8, perspective = 1000, flipHorizontal = true, flipVertical = false, useSpring = false, }) => {
const rotate = (0, react_native_reanimated_1.useSharedValue)(0);
react_1.default.useEffect(() => {
if (useSpring) {
rotate.value = (0, react_native_reanimated_1.withSpring)(flip ? 180 : 0, {
damping: friction,
stiffness: 100,
});
}
else {
rotate.value = (0, react_native_reanimated_1.withTiming)(flip ? 180 : 0, { duration });
}
}, [flip, duration, friction, useSpring]);
const frontAnimatedStyle = (0, react_native_reanimated_1.useAnimatedStyle)(() => {
const rotateValue = (0, react_native_reanimated_1.interpolate)(rotate.value, [0, 180], [0, 180]);
if (flipHorizontal) {
return {
transform: [
{ perspective },
{ rotateY: `${rotateValue}deg` },
],
backfaceVisibility: 'hidden',
};
}
else {
return {
transform: [
{ perspective },
{ rotateX: `${rotateValue}deg` },
],
backfaceVisibility: 'hidden',
};
}
});
const backAnimatedStyle = (0, react_native_reanimated_1.useAnimatedStyle)(() => {
const rotateValue = (0, react_native_reanimated_1.interpolate)(rotate.value, [0, 180], [180, 360]);
if (flipHorizontal) {
return {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
transform: [
{ perspective },
{ rotateY: `${rotateValue}deg` },
],
backfaceVisibility: 'hidden',
};
}
else {
return {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
transform: [
{ perspective },
{ rotateX: `${rotateValue}deg` },
],
backfaceVisibility: 'hidden',
};
}
});
const [frontComponent, backComponent] = children;
return ((0, jsx_runtime_1.jsxs)(react_native_reanimated_1.default.View, { style: style, children: [(0, jsx_runtime_1.jsx)(react_native_reanimated_1.default.View, { style: frontAnimatedStyle, children: frontComponent }), (0, jsx_runtime_1.jsx)(react_native_reanimated_1.default.View, { style: backAnimatedStyle, children: backComponent })] }));
};
exports.CustomFlipCard = CustomFlipCard;