Skip to content

Commit

Permalink
fix(Accordion): use max-height (#6429)
Browse files Browse the repository at this point in the history
margin-top при выставлении процентов берет значение от ширины, а не высоты
Для фикса бага используем max-height, вместо margin-top

- Fixes #6414
  • Loading branch information
SevereCloud authored Jan 22, 2024
1 parent ea190d0 commit a261ad2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/vkui/src/components/Accordion/Accordion.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
}

.AccordionContent__in {
margin-block-start: -100%;
transition: margin-top 100ms ease-in-out;
max-block-size: 0;
transition: max-height 100ms ease-in-out;
}
30 changes: 12 additions & 18 deletions packages/vkui/src/components/Accordion/AccordionContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,38 @@ import { classNames } from '@vkontakte/vkjs';
import { useExternRef } from '../../hooks/useExternRef';
import { useGlobalEventListener } from '../../hooks/useGlobalEventListener';
import { useDOM } from '../../lib/dom';
import { useIsomorphicLayoutEffect } from '../../lib/useIsomorphicLayoutEffect';
import { HasRef, HasRootRef } from '../../types';
import { AccordionContext } from './AccordionContext';
import styles from './Accordion.module.css';

/**
* Функция расчета отрицательного margin, для скрытия контента.
* Функция расчета max-height, для скрытия или раскрытия контента.
*/
function calcMarginTop(expanded: boolean, el: HTMLElement | null): string {
if (expanded) {
return `0px`;
function calcMaxHeight(expanded: boolean, el: HTMLElement | null): string {
if (!expanded) {
return '0px';
}

// В первый рендеринг нельзя узнать высоту элемента, поэтому прячем таким образом
// В первый рендеринг нельзя узнать высоту элемента
if (el === null) {
return '-100%';
return 'inherit';
}

return `${-el.clientHeight}px`;
}

/**
* В первый рендеринг отключаем анимации.
*/
function calcTransition(el: HTMLElement | null) {
return el === null ? 'none' : undefined;
return `${el.scrollHeight}px`;
}

/**
* Хук для отслеживания изменения высоты контента.
*/
function useResizeContent(expanded: boolean, inRef: React.MutableRefObject<HTMLDivElement | null>) {
const resize = () => {
inRef.current!.style.marginTop = calcMarginTop(expanded, inRef.current);
inRef.current!.style.maxHeight = calcMaxHeight(expanded, inRef.current);
};

const { window } = useDOM();
useGlobalEventListener(window, 'resize', resize);
useIsomorphicLayoutEffect(resize, []);
}

/**
Expand All @@ -49,12 +44,11 @@ function useAccordionContent(
expanded: boolean,
inRef: React.MutableRefObject<HTMLDivElement | null>,
) {
const marginTop = calcMarginTop(expanded, inRef.current);
const transition = calcTransition(inRef.current);
const maxHeight = calcMaxHeight(expanded, inRef.current);

useResizeContent(expanded, inRef);

return { marginTop, transition };
return { maxHeight };
}

export interface AccordionContentProps
Expand Down

0 comments on commit a261ad2

Please sign in to comment.