@navinc/base-react-components
Version:
Nav's Pattern Library
94 lines • 4.17 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ColumnCarousel = exports.useColumnCarouselContext = exports.ColumnCarouselProvider = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const styled_components_1 = __importDefault(require("styled-components"));
const ColumnCarouselContainer = styled_components_1.default.div.withConfig({ displayName: "brc-sc-ColumnCarouselContainer", componentId: "brc-sc-ye736j" }) `
display: flex;
flex-direction: row;
& > * {
flex: 1;
}
`;
const ColumnCarouselContext = (0, react_1.createContext)({
/** @private */
currentColumnIndex: 0,
/** @private */
setCurrentColumnIndex: (() => { }),
cycle: () => { },
goForward: () => { },
goBackward: () => { },
goToIndex: (_index) => { },
activeColumns: 0,
totalColumns: 0,
canCycle: false,
});
const ColumnCarouselProvider = ({ totalColumns, activeColumns = totalColumns, children, }) => {
const [currentColumnIndex, setCurrentColumnIndex] = (0, react_1.useState)(0);
const canCycle = activeColumns < totalColumns;
const cycle = (0, react_1.useCallback)(() => {
if (canCycle) {
setCurrentColumnIndex((i) => (i + 1) % totalColumns);
}
}, [canCycle, totalColumns]);
const goBackward = (0, react_1.useCallback)(() => {
if (canCycle) {
setCurrentColumnIndex((i) => (i - 1 + totalColumns) % totalColumns);
}
}, [canCycle, totalColumns]);
const goToIndex = (0, react_1.useCallback)((index) => {
if (canCycle) {
setCurrentColumnIndex(index % totalColumns);
}
}, [canCycle, totalColumns]);
const providerValue = {
currentColumnIndex,
setCurrentColumnIndex,
cycle,
goForward: cycle,
goBackward,
goToIndex,
activeColumns,
totalColumns,
canCycle,
};
return (0, jsx_runtime_1.jsx)(ColumnCarouselContext.Provider, Object.assign({ value: providerValue }, { children: children }));
};
exports.ColumnCarouselProvider = ColumnCarouselProvider;
const useColumnCarouselContext = () => {
return (0, react_1.useContext)(ColumnCarouselContext);
};
exports.useColumnCarouselContext = useColumnCarouselContext;
/**
* ColumnCarousel is used to display a subset of children at a time, cycling through them. This is different from a
* typical carousel in that there can be multiple carousels on the page each synced to the same column index.
* Essentially this is multiple carousels controlled by a single provider.
*/
const ColumnCarousel = (_a) => {
var { children } = _a, props = __rest(_a, ["children"]);
const { currentColumnIndex, activeColumns, totalColumns } = (0, exports.useColumnCarouselContext)();
const childrenArray = Array.isArray(children) ? children : [children];
(0, react_1.useEffect)(() => {
if (totalColumns !== childrenArray.length) {
console.warn(`ColumnCarousel: ColumnCarouselProvider's totalColumns (${totalColumns}) does not match number of children (${childrenArray.length}). This will cause issues when cycling.`);
}
}, [totalColumns, childrenArray.length]);
return ((0, jsx_runtime_1.jsx)(ColumnCarouselContainer, Object.assign({}, props, { children: [...childrenArray.slice(currentColumnIndex), ...childrenArray.slice(0, currentColumnIndex)].slice(0, activeColumns) })));
};
exports.ColumnCarousel = ColumnCarousel;
//# sourceMappingURL=column-carousel.js.map