question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript Props with css function

See original GitHub issue

This is arguably a usage question disguised as a request for documentation clarification. I’m struggling with adapting the css literals based on props, as I can’t find a nice way of “hooking” in the generics.

The following throws a type error as the css function does not recoqnize the props.primary interpolation:

import * as styledComponents from "styled-components";
import { ThemedStyledComponentsModule } from "styled-components";

interface Theme {
  primaryColor: string;
  secondaryColor: string;
}

const { css } = styledComponents as ThemedStyledComponentsModule<Theme>;

export const MyCss = css`
  color: ${(props) => props.theme.primaryColor};
  background: ${(props) => props.inverted ? props.theme.secondaryColor : props.theme.primaryColor};
`;

css does have a generic interface with regards to props, however, the following is a syntax error in TypeScript 2.2. I haven’t seen any examples of how (or if) one can use generic tagged template literals in TypeScript – which may be an issue with TypeScript (haven’t seen one reported):

interface Props {
  inverted: boolean;
}

export const MyCss = css<Props>`
  color: ${(props) => props.theme.primaryColor};
  background: ${(props) => props.inverted ? props.theme.secondaryColor : props.theme.primaryColor};
`;

A working solution is to explicitly give the type of props in the interpolated functions where it matters, which isn’t so bad if you use an alias (badly named here, however).

type P = ThemedStyledProps<Props, Theme>;

export const MyCss = css`
  color: ${(props) => props.theme.primaryColor};
  background: ${(props: P) => props.inverted ? props.theme.secondaryColor : props.theme.primaryColor};
`;

Could this be clarified in the docs? I can’t see any examples of CSS adapting based on props – only on theme.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
patrick91commented, Mar 18, 2017

@Igorbek I can write some docs tomorrow, I think that’s better to document this way:

// this will be declared inside your custom theme definitions
type MyThemedProps<P> = ThemedStyledProps<P, MyTheme>;


// then inside your component you'll import it and use with you custom props:

interface Props {
  inverted: boolean;
}

export const MyCss = css`
  color: ${(props) => props.theme.primaryColor};
  background: ${(props: MyThemedProps<Props>) => props.inverted ? props.theme.backgroundColor : props.theme.primaryColor};
`;

What do you think?

9reactions
fabbcommented, Mar 12, 2019

The code is very verbose compared to css/less variables. I’m not yet convinced of using styled components 😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript and React: Styles and CSS - fettblog.eu
The css template function takes CSS and returns an object you can pass to your components. The properties are compatible with CSS.Properties from...
Read more >
Pass CSS styles as props in React TypeScript | bobbyhadz
Use the React.CSSProperties type to pass CSS styles as props in React TypeScript, e.g. style: React.CSSProperties; . The CSSProperties type is used to...
Read more >
Type for style attribute passed to function - Stack Overflow
The style prop on regular DOM elements should be in the form of an object where the keys are the css attributes.
Read more >
Using styled-components and Props With Typescript React
Properties or props for short, are used to make UI components more dynamic, consider you need two buttons with a different border-color but ......
Read more >
Pass Props to CSS in styled-components with typescript.
CSS styling is important to customize any react component. So styled-component is a very good librar... Tagged with react, styledcomponents, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found