Search anything...⌘K
  • getting started

    • Welcome
    • Installation
    • Browser support
    • Performances
  • features

    • Styling
    • Styling props
    • Theming
    • Plugins
    • Breakpoints
    • SSR
    • Production
  • css utilities

    • Installation
    • Methods
    • Flex
    • Grid
    • Margin
    • Padding
  • guides

    • Using with Next.js
    • Using with Create-react-app
    • Using with Gatsby
    • Using with TailwindCSS
  • getting started

    • Welcome
    • Installation
    • Browser support
    • Performances
  • features

    • Styling
    • Styling props
    • Theming
    • Plugins
    • Breakpoints
    • SSR
    • Production
  • css utilities

    • Installation
    • Methods
    • Flex
    • Grid
    • Margin
    • Padding
  • guides

    • Using with Next.js
    • Using with Create-react-app
    • Using with Gatsby
    • Using with TailwindCSS

Styling props

You can use props to define style based on custom props declared at the usage-level, or component-level if you are using the styled API.

You will of course get auto-completion when you will use the prop style, but we recommend creating a type to also get auto-completion inside your style declaration. See type-safety.

Table of Contents

With css API

To do so, you will need to update your styleProvider and change it to a callback wich takes one argument, the props, and returns the styles:

const background = css(props => ({
    background: props.bg,
}))

To spectify the props when using the style, call the returned function with an object containing the props at the usage-level:

<div class={background({ bg: 'red' })} />

With styled API

Same as the css API, change your styleProvider the same way:

const Box = styled('div', props => ({
    background: props.bg,
}))

Then, you can specify the styling props directly at the component level, same as any React props:

<Box bg='red' />

Type-safety

To add types-safety, you can create a type that specify the schema of the props:

// css API
type Props = {
    size: string
};

const box = css<Props>(({ size }) => ({
    width: size,
    height: size,
    background: 'red',
}));

// styled API
type Props = {
    size: string
};

const Box = styled<Props>('div', ({ size }) => ({
    width: size,
    height: size,
    background: 'red',
}));