expo-like-button
Version:
Animated Like Button Component for React native apps made using expo.
132 lines • 7.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var react_native_reanimated_1 = __importStar(require("react-native-reanimated"));
var react_native_1 = require("react-native");
var vector_icons_1 = require("@expo/vector-icons");
// General Default Props
var DEFAULT_ICON_SIZE = 32;
// Default configs for Liked Component
var DEFAULT_LIKED_ICON_NAME = "heart-outline";
var DEFAULT_LIKED_ICON_COLOR = "red";
// Default configs for Unliked Component
var DEFAULT_UNLIKED_ICON_NAME = "heart";
var DEFAULT_UNLIKED_ICON_COLOR = "black";
function LikeButton(_a) {
var _this = this;
var
// Basic Props
_b = _a.liked,
// Basic Props
liked = _b === void 0 ? undefined : _b, onPress = _a.onPress, _c = _a.iconSize, iconSize = _c === void 0 ? DEFAULT_ICON_SIZE : _c,
// Liked Component Props
customLikedComponent = _a.customLikedComponent, _d = _a.likedColor, likedColor = _d === void 0 ? DEFAULT_LIKED_ICON_COLOR : _d, _e = _a.likedIconName, likedIconName = _e === void 0 ? DEFAULT_LIKED_ICON_NAME : _e,
// Unlike Component Props
customUnlikedComponent = _a.customUnlikedComponent, _f = _a.unlikedColor, unlikedColor = _f === void 0 ? DEFAULT_UNLIKED_ICON_COLOR : _f, _g = _a.unlikedIconName, unlikedIconName = _g === void 0 ? DEFAULT_UNLIKED_ICON_NAME : _g;
// This is a shared value for animation
var isLiked = react_native_reanimated_1.useSharedValue(liked !== undefined && liked === true ? 1 : 0);
// Check whether the component should be controlled or uncontrolled
var controlled = liked !== undefined;
// useEffect to detect when `liked` prop changes
// If component is controlled then animate according to `liked` prop
react_1.useEffect(function () {
if (controlled)
isLiked.value = react_native_reanimated_1.withSpring(liked ? 1 : 0);
}, [liked]);
// Animated Styles for the upper like button
var OutLineStyling = react_native_reanimated_1.useAnimatedStyle(function () {
return {
transform: [
{
scale: react_native_reanimated_1.interpolate(isLiked.value, [0, 1], [1, 0], react_native_reanimated_1.Extrapolate.CLAMP),
},
],
};
});
// Animated Styles for the bottom like button
var FillStyling = react_native_reanimated_1.useAnimatedStyle(function () {
return {
transform: [
{
scale: isLiked.value,
},
],
opacity: isLiked.value,
};
});
// If Component is uncontrolled, just invert the isLiked value
// Other wise do nothing
var onComponentPress = function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
try {
if (!controlled)
isLiked.value = react_native_reanimated_1.withSpring(isLiked.value ? 0 : 1);
if (onPress instanceof Function)
onPress();
}
catch (error) { }
return [2 /*return*/];
});
}); };
// Render
return (react_1.default.createElement(react_native_1.Pressable, { onPress: onComponentPress },
react_1.default.createElement(react_native_reanimated_1.default.View, { style: [react_native_1.StyleSheet.absoluteFillObject, OutLineStyling] }, customUnlikedComponent ? (customUnlikedComponent) : (react_1.default.createElement(vector_icons_1.MaterialCommunityIcons, { name: likedIconName, size: iconSize, color: unlikedColor }))),
react_1.default.createElement(react_native_reanimated_1.default.View, { style: FillStyling }, customLikedComponent ? (customLikedComponent) : (react_1.default.createElement(vector_icons_1.MaterialCommunityIcons, { name: unlikedIconName, size: iconSize, color: likedColor })))));
}
exports.default = LikeButton;
//# sourceMappingURL=LikeButton.js.map