Skip to content

Commit

Permalink
Implementation for Tooltip Component (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
KushalNerella07 authored Oct 25, 2023
1 parent 275fd8f commit 42f6dfb
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/packages/ToolTip/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { ReactNode } from 'react'
import * as S from './styles'

export type TooltipProps = {
children: ReactNode
content: ReactNode
position?: 'top' | 'bottom' | 'left' | 'right'
}

const Tooltip: React.FC<TooltipProps> = ({
children,
content,
position = 'top'
}) => {
return (
<S.Wrapper>
{children}
<S.Content position={position}>{content}</S.Content>
</S.Wrapper>
)
}

export default Tooltip
47 changes: 47 additions & 0 deletions src/packages/ToolTip/stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react'
import { StoryFn, Meta } from '@storybook/react'
import Tooltip, { TooltipProps } from '.'

export default {
title: 'Tooltip',
component: Tooltip,
args: {
children: 'Hover me',
content: 'Tooltip text here'
},
argTypes: {
position: {
control: {
type: 'select',
options: ['top', 'bottom', 'left', 'right']
}
}
}
} as Meta

export const Default: StoryFn<TooltipProps> = (args) => <Tooltip {...args} />

// Separated stories for each position
export const Top: StoryFn<TooltipProps> = (args) => (
<Tooltip {...args} position="top">
Top
</Tooltip>
)

export const Bottom: StoryFn<TooltipProps> = (args) => (
<Tooltip {...args} position="bottom">
Bottom
</Tooltip>
)

export const Left: StoryFn<TooltipProps> = (args) => (
<Tooltip {...args} position="left">
Left
</Tooltip>
)

export const Right: StoryFn<TooltipProps> = (args) => (
<Tooltip {...args} position="right">
Right
</Tooltip>
)
51 changes: 51 additions & 0 deletions src/packages/ToolTip/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import styled, { css } from 'styled-components'
import { TooltipProps } from '.'

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

export const Content = styled.span<Pick<TooltipProps, 'position'>>`
${({ theme, position }) => css`
visibility: hidden;
max-width: calc(${theme.container.small} / 2);
background-color: ${theme.colors.base.black};
color: ${theme.colors.base.white};
text-align: center;
border-radius: ${theme.border.radius};
padding: ${theme.spacings.xsmall} ${theme.spacings.small};
position: absolute;
z-index: ${theme.layers.menu};
// Dynamic positioning based on the provided position prop
${position === 'top' &&
css`
bottom: calc(100% + ${theme.spacings.xsmall});
left: 50%;
transform: translateX(-50%);
`}
${position === 'bottom' &&
css`
top: calc(100% + ${theme.spacings.xsmall});
left: 50%;
transform: translateX(-50%);
`}
${position === 'left' &&
css`
top: 50%;
right: calc(100% + ${theme.spacings.small});
transform: translateY(-50%);
`}
${position === 'right' &&
css`
top: 50%;
left: calc(100% + ${theme.spacings.small});
transform: translateY(-50%);
`}
${Wrapper}:hover & {
visibility: visible;
}
`}
`

0 comments on commit 42f6dfb

Please sign in to comment.