DEV Community

Cover image for Tailwind with React

Tailwind with React

Ryan Lanciaux on January 02, 2020

Tailwind is a CSS library in a similar space to things like Bootstrap or Bulma. Tailwind is different that instead of providing CSS for full co...
Collapse
 
digioi profile image
Mike DiGioia • Edited

You can simplify it a bit more with the following.

const Button = styled.button`
  ${tw`bg-gray-300 text-yellow-900 px-8 m-8 rounded h-20 text-3xl`}
`;
Enter fullscreen mode Exit fullscreen mode

to

const Button = tw.button`bg-gray-300 text-yellow-900 px-8 m-8 rounded h-20 text-3xl`;
Enter fullscreen mode Exit fullscreen mode

You do this by updating the babel macro config with something like

{
  tailwind: {
     config: tailwindConfig, // if you added a config file
     styled: '@emotion/styled',
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
stevenacoffman profile image
Steve Coffman

Hey Ryan,
Very nice! I'm not familiar with Tailwind.Macro or babel-plugin-tailwind-components. Would they provide the same cut-what-is-not-needed benefits as PostCSS + Purgecss, or would you consider using both?

Collapse
 
ryanlanciaux profile image
Ryan Lanciaux

That's a great question and I'm not entirely sure right now. I tried to find a bit more info on it but to no avail. I have a project where I'm using this and next time I'm in the code, I may try to do a production build and see if it's doing any of the purging type operations.

If it's not, it'd likely be good to do that still :D

Collapse
 
ryanlanciaux profile image
Ryan Lanciaux

Very nice! Thanks

Collapse
 
dbshanks profile image
Derek Shanks

Ryan! Thank you very much for your demonstration. You managed to sell Tailwind far better than anyone else has.

I’m a CSS specialist, my primary focus is designing efficient style workflows. I love styled-components.

I’ve been wanting to bring Tailwind on for awhile because there are several huge benefits as the UI styling is predictable and very nice to work with.

I absolutely cringed at the utility classes overtaking the template. React can get busy all on its own without Tailwinds class pollution.

The styled-component method is a game changer and now I have a new style workflow to integrate. Exciting!!! 🙌🙌

Collapse
 
ryanlanciaux profile image
Ryan Lanciaux

Thanks a ton for checking it out - so glad it was helpful! :D

I had a very similar reaction when looking at Tailwind initially. "This looks nice but I fear what it'll turn my components into" 😀

Collapse
 
alex_barashkov profile image
Alex Barashkov • Edited

Thank you for the article Ryan. I still can't understand tailwind approach that looks very similar to me with writing styles inline. So there are no difference between writing for example div class="overflow-hidden" and style="overflow:hidden".

Collapse
 
ryanlanciaux profile image
Ryan Lanciaux

Thank you. This is totally understandable that it looks similar to inline styling on occasion. I prefer the Tailwind approach using "micro CSS classes" opposed to inline styling. It shines when you have more properties you're applying especially on more than one page. If we wanted to apply a background, rounded corners, font-size, etc. it would be more likeclass="bg-indigo-300 rounded text-sm" (etc.).

Since these are classes and not inline, if we ever needed to update what font was used, anything using the Utility class would be styled the same. Tailwind's theme docs for a little more on this.

Collapse
 
liweiyi88 profile image
Julian Li • Edited

I would say the main benefit of tailwind is that it provides you with a consistent design system which scales well. you don't need to think about what is degree of green I need to use as background and what is degree of green I need to use as the text color. simply use the pre-defined class like text-green-100 or text-green-500 without thinking about the hex color. It also works the same way for scaling the font size, without thinking about what specific font size used for heading or paragraph. Simply try text-lg, text-base to see how it fits your design.

Collapse
 
lem01 profile image
Laurent Lemaire • Edited

Thanks for this good article!

How would you create the tailwind string parameter programmatically?
If let's say I want to add tailwind classes dynamically how would this work?

I tried adding my tailwind classes to a string and the pass it to the macro as a parameter but it doesn't seem to work.

  let twStr = ``
  twStr += (padding) ? 'pt-16 pb-16' : ''
  twStr += (altBackground) ? 'bg-gray-100' : ''

  const SSection = styled.section`
    ${tw`
      ${twStr}
    `}
    width: 2000px
  `
  return <SSection>{children}</SSection>

This works if rather than using the$(twStr) I use "pt-16 pb-16" (as a string a not as a variable).

Any hint?

Collapse
 
rodrigosaling profile image
Rodrigo Saling

Hello Ryan! Thanks for the tutorial!

I was using Tailwind with the PostCSS + Autoprefixer method and I thought that changing to styled-components would be an easy solution for when you want to disable a button or an input field.

But the main thing is that the Base style, they call it "Preflight", doesn't work anymore. Before I had this huge CSS file that was imported to my App, and the margins, paddings, and fonts were all working fine.

Now I have no clue on how to load these base styles. Do you have any idea on how to to that?

Thanks!

Collapse
 
astrit profile image
Astrit

Nice work Ryan 👍

Collapse
 
ryanlanciaux profile image
Ryan Lanciaux

Thank you :D