Skip to content

Instantly share code, notes, and snippets.

@hasparus
Created October 7, 2019 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasparus/62784a8ad3a43366560b410d739a8745 to your computer and use it in GitHub Desktop.
Save hasparus/62784a8ad3a43366560b410d739a8745 to your computer and use it in GitHub Desktop.
dynamic React props composition in TypeScript
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