UNPKG

@carbon/ibm-security

Version:

Carbon for Cloud & Cognitive IBM Security UI components

216 lines (215 loc) 9.05 kB
import _extends from "@babel/runtime/helpers/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; var _excluded = ["className", "children", "direction", "color", "onScroll", "scrollElementClassName", "getScrollElementRef", "hideStartGradient"]; var _ScrollGradient; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /** * @file Scroll gradient. * @copyright IBM Security 2019 - 2021 */ import classnames from 'classnames'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { throttle } from 'throttle-debounce'; import { getComponentNamespace } from '../../globals/namespace'; import { isClient } from '../../globals/utils/capabilities'; export var namespace = getComponentNamespace('scroll-gradient'); var scrollDirection = { X: 'X', Y: 'Y' }; var ScrollGradient = /*#__PURE__*/function (_Component) { function ScrollGradient() { var _this; _classCallCheck(this, ScrollGradient); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, ScrollGradient, [].concat(args)); _defineProperty(_this, "state", { position: ScrollGradient.ScrollStates.NONE }); /** * Saves references of scroll DOM element. * @param {HTMLElement} element Scroll DOM element. */ _defineProperty(_this, "setRefs", function (element) { _this.scrollContainer = element; _this.props.getScrollElementRef(element); }); /** * Handles scrolling event of scroll element. * @param {Event} event Scroll event generated by user. */ _defineProperty(_this, "scrollHandler", function (event) { _this.props.onScroll(event); _this.updateHandler(); }); /** * Updates the scroll state of component. */ _defineProperty(_this, "updateScrollState", function () { return _this.setState({ position: ScrollGradient.getScrollState(_this.scrollContainer, _this.props.direction) }); }); /** * @type {Function} Debounces the execution of scroll state update. */ _defineProperty(_this, "updateHandler", throttle(150, _this.updateScrollState)); /** @type {HTMLElement} Scrollable element reference. */ _defineProperty(_this, "scrollContainer", null); return _this; } _inherits(ScrollGradient, _Component); return _createClass(ScrollGradient, [{ key: "componentDidMount", value: function componentDidMount() { if (this.scrollContainer) { this.updateScrollState(); } if (isClient()) { // https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver this.resizeObserver = new ResizeObserver(this.updateHandler); this.resizeObserver.observe(this.scrollContainer); } } }, { key: "componentDidUpdate", value: function componentDidUpdate() { if (this.state.position !== ScrollGradient.getScrollState(this.scrollContainer, this.props.direction)) { this.updateScrollState(); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.resizeObserver.disconnect(); } }, { key: "render", value: function render() { var _this$props = this.props, className = _this$props.className, children = _this$props.children, direction = _this$props.direction, color = _this$props.color, _ = _this$props.onScroll, scrollElementClassName = _this$props.scrollElementClassName, __ = _this$props.getScrollElementRef, hideStartGradient = _this$props.hideStartGradient, other = _objectWithoutProperties(_this$props, _excluded); var position = this.state.position; var gradientRotation = direction === ScrollGradient.ScrollDirection.X ? -90 : 0; return /*#__PURE__*/React.createElement("div", _extends({ className: classnames(namespace, "".concat(namespace, "--").concat(position.toLowerCase()), "".concat(namespace, "--").concat(direction.toLowerCase()), className), role: "presentation" }, other), !hideStartGradient && /*#__PURE__*/React.createElement("div", { className: "".concat(namespace, "__before"), style: { backgroundImage: "linear-gradient(".concat(gradientRotation, "deg, rgba(0,0,0,0), ").concat(color, " 90%)") }, role: "presentation", "aria-hidden": true }), /*#__PURE__*/React.createElement("div", { onScroll: this.scrollHandler, ref: this.setRefs, className: classnames("".concat(namespace, "__content"), scrollElementClassName) }, children), /*#__PURE__*/React.createElement("div", { className: "".concat(namespace, "__after"), style: { backgroundImage: "linear-gradient(".concat(gradientRotation, "deg, ").concat(color, " 10%, rgba(0,0,0,0))") }, role: "presentation", "aria-hidden": true })); } }]); }(Component); _ScrollGradient = ScrollGradient; _defineProperty(ScrollGradient, "propTypes", { /** @type {string} Scroll area children */ children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]), /** @type {string} Optional classname */ className: PropTypes.string, /** @type {string} Fade out color. Any valid CSS color value works */ color: PropTypes.string.isRequired, /** @type {string} Scroll direction */ direction: PropTypes.oneOf(['X', 'Y']), /** @type {(element: HTMLElement) => {}} Optional function to get reference to scrollable DOM element */ getScrollElementRef: PropTypes.func, /** @type {boolean} Set to true if you want to hide gradient on the start side (top or left) of scrollable element. */ hideStartGradient: PropTypes.bool, /** @type {Function} Optional scroll handler */ onScroll: PropTypes.func, /** @type {string} Optional classname for scroll element. */ scrollElementClassName: PropTypes.string }); _defineProperty(ScrollGradient, "defaultProps", { className: undefined, children: undefined, scrollElementClassName: undefined, direction: scrollDirection.Y, hideStartGradient: false, onScroll: function onScroll() {}, getScrollElementRef: function getScrollElementRef() {} }); /** * Gets the scroll state position of a given element. * @param {HTMLElement} element The element to get scroll state of. * @param {string} scrollDirection The scroll direction to get the state of. * @returns {string} State of scroll position. * @static */ _defineProperty(ScrollGradient, "getScrollState", function (element, scrollDirection) { switch (scrollDirection) { case _ScrollGradient.ScrollDirection.X: { if (element.scrollWidth === element.clientWidth) { return _ScrollGradient.ScrollStates.NONE; } if (element.scrollLeft === 0) { return _ScrollGradient.ScrollStates.INITIAL; } if (element.scrollLeft + element.clientWidth === element.scrollWidth) { return _ScrollGradient.ScrollStates.END; } return _ScrollGradient.ScrollStates.STARTED; } case _ScrollGradient.ScrollDirection.Y: default: { if (element.scrollHeight === element.clientHeight) { return _ScrollGradient.ScrollStates.NONE; } if (element.scrollTop === 0) { return _ScrollGradient.ScrollStates.INITIAL; } if (element.scrollTop + element.clientHeight === element.scrollHeight) { return _ScrollGradient.ScrollStates.END; } return _ScrollGradient.ScrollStates.STARTED; } } }); /** @enum Possible scroll directions */ _defineProperty(ScrollGradient, "ScrollDirection", scrollDirection); /** @enum Scroll position states */ _defineProperty(ScrollGradient, "ScrollStates", { // No scrolling required because content fits within container. NONE: 'NONE', // Scroll position is a the start of the scrollable content. INITIAL: 'INITIAL', // Scroll position is neither at start or end of scrollable content. STARTED: 'STARTED', // Scroll position is a the end of the scrollable content. END: 'END' }); export default ScrollGradient;