{"id":15250456,"url":"https://github.com/sgtpep/csstag","last_synced_at":"2025-04-10T19:42:01.987Z","repository":{"id":47769079,"uuid":"169158568","full_name":"sgtpep/csstag","owner":"sgtpep","description":"Tagged template for CSS Modules: const styles = css`.root { color: red; }`; \u003cdiv class={styles.root}\u003e\u003c/div\u003e","archived":false,"fork":false,"pushed_at":"2023-03-02T18:43:38.000Z","size":2030,"stargazers_count":18,"open_issues_count":10,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T17:21:17.064Z","etag":null,"topics":["browser","css","css-modules","esm","postcss","tagged-template","tagged-template-literal","template-literal"],"latest_commit_sha":null,"homepage":"https://sgtpep.github.io/csstag/demo/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sgtpep.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-04T22:17:05.000Z","updated_at":"2024-02-23T19:58:12.000Z","dependencies_parsed_at":"2024-10-13T06:20:45.285Z","dependency_job_id":"f6d557f4-8444-4c97-8592-c931de344737","html_url":"https://github.com/sgtpep/csstag","commit_stats":{"total_commits":223,"total_committers":5,"mean_commits":44.6,"dds":0.04035874439461884,"last_synced_commit":"29fb034ed0f5942f67a4bb4b99f9e30638ef9601"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgtpep%2Fcsstag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgtpep%2Fcsstag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgtpep%2Fcsstag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sgtpep%2Fcsstag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sgtpep","download_url":"https://codeload.github.com/sgtpep/csstag/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248281415,"owners_count":21077423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["browser","css","css-modules","esm","postcss","tagged-template","tagged-template-literal","template-literal"],"created_at":"2024-09-29T16:04:55.930Z","updated_at":"2025-04-10T19:42:01.965Z","avatar_url":"https://github.com/sgtpep.png","language":"JavaScript","funding_links":[],"categories":["Libraries"],"sub_categories":["CSS"],"readme":"# csstag\n\n[![Build Status](https://travis-ci.org/sgtpep/csstag.svg?branch=master)](https://travis-ci.org/sgtpep/csstag)\n\nUse [CSS Modules](https://github.com/css-modules/css-modules) as [tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates) in a browser and [Node.js](https://nodejs.org/). Example:\n\n```javascript\nimport css from 'csstag';\nconst styles = css`\n  .root {\n    color: red;\n  }\n`;\n\u003cdiv class={styles.root} /\u003e;\n```\n\nDemo: https://sgtpep.github.io/csstag/demo/.\n\n## Benefits over other CSS-in-JS libraries\n\n- The majority of [CSS-in-JS](https://github.com/tuchk4/awesome-css-in-js) libraries are opinionated and bloated, while `csstag` relies only on [CSS Modules](https://github.com/css-modules/css-modules) which is a widely adopted and well-understood open specification very close to standard CSS with a number of implementations.\n- With CSS Modules you can write any valid CSS.\n- It's framework agnostic and may be used together with [React](https://reactjs.org/), [Preact](https://preactjs.com/), any other library or framework or no framework at all.\n- Has zero dependencies (it made possible by bundling them in one file).\n- Provided also as a native ES module which can be imported directly in browser without bundlers: `import css from './node_modules/csstag/index.min.js';` or `import css from 'https://unpkg.com/csstag';`\n- Can by excluded from production code and replaced with static CSS using a [Babel](https://babeljs.io/) plugin [babel-plugin-csstag](https://github.com/sgtpep/csstag/tree/master/babel-plugin-csstag).\n\n## Installation\n\n### Using `npm`\n\n```shell\nnpm install csstag\n```\n\n### Using `yarn`\n\n```shell\nyarn add csstag\n```\n\n## Importing\n\n### From Node.js\n\n```javascript\nconst { css } = require('csstag');\n```\n\nOr from `.mjs`:\n\n```javascript\nimport css from 'csstag';\n```\n\n### From a bundler ([webpack](https://webpack.js.org/), etc.)\n\n```javascript\nimport css from 'csstag';\n```\n\n### From a browser\n\nIf you don't use a bundler ([webpack](https://webpack.js.org/), etc.) but prefer native [ES6 modules](http://exploringjs.com/es6/ch_modules.html) instead (`\u003cscript type=\"module\"\u003e\u003c/script\u003e`):\n\n```javascript\nimport css from './node_modules/csstag/index.min.js';\n```\n\nOr use [unpkg](https://unpkg.com/) CDN:\n\n```javascript\nimport css from 'https://unpkg.com/csstag';\n```\n\n## Usage\n\nOnce imported you can declare a CSS module as a tagged template using the tag function `css`:\n\n```javascript\nimport css, { appendStyles } from 'csstag';\n\nconst styles = css`\n  .foo {\n    color: red;\n  }\n  .bar {\n    color: blue;\n  }\n`;\n```\n\nIt returns an object with all exported class names in it:\n\n```javascript\nconsole.log(styles);\n{ foo: 'csstag__foo___LTk0O', bar: 'csstag__bar___LTk0O' }\n```\n\nThe module `csstag` contains the array of compiled styles called `styles`, which is populated with a new item every time `css` tag function is being called, and now contains:\n\n```javascript\n[\n  `.csstag__foo___LTk0O {\n    color: red;\n  }\n  .csstag__bar___LTk0O {\n    color: blue;\n  }`,\n];\n```\n\nYou may want to append all newly added items from that array as a stylesheet as `\u003cstyle\u003e` in your document's `\u003chead\u003e` using the function `appendStyles` imported earlier (in React-like projects it makes sense to call it together with `render()` in your entry point):\n\n```javascript\nappendStyles();\n```\n\nIf you're using [Prettier](https://prettier.io/), it will format the CSS code inside `css` tag function.\n\n`csstag` itself comes as a bundle containing some heavy-weight packages like [PostCSS](https://postcss.org/) and has a size of ~100K minimized. To exclude it from production code and leave only compiled styles you might use a [Babel](https://babeljs.io/) plugin [babel-plugin-csstag](https://github.com/sgtpep/csstag/tree/master/babel-plugin-csstag).\n\n## API\n\n- `css`: The main tag function itself which can be imported as a default or named import.\n- `appendStyles()`: Appends all stylesheets built from recent uses of `css` tag function as `\u003cstyle\u003e` in your document's `\u003chead\u003e`. The consecutive calls to this function will produce no effect until next calls to `css` tag function. Uses `popStyles()` internally.\n- `popStyles()`: Empties and returns a list of stylesheets built from recent uses of `css` tag function.\n- `styles`: A list of recently built stylesheets for direct access.\n\nIf you don't like storing a list of built stylesheets in `csstag` module's `styles` array, or you're dealing, for instance, with multiple applications at once, you may create new functions bound to any other array using `.bind()`:\n\n```javascript\nimport csstag, { appendStyles } from 'csstag';\n\nconst styles = [];\nconst css = csstag.bind(styles);\ncss`\n  .root {\n    color: red;\n  }\n`;\nappendStyles.bind(styles)();\n```\n\n## Options\n\nTo pass options pass an object with them as a second argument of `.bind()`:\n\n```javascript\nimport csstag from 'csstag';\n\nconst css = csstag.bind(null, { prefix: 'myapp' });\n```\n\n- `modulesLocalByDefault`: Options passed to [postcss-modules-local-by-default](https://www.npmjs.com/package/postcss-modules-local-by-default) (default: `null`).\n- `modulesParser`: Options passed to [postcss-modules-parser](https://www.npmjs.com/package/postcss-modules-parser) (default: `null`).\n- `modulesScope`: Options passed to [postcss-modules-scope](https://www.npmjs.com/package/postcss-modules-scope) (default: `{ generateScopedName: ... }`, a function generating compiled class names).\n- `modulesValues`: Options passed to [postcss-modules-values](https://www.npmjs.com/package/postcss-modules-values) (default: `null`).\n- `pluginsBefore`: An array of `PostCSS` plugin before the default plugin list (default: `[]`).\n- `plugins`: An array of `PostCSS` plugins added to the end of the default plugin list (default: `[]`).\n- `prefix`: A first component of a generated class name (default: `csstag`).\n- `process`: Options passed to the [`process()`](https://api.postcss.org/Processor.html#process) method of `PostCSS` (default: `null`).\n- `hashLength`: Class name hash length to minimize collision risk (default: `5`).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgtpep%2Fcsstag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsgtpep%2Fcsstag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsgtpep%2Fcsstag/lists"}