{"id":17138186,"url":"https://github.com/esamattis/postcss-ts-classnames","last_synced_at":"2025-10-13T18:47:13.994Z","repository":{"id":48521137,"uuid":"219158292","full_name":"esamattis/postcss-ts-classnames","owner":"esamattis","description":"PostCSS plugin to generate TypeScript types from your CSS class names.","archived":false,"fork":false,"pushed_at":"2021-02-26T15:35:39.000Z","size":937,"stargazers_count":71,"open_issues_count":2,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-12T06:53:10.549Z","etag":null,"topics":["codegen","css","typescript"],"latest_commit_sha":null,"homepage":"https://npm.im/postcss-ts-classnames","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esamattis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-02T13:32:57.000Z","updated_at":"2025-10-10T14:44:27.000Z","dependencies_parsed_at":"2022-09-09T13:41:46.401Z","dependency_job_id":null,"html_url":"https://github.com/esamattis/postcss-ts-classnames","commit_stats":null,"previous_names":["epeli/postcss-ts-classnames"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/esamattis/postcss-ts-classnames","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esamattis%2Fpostcss-ts-classnames","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esamattis%2Fpostcss-ts-classnames/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esamattis%2Fpostcss-ts-classnames/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esamattis%2Fpostcss-ts-classnames/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esamattis","download_url":"https://codeload.github.com/esamattis/postcss-ts-classnames/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esamattis%2Fpostcss-ts-classnames/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279010963,"owners_count":26084840,"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","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["codegen","css","typescript"],"created_at":"2024-10-14T20:09:00.973Z","updated_at":"2025-10-13T18:47:13.947Z","avatar_url":"https://github.com/esamattis.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# postcss-ts-classnames\n\n[PostCSS][] plugin to generate TypeScript types from **your** CSS class names.\n\n[postcss]: https://postcss.org/\n\nIt generates a global `ClassNames` type which is a union of all classes\nused in your project whether written by you or from a framework such as\nBootstrap or Tailwind (which can get [bit too slow](https://github.com/esamattis/postcss-ts-classnames/issues/5)).\n\nEx. for css\n\n```css\n.button {\n    background: green;\n}\n\n.button-danger {\n    background: red;\n}\n```\n\nyou'll get\n\n```ts\ntype ClassNames = \"button\" | \"button-danger\";\n```\n\nWith it you can create a helper function like\n\n```ts\nfunction cn(...args: ClassNames[]) {\n    return args.join(\" \");\n}\n```\n\nand have your editor autocomplete and validate the class names:\n\n![vscode demo](.demos/autocomplete.gif?raw=true \"VSCode demo\")\n\n## Setup\n\nInstall the plugin\n\n    npm install postcss-ts-classnames\n\nIn your PostCSS config add it close to the end before optimizing plugins such\nas cssnano or purgecss:\n\n```js\nmodule.exports = {\n    plugins: [\n        require(\"postcss-import\"),\n        require(\"tailwindcss\"),\n\n        require(\"postcss-ts-classnames\")({\n            dest: \"src/classnames.d.ts\",\n            // Set isModule if you want to import ClassNames from another file\n            isModule: true,\n            exportAsDefault: true, // to use in combination with isModule\n        }),\n\n        require(\"@fullhuman/postcss-purgecss\")({\n            content: [\"./src/**/*.html\"],\n        }),\n    ],\n};\n```\n\n## ts-classnames\n\nThere's also a `ts-classnames` module which is re-exported version of the\noriginal [classnames][] which uses the generated `ClassNames` type to\nvalidate the class names\n\n[classnames]: https://www.npmjs.com/package/classnames\n\nInstall\n\n    npm install ts-classnames\n\nImport\n\n```ts\nimport { cn } from \"ts-classnames\";\n```\n\n## Vanilla JavaScript\n\nIf you don't use TypeScript you can still leverage this as VSCode can pick up\nTypeScript types from JSDoc comments so you can do\n\n```js\n/**\n * @param {ClassNames[]} args\n */\nfunction cn(...args) {\n    return args.join(\" \");\n}\n```\n\nThis will give the autocomplete but if you want the class names checking you\ncan add [`// @ts-check`][js] to the top of the file.\n\nThe `ts-classnames` will work with Vanilla JS too.\n\n[js]: https://github.com/microsoft/TypeScript/wiki/Type-Checking-JavaScript-Files\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesamattis%2Fpostcss-ts-classnames","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesamattis%2Fpostcss-ts-classnames","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesamattis%2Fpostcss-ts-classnames/lists"}