UNPKG

@carbon/ibm-products

Version:

Carbon for IBM Products

211 lines (202 loc) 8.84 kB
/** * Copyright IBM Corp. 2020, 2025 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js'); var React = require('react'); var index = require('../../_virtual/index.js'); var cx = require('classnames'); var devtools = require('../../global/js/utils/devtools.js'); var settings = require('../../settings.js'); var constants = require('./constants.js'); var useIsomorphicEffect = require('../../global/js/hooks/useIsomorphicEffect.js'); const blockClass = `${settings.pkg.prefix}--scroll-gradient`; const componentName = 'ScrollGradient'; // Default values for props const defaults = { color: `var(--${settings.carbon.prefix}-layer-01)`, hideStartGradient: false, onScroll: () => {}, getScrollElementRef: () => {} }; /** * TODO: A description of the component. */ exports.ScrollGradient = /*#__PURE__*/React.forwardRef((_ref, ref) => { let { children, className, color = defaults.color, onScroll = defaults.onScroll, scrollElementClassName, getScrollElementRef = defaults.getScrollElementRef, hideStartGradient = defaults.hideStartGradient, ...rest } = _ref; const intersectionStartRef = React.useRef(); const intersectionEndRef = React.useRef(); const intersectionLeftRef = React.useRef(); const intersectionRightRef = React.useRef(); const scrollContainer = React.useRef(undefined); const contentChildrenContainer = React.useRef(undefined); const { xScrollable, yScrollable } = constants.useIsOverflow(scrollContainer); const gradientRight = yScrollable && scrollContainer.current && contentChildrenContainer.current ? scrollContainer.current.offsetWidth - contentChildrenContainer.current.offsetWidth : 0; const gradientBottom = xScrollable && scrollContainer.current && contentChildrenContainer.current ? scrollContainer.current.offsetHeight - contentChildrenContainer.current.offsetHeight : 0; const [verticalPosition] = React.useState(constants.ScrollStates.NONE); const [horizontalPosition] = React.useState(constants.ScrollStates.NONE); const startVerticalRef = React.useRef(null); const startHorizontalRef = React.useRef(null); const endVerticalRef = React.useRef(null); const endHorizontalRef = React.useRef(null); useIsomorphicEffect.useIsomorphicEffect(() => { // start vertical styles startVerticalRef.current.style.right = gradientRight; startVerticalRef.current.style.backgroundImage = `linear-gradient(0deg, transparent, ${color} 90%)`; // start horizontal styles startHorizontalRef.current.backgroundImage = `linear-gradient(-90deg, transparent, ${color} 90%)`; startHorizontalRef.current.bottom = gradientBottom; // end vertical styles endVerticalRef.current.style.right = gradientRight; endVerticalRef.current.style.bottom = gradientBottom; endVerticalRef.current.style.backgroundImage = `linear-gradient(0deg, ${color} 10%, transparent)`; // end horizontal styles endHorizontalRef.current.style.right = gradientRight; endHorizontalRef.current.style.bottom = gradientBottom; endHorizontalRef.current.style.backgroundImage = `linear-gradient(-90deg, ${color} 10%, transparent)`; }, [color, gradientRight, gradientBottom]); const setGradientOnIntersection = (entry, gradientRef) => { if (gradientRef.current) { if (entry.isIntersecting) { gradientRef.current.style.opacity = 0; gradientRef.current.style.display = 'none'; gradientRef?.current.setAttribute('aria-hidden', false); } else { gradientRef.current.style.opacity = 1; gradientRef.current.style.display = 'block'; gradientRef?.current.setAttribute('aria-hidden', true); } } }; React.useEffect(() => { const observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (entry.target.hasAttribute('data-start-vertical')) { setGradientOnIntersection(entry, startVerticalRef); } if (entry.target.hasAttribute('data-end-vertical')) { setGradientOnIntersection(entry, endVerticalRef); } if (entry.target.hasAttribute('data-start-horizontal')) { setGradientOnIntersection(entry, startHorizontalRef); } if (entry.target.hasAttribute('data-end-horizontal')) { setGradientOnIntersection(entry, endHorizontalRef); } }); }, { root: null, rootMargin: '0px', threshold: 0.1 }); observer.observe(intersectionStartRef.current); observer.observe(intersectionEndRef.current); observer.observe(intersectionLeftRef.current); observer.observe(intersectionRightRef.current); const startVerticalRefValue = intersectionStartRef.current; const endVerticalRefValue = intersectionEndRef.current; const startHorizontalRefValue = intersectionLeftRef.current; const endHorizontalRefValue = intersectionRightRef.current; return () => { observer.unobserve(startVerticalRefValue); observer.unobserve(endVerticalRefValue); observer.unobserve(startHorizontalRefValue); observer.unobserve(endHorizontalRefValue); }; }, []); const setRefs = element => { scrollContainer.current = element; getScrollElementRef(element); }; return /*#__PURE__*/React.createElement("div", _rollupPluginBabelHelpers.extends({}, rest, { className: cx(blockClass, `${blockClass}--x-${horizontalPosition.toLowerCase()}`, `${blockClass}--y-${verticalPosition.toLowerCase()}`, { [`${blockClass}--x-scrollable`]: xScrollable, [`${blockClass}--y-scrollable`]: yScrollable }, className), ref: ref, role: "presentation" }, devtools.getDevtoolsProps(componentName)), /*#__PURE__*/React.createElement("div", { onScroll: onScroll, ref: setRefs, className: cx(`${blockClass}__content`, scrollElementClassName), tabIndex: 0 }, /*#__PURE__*/React.createElement("span", { ref: intersectionStartRef, "data-start-vertical": true }), /*#__PURE__*/React.createElement("span", { ref: intersectionLeftRef, "data-start-horizontal": true }), /*#__PURE__*/React.createElement("div", { ref: contentChildrenContainer, className: `${blockClass}__content-children` }, children), /*#__PURE__*/React.createElement("span", { ref: intersectionEndRef, "data-end-vertical": true }), /*#__PURE__*/React.createElement("span", { ref: intersectionRightRef, "data-end-horizontal": true })), !hideStartGradient && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", { ref: startVerticalRef, className: `${blockClass}__start-vertical`, role: "presentation", "aria-hidden": true }), /*#__PURE__*/React.createElement("div", { ref: startHorizontalRef, className: `${blockClass}__start-horizontal`, role: "presentation", "aria-hidden": true })), /*#__PURE__*/React.createElement("div", { ref: endVerticalRef, className: `${blockClass}__end-vertical`, role: "presentation", "aria-hidden": true }), /*#__PURE__*/React.createElement("div", { ref: endHorizontalRef, className: `${blockClass}__end-horizontal`, role: "presentation", "aria-hidden": true })); }); // Return a placeholder if not released and not enabled by feature flag exports.ScrollGradient = settings.pkg.checkComponentEnabled(exports.ScrollGradient, componentName); // The display name of the component, used by React. Note that displayName // is used in preference to relying on function.name. exports.ScrollGradient.displayName = componentName; // The types and DocGen commentary for the component props, // in alphabetical order (for consistency). // See https://www.npmjs.com/package/prop-types#usage. exports.ScrollGradient.propTypes = { /** * Provide the contents of the ScrollGradient. */ children: index.default.oneOfType([index.default.arrayOf(index.default.node), index.default.node]), /** * Provide an optional class to be applied to the containing node. */ className: index.default.string, /** @type {string} Fade out color. Any valid CSS color value works */ color: index.default.string, /** @type {(element: HTMLElement) => {}} Optional function to get reference to scrollable DOM element */ getScrollElementRef: index.default.func, /** @type {boolean} Set to true if you want to hide gradient on the start side (top or left) of scrollable element. */ hideStartGradient: index.default.bool, /** @type {Function} Optional scroll handler */ onScroll: index.default.func, /** @type {string} Optional classname for scroll element. */ scrollElementClassName: index.default.string };