Adding negative values to the Tailwind CSS utility plugin is a simple process that can greatly enhance

Adding Negative Values to Tailwind CSS Utility Plugin

Quick Summary:

I've developed a custom Tailwind utility plugin that includes numeric values. I'm looking for a way to introduce negative numbers by adding a - at the start of the class, similar to the existing translate classes.

Aim and Challenge

Tailwind CSS operates on a straightforward principle. When dealing with properties that accept negative values, you can simply prepend a - to the class.

For instance:

<div className='left-16' />
(left: 64px) —>
<div className='-left-16' />
(left: -64px)

While developing a custom utility plugin for 3D rotation, I encountered an obstacle where negative values weren't recognized. Specifically, the class rotate-x-6 works, but -rotate-x-6 isn't accepted as a valid class.

The Code Snippet

plugin(function ({ matchUtilities, theme }) {
  matchUtilities(
    {
      'rotate-x': (value) => ({
        '--tw-rotate-x': value,
        transform: 'translate3d(var(--tw-translate-x), var(--tw-translate-y), var(--tw-translate-z)) rotate3d(var(--tw-rotate-x), var(--tw-rotate-y), var(--tw-rotate-z)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))',
      }),
    },
    { values: theme('rotate') },
  )

  matchUtilities(
    {
      'rotate-y': (value) => ({
        '--tw-rotate-y': value,
        transform:
          'translate3d(var(--tw-translate-x), var(--tw-translate-y), var(--tw-translate-z)) rotate3d(var(--tw-rotate-x), var(--tw-rotate-y), var(--tw-rotate-z)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))',
      }),
    },
    { values: theme('rotate') },
  )

  matchUtilities(
    {
      'rotate-z': (value) => ({
        '--tw-rotate-z': value,
        transform:
          'translate3d(var(--tw-translate-x), var(--tw-translate-y), var(--tw-translate-z)) rotate3d(var(--tw-rotate-x), var(--tw-rotate-y), var(--tw-rotate-z)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))',
      }),
    },
    { values: theme('rotate') },
  )
})

Potential Challenges

• The issue might stem from using the default rotate theme

• It's possible that only rotate-x is defined as a class, requiring a method to define -rotate-x. However, attempts to create a separate class haven't yielded recognition

Answer №1

To implement negative values, include supportsNegativeValues: true in the object when using matchUtilities() with the second parameter:

tailwind.config = {
  theme: {
    rotate: {
      1: '1',
    },
  },
  plugins: [
    tailwind.plugin(function({
      matchUtilities,
      theme
    }) {
      matchUtilities({
        'rotate-x': (value) => ({
          '--tw-rotate-x': value,
          transform: 'translate3d(var(--tw-translate-x), var(--tw-translate-y), var(--tw-translate-z)) rotate3d(var(--tw-rotate-x), var(--tw-rotate-y), var(--tw-rotate-z), 45deg) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))',
        }),
      }, {
        values: theme('rotate'),
        supportsNegativeValues: true,
      })

      matchUtilities({
        'rotate-y': (value) => ({
          '--tw-rotate-y': value,
          transform: 'translate3d(var(--tw-translate-x), var(--tw-translate-y), var(--tw-translate-z)) rotate3d(var(--tw-rotate-x), var(--tw-rotate-y), var(--tw-rotate-z), 45deg) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))',
        }),
      }, {
        values: theme('rotate'),
        supportsNegativeValues: true,
      })

      matchUtilities({
        'rotate-z': (value) => ({
          '--tw-rotate-z': value,
          transform: 'translate3d(var(--tw-translate-x), var(--tw-translate-y), var(--tw-translate-z)) rotate3d(var(--tw-rotate-x), var(--tw-rotate-y), var(--tw-rotate-z), 45deg) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))',
        }),
      }, {
        values: theme('rotate'),
        supportsNegativeValues: true,
      })
    }),
  ]
};
*, ::before, ::after {
  --tw-rotate-x: 0;
  --tw-rotate-y: 0;
  --tw-rotate-z: 0;
  --tw-translate-z: 0;
}
<script src="https://cdn.tailwindcss.com/3.3.5"></script>

<div class="w-10 h-10 bg-red-500 -rotate-x-1"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A method for performing precise division on numbers in JavaScript, allowing for a specific precision level (such as 28 decimal places)

I've searched extensively for a library that can handle division operations with more than 19 decimal places, but to no avail. Despite trying popular libraries like exact-math, decimal.js, and bignumber.js, I have not found a solution. How would you ...

Redux failing to trigger the action

