Skip to content
Discussion options

You must be logged in to vote

Your approach with css<{ $width: WidthBreakpoints }> is correct. The props type on the css helper merges with the styled component's props when used as an interpolation:

const WidthStyles = css<{ $width: WidthBreakpoints }>`
  max-width: ${({ $width }) => formatPx($width?.sm)};
`;

const MyComponent = styled.div<{ $width: WidthBreakpoints }>`
  ${WidthStyles}
`;

The component using the shared styles needs to declare the same prop in its generic parameter. The function-returning-css approach you mentioned also works and avoids the prop type duplication:

const widthStyles = (width: WidthBreakpoints) => css`
  max-width: ${formatPx(width?.sm)};
`;

const MyComponent = styled.div<{ $width: Wi…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by quantizor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants