question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

String interpolation inside tw macro doesn't work

See original GitHub issue

Hi!

I have a problem with string interpolation in tw macro. For example I need to customize border color, so I’m trying to do it like this:

export const PrimaryButton = styled(Button)`
  ${({ color }) => tw`hover:border-${color ?? 'blue'}`};
`;

But I get the following error: ✕ Class “hover:border-” shouldn’t have a trailing dash.

It also doesn’t work without variables:

export const PrimaryButton = styled(Button)`
  ${({ color }) => tw`hover:border-${'blue'}`};
`;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

11reactions
ben-rogersoncommented, Apr 15, 2021

Edit: Check out the discussion on this topic.

You could try a style map to reuse the styleMap object:

const styleMap = {
  primary: tw`bg-black text-white`,
  secondary: tw`bg-white text-black`,
  default: tw`bg-gray-500`
}

const getStyleName = ({ name }) => styleMap[name] || styleMap.default

const Button = styled.button(getStyleName)

const Buttons = () => (
  <>
    <Button name="primary">Primary</Button>
    <Button name="secondary">Secondary</Button>
    <Button>Default</Button>
  </>
)

You could also separate the styles and import them from another file:

// styles.js
const primary = tw`bg-black text-white`
const secondary = tw`bg-white text-black`
const other = tw`bg-gray-500`

const stylesCombined = { primary, secondary }
const getStyleName = ({ name }) => stylesCombined[name] || other

export default getStyleName
// Button.js
import getStyleName from './styles'

const Button = styled.button(getStyleName)

const Buttons = () => (
  <>
    <Button name="primary">Primary</Button>
    <Button name="secondary">Secondary</Button>
    <Button>Default</Button>
  </>
)
2reactions
tqwewecommented, Oct 8, 2020

This is especially useful when using padding in the tailwind macro - eg: gap.

declare const gap: number

const styles = tw`px-${gap}`

I’m not sure whats the best workaround currently.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Props, TailwindCSS & Twin.Macro - Stack Overflow
I am trying to get the following component to work properly with Twin.Macro: import ...
Read more >
Expressions - Apache FreeMarker Manual
When you supply values for interpolations or directive parameters you can use variables or more complex expressions.
Read more >
FAQs - styled-components
In general, always use the css helper when composing styling partials to be interpolated into a styled component.. import styled, { keyframes }...
Read more >
Introduction into Macro Programming - ImageJ Wiki
Variables. The most important concept when starting to program macros are variables. A variable is a placeholder for a changing entity.
Read more >
Bel Curcio on Twitter: "If I do string interpolation within ...
If I do string interpolation within classnames Tailwinds won't notice it. Any advice on how to force those styles to land the final...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found