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

refactor(AnimateHeight): ♻️ Move styling to utils.css #1969

Merged
merged 4 commits into from
May 7, 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
16 changes: 16 additions & 0 deletions packages/css/utils.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,20 @@
outline-offset: var(--fds-focus-border-width);
box-shadow: 0 0 0 var(--fds-focus-border-width) var(--fds-semantic-border-focus-boxshadow);
}

@layer animate-height {
.fds-animate-height--openingOrClosing,
.fds-animate-height--closed {
overflow: hidden;
}

.fds-animate-height--open .fds-animate-height__content {
height: auto;
}

.fds-animate-height--closed .fds-animate-height__content {
height: 0;
display: none;
}
}
}

This file was deleted.

38 changes: 25 additions & 13 deletions packages/react/src/utilities/AnimateHeight/AnimateHeight.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('AnimateHeight', () => {
it('Appends given className to root element', () => {
const className = 'foo';
const { container } = render({ className });
expect(container.firstChild).toHaveClass('root');
expect(container.firstChild).toHaveClass('fds-animate-height');
expect(container.firstChild).toHaveClass(className);
});

Expand All @@ -46,48 +46,60 @@ describe('AnimateHeight', () => {

it('Sets class to "open" when open', () => {
const { container } = render({ open: true });
expect(container.firstChild).toHaveClass('open');
expect(container.firstChild).toHaveClass('fds-animate-height--open');
});

it('Sets class to "closed" when closed', () => {
const { container } = render({ open: false });
expect(container.firstChild).toHaveClass('closed');
expect(container.firstChild).toHaveClass('fds-animate-height--closed');
});

it('Sets class to "openingOrClosing" when opening and "open" when timer has run', async () => {
const { container, rerender } = render({ open: false });
rerender(<AnimateHeight open />);
expect(container.firstChild).toHaveClass('openingOrClosing');
expect(container.firstChild).toHaveClass(
'fds-animate-height--openingOrClosing',
);
await vi.waitFor(() => {
expect(container.firstChild).not.toHaveClass('openingOrClosing');
expect(container.firstChild).not.toHaveClass(
'fds-animate-height--openingOrClosing',
);
});
expect(container.firstChild).toHaveClass('open');
expect(container.firstChild).toHaveClass('fds-animate-height--open');
});

it('Sets class to "openingOrClosing" when closing and "closed" when timer has run', async () => {
const { container, rerender } = render({ open: true });
rerender(<AnimateHeight open={false} />);
expect(container.firstChild).toHaveClass('openingOrClosing');
expect(container.firstChild).toHaveClass(
'fds-animate-height--openingOrClosing',
);
await vi.waitFor(() => {
expect(container.firstChild).not.toHaveClass('openingOrClosing');
expect(container.firstChild).not.toHaveClass(
'fds-animate-height--openingOrClosing',
);
});
expect(container.firstChild).toHaveClass('closed');
expect(container.firstChild).toHaveClass('fds-animate-height--closed');
});

it('Sets class to "open" immediately when opening and "prefers-reduced-motion" is set', () => {
vi.spyOn(hooks, 'useMediaQuery').mockReturnValue(true);
const { container, rerender } = render({ open: false });
rerender(<AnimateHeight open />);
expect(container.firstChild).toHaveClass('open');
expect(container.firstChild).not.toHaveClass('openingOrClosing');
expect(container.firstChild).toHaveClass('fds-animate-height--open');
expect(container.firstChild).not.toHaveClass(
'fds-animate-height--openingOrClosing',
);
});

it('Sets class to "closed" immediately when closing and "prefers-reduced-motion" is set', () => {
vi.spyOn(hooks, 'useMediaQuery').mockReturnValue(true);
const { container, rerender } = render({ open: true });
rerender(<AnimateHeight open={false} />);
expect(container.firstChild).toHaveClass('closed');
expect(container.firstChild).not.toHaveClass('openingOrClosing');
expect(container.firstChild).toHaveClass('fds-animate-height--closed');
expect(container.firstChild).not.toHaveClass(
'fds-animate-height--openingOrClosing',
);
});
});

Expand Down
10 changes: 6 additions & 4 deletions packages/react/src/utilities/AnimateHeight/AnimateHeight.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import cl from 'clsx';

import { useMediaQuery, usePrevious } from '../../hooks';

import classes from './AnimateHeight.module.css';

export type AnimateHeightProps = {
open: boolean;
} & React.HTMLAttributes<HTMLDivElement>;
Expand Down Expand Up @@ -59,12 +57,16 @@ export const AnimateHeight = ({
return (
<div
{...rest}
className={cl(classes.root, classes[state], className)}
className={cl(
'fds-animate-height',
`fds-animate-height--${state}`,
className,
)}
style={{ height, transition, ...style }}
>
<div
ref={contentRef}
className={classes.content}
className='fds-animate-height__content'
>
{children}
</div>
Expand Down
Loading