New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

tailwindcss-oklch

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tailwindcss-oklch

Bring OKLCH colors to tailwind and introduces handy color utilities

  • 0.0.1
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Tailwind OKLCH

Brings OKLCH to Tailwind and introduces these helpful utilities:

  • Contrast utilities that automatically calculate the contrast color (black/white) of any color in CSS.
  • Match utilities that will match the color of a property with another property (e.g. outline color matches the background color), with support for opacity modifiers.
  • Lightness offset utilities that darken or lighten colors (e.g. on hover).

You don't need to change your theme colors, since this plugin uses color.js to convert all existing colors.

Installation

To use this package, install it via npm:

npm install tailwindcss-oklch

Then, enable the plugin in your Tailwind config:

/** @type {import('tailwindcss').Config} */
module.exports = {
  plugins: [
    require('tailwindcss-oklch')(),
  ],
}

Features

Contrast utilities

The text-bg-contrast class added to an element with the bg-blue-500 class will set the text color to the contrast color (white or black) of the blue button. If the color of the button changes lightness over time, the contrast color will be recalculated in CSS. Opacity modifiers can be added to change the opacity of the color you're setting. Examples:

  • text-bg-contrast sets the text color to the contrast color of the background color.
  • text-border-contrast sets the text color to the contrast color of the border color.
  • bg-border-contrast sets the background color to the contrast color of the border color.
  • text-bg-contrast/50 sets the text color to the contrast color of the background color with an opacity of 50%.
  • bg-border-contrast/50 sets the background color to the contrast color of the border color with an opacity of 50%.

Match utilities

The text-border class sets the text color to the same color as the border of the element. Other examples:

  • border-text sets the border color to the text color. The difference with border-current is the ability to set opacity or change lightness with additional classes.
  • border-text/50 sets the border color to the text color with an opacity of 50%.

Lightness offset

The text-lightness-offset-10 increases the lightness of the text with 10%. Other examples:

  • hover:-bg-lightness-offset-10 darkens the background on hover with 10%.

Configuration

If you prefer to change the threshold when the contrast color switches to black or white, you can adjust the value of contrastThreshold. This value should be somewhere between 0 and 1. The higher the number, the more likely white will be chosen as the contrast color. While I was working on this plugin, I noticed that a contrast threshold of .6 looked better on different screens than .5, since dark colors seem to have lesser contrast then lighter colors in reality.

Precision was added since color.js uses floats to calculate the OKLCH values, which can result in long numbers. Defaults to 6.

/** @type {import('tailwindcss').Config} */
module.exports = {
  plugins: [require('tailwindcss-oklch')({
    contrastThreshold: .5,
    precision: 8,
  })],
}

Demo

View the demo site.

Custom properties / CSS variables support

In case you prefer named color names, you can add them in your Tailwind config:

/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      colors: {
        primary: `oklch(var(--color-primary-l) var(--color-primary-c) var(--color-primary-h) / <alpha-value>)`,
        secondary: `oklch(var(--color-secondary-l) var(--color-secondary-c) var(--color-secondary-h) / <alpha-value>)`,
        success: `oklch(var(--color-success-l) var(--color-success-c) var(--color-success-h) / <alpha-value>)`,
        warning: `oklch(var(--color-warning-l) var(--color-warning-c) var(--color-warning-h) / <alpha-value>)`,
        danger: `oklch(var(--color-danger-l) var(--color-danger-c) var(--color-danger-h) / <alpha-value>)`,
      },
    },
  },
};

Afterwards, you can set the separate color values in CSS. Use tools like oklch.com to convert your colors. Make sure to use decimals instead of percentages for the lightness, since these are needed to calculate contrast colors.

@layer base {
  :root {
    --color-primary-l: .32;
    --color-primary-c: .1;
    --color-primary-h: 150;
  }
}

Keywords

FAQs

Package last updated on 08 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc