UNPKG

@carbon/ibm-products

Version:
192 lines (190 loc) 8.9 kB
/** * 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. */ const require_runtime = require("../../_virtual/_rolldown/runtime.js"); const require_index = require("../../node_modules/classnames/index.js"); const require_settings = require("../../settings.js"); const require_useIsomorphicEffect = require("../../global/js/hooks/useIsomorphicEffect.js"); const require_devtools = require("../../global/js/utils/devtools.js"); const require_constants = require("./constants.js"); let react = require("react"); react = require_runtime.__toESM(react); let prop_types = require("prop-types"); prop_types = require_runtime.__toESM(prop_types); let _carbon_react = require("@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__ */ require_runtime.__toESM(require_index.default); const blockClass = `${require_settings.pkg.prefix}--scroll-gradient`; const componentName = "ScrollGradient"; const defaults = { hideStartGradient: false, onScroll: () => {}, getScrollElementRef: () => {} }; /** * TODO: A description of the component. */ let ScrollGradient = react.default.forwardRef(({ children, className, color, onScroll = defaults.onScroll, scrollElementClassName, getScrollElementRef = defaults.getScrollElementRef, hideStartGradient = defaults.hideStartGradient, ...rest }, ref) => { const intersectionStartRef = (0, react.useRef)(void 0); const intersectionEndRef = (0, react.useRef)(void 0); const intersectionLeftRef = (0, react.useRef)(void 0); const intersectionRightRef = (0, react.useRef)(void 0); const fallbackColor = `var(--${(0, _carbon_react.usePrefix)()}-layer-01)`; const scrollContainer = (0, react.useRef)(void 0); const contentChildrenContainer = (0, react.useRef)(void 0); const { xScrollable, yScrollable } = require_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] = (0, react.useState)(require_constants.ScrollStates.NONE); const [horizontalPosition] = (0, react.useState)(require_constants.ScrollStates.NONE); const startVerticalRef = (0, react.useRef)(null); const startHorizontalRef = (0, react.useRef)(null); const endVerticalRef = (0, react.useRef)(null); const endHorizontalRef = (0, react.useRef)(null); require_useIsomorphicEffect.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); } }; (0, 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: .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.default.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", ...require_devtools.getDevtoolsProps(componentName) }, /* @__PURE__ */ react.default.createElement("div", { onScroll, ref: setRefs, className: (0, import_classnames.default)(`${blockClass}__content`, scrollElementClassName) }, /* @__PURE__ */ react.default.createElement("span", { ref: intersectionStartRef, "data-start-vertical": true }), /* @__PURE__ */ react.default.createElement("span", { ref: intersectionLeftRef, "data-start-horizontal": true }), /* @__PURE__ */ react.default.createElement("div", { ref: contentChildrenContainer, className: `${blockClass}__content-children` }, children), /* @__PURE__ */ react.default.createElement("span", { ref: intersectionEndRef, "data-end-vertical": true }), /* @__PURE__ */ react.default.createElement("span", { ref: intersectionRightRef, "data-end-horizontal": true })), !hideStartGradient && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { ref: startVerticalRef, className: `${blockClass}__start-vertical`, role: "presentation", "aria-hidden": true }), /* @__PURE__ */ react.default.createElement("div", { ref: startHorizontalRef, className: `${blockClass}__start-horizontal`, role: "presentation", "aria-hidden": true })), /* @__PURE__ */ react.default.createElement("div", { ref: endVerticalRef, className: `${blockClass}__end-vertical`, role: "presentation", "aria-hidden": true }), /* @__PURE__ */ react.default.createElement("div", { ref: endHorizontalRef, className: `${blockClass}__end-horizontal`, role: "presentation", "aria-hidden": true })); }); ScrollGradient = require_settings.pkg.checkComponentEnabled(ScrollGradient, componentName); ScrollGradient.displayName = componentName; ScrollGradient.propTypes = { /** * Provide the contents of the ScrollGradient. */ children: prop_types.default.oneOfType([prop_types.default.arrayOf(prop_types.default.node), prop_types.default.node]), /** * Provide an optional class to be applied to the containing node. */ className: prop_types.default.string, /** @type {string} Fade out color. Any valid CSS color value works */ color: prop_types.default.string, /** @type {(element: HTMLElement) => {}} Optional function to get reference to scrollable DOM element */ getScrollElementRef: prop_types.default.func, /** @type {boolean} Set to true if you want to hide gradient on the start side (top or left) of scrollable element. */ hideStartGradient: prop_types.default.bool, /** @type {Function} Optional scroll handler */ onScroll: prop_types.default.func, /** @type {string} Optional classname for scroll element. */ scrollElementClassName: prop_types.default.string }; //#endregion Object.defineProperty(exports, "ScrollGradient", { enumerable: true, get: function() { return ScrollGradient; } });