-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tooltip): add radix-ui tooltip component (#39)
* feat(tooltip): add radix-ui tooltip component * feat(tooltip): fix export
- Loading branch information
1 parent
6e8b2f3
commit f9d337b
Showing
13 changed files
with
270 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
module.exports = { | ||
setupFilesAfterEnv: ['./src/setupTests.ts'], | ||
clearMocks: true, | ||
moduleNameMapper: { | ||
'\\.(css|sass|scss)$': 'identity-obj-proxy', | ||
'\\.svg$': 'jest-transform-stub', | ||
}, | ||
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'], | ||
testEnvironment: 'jsdom', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class DOMRect { | ||
static fromRect() { | ||
return { | ||
height: 0, | ||
width: 0, | ||
x: 0, | ||
y: 0, | ||
}; | ||
} | ||
} | ||
|
||
global.DOMRect = DOMRect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class ResizeObserver { | ||
observe() {} | ||
unobserve() {} | ||
} | ||
|
||
global.ResizeObserver = ResizeObserver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import './style.css'; | ||
|
||
export * from './models'; | ||
export * from './ui'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './tooltip.component'; | ||
export * from './tooltip.context'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { ReactNode } from 'react'; | ||
import { | ||
Arrow, | ||
Content, | ||
Root, | ||
TooltipContentProps, | ||
TooltipProps, | ||
Trigger, | ||
} from '@radix-ui/react-tooltip'; | ||
import clsx from 'clsx'; | ||
|
||
export type TooltipOwnProps = { | ||
children: ReactNode; | ||
content: ReactNode; | ||
side?: TooltipContentProps['side']; | ||
padding?: 'sm' | 'md'; | ||
defaultOpen?: boolean; | ||
open?: boolean; | ||
onOpenChange?: TooltipProps['onOpenChange']; | ||
}; | ||
|
||
export const Tooltip = ({ | ||
children, | ||
content, | ||
side = 'top', | ||
padding = 'sm', | ||
open, | ||
defaultOpen, | ||
onOpenChange, | ||
}: TooltipOwnProps) => ( | ||
<Root | ||
open={open} | ||
defaultOpen={defaultOpen} | ||
delayDuration={300} | ||
onOpenChange={onOpenChange} | ||
> | ||
<Trigger asChild>{children}</Trigger> | ||
<Content side={side} sideOffset={6} align="center" asChild> | ||
<div | ||
className={clsx('max-w-xs rounded bg-white shadow-lg text-sm', { | ||
'p-2': padding === 'sm', | ||
'p-4': padding === 'md', | ||
})} | ||
> | ||
{content} | ||
<Arrow className="fill-current text-white" width={14} height={8} /> | ||
</div> | ||
</Content> | ||
</Root> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Provider } from '@radix-ui/react-tooltip'; | ||
|
||
export const TooltipProvider = Provider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { axe } from 'jest-axe'; | ||
|
||
import { Tooltip } from './tooltip.component'; | ||
import { TooltipProvider } from './tooltip.context'; | ||
|
||
describe('Tooltip', () => { | ||
test('has no accessibility violations', async () => { | ||
const { container } = render( | ||
<TooltipProvider> | ||
<Tooltip content="Tooltip Content" defaultOpen> | ||
<button type="button">Trigger</button> | ||
</Tooltip> | ||
</TooltipProvider> | ||
); | ||
|
||
expect(screen.getByRole('button', { name: 'Trigger' })).toBeInTheDocument(); | ||
|
||
expect(await axe(container)).toHaveNoViolations(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react'; | ||
import { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
import { Tooltip } from './tooltip.component'; | ||
import { TooltipProvider } from './tooltip.context'; | ||
|
||
const meta: ComponentMeta<typeof Tooltip> = { | ||
title: 'Atoms/Tooltip', | ||
component: Tooltip, | ||
parameters: { | ||
backgrounds: { | ||
default: 'light', | ||
}, | ||
}, | ||
decorators: [ | ||
(Story) => ( | ||
<div className="m-32"> | ||
<TooltipProvider> | ||
<Story /> | ||
</TooltipProvider> | ||
</div> | ||
), | ||
], | ||
}; | ||
|
||
export default meta; | ||
|
||
const Template: ComponentStory<typeof Tooltip> = (args) => ( | ||
<Tooltip {...args} /> | ||
); | ||
|
||
export const Default = Template.bind({}); | ||
Default.args = { | ||
children: <button>Show Tooltip</button>, | ||
content: 'This is the content!', | ||
side: 'bottom', | ||
}; | ||
|
||
export const OpenByDefault = Template.bind({}); | ||
OpenByDefault.args = { | ||
...Default.args, | ||
defaultOpen: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters