{"version":3,"file":"ScrollWrapper.mjs","sources":["../../../src/components/ScrollWrapper/ScrollWrapper.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport debounce from 'lodash/debounce.js';\n\nimport React, {\n  type ReactNode,\n  type HTMLAttributes,\n  useRef,\n  useEffect,\n  useState,\n} from 'react';\nimport styles from './ScrollWrapper.module.css';\n\nexport type ScrollWrapperProps = HTMLAttributes<HTMLDivElement> & {\n  // Component API\n  /**\n   * Contents of the wrapper element\n   */\n  children: ReactNode;\n  /**\n   * CSS class names that can be appended to the component.\n   */\n  className?: string;\n  /**\n   * Determines the direction that the shadows apply\n   */\n  orientation?: 'horizontal' | 'vertical';\n  // Design API\n  /**\n   * Type of shadow treatment for the wrapper:\n   * - **cover** - full-width shadow in the wrapper based on shadow context\n   * - **contain** - shadow whose edges fit within the width of the wrapper\n   */\n  shadowType?: 'cover' | 'contain';\n};\n\ntype ShadowStates = {\n  top: boolean;\n  bottom: boolean;\n  start: boolean;\n  end: boolean;\n};\n\nexport const setShadowStates = (targetData: HTMLDivElement): ShadowStates => {\n  const {\n    scrollTop,\n    scrollLeft,\n    scrollHeight,\n    clientHeight,\n    scrollWidth,\n    clientWidth,\n  } = targetData;\n  const showShadows = { top: false, bottom: false, start: false, end: false };\n\n  // handle verticals\n  if (scrollTop === 0) {\n    showShadows.top = false;\n  } else {\n    showShadows.top = true;\n  }\n\n  if (scrollTop < scrollHeight - clientHeight) {\n    showShadows.bottom = true;\n  } else {\n    showShadows.bottom = false;\n  }\n\n  // handle horizontals\n  if (scrollLeft === 0) {\n    showShadows.start = false;\n  } else {\n    showShadows.start = true;\n  }\n\n  if (scrollLeft < scrollWidth - clientWidth) {\n    showShadows.end = true;\n  } else {\n    showShadows.end = false;\n  }\n\n  return showShadows;\n};\n\n/**\n * ## Usage\n *\n * Wrap content that has a fixed height but is scrollable, so that users have an indication that\n * more content is available. Subtle shadows come in several variants.\n *\n * | Type/Use | Description | Example |\n * |----------|-------------|---------|\n * | Vertical | The default. Scrolls on the y axis and shades the top and bottom edges. | Long dialog bodies. Tall option lists. |\n * | Horizontal | `orientation=\"horizontal\"` scrolls on the x axis and shades the start and end edges. | Wide tables. Toolbars that overflow. |\n *\n * `orientation` picks one axis at a time, so a single wrapper shades either the vertical or the\n * horizontal edges, not both.\n *\n * `shadowType` controls the treatment: `cover` (the default) spans the full width of the wrapper,\n * while `contain` keeps the shadow's edges inside it.\n *\n * The effect depends on the container being shorter than its content, so the element above the\n * scroll wrapper must have a fixed height. Without that, nothing overflows and no shadow appears.\n *\n * ## Interaction\n *\n * Shadows are set for the starting position as soon as the wrapper mounts, so content that\n * overflows shows its bottom (or end) shadow before any scrolling.\n *\n * When vertical, scrolling will enable both shadows when there is more content above and below the\n * current position. When there is only content above the current position (scrolled to the end of\n * the container), only the top shadow is shown. When scrolled to the very top of the container,\n * only the bottom shadow is shown. The same logic applies to the horizontal scrolling scenario,\n * against the start and end edges.\n *\n * `Modal` renders a `ScrollWrapper` around its body for you, so its content scrolls without any\n * extra setup.\n *\n * ## Content & Accessibility\n *\n * ### Do's\n *\n * * Use `ScrollWrapper` in any overlay components that need to exceed the available screen real estate. This is not for ones that definitely will, but ones that have variable content which could grow to be either too tall or too wide.\n * * Use `cover` for the shadow type to emphasize and make clear that more content is available.\n * * Leave the scrollable region in the tab order. It carries `tabIndex={0}` so that people navigating by keyboard can scroll it.\n *\n * ### Don'ts\n *\n * * Avoid wrapping entire pages with `ScrollWrapper`.\n * * Never use multiple, adjacent `ScrollWrapper` instances.\n */\nexport const ScrollWrapper = ({\n  children,\n  className,\n  orientation = 'vertical',\n  shadowType = 'cover',\n  ...other\n}: ScrollWrapperProps) => {\n  const scrollRef = useRef<HTMLDivElement>(null);\n  const [shadowState, setShadowState] = useState<ShadowStates>({\n    top: false,\n    bottom: false,\n    start: false,\n    end: false,\n  });\n\n  const outerClassName = clsx(\n    styles['scroll-wrapper'],\n    orientation && styles[`scroll-wrapper--orientation-${orientation}`],\n    shadowState.top && styles['scroll-wrapper--has-top-shadow'],\n    shadowState.bottom && styles['scroll-wrapper--has-bottom-shadow'],\n    shadowState.start && styles['scroll-wrapper--has-start-shadow'],\n    shadowState.end && styles['scroll-wrapper--has-end-shadow'],\n    shadowType && styles[`scroll-wrapper--shadow-type-${shadowType}`],\n    className,\n  );\n\n  // This handler fires upon every scroll event. changes are \"debounced\" by the set state calls\n  const handler = (ev: Event) => {\n    if (ev.target) {\n      setShadowState(setShadowStates(ev.target as HTMLDivElement));\n    }\n  };\n\n  // remove the shadows when resizing occurs, so they aren't in the previous state\n  const debouncedHandler = debounce(() => {\n    setShadowState({ top: false, bottom: false, start: false, end: false });\n  }, 250);\n\n  // Show the shadows for the starting position, so overflowing content is signposted before the first scroll\n  useEffect(() => {\n    if (scrollRef.current) {\n      setShadowState(setShadowStates(scrollRef.current));\n    }\n  }, []);\n\n  // Hook up the event handlers, monitoring resizes (reset the shadows) and scroll (add based on position)\n  useEffect(() => {\n    const currentElement = scrollRef.current;\n    if (currentElement) {\n      currentElement.addEventListener('scroll', handler);\n      window.addEventListener('resize', debouncedHandler);\n    }\n\n    return () => {\n      if (currentElement) {\n        currentElement.removeEventListener('scroll', handler);\n        window.removeEventListener('resize', debouncedHandler);\n      }\n    };\n  }, [debouncedHandler]);\n\n  return (\n    <div className={outerClassName} {...other}>\n      <div\n        className={clsx(styles['scroll-wrapper__inner'])}\n        ref={scrollRef}\n        // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex\n        tabIndex={0}\n      >\n        {children}\n      </div>\n    </div>\n  );\n};\n\nScrollWrapper.displayName = 'ScrollWrapper';\n"],"names":["React"],"mappings":";;;;;AA0CO,MAAM,eAAe,GAAG,CAAC,UAA0B,KAAkB;AAC1E,IAAA,MAAM,EACJ,SAAS,EACT,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,WAAW,GACZ,GAAG,UAAU;AACd,IAAA,MAAM,WAAW,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,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