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

Spread operators again #89

Open
mostrecent opened this issue Jan 30, 2019 · 3 comments
Open

Spread operators again #89

mostrecent opened this issue Jan 30, 2019 · 3 comments

Comments

@mostrecent
Copy link

I checked all the prior issues but I found a strange case where a spread doesn't work within pug (I am aware that attr values are pure JS).

Following doesn't work:

  return pug`
    ${images.map((e, i) => {
    return pug`
      div.h-100.bg-green(
        key=e.backgroundImage
        css=css(mq({
          ...e,
          display: i == context.slideId ? 'block' : 'none'
        }))
      )
      `
  })}
  `

Following does work:

return pug`
    ${images.map((e, i) => {
    const xE = {
      ...e,
      display: i == context.slideId ? 'block' : 'none'
    }
    return pug`
      div.h-100.bg-green(
        key=e.backgroundImage
        css=css(mq(xE))
      )
      `
  })}
  `

A bit context: css is emotion's css prop, css() is emotion's css helper, mq a media query helper from facepaint.

@ezhlobo
Copy link
Member

ezhlobo commented Feb 3, 2019

@mostrecent hey, thank you for this issue. For me that looks like a bug, but in fact, it just shows that pug doesn't understand new JavaScript syntax, while in the second case we use spreading outside of pug template.

There is a workaround, you can still use Object.assign inside the template.

And just in case, take a look how you can also write your code with pure pug.

But in general I agree, that supporting of new js syntax inside pug will be a good idea. There is some workaround, and some discussion regarding that in pug repository: pugjs/pug#2921

@mostrecent
Copy link
Author

Hey @ezhlobo, thanks for your reply and also providing a Pug-in-js REPL, this is awesome, I just had a good use for it!

@pruge
Copy link

pruge commented Dec 8, 2021

You can do this

  return pug`
    ${images.map((e, i) => {
    return pug`
      div.h-100.bg-green(
        key=e.backgroundImage
        css=${css(mq({
          ...e,
          display: i == context.slideId ? 'block' : 'none'
        }))}
      )
      `
  })}
  `

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

3 participants