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

Styled-Component Object Notation Example #27

Open
jvanderen1 opened this issue Aug 11, 2021 · 1 comment
Open

Styled-Component Object Notation Example #27

jvanderen1 opened this issue Aug 11, 2021 · 1 comment

Comments

@jvanderen1
Copy link

jvanderen1 commented Aug 11, 2021

I would like to use facepaint inside an object, but I am not sure this is possible.

Something like:

const mq = facepaint([
  '@media(min-width: 420px)',
])
const Container = styled.div({
  ...mq({
    color: ['#f2f', '#00ff00'],
  }),
})

Is this not possible?

@craig-jennings
Copy link

mq({ color: ['#f2f', '#00ff00'] }) will actually return an array of objects, each with the values assigned for the breakpoints.

mq({ color: ['#f2f', '#00ff00'] });

// Output
[
  {
    "color": "#f2f",
      "@media only screen and (min-width: 420px)": {
        "color": "#00ff00"
      }
  }
]

This means when you're actually spreading the array into the object which isn't what you want.

const Container = styled.div({
  ...mq({
    color: ['#f2f', '#00ff00'],
  }),
})

// Result
const Container = styled.div({
  0: {
    "color": "#f2f",
      "@media only screen and (min-width: 420px)": {
        "color": "#00ff00"
      }
  },
})

Instead, you probably want to spread the resulting object from each entry in the array to the final object. Something like this should work:

const Container = styled.div({
  ...mq({
    color: ['#f2f', '#00ff00'],
  }).reduce((acc, val) => ({ ...acc, ...val }), {}),
})

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

No branches or pull requests

2 participants