npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dlgshi/cypress-plugin-designtokens

v0.0.1-development

Published

This project will allow you to check / verify designTokens assigned to css properties in the project

Downloads

14

Readme

cypress-plugin-designTokens

This project will allow you to check / verify designTokens assigned to css properties in the project

Description

This is a custom Cypress command that allows you to get the design tokens used for a specific CSS property on a given element. It searches through all the stylesheets in the document and returns an array of the design tokens used for the given CSS property on the element.

The cy.getDesignTokensByCssProperty command takes two arguments:

  • subject: A jQuery element that you want to search for design tokens on.
  • cssProperty: The name of the CSS property that you want to get design tokens for. Note - If no design tokens are found for the given CSS property on the element, an error will be thrown.

Installation

To use cy.getDesignTokensByCssProperty, you can copy the code for the command into your project's Cypress commands file (cypress/support/commands.{js|ts} by default). Alternatively, you can import the command from a separate JavaScript module.

Usage

You can then use the command like this:

Examples

import "@dlgshi/cypress-plugin-designtokens";
cy.get("#my-element").getDesignTokensByCssProperty("color").then((tokens) => {
  // Do something with the tokens
});

Get the design tokens for the color property on an element with ID my-element:

cy.get("#my-element").getDesignTokensByCssProperty("color").then((tokens) => {
  // Do something with the tokens
});

Get the design tokens for the margin property on all button elements:

cy.get("button").getDesignTokensByCssProperty("margin").then((tokens) => {
  // Do something with the tokens
});

Get the design tokens for the background-color property on an element with class my-class:

cy.get(".my-class").getDesignTokensByCssProperty("background-color").then((tokens) => {
  // Do something with the tokens
});

If the matching CSS rules use only one design token, the command returns a single-element array. If the rules use more than one token, the command returns an array of all the token names.

cy.get('button').getDesignTokensByCssProperty('color').then((tokens) => {
  // `tokens` is an array of design token names, or an empty array if no tokens were found
  console.log('Design tokens used in button color:', tokens);
});

To retrieve the first token name in the array, you can use array destructuring like this:

cy.get('button').getDesignTokensByCssProperty('color').then(([token]) => {
  // `token` is the first design token name used in the matching CSS rules, or `undefined` if no tokens were found
  console.log('First design token used in button color:', token);
});

If you're using TypeScript, you can import the DesignToken type from the @your-org/design-tokens package and use it to annotate the return type of the getDesignTokensByCssProperty command:

import type { DesignToken } from '@your-org/design-tokens';

declare global {
  namespace Cypress {
    interface Chainable {
      getDesignTokensByCssProperty(
        cssProperty: string
      ): Chainable<DesignToken[] | [DesignToken] | []>;
    }
  }
}

Example usage

cy.get('button').getDesignTokensByCssProperty('color').then((tokens) => {
  // `tokens` is an array of `DesignToken` objects, or an empty array if no tokens were found
  console.log('Design tokens used in button color:', tokens);
});

TypeScript support

In most cases, types work just by installing plugin, but you can add the types to your tsconfig.json

{
  "types": ["@dlgshi/cypress-plugin-designtokens"]
}

Dependencies

cy.getDesignTokensByCssProperty depends on the jQuery and Cypress libraries, and assumes that the design tokens used in the CSS styles are defined as CSS variables in the document's stylesheets.