Giter VIP home page Giter VIP logo

tailwindcss-class-parser's Introduction

Tailwindcss Class Parser

Tailwindcss Parser is an open-source library for parsing Tailwind CSS classes into an Abstract Syntax Tree (AST) and AST to classname. This allows for easier manipulation and analysis of Tailwind CSS classes in a declarative manner. If you are into a WYSIWYG like CMS builders this library can parse classes from blocks (or whatever you are using as a name for your components) or convert class definitions into classnames into your blocks.

Inspired by the Tailwind CSS v4.0.0's candidate.ts parser.


It is probably missing many Tailwind classes and might throw some exceptions. You can create a PR to add Tailwind class names to the plugins.ts file to support class names that I've missed.

Installation

You can install the Tailwindcss Parser via npm:

npm install @xengine/tailwindcss-class-parser

Usage

To use the Tailwindcss Parser, you need to import the parse function from the package and pass the Tailwind CSS class you want to parse. The function returns an AST representation of the class.

Example

import { parse } from '@xengine/tailwindcss-class-parser';

const ast = parse('lg:hover:text-red-500/70');

console.log(ast);
/*
Output:
{
    root: "text",
    property: "textColor",
    value: {
        value: "#ef4444",
        kind: "color",
        class: ["color"],
        raw: "red-500"
    },
    modifier: "70",
    variants: [
        {
            kind: "named",
            type: "interaction",
            value: "hover"
        },
        {
            kind: "named",
            type: "media",
            value: "lg"
        }
    ],
    important: false,
    negative: false
}
*/

API

parse(className: string): object

Parses a given Tailwind CSS class and returns an AST object.

Parameters

  • className (string): The Tailwind CSS class to parse.

Returns

  • object: The AST representation of the Tailwind CSS class.

className(ast: object): string

Converts a given Tailwind CSS AST to the original class string.

Parameters

  • ast (object): small Tailwind CSS declaration
  AstDeclaration = {
    property: string
    value: string
    variants?: Variant[]
    modifier?: string,
    important?: boolean
    negative?: boolean,
}

Returns

  • className (string): The Tailwind CSS class.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.

Acknowledgements

Special thanks to the Tailwind CSS team for their amazing work on Tailwind CSS.

@siddharthkp from https://github.com/ui-devtools/tailwind-utils

tailwindcss-class-parser's People

Contributors

erdemuslu avatar xengine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

erdemuslu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.