Coder Social home page Coder Social logo

mapcss's Introduction

mapcss

Tiny, composable Atomic CSS engine

deno land nest.land

release deno version deno doc

test codecov DeepSource DeepScan grade

Semver Conventional Commits semantic-release: angular license

Docs Playground


๐Ÿšš This repository will be transferred to mapcss shortly.

๐Ÿšง This project is currently in beta release. All interfaces are subject to change.

Features

  • ๐ŸŽ“ Hierarchical mapping strategy

    Map CSS Statements with hierarchical identifiers. Compared to flat, it is more readable and uses as few regular expressions as possible. In addition, regular expression matching is scoped, improving performance.

  • ๐Ÿ’… Flexible

    The CSS Statement is written in CSS-in-JS style. It is a plain JavaScript Object with excellent readability.

  • ๐ŸŒณ AST based

    CSS-in-JS is converted to AST(postcss AST). postcss AST is a widely known standard format that can be freely converted and added to and benefits from a large ecosystem.

  • ๐ŸŒ Universal

    It works with Browser, Deno, and Node.js without polyfill.

    Internally using the universal version of postcss postcss-core.

    The bundle size has been taken into consideration, and the code base is created with pure functions.

  • ๐Ÿ” Orderless(experiment)

    User does not need to care about the order of the CSS Statement at all. Therefore, there is no concept of order or layer.

    The RuleSet will be sorted by the number of properties in the Declaration Block.

Usage

mapcss provides several preset.

For example, using presetTw, you can use the utility class of TailwindCSS.

import {
  extractSimple,
  generate,
} from "https://deno.land/x/mapcss@$VERSION/core/mod.ts";
import { presetTw } from "https://deno.land/x/mapcss@$VERSION/preset_tw/mod.ts";

const code = `<div className="relative flex">
  <p className="text-red-500/20"></p>  
</div>
`;
const tokens = extractSimple(code);
const output = await generate(tokens, { preset: [presetTw()] });
console.log(output.css);
/*
  .relative{position:relative;}
  .flex{display:flex;}
  .text-red-500\/20{color:rgb(239 68 68/.2);}
*/

What

mapcss is an Atomic-oriented CSS generator.

It is strongly influenced by TailwindCSS and UnocCSS, but with the following differences.

identifier to CSS-in-JS

The essence of mapcss is to map an identifier to a CSS Statement with JavaScript Object notation (CSS-in-JS).

A Map is a Plain Object with a hierarchical structure, which can be expressed concisely with Object literals.

For example, the following CSS Statement can be mapped as follows:

.inline-block{display: inline;}
import type { CSSMap } from "https://deno.land/x/mapcss@$VERSION/core/mod.ts";
const cssMap: CSSMap = {
  inline: {
    block: { display: "inline" },
  },
};

It is also possible to express dynamic identifiers using regular expressions.

.z-123{z-index: 123;}
import type { CSSMap } from "https://deno.land/x/mapcss@$VERSION/core/mod.ts";
const rePositiveNumber = /^(\d+)$/;
const cssMap: CSSMap = {
  z: {
    "*": ({ id }) => {
      // It actually checks the validity of the numbers
      const regExpExecResult = rePositiveNumber.exec(id);
      if (regExpExecResult) {
        return { zIndex: Number(regExpExecResult[1]) };
      }
    },
  },
};

We support the first class because it is the most frequent mapping to CSS declaration block.

For the definition of any CSS Statement, CSS-in-JS representation is also supported.

@media (min-width: 640px) {
  .container {
    max-width: 640px;
  }
}
.container{width: 100%;}
import type { CSSMap } from "https://deno.land/x/mapcss@$VERSION/core/mod.ts";
const cssMap: CSSMap = {
  // className: .container
  container: (_, { className }) => ({
    type: "css",
    value: {
      "@media (min-width: 640px)": {
        [className]: {
          maxWidth: "640px",
        },
      },
      [className]: {
        width: "100%",
      },
    },
  }),
};

The Object search model

