UNPKG

@conduction/components

Version:

React (Gatsby) components used within the Conduction Skeleton Application (and its implementations)

37 lines (36 loc) 2.31 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import * as React from "react"; import * as styles from "./HorizontalOverflowWrapper.module.css"; import clsx from "clsx"; import { Button } from "@utrecht/component-library-react/dist/css-module"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faChevronLeft, faChevronRight } from "@fortawesome/free-solid-svg-icons"; export const HorizontalOverflowWrapper = ({ children, ariaLabels }) => { const [canScrollRight, setCanScrollRight] = React.useState(false); const [canScrollLeft, setCanScrollLeft] = React.useState(false); const wrapperRef = React.useRef(null); const scrollRight = () => { wrapperRef.current?.scrollTo({ left: wrapperRef.current.scrollLeft + wrapperRef.current.clientWidth * 0.9, behavior: "smooth", }); }; const scrollLeft = () => { wrapperRef.current?.scrollTo({ left: wrapperRef.current.scrollLeft - wrapperRef.current.clientWidth * 0.9, behavior: "smooth", }); }; React.useEffect(() => { checkScrollDirections(); // initiate available scroll directions window.addEventListener("resize", checkScrollDirections); return () => window.removeEventListener("resize", checkScrollDirections); }, []); const checkScrollDirections = () => { if (!wrapperRef.current) return; setCanScrollRight(wrapperRef.current.scrollLeft + wrapperRef.current.clientWidth < wrapperRef.current.scrollWidth); setCanScrollLeft(wrapperRef.current.scrollLeft > 0); }; return (_jsxs("div", { className: styles.container, children: [canScrollLeft && (_jsx(Button, { className: clsx(styles.scrollButton), onClick: scrollLeft, appearance: "secondary-action-button", "aria-label": ariaLabels.scrollLeftButton, children: _jsx(FontAwesomeIcon, { icon: faChevronLeft }) })), canScrollRight && (_jsx(Button, { className: clsx(styles.scrollButton, styles.right), onClick: scrollRight, appearance: "secondary-action-button", "aria-label": ariaLabels.scrollRightButton, children: _jsx(FontAwesomeIcon, { icon: faChevronRight }) })), _jsx("div", { ref: wrapperRef, className: styles.wrapper, onScroll: checkScrollDirections, children: children })] })); };