diff --git a/.changeset/flat-moose-guess.md b/.changeset/flat-moose-guess.md new file mode 100644 index 00000000000..a63cbd4b1f9 --- /dev/null +++ b/.changeset/flat-moose-guess.md @@ -0,0 +1,5 @@ +--- +"@primer/components": patch +--- + +Migrate `BaseStyles` to TypeScript diff --git a/src/BaseStyles.js b/src/BaseStyles.tsx similarity index 72% rename from src/BaseStyles.js rename to src/BaseStyles.tsx index 50e7333b93e..2a4213846b3 100644 --- a/src/BaseStyles.js +++ b/src/BaseStyles.tsx @@ -1,9 +1,10 @@ +import PropTypes from 'prop-types' import React from 'react' import styled, {createGlobalStyle} from 'styled-components' -import PropTypes from 'prop-types' -import {TYPOGRAPHY, COMMON} from './constants' +import {COMMON, SystemCommonProps, SystemTypographyProps, TYPOGRAPHY} from './constants' import useMouseIntent from './hooks/useMouseIntent' import theme from './theme' +import {ComponentProps} from './utils/types' const GlobalStyle = createGlobalStyle` * { box-sizing: border-box; } @@ -24,21 +25,25 @@ const GlobalStyle = createGlobalStyle` outline: none; } } +` +const Base = styled.div` + ${TYPOGRAPHY}; + ${COMMON}; ` -const Base = props => { + +export type BaseStylesProps = ComponentProps + +function BaseStyles(props: BaseStylesProps) { const {color, lineHeight, fontFamily, theme, ...rest} = props useMouseIntent() return ( -
+ {props.children} -
+ ) } -const BaseStyles = styled(Base)` - ${TYPOGRAPHY} ${COMMON}; -` BaseStyles.defaultProps = { color: 'gray.9', @@ -52,4 +57,5 @@ BaseStyles.propTypes = { ...COMMON.propTypes, theme: PropTypes.object } + export default BaseStyles diff --git a/src/Box.tsx b/src/Box.tsx index 8ec05558bb0..328ccde56e7 100644 --- a/src/Box.tsx +++ b/src/Box.tsx @@ -1,8 +1,9 @@ import PropTypes from 'prop-types' import styled from 'styled-components' -import {COMMON, SystemCommonProps, FLEX, SystemFlexProps, LAYOUT, SystemLayoutProps} from './constants' +import {COMMON, FLEX, LAYOUT, SystemCommonProps, SystemFlexProps, SystemLayoutProps} from './constants' import sx, {SxProp} from './sx' import theme from './theme' +import {ComponentProps} from './utils/types' const Box = styled.div` ${COMMON} @@ -18,8 +19,8 @@ Box.propTypes = { ...FLEX.propTypes, ...LAYOUT.propTypes, ...sx.propTypes, - theme: PropTypes.object, + theme: PropTypes.object } -export type BoxProps = React.ComponentProps +export type BoxProps = ComponentProps export default Box diff --git a/src/BranchName.tsx b/src/BranchName.tsx index 0cbc095723e..4b1bca27b18 100644 --- a/src/BranchName.tsx +++ b/src/BranchName.tsx @@ -1,8 +1,9 @@ import PropTypes from 'prop-types' import styled from 'styled-components' +import {COMMON, get, SystemCommonProps} from './constants' import sx, {SxProp} from './sx' import theme from './theme' -import {COMMON, get, SystemCommonProps} from './constants' +import {ComponentProps} from './utils/types' const BranchName = styled.a` display: inline-block; @@ -27,5 +28,5 @@ BranchName.propTypes = { theme: PropTypes.object } -export type BranchNameProps = React.ComponentProps +export type BranchNameProps = ComponentProps export default BranchName diff --git a/src/Heading.tsx b/src/Heading.tsx index 30cc12f5da9..51857fc4463 100644 --- a/src/Heading.tsx +++ b/src/Heading.tsx @@ -1,8 +1,9 @@ +import PropTypes from 'prop-types' import styled from 'styled-components' +import {COMMON, get, SystemCommonProps, SystemTypographyProps, TYPOGRAPHY} from './constants' import sx, {SxProp} from './sx' -import PropTypes from 'prop-types' -import {TYPOGRAPHY, COMMON, get, SystemTypographyProps, SystemCommonProps} from './constants' import theme from './theme' +import {ComponentProps} from './utils/types' const Heading = styled.h2` font-weight: ${get('fontWeights.bold')}; @@ -24,5 +25,5 @@ Heading.propTypes = { ...TYPOGRAPHY.propTypes } -export type HeadingProps = React.ComponentProps +export type HeadingProps = ComponentProps export default Heading diff --git a/src/Label.tsx b/src/Label.tsx index 29154636a24..cf742035210 100644 --- a/src/Label.tsx +++ b/src/Label.tsx @@ -4,6 +4,7 @@ import {borderColor, BorderColorProps, variant} from 'styled-system' import {COMMON, get, SystemCommonProps} from './constants' import sx, {SxProp} from './sx' import theme from './theme' +import {ComponentProps} from './utils/types' const outlineStyles = css` margin-top: -1px; // offsets the 1px border @@ -82,5 +83,5 @@ Label.propTypes = { ...sx.propTypes } -export type LabelProps = React.ComponentProps +export type LabelProps = ComponentProps export default Label diff --git a/src/LabelGroup.tsx b/src/LabelGroup.tsx index 7b82e408a8e..365e35f44a2 100644 --- a/src/LabelGroup.tsx +++ b/src/LabelGroup.tsx @@ -2,6 +2,7 @@ import styled from 'styled-components' import {COMMON, get, SystemCommonProps} from './constants' import sx, {SxProp} from './sx' import theme from './theme' +import {ComponentProps} from './utils/types' const LabelGroup = styled.span` ${COMMON} @@ -23,5 +24,5 @@ LabelGroup.propTypes = { ...sx.propTypes } -export type LabelGroupProps = React.ComponentProps +export type LabelGroupProps = ComponentProps export default LabelGroup diff --git a/src/Pagehead.tsx b/src/Pagehead.tsx index fc573010d74..f8c599e7787 100644 --- a/src/Pagehead.tsx +++ b/src/Pagehead.tsx @@ -3,6 +3,7 @@ import styled from 'styled-components' import {COMMON, get, SystemCommonProps} from './constants' import sx, {SxProp} from './sx' import theme from './theme' +import {ComponentProps} from './utils/types' const Pagehead = styled.div` position: relative; @@ -24,5 +25,5 @@ Pagehead.propTypes = { ...sx.propTypes } -export type PageheadProps = React.ComponentProps +export type PageheadProps = ComponentProps export default Pagehead diff --git a/src/utils/types.ts b/src/utils/types.ts new file mode 100644 index 00000000000..72c4c38199c --- /dev/null +++ b/src/utils/types.ts @@ -0,0 +1,12 @@ +/** + * Extract a component's props + * + * Source: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase#wrappingmirroring-a-component + * + * @example ComponentProps + */ +export type ComponentProps = T extends React.ComponentType + ? Props extends object + ? Props + : never + : never