Explore the object hierarchy based on identifier. Hierarchy traversal is much more performant than flat traversal.

For example, the computational complexity of regular expression matching from a flat structure is O(N).

If the search finds CSS-in-JS, it will be converted to AST. The AST currently uses the postcss AST.

This will benefit from the postcss ecosystem.

Finally, we show the conversion transition.

token -> DeepMap { identifier -> CSS-in-JS } -> AST -> Style Sheet

License

Copyright ยฉ 2021-present TomokiMiyauci.

Released under the MIT license

mapcss's People

Contributors

semantic-release-bot avatar tomokimiyauci avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

mapcss's Issues

build docs is never-ending

When I build a certain component, it hangs.

Probably occurs when using global variables, and occur memory leak.
Since it is not possible to deploy automatically by CI/CD, substitute the component in question.

denoland/deno#7087

Architecture: Use CSS AST internally

From the standpoint of compatibility and versatility, the role of mapcss is to be the interface for conversion to AST.

mapping rule -> plain object -> AST -> CSS

Change core to asynchronous code

Assuming asynchronous has the following advantages

  • dynamic map
{
  cssMap: {
    dynamic:
    (async () => import("source.json", { assert: { type: "json" } }));
  }
}
  • async postcss plugin

Breaking Change

  • generate
  • transform

named types export should not yet

Although named type export has been supported since Deno 14, it may be too early to use it.

Since it is not available in Aleph.js, it is temporarily refactored.

Redesign the cssMap

Redesign cssMap to be only object literals, which are easier to declare.

Treat "" (empty) and $DEFAULT properties as special keys

content with bracket should emit class

Describe the bug

content with quote does not work

Input

<div class="content-['']"></div>

Config

import {  presetTw } from "https://esm.sh/@mapcss/preset-tw@beta";
import { presetTypography } from "https://esm.sh/@mapcss/preset-typography@beta";
import { simpleExtractor } from "https://esm.sh/@mapcss/config@beta";
import type { Config } from "config";

/** You can try the following features:
 * - Chrome 80+
 * - iOS Safari 15+
 * - Other browsers that support module workers
 */
// import { presetSVG, iconifyJSON } from "https://esm.sh/@mapcss/preset-svg@beta"
// import mdi from "https://esm.sh/@iconify-json/mdi/icons.json" assert {
// type: "json",
// };
// import autoprefixer from "https://esm.sh/autoprefixer"

export default <Config> {
  separator: "-",
  variablePrefix: "map-",
  extractor: simpleExtractor,
  preset: [
    presetTw({
      darkMode: "class",
    }),
    presetTypography({
      css: {
        p: false,
        a: {
          textDecoration: false,
          color: false,
        },
      },
    }),
    // presetSVG({
    //   mdi: iconifyJSON(mdi)
    // })
  ],
  minify: false,
  // postcssPlugin: [autoprefixer]
};

Playground link

https://mapcss.miyauchi.dev/playground?input=PGRpdiBjbGFzcz0iY29udGVudC1bJyddIj48L2Rpdj4&config=aW1wb3J0IHsgIHByZXNldFR3IH0gZnJvbSAiaHR0cHM6Ly9lc20uc2gvQG1hcGNzcy9wcmVzZXQtdHdAYmV0YSI7CmltcG9ydCB7IHByZXNldFR5cG9ncmFwaHkgfSBmcm9tICJodHRwczovL2VzbS5zaC9AbWFwY3NzL3ByZXNldC10eXBvZ3JhcGh5QGJldGEiOwppbXBvcnQgeyBzaW1wbGVFeHRy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SwKICBtaW5pZnk6IGZhbHNlLAogIC8vIHBvc3Rjc3NQbHVnaW46IFthdXRvcHJlZml4ZXJdCn07Cg&version=djEuMC4wLWJldGEuNTA

Expected behavior

.content-\[\'\'\]{
    --map-content: '';
    content: var(--map-content);
}

Actual behavior

no emit

Version

v1.0.0-beta.50

Registry

npm

Runtime

Chrome 99.0.4844.74

Additional context

No response

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.