Welcome to the login.js file! import {Form, Icon, Input, Button} from 'antd'; import React from 'react' import axios from 'axios' import {connect} from 'react-redux' import {loading} from '../actions/authentica ...

Toggle class in Angular ngMessages based on validity, not just on error

As a beginner with ngMessages, I'm curious to know if there is a $valid counterpart for the $error object. In my exploration of the ngMessages documentation in Angular, I have only encountered discussions about the $error object. <form name="conta ...

Encountering multiple npm warnings and issues in my React Native project resulting in app crashes

Having a bit of trouble with react-native addiction since yesterday................ https://i.stack.imgur.com/9QNsi.png It's causing issues with navigating to certain screens. Below is a snippet from my package.json: { "name": "test2", "version ...

Error occurs when a handlebar helper is nested too deeply

I have set up two handlebar helpers with the names 'outer' and 'inner'. In my template, I am using them as shown below: {{#outer (inner data)}} {{/outer}} However, I am encountering an error in the console related to the inner helper, ...

Unable to utilize the fetch method in Node.js for sending the API request

I am currently using fetch to send a POST request and retrieve data from an API on the server side (using nextjs 13 + node 18). Interestingly, I can successfully retrieve the data using postman and axios, but not with fetch. Below is the snippet of code ...

After upgrading to node version 20 and other dependencies, encountering ERR_REQUIRE_ESM issue

Attempting to update node from version 16 to 20 has led me to also consider upgrading some other libraries simultaneously. Upon trying to start my backend after completing the updates, the following error occurred: % yarn run dev [nodemon] 3.0.1 [nodemon] ...

Displaying Notification Messages with React Redux

Being new to React and Redux, I'm encountering some challenges with displaying the material-ui Snackbar as a notification panel after dispatching an event. In my example code snippet, the notification isn't being shown because this.props.sending ...

AngularJS Large file size

After successfully building the 5 MIN QUICKSTART app, I decided to minify it with webpack following the recommendations in the angularJS docs. To my surprise, the size of the minified AngularJS file turned out to be approximately 700 KB, which is significa ...

Discover the process of loading one controller from another controller in Angular JS

Is it possible to load an entire controller1 from a different controller2, not just a function? ...

Ways to update a value in a table by comparing two JSON objects

I am currently working with two JSON files containing data as shown below: JSON 1: let classes = [{ "Class": "A", "as_of": "12/31/2020", "student": [{ "raji": { "eng": ...

The external embedded CSS seems to be missing from index.html after the Docker build process

My Vue/Webpack application includes an embedded CSS link in my index.html file that references an external source. Here is an example snippet: <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" con ...

JSON to URN Converter: A tool for converting JSON references

Is there a way to convert URL references to JSON and back again programmatically? (include:(and:List((or:(urn:li:adTargetingFacet:locations:List(urn:li:geo:102221843)) ),(or:(urn:li:adTargetingFacet:skills:List(urn:li:skill:17)))))) Are there any JavaScri ...

"What is the best way to prevent a button from being enabled in Angular if a list or array is not

I am incorporating data from my service in the component: ngOnInit() { this._sharedService. getReceiptItem().takeUntil(this.ngUnsubscribe). subscribe(products => this.receiptItems = products); } Is there a way to deactivate a button if there ...

Ensure the validation of multiple form fields in a single function

I've implemented code to validate a form field as you input values, changing the border color to red if invalid or green if valid: function FormValidation(){ var fn=document.getElementById("firstName").value; if(fn == ""){ document.ge ...

In HTML, the size and position of an <a> element cannot be altered or adjusted

I am having trouble with an anchor element that is contained within a div. No matter what I try, I cannot resize or move it up or down without it covering its sibling element. I have attempted using transform-translate and margin properties, but to no avai ...

Extra spaces within the source code visible after the page has been processed by Angular

Within my angular application, I am retrieving a list of addresses from a service that provides an object structured like this: { Flat: "10B", BuildingName: "Some Building", Line: "10B Some Building Road Town POST CODE", Postcode: "POST CODE", ...

concealing the upper header while scrolling and shifting the primary header upwards

Is there a way to use CSS to move the main header navigation, including the logo and links, on my website up when scrolling down in order to hide the top black bar header that contains contact information? The website in question is atm.truenorthmediasol ...

Leveraging the power of nested selectors in Sass within your Next.js

What could be the issue at hand? I am interested in switching the theme or activating a menu, among other potential changes... ...

Is transforming lengthy code into a function the way to go?

I have successfully implemented this code on my page without any errors, but I am wondering if there is a more concise way to achieve the same result. Currently, there is a lot of repeated code with only minor changes in class names. Is it possible to sh ...