Created
November 18, 2022 05:14
-
-
Save arackaf/896be89aeb4fd5e2deeebb02d52d3016 to your computer and use it in GitHub Desktop.
ts-component-wrapper.ts
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
// 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