Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining variants and custom props #1930

Open
sindresola opened this issue Apr 21, 2021 · 2 comments
Open

Combining variants and custom props #1930

sindresola opened this issue Apr 21, 2021 · 2 comments
Labels

Comments

@sindresola
Copy link

sindresola commented Apr 21, 2021

Yo ✌️

I have had issues trying to use custom props inside tof a variant.

const customProp = system({
  rounded: {
    property: "borderRadius",
    scale: "radii"
  }
});

const boxShapes = variant({
  variants: {
    round: {
      rounded: "full",
      bg: "pink"
    },
    square: {
      rounded: "none",
      bg: "green"
    }
  }
});

const Box = styled.div`
  ${boxShapes}
  ${color}
  ${space}
  ${customProp}
`;

Using rounded inside variants does not work when setting variant on the Box

<Box m="4" p="4" variant="round">
  Custom prop inside variant
</Box>

But setting the custom prop directly on the Box like this work.

<Box bg="blue" m="4" p="4" variant="square" rounded="full">
  Custom prop on element
</Box>

Example project here: CodeSandbox

How can I combine custom props and variants?

@casesandberg
Copy link

I also ran into this when creating a custom prop for fill color on an SVG component. I created a custom prop for fill to hook it up to the colors theme obj. It works as expected when you pass in a color via the prop of that component, but it doesn't work when trying to define different fills as variants.

Seems to be the same issue as #810

@SnaiNeR
Copy link

SnaiNeR commented Jun 13, 2022

If i understand you right, you can do it in next way,

const boxShapes = (props) => variant({
  variants: {
    square: {
      color: props.theme.colors.red
      bg: props.color
    }
  }
});

const Box = styled.div`
  ${(props) => boxShapes(props)} // pass all props here
  ...
  ${customProp}
`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants