1

Similar to how Tailwind does border-l and border-blue-200, how could I do this with a custom utility class? Eg. custom custom-blue-200.

At the moment I have a utility class defined like this:

@layer utilities {
    .corner-cut {
         background-image: linear-gradient(315deg, transparent 10px, rgba(255, 255, 255, 0.9) 10px);
    }
}

This has been working fine, but now I need to combine these with a color parameter in order to make the rgb part of the gradient not hard-coded to one color.

2 Answers 2

1

You need to create a plugin that defines a dynamic utility:

// tailwind.config.js
const plugin = require('tailwindcss/plugin')
const flattenColorPalette = require('tailwindcss/lib/util/flattenColorPalette').default
const { parseColor } = require('tailwindcss/lib/util/color')

module.exports = {
  plugins: [
    plugin(({ matchUtilities, theme }) => {
      matchUtilities(
        {
          'corner-cut': (value) => {
            const { color } = parseColor(value)

            return {
              backgroundImage: `linear-gradient(315deg, transparent 10px, rgba(${color[0]} ${color[1]} ${color[2]} / 0.9) 10px`,
            }
          },
        },
        { values: flattenColorPalette(theme('colors')), type: 'color' },
      )
    }),
  ]
}

You can then combine the class with a custom colour, e.g. corner-cut-blue-200.

Sign up to request clarification or add additional context in comments.

Comments

-1

Try to add !important to the end of each declaration yourself:

@layer utilities {
  .corner-cut {
    background-image: linear-gradient(315deg, transparent 10px, rgba(255, 255, 255, 0.9) 10px) !important;
  }
}

1 Comment

The colour needs to come from the class and not be hard-coded. Important isn't needed as it was already working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.