|
| 1 | +import type { TranslatedCategory } from '@prezly/sdk'; |
| 2 | +import { FormattedMessage, translations } from '@prezly/theme-kit-nextjs'; |
| 3 | +import { useMeasure } from '@react-hookz/web'; |
| 4 | +import classNames from 'classnames'; |
| 5 | +import { useParams } from 'next/navigation'; |
| 6 | +import { useMemo } from 'react'; |
| 7 | + |
| 8 | +import { useLocale } from 'adapters/client'; |
| 9 | + |
| 10 | +import { Dropdown } from '../Dropdown'; |
| 11 | +import { Link } from '../Link'; |
| 12 | + |
| 13 | +import { CategoryItem } from './CategoryItem'; |
| 14 | + |
| 15 | +import styles from './CategoriesBar.module.scss'; |
| 16 | + |
| 17 | +const MORE_BUTTON_WIDTH = +styles.MORE_BUTTON_WIDTH.replace('px', ''); |
| 18 | + |
| 19 | +type Props = { |
| 20 | + translatedCategories: TranslatedCategory[]; |
| 21 | +}; |
| 22 | + |
| 23 | +export function CategoriesBar({ translatedCategories }: Props) { |
| 24 | + const locale = useLocale(); |
| 25 | + const params = useParams(); |
| 26 | + const [measurements, containerRef] = useMeasure<HTMLDivElement>(); |
| 27 | + const containerWidth = measurements?.width ?? 0; |
| 28 | + |
| 29 | + const [visibleCategories, hiddenCategories] = useMemo< |
| 30 | + [TranslatedCategory[], TranslatedCategory[]] |
| 31 | + >(() => { |
| 32 | + if (!containerRef.current) { |
| 33 | + return [translatedCategories, []]; |
| 34 | + } |
| 35 | + |
| 36 | + const { paddingLeft, paddingRight } = getComputedStyle(containerRef.current); |
| 37 | + const containerWidthWithoutPadding = |
| 38 | + containerWidth - parseInt(paddingLeft) - parseInt(paddingRight); |
| 39 | + |
| 40 | + if (containerRef.current.scrollWidth <= containerWidthWithoutPadding) { |
| 41 | + return [translatedCategories, []]; |
| 42 | + } |
| 43 | + |
| 44 | + let index = 0; |
| 45 | + let width = 0; |
| 46 | + const widthLimit = containerWidthWithoutPadding - MORE_BUTTON_WIDTH; |
| 47 | + const nodesArray = Array.from(containerRef.current.children); |
| 48 | + |
| 49 | + for (let i = 0; i < nodesArray.length; i += 1) { |
| 50 | + const { width: nodeWidth } = nodesArray[i].getBoundingClientRect(); |
| 51 | + |
| 52 | + if (nodeWidth + width > widthLimit) { |
| 53 | + break; |
| 54 | + } |
| 55 | + width += nodeWidth; |
| 56 | + index = i + 1; |
| 57 | + } |
| 58 | + |
| 59 | + return [translatedCategories.slice(0, index), translatedCategories.slice(index)]; |
| 60 | + }, [translatedCategories, containerRef, containerWidth]); |
| 61 | + |
| 62 | + if (!visibleCategories.length) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <div className={styles.wrapper}> |
| 68 | + <div className={styles.container} ref={containerRef}> |
| 69 | + {visibleCategories.map((category) => ( |
| 70 | + <Link |
| 71 | + key={category.id} |
| 72 | + href={{ |
| 73 | + routeName: 'category', |
| 74 | + params: { slug: category.slug }, |
| 75 | + }} |
| 76 | + className={classNames(styles.link, { |
| 77 | + [styles.active]: category.slug === params.slug, |
| 78 | + })} |
| 79 | + title={category.description || undefined} |
| 80 | + > |
| 81 | + {category.name} |
| 82 | + </Link> |
| 83 | + ))} |
| 84 | + {hiddenCategories.length > 0 && ( |
| 85 | + <Dropdown |
| 86 | + label={<FormattedMessage locale={locale} for={translations.actions.more} />} |
| 87 | + buttonClassName={styles.more} |
| 88 | + > |
| 89 | + {hiddenCategories.map((category) => ( |
| 90 | + <CategoryItem category={category} key={category.id} /> |
| 91 | + ))} |
| 92 | + </Dropdown> |
| 93 | + )} |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +} |
0 commit comments