Socket
Book a DemoInstallSign in
Socket

@daviddarnes/contrast-details

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

@daviddarnes/contrast-details

A Web Component to display the contrast ratio and level of two colours using CSS custom properties

1.1.0
latest
Source
npmnpm
Version published
Weekly downloads
11
266.67%
Maintainers
1
Weekly downloads
 
Created
Source

contrast-details

A Web Component to display the contrast ratio and level of two colours using CSS custom properties

Demo | Custom template demo | Further reading

Examples

General usage example:

<script type="module" src="contrast-details.js"></script>

<contrast-details
  style="--foreground: #444; --background: #ccc"
></contrast-details>
<contrast-details
  style="--foreground: #444; --background: #613fe8"
></contrast-details>

Example using inherited values:

<script type="module" src="contrast-details.js"></script>

<div style="--foreground: #444">
  <contrast-details style="--background: #ccc"></contrast-details>
  <contrast-details style="--background: #ffffff"></contrast-details>
  <contrast-details style="--background: #613fe8"></contrast-details>
</div>

Example using level attribute for styling

<style>
  contrast-details[level^="aa"] [data-key="level"]::after {
    content: " ✅";
  }

  contrast-details[level="fail"] [data-key="level"]::after {
    content: " ❌";
  }
</style>

<script type="module" src="contrast-details.js"></script>

<contrast-details
  style="--foreground: #444; --background: #ccc"
></contrast-details>
<contrast-details
  style="--foreground: #444; --background: #613fe8"
></contrast-details>

Example using a custom template:

<script type="module" src="contrast-details.js"></script>

<template id="contrast-details-template">
  <p>
    Contrast: <span data-key="ratio"></span><span data-key="level"></span>
  </p>
</template>

<contrast-details
  style="--foreground: #444; --background: #ccc"
></contrast-details>
<contrast-details
  style="--foreground: #444; --background: #613fe8"
></contrast-details>

Features

This Web Component allows you to:

  • Compare two colours and render their contrast details
  • Render the contrast ratio
  • Render the contrast level, as per WCAG grading
  • Utilise CSS custom properties to provide values, either on the element or through inheritance, which also allows the element to use those colours as you wish
  • Utilise a level attribute selector to style elements differently depending on aaa, aa and fail grades

Installation

You have a few options (choose one of these):

  • Install via npm: npm install @daviddarnes/contrast-details
  • Download the source manually from GitHub into your project.
  • Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)

Usage

Make sure you include the <script> in your project (choose one of these):

<!-- Host yourself -->
<script type="module" src="contrast-details.js"></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://www.unpkg.com/@daviddarnes/contrast-details@1.1.0/contrast-details.js"
></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://esm.sh/@daviddarnes/contrast-details@1.1.0"
></script>

Credit

With thanks to the following people:

Keywords

web

FAQs

Package last updated on 29 May 2024

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.