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

Migrate Tooltip to TypeScript #1033

Merged
merged 2 commits into from
Feb 9, 2021
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
5 changes: 5 additions & 0 deletions .changeset/odd-weeks-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Migrate `Tooltip` to TypeScript
47 changes: 27 additions & 20 deletions src/Tooltip.js → src/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,12 @@ import React from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import styled from 'styled-components'
import {COMMON, get} from './constants'
import {COMMON, get, SystemCommonProps} from './constants'
import theme from './theme'
import sx from './sx'
import sx, {SxProp} from './sx'
import {ComponentProps} from './utils/types'

function TooltipBase({direction, children, className, text, noDelay, align, wrap, theme, ...rest}) {
const classes = classnames(
className,
`tooltipped-${direction}`,
align && `tooltipped-align-${align}-2`,
noDelay && 'tooltipped-no-delay',
wrap && 'tooltipped-multiline'
)
return (
<span aria-label={text} {...rest} className={classes}>
{children}
</span>
)
}

const Tooltip = styled(TooltipBase)`
const TooltipBase = styled.span<SystemCommonProps & SxProp>`
position: relative;

&::before {
Expand Down Expand Up @@ -249,13 +235,34 @@ const Tooltip = styled(TooltipBase)`
${sx};
`

export type TooltipProps = {
direction?: 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'
text?: string
noDelay?: boolean
align?: 'left' | 'right'
wrap?: boolean
} & ComponentProps<typeof TooltipBase>

function Tooltip({direction = 'n', children, className, text, noDelay, align, wrap, ...rest}: TooltipProps) {
const classes = classnames(
className,
`tooltipped-${direction}`,
align && `tooltipped-align-${align}-2`,
noDelay && 'tooltipped-no-delay',
wrap && 'tooltipped-multiline'
)
return (
<TooltipBase aria-label={text} {...rest} className={classes}>
{children}
</TooltipBase>
)
}
Tooltip.alignments = ['left', 'right']

Tooltip.directions = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw']

Tooltip.defaultProps = {
theme,
direction: 'n'
theme
}

Tooltip.propTypes = {
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/Tooltip.js → src/__tests__/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import {Tooltip} from '..'
import Tooltip, {TooltipProps} from '../Tooltip'
import {render, renderClasses, rendersClass, behavesAsComponent, checkExports} from '../utils/testing'
import {COMMON} from '../constants'
import {render as HTMLRender, cleanup} from '@testing-library/react'
Expand Down Expand Up @@ -33,7 +33,9 @@ describe('Tooltip', () => {

it('respects the "direction" prop', () => {
for (const direction of Tooltip.directions) {
expect(rendersClass(<Tooltip direction={direction} />, `tooltipped-${direction}`)).toBe(true)
expect(
rendersClass(<Tooltip direction={direction as TooltipProps['direction']} />, `tooltipped-${direction}`)
).toBe(true)
}
})

Expand Down