Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Accordion): use max-height #6429

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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';

Check warning on line 21 in packages/vkui/src/components/Accordion/AccordionContent.tsx

View check run for this annotation

Codecov / codecov/patch

packages/vkui/src/components/Accordion/AccordionContent.tsx#L21

Added line #L21 was not covered by tests
}

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 @@
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
Loading