Skip to content

Commit

Permalink
Migrate Header component to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
VanAnderson committed Feb 5, 2021
1 parent b894ead commit 2bb6dff
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions src/Header.js → src/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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 sx, {SxProp} from './sx'

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

const Header = styled.div<StyledHeaderProps>`
z-index: 32;
display: flex;
padding: ${get('space.3')};
Expand All @@ -19,16 +23,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 +40,21 @@ Header.Item = styled.div`
${BORDER};
${sx};
`
Header.Item.displayName = 'Header.Item'
HeaderItem.displayName = 'Header.Item'

HeaderItem.defaultProps = {
theme
}

Header.Link = styled.a.attrs(props => {
const isReactRouter = typeof props.to === 'string'
HeaderItem.propTypes = {
full: PropTypes.bool,
...COMMON.propTypes,
...BORDER.propTypes,
...sx.propTypes
}

const HeaderLink = styled.a.attrs(({to}: StyledHeaderLinkProps) => {
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
Expand All @@ -67,41 +81,29 @@ Header.Link = styled.a.attrs(props => {
${TYPOGRAPHY};
${sx};
`
Header.Link.displayName = 'Header.Link'

Header.propTypes = {
...sx.propTypes,
...COMMON.propTypes,
...BORDER.propTypes
}

Header.defaultProps = {
theme
}
HeaderLink.displayName = 'Header.Link'

Header.Item.defaultProps = {
HeaderLink.defaultProps = {
theme
}

Header.Item.propTypes = {
full: PropTypes.bool,
HeaderLink.propTypes = {
href: PropTypes.string,
theme: PropTypes.object,
...COMMON.propTypes,
...BORDER.propTypes,
...TYPOGRAPHY.propTypes,
...sx.propTypes
}

Header.Link.defaultProps = {
theme
Header.propTypes = {
...sx.propTypes,
...COMMON.propTypes,
...BORDER.propTypes
}

Header.Link.propTypes = {
as: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]),
href: PropTypes.string,
theme: PropTypes.object,
...COMMON.propTypes,
...BORDER.propTypes,
...TYPOGRAPHY.propTypes,
...sx.propTypes
Header.defaultProps = {
theme
}

export default Header
export default Object.assign(Header, {Link: HeaderLink, Item: HeaderItem})

0 comments on commit 2bb6dff

Please sign in to comment.