@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
140 lines (118 loc) • 5.6 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var React = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireWildcard(require("styled-components"));
var _Stack = _interopRequireDefault(require("../Stack"));
var _mergeRefs = _interopRequireDefault(require("../utils/mergeRefs"));
var _defaultTheme = _interopRequireDefault(require("../defaultTheme"));
var _useTheme = _interopRequireDefault(require("../hooks/useTheme"));
var _useScroll = _interopRequireDefault(require("./useScroll"));
const _excluded = ["children", "spacing", "scrollSnap", "onOverflow", "elevationColor", "overflowElevation", "scrollPadding", "dataTest", "minHeight"];
const shadowMixin = (0, _styledComponents.css)(["content:\"\";position:absolute;top:0;z-index:1;height:100%;"]);
const StyledWrapper = _styledComponents.default.div.withConfig({
displayName: "HorizontalScroll__StyledWrapper",
componentId: "sc-1rcvh7b-0"
})(["", ""], ({
isDragging,
$minHeight,
elevationColor,
overflowElevation,
isStart,
isOverflowing
}) => (0, _styledComponents.css)(["position:relative;width:100%;min-height:", ";cursor:", ";overflow:hidden;", " ", ""], $minHeight && `${$minHeight}px`, isOverflowing && (isDragging ? "grabbing" : "grab"), isOverflowing && overflowElevation && !isStart && (0, _styledComponents.css)(["&:before{", ";left:0;box-shadow:5px 0px 20px 20px ", ";}"], shadowMixin, elevationColor), isOverflowing && overflowElevation && (0, _styledComponents.css)(["&:after{", ";right:0;box-shadow:-5px 0px 20px 20px ", ";}"], shadowMixin, elevationColor))); // $FlowFixMe: https://github.com/flow-typed/flow-typed/issues/3653#issuecomment-568539198
StyledWrapper.defaultProps = {
theme: _defaultTheme.default
};
const getSnap = ({
$scrollSnap
}) => {
if ($scrollSnap === "mandatory") return "x mandatory";
if ($scrollSnap === "proximity") return "x proximity";
return $scrollSnap;
};
const StyledOverflow = _styledComponents.default.div.withConfig({
displayName: "HorizontalScroll__StyledOverflow",
componentId: "sc-1rcvh7b-1"
})(["", ""], ({
isDragging,
scrollPadding
}) => (0, _styledComponents.css)(["width:100%;height:100%;overflow-y:hidden;overflow-x:auto;scroll-snap-type:", ";scroll-padding:", ";box-sizing:border-box;-ms-overflow-style:none;overflow:-moz-scrollbars-none;scrollbar-width:none;::-webkit-scrollbar{display:none;}"], isDragging ? "none" : getSnap, scrollPadding && `${scrollPadding}px`));
const StyledContainer = _styledComponents.default.div.withConfig({
displayName: "HorizontalScroll__StyledContainer",
componentId: "sc-1rcvh7b-2"
})(["", ""], ({
isDragging
}) => (0, _styledComponents.css)(["height:100%;width:100%;display:inline-flex;pointer-events:", ";"], isDragging && "none"));
const HorizontalScroll = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
children,
spacing = "small",
scrollSnap = "none",
onOverflow,
elevationColor = "paletteCloudDark",
overflowElevation,
scrollPadding,
dataTest,
minHeight
} = _ref,
props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
const scrollWrapperRef = React.useRef(null);
const containerRef = React.useRef(null);
const {
isDragging,
reachedStart
} = (0, _useScroll.default)(scrollWrapperRef);
const theme = (0, _useTheme.default)();
const [isOverflowing, setOverflowing] = React.useState(false);
const handleOverflow = React.useCallback(() => {
var _scrollWrapperRef$cur, _containerRef$current;
if ((_scrollWrapperRef$cur = scrollWrapperRef.current) !== null && _scrollWrapperRef$cur !== void 0 && _scrollWrapperRef$cur.scrollWidth && (_containerRef$current = containerRef.current) !== null && _containerRef$current !== void 0 && _containerRef$current.offsetWidth) {
const {
scrollWidth: containerScrollWidth
} = scrollWrapperRef.current;
const {
offsetWidth
} = containerRef.current;
if (containerScrollWidth > offsetWidth) {
setOverflowing(true);
if (onOverflow) onOverflow();
} else {
setOverflowing(false);
}
}
}, [onOverflow]);
React.useEffect(() => {
handleOverflow();
window.addEventListener("resize", handleOverflow);
return () => window.addEventListener("resize", handleOverflow);
}, [handleOverflow]);
return /*#__PURE__*/React.createElement(StyledWrapper, (0, _extends2.default)({}, props, {
$minHeight: minHeight,
overflowElevation: overflowElevation,
"data-test": dataTest,
isDragging: isDragging,
isStart: reachedStart,
isOverflowing: isOverflowing,
ref: (0, _mergeRefs.default)([ref, containerRef]),
elevationColor: theme.orbit[elevationColor]
}), /*#__PURE__*/React.createElement(StyledOverflow, {
$scrollSnap: scrollSnap,
scrollPadding: scrollPadding,
isDragging: isDragging,
ref: scrollWrapperRef
}, /*#__PURE__*/React.createElement(StyledContainer, {
isDragging: isDragging
}, /*#__PURE__*/React.createElement(_Stack.default, {
inline: true,
spacing: spacing
}, children))));
});
var _default = HorizontalScroll;
exports.default = _default;