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

Added tooltip components #24

Merged
merged 5 commits into from
Oct 28, 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
15 changes: 15 additions & 0 deletions src/components/Tooltip/Tooltip.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Meta, StoryObj } from '@storybook/react';
import { Tooltip } from './Tooltip';

const meta: Meta<typeof Tooltip> = { title: 'Components/Tooltip', component: Tooltip,
};

export default meta;
type Story = StoryObj<typeof Tooltip>;

const defaultProps = { text: 'Hover over me', tooltip: 'This is a tooltip',
};

export const Default: Story = { args: { ...defaultProps, },
};

27 changes: 27 additions & 0 deletions src/components/Tooltip/Tooltip.style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import styled from 'styled-components';

export const TooltipContainer = styled.div`
position: relative;
display: inline-block;
`;

export const TooltipText = styled.div`
visibility: hidden;
width: 120px;
background-color: ${({ theme }) => theme?.colors?.tooltipBg || '#555'};
color: ${({ theme }) => theme?.colors?.tooltipText || '#fff'};
text-align: center;
padding: ${({ theme }) => theme?.spacing?.xs || '5px'};
border-radius: ${({ theme }) => theme?.borderRadius?.sm || '6px'};
position: absolute;
z-index: 1;
bottom: 20%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
${TooltipContainer}:hover & {
visibility: visible;
opacity: 1;
}
`;
17 changes: 17 additions & 0 deletions src/components/Tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { render } from '@testing-library/react';
import { Tooltip } from './Tooltip';
import { axe } from 'jest-axe';

describe('<Tooltip />', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an accessibility test here. You can find an example of how to do this in the updated testing template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the accessibility test.

it('renders the tooltip component', () => {
const { getByText } = render(<Tooltip text="Hover over me" tooltip="Tooltip text" />);
expect(getByText('Hover over me')).toBeInTheDocument();
});

it('has no accessibility violations', async () => {
const { container } = render(<Tooltip text="Hover over me" tooltip="Tooltip text" />);
const results = await axe(container);

expect(results).toHaveNoViolations();
});
});
11 changes: 11 additions & 0 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FC } from 'react';
import { TooltipContainer, TooltipText } from './Tooltip.style';

type TooltipProps = {
text: string;
tooltip: string;
};

export const Tooltip: FC<TooltipProps> = ({ text, tooltip }) => ( <TooltipContainer> {text} <TooltipText>{tooltip}</TooltipText> </TooltipContainer>
);

Loading