Skip to content

Instantly share code, notes, and snippets.

@arackaf
Created November 18, 2022 05:14
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 arackaf/896be89aeb4fd5e2deeebb02d52d3016 to your computer and use it in GitHub Desktop.
Save arackaf/896be89aeb4fd5e2deeebb02d52d3016 to your computer and use it in GitHub Desktop.
ts-component-wrapper.ts
// This should be declared as
// export function withActiveIndicator<T>(Component: FunctionComponent<T>): FunctionComponent<T>
// but styled components makes this extremely hard. For now, this approached brute forces the correct return type,
// so the result will be properly typed for the consumer, at the expense of some casts inside the function
export function withActiveIndicator<T>(Component: T): T {
const Wrapper = styled(Component as any)`
outline: 0;
&:focus > :first-child {
display: block;
}
`;
type WrapperProps = ComponentProps<typeof Wrapper>;
const Result: FunctionComponent<PropsWithChildren<T>> = (
props: WrapperProps,
) => {
const { children, ...rest } = props;
return (
<Wrapper {...rest}>
<ActiveIndicator />
{children}
</Wrapper>
);
};
return Result as any;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment