Skip to content

Commit

Permalink
Merge pull request #903 from prezly/feature/care-4909-ikea-site-loses…
Browse files Browse the repository at this point in the history
…-categories-and-errors

[CARE-4909] Fix - Categories bar sometimes disappearing
  • Loading branch information
kudlajz authored May 3, 2024
2 parents 61c7ef3 + bc9d10d commit ba14fd3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 50 deletions.
20 changes: 10 additions & 10 deletions components/CategoriesBar/CategoriesBar.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import type { Category } from '@prezly/sdk';
import { translations } from '@prezly/theme-kit-intl';
import { useMemo, useRef } from 'react';
import { useMeasure } from '@react-hookz/web';
import { useMemo } from 'react';
import { useIntl } from 'react-intl';

import { CategoryItem } from '../CategoryItem';
import Dropdown from '../Dropdown';

import CategoryLink from './CategoryLink';
import { useCategoriesWithStoriesInCurrentLocale, useRect } from './lib';
import { useCategoriesWithStoriesInCurrentLocale } from './lib';

import styles from './CategoriesBar.module.scss';

const MORE_BUTTON_WIDTH = +styles.MORE_BUTTON_WIDTH.replace('px', '');

function CategoriesBar() {
const containerRef = useRef<HTMLDivElement>(null);
const { formatMessage } = useIntl();
const { width: containerWidth } = useRect(containerRef);
const [measurements, containerRef] = useMeasure<HTMLDivElement>();
const categories = useCategoriesWithStoriesInCurrentLocale();
const container = containerRef.current;
const containerWidth = measurements?.width ?? 0;

const [visibleCategories, hiddenCategories] = useMemo<[Category[], Category[]]>(() => {
if (!container) {
if (!containerRef.current) {
return [categories, []];
}

const { paddingLeft, paddingRight } = getComputedStyle(container);
const { paddingLeft, paddingRight } = getComputedStyle(containerRef.current);
const containerWidthWithoutPadding =
containerWidth - parseInt(paddingLeft) - parseInt(paddingRight);

if (!container || container.scrollWidth <= containerWidthWithoutPadding) {
if (containerRef.current.scrollWidth <= containerWidthWithoutPadding) {
return [categories, []];
}

let index = 0;
let width = 0;
const widthLimit = containerWidthWithoutPadding - MORE_BUTTON_WIDTH;
const nodesArray = Array.from(container.children);
const nodesArray = Array.from(containerRef.current.children);

for (let i = 0; i < nodesArray.length; i += 1) {
const { width: nodeWidth } = nodesArray[i].getBoundingClientRect();
Expand All @@ -49,7 +49,7 @@ function CategoriesBar() {
}

return [categories.slice(0, index), categories.slice(index)];
}, [categories, container, containerWidth]);
}, [categories, containerRef, containerWidth]);

if (!visibleCategories.length) {
return null;
Expand Down
1 change: 0 additions & 1 deletion components/CategoriesBar/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './useCategoriesWithStoriesInCurrentLocale';
export * from './useRect';
39 changes: 0 additions & 39 deletions components/CategoriesBar/lib/useRect.ts

This file was deleted.

0 comments on commit ba14fd3

Please sign in to comment.