{"id":22025704,"url":"https://github.com/dwango-js/suitcss-classnames","last_synced_at":"2025-05-07T09:36:11.487Z","repository":{"id":57374250,"uuid":"54695480","full_name":"dwango-js/suitcss-classnames","owner":"dwango-js","description":"Class names builder library compatible for Suit CSS naming conventions.","archived":false,"fork":false,"pushed_at":"2021-05-10T15:40:49.000Z","size":44,"stargazers_count":5,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-18T18:34:22.303Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/dwango-js.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}},"created_at":"2016-03-25T04:56:18.000Z","updated_at":"2020-04-06T03:36:26.000Z","dependencies_parsed_at":"2022-09-17T15:51:02.619Z","dependency_job_id":null,"html_url":"https://github.com/dwango-js/suitcss-classnames","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwango-js%2Fsuitcss-classnames","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwango-js%2Fsuitcss-classnames/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwango-js%2Fsuitcss-classnames/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dwango-js%2Fsuitcss-classnames/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dwango-js","download_url":"https://codeload.github.com/dwango-js/suitcss-classnames/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252851930,"owners_count":21814249,"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":[],"created_at":"2024-11-30T07:19:06.171Z","updated_at":"2025-05-07T09:36:11.459Z","avatar_url":"https://github.com/dwango-js.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# suitcss-classnames [![Build Status](https://travis-ci.org/dwango-js/suitcss-classnames.svg?branch=master)](https://travis-ci.org/dwango-js/suitcss-classnames)\n\nClass names builder library compatible for Suit CSS naming conventions.\n\n- [SUIT CSS: style tools for UI components](http://suitcss.github.io/ \"SUIT CSS: style tools for UI components\")\n- [SUIT CSS naming conventions](https://github.com/suitcss/suit/blob/master/doc/naming-conventions.md \"SUIT CSS naming conventions\")\n\n## Installation\n\n    npm install suitcss-classnames\n\n## Usage\n\n### `suitClassNames(suitCSSObject): string`\n\n`suitCSSObject` is a following object:\n\n```js\nexport interface SuitCSSObject {\n    /**\n     * class=\"namespace-Component\"\n     */\n    namespace?: string;\n    /**\n     * class=\"namespace-Component\"\n     */\n    descendant?: string;\n    /**\n     * class=\"Component\"\n     */\n    component: string;\n    /**\n     * class=\"Component--modifier\"\n     */\n    modifiers?: string[] | {\n        [index: string]: boolean;\n    };\n    /**\n     * class=\"Component is-state\"\n     */\n    states?: string[] | {\n        [index: string]: boolean;\n    };\n    /**\n     * class=\"u-utility Component\"\n     */\n    utilities?: string[] | {\n        [index: string]: boolean;\n    };\n}\n```\n\n`component` property is required, other properties is optional.\n\n```js\nimport { suitClassNames } from \"suitcss-classnames\";\nconst className = suitClassNames({\n    namespace: \"ns\",\n    component: \"ComponentName\", // \u003c= require\n    descendant: \"descendantName\",\n    modifiers: [\"default\", \"small\"],\n    states: [\"disable\", \"active\"]\n});\nconst classes = className.split(\" \");\nassert(classes.indexOf(\"ns-ComponentName-descendantName\") !== -1);\nassert(classes.indexOf(\"ns-ComponentName-descendantName--default\") !== -1);\nassert(classes.indexOf(\"ns-ComponentName-descendantName--small\") !== -1);\nassert(classes.indexOf(\"is-disable\") !== -1);\nassert(classes.indexOf(\"is-active\") !== -1);\n```\n\n`state`, `modifiers` and `utilities` property accept array or object.\n\nIt means that you can write following:\n\n```js\nconst className = suitClassNames({\ncomponent: \"ComponentName\",\n    states: {\n        // ignore\n        \"disable\": false,\n        // accept\n        \"active\": true\n    }\n});\nconst classes = className.split(/\\s+/);\nconsole.log(classes);\n// [ 'ComponentName', 'is-active' ]\n```\n\n### Use case\n\nIf you use it in react component, write following pattern:\n\n```js\nimport { suitClassNames } from \"suitcss-classnames\";\nclass MyComponent extends React.Component {\n    render() {\n        const className = suitClassNames({\n            component: \"MyComponent\",\n            states: {\n                // on/off by active state\n                \"active\": this.props.active\n            }\n        });\n        return \u003cdiv className={className}\u003e\n           {this.props.active ? \"active!\" : \"none\"}\n        \u003c/div\u003e;\n    }\n}\n```\n\n## Behavior Note :memo:\n\n### When `key` already has `prefix`\n\n`suitcss-classnames` don't throw error, just ignore this.\n\n```js\nimport { suitClassNames } from \"suitcss-classnames\";\nconst className = suitClassNames({\n    component: \"ComponentName\",\n    // key alredy has \"is-\" prefix\n    states: {\n        \"is-active\": true\n    }\n});\nconst classes = className.split(/\\s+/);\nassert.equal(classes.length, 2);\nassert(classes.indexOf(\"ComponentName\") !== -1);\n// don't add duplicated prefix, it will be \"is-active\"\nassert(classes.indexOf(\"is-active\") !== -1);\n```\n\n### suitcss-classnames don't allowed the key name:\"state\"\n\n`suitcss-classnames` don't allowed the key name that except \"namespace\", \"descendant\", \"component\", \"modifiers\", \"states\", \"utilities\".\n\n```js\nimport { suitClassNames } from \"suitcss-classnames\";\nsuitClassNames({\n    component: \"ComponentName\",\n    // typo =\u003e states\n    state: {\n        \"is-active\": true\n    }\n});\n```\n\nThe code throw error, because it contain a typo(`state` =\u003e `states`).\n\n## Tests\n\n    npm test\n\n## Contributing\n\n1. Fork it!\n2. Create your feature branch: `git checkout -b my-new-feature`\n3. Commit your changes: `git commit -am 'Add some feature'`\n4. Push to the branch: `git push origin my-new-feature`\n5. Submit a pull request :D\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdwango-js%2Fsuitcss-classnames","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdwango-js%2Fsuitcss-classnames","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdwango-js%2Fsuitcss-classnames/lists"}