@carbon/ibm-products
Version:
Carbon for IBM Products
185 lines (183 loc) • 8.21 kB
JavaScript
/**
* Copyright IBM Corp. 2020, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import { __toESM } from "../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../node_modules/classnames/index.js";
import { pkg } from "../../settings.js";
import { useIsomorphicEffect } from "../../global/js/hooks/useIsomorphicEffect.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { ScrollStates, useIsOverflow } from "./constants.js";
import React, { useEffect, useRef, useState } from "react";
import PropTypes from "prop-types";
import { usePrefix } from "@carbon/react";
//#region src/components/ScrollGradient/ScrollGradient.jsx
/**
* Copyright IBM Corp. 2024, 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.
*/
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--scroll-gradient`;
const componentName = "ScrollGradient";
const defaults = {
hideStartGradient: false,
onScroll: () => {},
getScrollElementRef: () => {}
};
/**
* TODO: A description of the component.
*/
let ScrollGradient = React.forwardRef(({ children, className, color, onScroll = defaults.onScroll, scrollElementClassName, getScrollElementRef = defaults.getScrollElementRef, hideStartGradient = defaults.hideStartGradient, ...rest }, ref) => {
const intersectionStartRef = useRef(void 0);
const intersectionEndRef = useRef(void 0);
const intersectionLeftRef = useRef(void 0);
const intersectionRightRef = useRef(void 0);
const fallbackColor = `var(--${usePrefix()}-layer-01)`;
const scrollContainer = useRef(void 0);
const contentChildrenContainer = useRef(void 0);
const { xScrollable, yScrollable } = 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] = useState(ScrollStates.NONE);
const [horizontalPosition] = useState(ScrollStates.NONE);
const startVerticalRef = useRef(null);
const startHorizontalRef = useRef(null);
const endVerticalRef = useRef(null);
const endHorizontalRef = useRef(null);
useIsomorphicEffect(() => {
startVerticalRef.current.style.right = gradientRight;
startVerticalRef.current.style.backgroundImage = `linear-gradient(0deg, transparent, ${color ?? fallbackColor} 90%)`;
startHorizontalRef.current.backgroundImage = `linear-gradient(-90deg, transparent, ${color ?? fallbackColor} 90%)`;
startHorizontalRef.current.bottom = gradientBottom;
endVerticalRef.current.style.right = gradientRight;
endVerticalRef.current.style.bottom = gradientBottom;
endVerticalRef.current.style.backgroundImage = `linear-gradient(0deg, ${color ?? fallbackColor} 10%, transparent)`;
endHorizontalRef.current.style.right = gradientRight;
endHorizontalRef.current.style.bottom = gradientBottom;
endHorizontalRef.current.style.backgroundImage = `linear-gradient(-90deg, ${color ?? fallbackColor} 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);
}
};
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: .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", {
...rest,
className: (0, import_classnames.default)(blockClass, `${blockClass}--x-${horizontalPosition.toLowerCase()}`, `${blockClass}--y-${verticalPosition.toLowerCase()}`, {
[`${blockClass}--x-scrollable`]: xScrollable,
[`${blockClass}--y-scrollable`]: yScrollable
}, className),
ref,
role: "presentation",
...getDevtoolsProps(componentName)
}, /* @__PURE__ */ React.createElement("div", {
onScroll,
ref: setRefs,
className: (0, import_classnames.default)(`${blockClass}__content`, scrollElementClassName)
}, /* @__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
}));
});
ScrollGradient = pkg.checkComponentEnabled(ScrollGradient, componentName);
ScrollGradient.displayName = componentName;
ScrollGradient.propTypes = {
/**
* Provide the contents of the ScrollGradient.
*/
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]),
/**
* Provide an optional class to be applied to the containing node.
*/
className: PropTypes.string,
/** @type {string} Fade out color. Any valid CSS color value works */
color: PropTypes.string,
/** @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
};
//#endregion
export { ScrollGradient };