styled-components / xstyled

A utility-first CSS-in-JS framework built for React. 💅👩‍🎤⚡️
https://xstyled.dev
MIT License
2.28k stars 105 forks source link

Interpolation inside interpolation? #256

Closed andre-freitas closed 3 years ago

andre-freitas commented 3 years ago

I think this does not work because I have an interpolation inside an interpolation:

const StyledComponent = styled(({ component, ...props }) => React.cloneElement(component, props))`
  color: ${(props) => (props.color ? "gray100" : "gray100")};
`

This works:

const StyledComponent = styled(({ component, ...props }) => React.cloneElement(component, props))`
  color: gray100;
`

This is the complete component:

import { CloseOutlined, MenuOutlined } from "@ant-design/icons"
import styled from "@xstyled/styled-components"
import React from "react"
import { ColorType } from "../../themes/theme"

const Icons = {
  menu: MenuOutlined,
  close: CloseOutlined,
}

type IconType = keyof typeof Icons

type IconProps = {
  type: IconType
  color?: ColorType
}

const StyledComponent = styled(({ component, ...props }) => React.cloneElement(component, props))`
  color: ${(props) => (props.color ? "gray100" : "gray100")};
`

const Icon = ({ type, color }: IconProps) => {
  const Component = Icons[type]

  return (
    <StyledComponent component={<Component />} color={color} />
  )
}

export { Icon }
export type { IconProps, IconType }

Tips?

gregberge commented 3 years ago

Hi @andre-freitas, your styled components must be statical, in your example they are not, that's why it does not work.

You could do something like:

<x.div as={Component} color={color} />