3

I'm trying to import tailwind inside my styled components globalstyle, to set base styles.

Code below doesn't work, so any suggestions on how to make it work?

import {createGlobalStyle} from 'styled-components';


const GlobalStyle = createGlobalStyle`
  @tailwind base;
  @tailwind components;
  @tailwind utilities;
  
  @layer base {
    h1 {
      @apply text-2xl;
    }
    h2 {
      @apply text-xl;
    }
  }
`
Jonatanbs
  • 223
  • 2
  • 9

1 Answers1

0

Try twin.macro !!~~~

Click here!

// src/styles/GlobalStyles.js
import React from 'react'
import { createGlobalStyle } from 'styled-components'
import tw, { theme, GlobalStyles as BaseStyles } from 'twin.macro'

const CustomStyles = createGlobalStyle`
  body {
    -webkit-tap-highlight-color: ${theme`colors.purple.500`};
    ${tw`antialiased`}
  }
`

const GlobalStyles = () => (
  <>
    <BaseStyles />
    <CustomStyles />
  </>
)

export default GlobalStyles

Add the twin config

// babel-plugin-macros.config.js
module.exports = {
  twin: {
    preset: 'styled-components',
  },
}
JIAM SEO
  • 18
  • 2
  • Unfortunately this library only works with projects that use Babel. As of today, SWC isn't supported. – SimoAmi Jul 16 '23 at 18:11