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 Header component to TSX #1020

Merged
merged 11 commits into from
Feb 8, 2021
5 changes: 5 additions & 0 deletions .changeset/yellow-suits-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/components": patch
---

Migrate `Header` component to TypeScript
VanAnderson marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 26 additions & 20 deletions src/Header.js → src/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import styled, {css} from 'styled-components'
import PropTypes from 'prop-types'
import theme from './theme'
import {get, COMMON, TYPOGRAPHY, BORDER} from './constants'
import sx from './sx'
import {BORDER, COMMON, get, SystemCommonProps, SystemTypographyProps, SystemBorderProps, TYPOGRAPHY} from './constants'
import {ComponentProps} from './utils/types'
import sx, {SxProp} from './sx'

const Header = styled.div`
type StyledHeaderItemProps = SystemCommonProps & SxProp & {full?: boolean}
VanAnderson marked this conversation as resolved.
Show resolved Hide resolved
type StyledHeaderProps = SystemCommonProps & SxProp
VanAnderson marked this conversation as resolved.
Show resolved Hide resolved
type StyledHeaderLinkProps = SystemCommonProps & SxProp & SystemTypographyProps & SystemBorderProps & {to?: boolean}

Copy link
Contributor

Choose a reason for hiding this comment

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

The type of to should be History.LocationDescriptor.

Suggested change
type StyledHeaderLinkProps = SystemCommonProps & SxProp & SystemTypographyProps & SystemBorderProps & {to?: boolean}
type StyledHeaderLinkProps = {to?: History.LocationDescriptor} & SystemCommonProps & SxProp & SystemTypographyProps & SystemBorderProps

You'll need to the History import at the top of the file:

import * as History from 'history'

const Header = styled.div<StyledHeaderProps>`
z-index: 32;
display: flex;
padding: ${get('space.3')};
Expand All @@ -19,16 +24,15 @@ const Header = styled.div`
${BORDER}
${sx};
`

Header.Item = styled.div`
const HeaderItem = styled.div<StyledHeaderItemProps>`
display: flex;
margin-right: ${get('space.3')};
align-self: stretch;
align-items: center;
flex-wrap: nowrap;

${props =>
props.full &&
${({full}) =>
full &&
css`
flex: auto;
`};
Expand All @@ -37,10 +41,11 @@ Header.Item = styled.div`
${BORDER};
${sx};
`
Header.Item.displayName = 'Header.Item'

Header.Link = styled.a.attrs(props => {
const isReactRouter = typeof props.to === 'string'
HeaderItem.displayName = 'Header.Item'

const HeaderLink = styled.a.attrs(({to}: StyledHeaderLinkProps) => {
const isReactRouter = typeof to === 'string'
Copy link
Contributor

Choose a reason for hiding this comment

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

When using attrs, we've been passing the props type using the <> notation:

Suggested change
const HeaderLink = styled.a.attrs(({to}: StyledHeaderLinkProps) => {
const isReactRouter = typeof to === 'string'
const HeaderLink = styled.a.attrs<StyledHeaderLinkProps>(({to}) => {
const isReactRouter = typeof to === 'string'

Line 56-57 should look like this:

})<StyledHeaderLinkProps>`
  font-weight: ${get('fontWeights.bold')};

if (isReactRouter) {
// according to their docs, NavLink supports aria-current:
// https://reacttraining.com/react-router/web/api/NavLink/aria-current-string
Expand All @@ -67,7 +72,8 @@ Header.Link = styled.a.attrs(props => {
${TYPOGRAPHY};
${sx};
`
Header.Link.displayName = 'Header.Link'

HeaderLink.displayName = 'Header.Link'

Header.propTypes = {
...sx.propTypes,
Expand All @@ -79,23 +85,18 @@ Header.defaultProps = {
theme
}

Header.Item.defaultProps = {
theme
}

Header.Item.propTypes = {
HeaderItem.propTypes = {
full: PropTypes.bool,
...COMMON.propTypes,
...BORDER.propTypes,
...sx.propTypes
}

Header.Link.defaultProps = {
HeaderItem.defaultProps = {
theme
}

Header.Link.propTypes = {
as: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]),
HeaderLink.propTypes = {
href: PropTypes.string,
theme: PropTypes.object,
...COMMON.propTypes,
Expand All @@ -104,4 +105,9 @@ Header.Link.propTypes = {
...sx.propTypes
}

export default Header
HeaderLink.defaultProps = {
theme
}

export type HeaderProps = ComponentProps<typeof Header>
export default Object.assign(Header, {Link: HeaderLink, Item: HeaderItem})
VanAnderson marked this conversation as resolved.
Show resolved Hide resolved
File renamed without changes.