I sometimes extract local Tailwind-styled components like this:
const Container: React.FC = ({ children }) => (
<div className="bg-white px-5 py-5">
{children}
</div>
);
To make this easier, I would love to have a helper styleElement
function that would create the Container
for me, a bit like this:
const Container = styleElement("div", "bg-white px-5 py-5");
How would I write the styleElement
function so that Container
is correctly typed as a div
and setting its className
adds to the defaults? Ie:
// Renders as <div className="bg-white px-5 py-5 text-red">…</div>
<Container className="text-red">…</Container>
I stress that I want to keep the typing right/tight, so that calling styleElement("a", …)
would produce a component that accepts the correct props for an a
.
PS. I have seen this related question, but I’m having trouble pivoting the types from the answer there to what I want.