use-collapse
use-collapse
높이를 0에서 auto로, 그 반대로 애니메이션하는 use-collapse 훅에 대해 설명해 드릴게요. Collapse 컴포넌트의 훅 버전이에요.
출처: 문서
본문
사용법
use-collapse는 Collapse 컴포넌트의 훅 버전이에요. 높이를 0에서 auto로, 그 반대로 애니메이션할 수 있게 해줘요:
import { Button, Typography } from '@mantine/core';
import { useCollapse, useDisclosure } from '@mantine/hooks';
function Demo() {
const [expanded, handlers] = useDisclosure(false);
const { getCollapseProps } = useCollapse({ expanded });
return (
<>
<Button onClick={handlers.toggle} mb="md">
{expanded ? 'Collapse' : 'Expand'}
</Button>
<div {...getCollapseProps()}>
<Typography bg="var(--mantine-color-blue-light)" p="xs" bdrs="md">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
</Typography>
</div>
</>
);
}
가로 접기 (Horizontal collapse)
use-horizontal-collapse는 use-collapse와 같은 방식으로 동작하지만 높이 대신 너비를 애니메이션해요:
import { Button, Stack, Typography } from '@mantine/core';
import { useDisclosure, useHorizontalCollapse } from '@mantine/hooks';
function Demo() {
const [expanded, handlers] = useDisclosure(false);
const { getCollapseProps } = useHorizontalCollapse({ expanded });
return (
<Stack h={240}>
<Button onClick={handlers.toggle} w="fit-content">
{expanded ? 'Collapse' : 'Expand'}
</Button>
<div {...getCollapseProps({ style: { width: 200 } })}>
<Typography bg="var(--mantine-color-blue-light)" p="xs" bdrs="md" w={200}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.
</Typography>
</div>
</Stack>
);
}
ref prop
getCollapseProps 반환 값에는 이제 ref prop이 포함돼요. 훅이 올바르게 동작하려면 접을 수 있는 요소에 전달해야 해요.
정의
interface UseCollapseInput {
/** Expanded state */
expanded: boolean;
/** Transition duration in milliseconds, by default calculated based on content height */
transitionDuration?: number;
/** Transition timing function, `ease` by default */
transitionTimingFunction?: string;
/** Called when transition ends */
onTransitionEnd?: () => void;
/** Called when transition starts */
onTransitionStart?: () => void;
/** If true, collapsed content is kept in the DOM and hidden with `display: none` styles */
keepMounted?: boolean;
}
interface GetCollapsePropsInput {
style?: CSSProperties;
ref?: React.Ref<HTMLDivElement>;
}
interface GetCollapsePropsReturnValue {
'aria-hidden': boolean;
inert: boolean;
ref: React.RefCallback<HTMLDivElement>;
onTransitionEnd: (event: React.TransitionEvent<Element>) => void;
style: React.CSSProperties;
}
type UseCollapseState = 'entering' | 'entered' | 'exiting' | 'exited';
interface UseCollapseReturnValue {
state: UseCollapseState;
getCollapseProps: (input?: GetCollapsePropsInput) => GetCollapsePropsReturnValue;
}
function useCollapse(input: UseCollapseInput): UseCollapseReturnValue;
내보내는 타입
UseCollapseInput, UseCollapseState, UseCollapseReturnValue 타입은 @mantine/hooks 패키지에서 내보내지므로 애플리케이션에서 import할 수 있어요:
import type { UseCollapseInput, UseCollapseState, UseCollapseReturnValue } from '@mantine/hooks';
더 알아보기 (Learn more)
- use-click-outside — use-click-outside 훅
- use-color-scheme — use-color-scheme 훅