react-quizz-spanish
Version:
Quizz en react, traducido al español, autor original: hugobarragon
81 lines • 3.59 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.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importStar(require("react"));
var react_dnd_1 = require("react-dnd");
var ItemTypes = {
CARD: "card",
};
exports.default = (function (props) {
var id = props.id, index = props.index, moveCard = props.moveCard, children = props.children;
var ref = react_1.useRef(null);
var _a = react_dnd_1.useDrop({
accept: ItemTypes.CARD,
hover: function (item, monitor) {
var _a;
if (!ref.current) {
return;
}
var dragIndex = item.index;
var hoverIndex = index;
// Don't replace items with themselves
if (dragIndex === hoverIndex) {
return;
}
// Determine rectangle on screen
var hoverBoundingRect = (_a = ref.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
// Get vertical middle
var hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
// Determine mouse position
var clientOffset = monitor.getClientOffset();
// Get pixels to the top
var hoverClientY = clientOffset.y - hoverBoundingRect.top;
// Only perform the move when the mouse has crossed half of the items height
// When dragging downwards, only move when the cursor is below 50%
// When dragging upwards, only move when the cursor is above 50%
// Dragging downwards
if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
return;
}
// Dragging upwards
if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
return;
}
// Time to actually perform the action
moveCard(dragIndex, hoverIndex);
// Note: we're mutating the monitor item here!
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
item.index = hoverIndex;
},
}), drop = _a[1];
var _b = react_dnd_1.useDrag({
item: { type: ItemTypes.CARD, id: id, index: index },
collect: function (monitor) { return ({
isDragging: monitor.isDragging(),
}); },
}), isDragging = _b[0].isDragging, drag = _b[1];
var opacity = isDragging ? 0 : 1;
drag(drop(ref));
return (react_1.default.createElement("div", { ref: ref, style: { opacity: opacity } }, children));
});
//# sourceMappingURL=index.js.map