Created
October 7, 2019 15:08
-
-
Save hasparus/62784a8ad3a43366560b410d739a8745 to your computer and use it in GitHub Desktop.
dynamic React props composition in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type HeaderLinkProps<As extends ElementType> = { as?: As } & Omit< | |
ComponentPropsWithoutRef<typeof s.a> & ComponentPropsWithoutRef<As>, | |
"as" | |
>; | |
const HeaderLink = <As extends ElementType = "a">( | |
props: HeaderLinkProps<As> | |
) => ( | |
<s.a | |
sx={{ | |
color: "currentColor", | |
textTransform: "lowercase", | |
":not(:last-of-type)": { | |
marginRight: "1em", | |
}, | |
}} | |
{...props} | |
/> | |
); | |
type HeaderProps = { showHome?: boolean }; | |
export const Header: React.FC<HeaderProps> = ({ showHome }) => ( | |
<header | |
sx={{ | |
display: "flex", | |
flexDirection: "row", | |
flexWrap: "wrap", | |
}} | |
> | |
{showHome && ( | |
<HeaderLink as={Link} to="/"> | |
haspar.us | |
</HeaderLink> | |
)} | |
<div sx={{ flex: 1 }} /> | |
<nav> | |
<HeaderLink href="https://github.com/hasparus">GitHub</HeaderLink> | |
<HeaderLink href="https://twitter.com/hasparus">Twitter</HeaderLink> | |
</nav> | |
</header> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment