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

feat: fadein: add fadein component #737

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
43 changes: 43 additions & 0 deletions src/components/FadeIn/FadeIn.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import { Stories } from '@storybook/addon-docs';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import { FadeIn } from './';

export default {
title: 'FadeIn',
parameters: {
docs: {
page: (): JSX.Element => (
<main>
<article>
<section>
<h1>FadeIn</h1>
<p>A FadeIn allows to render content with a fade in animation.</p>
</section>
<section>
<Stories includePrimary title="" />
</section>
</article>
</main>
),
},
},
} as ComponentMeta<typeof FadeIn>;

const FadeIn_Story: ComponentStory<typeof FadeIn> = (args) => {
return (
<>
<FadeIn {...args} />
</>
);
};

export const Default = FadeIn_Story.bind({});

Default.args = {
children: <>Hello, I was faded into view.</>,
classNames: 'my-fadein-classname',
delay: 0,
disabled: false,
duration: 300,
};
73 changes: 73 additions & 0 deletions src/components/FadeIn/FadeIn.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react';
import Enzyme from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import MatchMediaMock from 'jest-matchmedia-mock';
import { render } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { FadeIn } from './FadeIn';
import { sleep } from '../../tests/Utilities';

Enzyme.configure({ adapter: new Adapter() });

let matchMedia: any;

describe('FadeIn', () => {
beforeAll(() => {
matchMedia = new MatchMediaMock();
});

afterEach(() => {
matchMedia.clear();
});

test('Should render children', () => {
const { getByText } = render(<FadeIn>Hello, World!</FadeIn>);
expect(getByText('Hello, World!')).toBeInTheDocument();
});

test('Should apply the given classNames', () => {
const { container } = render(
<FadeIn classNames="custom-class">Hello, World!</FadeIn>
);
const fadeInElement = container.querySelector('.custom-class');
expect(fadeInElement).toBeInTheDocument();
});

test('Should apply the animation styles', async () => {
const { getByTestId } = render(
<FadeIn data-testid="test-fadein">Hello, World!</FadeIn>
);
const fadeInElement = getByTestId('test-fadein');

expect(fadeInElement).toHaveStyle('opacity: 0');

await sleep(400);
expect(fadeInElement).toHaveStyle('opacity: 1');
});

test('Should not animate if disabled prop is set to true', async () => {
const { getByTestId } = render(
<FadeIn data-testid="test-fadein" disabled>
Hello, World!
</FadeIn>
);
const fadeInElement = getByTestId('test-fadein');

expect(fadeInElement).toHaveStyle('opacity: 0');

await sleep(100);
expect(fadeInElement).toHaveStyle('opacity: 1');
});

test('Should apply the given duration for animation', async () => {
const { getByTestId } = render(
<FadeIn data-testid="test-fadein" duration={1000}>
Hello, World!
</FadeIn>
);
const fadeInElement = getByTestId('test-fadein');

await sleep(1100);
expect(fadeInElement).toHaveStyle('opacity: 1');
});
});
38 changes: 38 additions & 0 deletions src/components/FadeIn/FadeIn.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { FC, Ref } from 'react';
import { FadeInProps } from './FadeIn.types';
import { animated, useSpring, UseSpringProps } from '@react-spring/web';

export const FadeIn: FC<FadeInProps> = React.forwardRef(
(props: FadeInProps, ref: Ref<HTMLDivElement>) => {
const {
children,
classNames,
delay = 0,
disabled = false,
duration = 300,
'data-testid': dataTestId,
...rest
} = props;
const styles: UseSpringProps<{ opacity: number }> = useSpring({
to: { opacity: 1 },
from: { opacity: 0 },
config: {
duration,
},
delay,
immediate: disabled,
});

return (
<animated.div
data-testid={dataTestId}
className={classNames}
ref={ref}
style={styles}
{...rest}
>
{children}
</animated.div>
);
}
);
23 changes: 23 additions & 0 deletions src/components/FadeIn/FadeIn.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ReactNode } from 'react';
import { OcBaseProps } from '../OcBase';

export interface FadeInProps extends OcBaseProps<HTMLDivElement> {
/**
* The FadeIn child renderer.
*/
children: ReactNode;
/**
* The FadeIn delay amount.
*/
delay?: number;
/**
* The FadeIn animation duration.
* @default 300
*/
duration?: number;
/**
* Whether the FadeIn style is disabled.
* @default false
*/
disabled?: boolean;
}
2 changes: 2 additions & 0 deletions src/components/FadeIn/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './FadeIn';
export * from './FadeIn.types';
3 changes: 3 additions & 0 deletions src/octuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ import { Dropdown } from './components/Dropdown';

import { Empty, EmptyMode } from './components/Empty';

import { FadeIn } from './components/FadeIn';

import Form, { FormInstance } from './components/Form';

import Grid, { Col, Row } from './components/Grid';
Expand Down Expand Up @@ -297,6 +299,7 @@ export {
Empty,
EmptyMode,
ExpandableConfig,
FadeIn,
FilterConfirmProps,
FilterValue,
Form,
Expand Down