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

Add @apply support in plugins #770

Merged
merged 2 commits into from
Dec 6, 2022
Merged

Conversation

ben-rogerson
Copy link
Owner

@ben-rogerson ben-rogerson commented Dec 4, 2022

This PR adds support for the usage of @apply to create tailwindcss plugins.

Use twin classes including class groups (eg: first:(block mt-4)) to define your plugin styles:

// tailwind.config.js

// Usage: `tw("block bg-black", "[.comma-divided-sets]:visible")`
const tw = (...classes) => {
  const result = {}
  result[`@apply ${classes.join(" ")}`] = {}
  return result
}

module.exports = {
  theme: { colors: { primary: "#F07E22" } },
  plugins: [
    ({ addBase, addComponents, addUtilities }) => {
      addBase({
        // Add colors from your theme
        "html, body": tw("selection:text-primary"),
      })

      addComponents({
        ".component": tw(
          // Use arbitrary classes
          "content-[arbitrary value] [.arbitrary-variant]:content [content:arbitrary-property]]"
        ),
      })

      addUtilities({
        // Use bracketed groups to style multiple classes at once
        ".utility": tw("first:(block mt-4)"),
      })
    },
  ],
}

Original feature reference: tailwindlabs/tailwindcss#2049

@ben-rogerson
Copy link
Owner Author

I've also made Twin warn us when using a class that doesn't exist within @apply - rather than silently fail like the tailwindcss default.

@ben-rogerson ben-rogerson merged commit 44bef1d into master Dec 6, 2022
@ben-rogerson ben-rogerson deleted the bugfix/allow-plugin-apply branch December 6, 2022 20:47
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

Successfully merging this pull request may close these issues.

1 participant