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` to TypeScript
54 changes: 33 additions & 21 deletions src/Header.js → src/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
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'
import * as History from 'history'

const Header = styled.div`
type StyledHeaderItemProps = {full?: boolean} & SystemCommonProps & SxProp
type StyledHeaderProps = SystemBorderProps & SystemCommonProps & SxProp
type StyledHeaderLinkProps = {to?: History.LocationDescriptor} & SystemCommonProps &
SxProp &
SystemTypographyProps &
SystemBorderProps

const Header = styled.div<StyledHeaderProps>`
z-index: 32;
display: flex;
padding: ${get('space.3')};
Expand All @@ -19,16 +28,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,18 +45,19 @@ 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<StyledHeaderLinkProps>(({to}) => {
const isReactRouter = typeof to === 'string'
if (isReactRouter) {
// according to their docs, NavLink supports aria-current:
// https://reacttraining.com/react-router/web/api/NavLink/aria-current-string
return {'aria-current': 'page'}
} else {
return {}
}
})`
})<StyledHeaderLinkProps>`
font-weight: ${get('fontWeights.bold')};
color: ${get('colors.text.white')};
white-space: nowrap;
Expand All @@ -67,7 +76,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 +89,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 +109,11 @@ Header.Link.propTypes = {
...sx.propTypes
}

export default Header
HeaderLink.defaultProps = {
theme
}

export type HeaderProps = ComponentProps<typeof Header>
export type HeaderLinkProps = ComponentProps<typeof HeaderLink>
export type HeaderItemProps = ComponentProps<typeof HeaderItem>
export default Object.assign(Header, {Link: HeaderLink, Item: HeaderItem})
File renamed without changes.