{"id":16019508,"url":"https://github.com/fjc0k/vue-css-modules","last_synced_at":"2025-03-17T16:30:41.180Z","repository":{"id":57395452,"uuid":"127697124","full_name":"fjc0k/vue-css-modules","owner":"fjc0k","description":"Seamless mapping of class names to CSS Modules inside of Vue components.","archived":false,"fork":false,"pushed_at":"2018-06-29T02:19:56.000Z","size":249,"stargazers_count":37,"open_issues_count":3,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-08T22:32:16.120Z","etag":null,"topics":["css-modules","vue","vuejs"],"latest_commit_sha":null,"homepage":"https://npm.im/vue-css-modules","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/fjc0k.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-04-02T03:18:42.000Z","updated_at":"2024-06-25T21:22:41.000Z","dependencies_parsed_at":"2022-08-27T12:40:59.545Z","dependency_job_id":null,"html_url":"https://github.com/fjc0k/vue-css-modules","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fjc0k%2Fvue-css-modules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fjc0k%2Fvue-css-modules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fjc0k%2Fvue-css-modules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fjc0k%2Fvue-css-modules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fjc0k","download_url":"https://codeload.github.com/fjc0k/vue-css-modules/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243871341,"owners_count":20361334,"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":["css-modules","vue","vuejs"],"created_at":"2024-10-08T17:04:37.838Z","updated_at":"2025-03-17T16:30:40.910Z","avatar_url":"https://github.com/fjc0k.png","language":"JavaScript","funding_links":[],"categories":["公用事业","Components \u0026 Libraries","Utilities","Utilities [🔝](#readme)"],"sub_categories":["CSS","Utilities"],"readme":"English | [🇨🇳中文](./README_zh-CN.md)\n\n# Vue CSS Modules\n\n[![Travis](https://img.shields.io/travis/fjc0k/vue-css-modules.svg)](https://travis-ci.org/fjc0k/vue-css-modules)\n[![minified size](https://img.shields.io/badge/minified%20size-2.14%20KB-blue.svg?MIN)](https://github.com/fjc0k/vue-css-modules/blob/master/dist/vue-css-modules.min.js)\n[![minzipped size](https://img.shields.io/badge/minzipped%20size-1.05%20KB-blue.svg?MZIP)](https://github.com/fjc0k/vue-css-modules/blob/master/dist/vue-css-modules.min.js)\n\nSeamless mapping of class names to CSS modules inside of Vue components.\n\n```shell\nyarn add vue-css-modules\n```\n\nCDN: [jsDelivr](//www.jsdelivr.com/package/npm/vue-css-modules) | [UNPKG](//unpkg.com/vue-css-modules/) (Avaliable as `window.VueCSSModules`)\n\n\u003cimg src=\"./assets/logo.png\" width=\"150\" height=\"150\" /\u003e\n\n## CSS Modules: local scope \u0026 modular\n\n[`CSS Modules`](https://github.com/css-modules/css-modules) assigns a local class a global unique name, so a component styles will not affect other components. e.g.\n\n```css\n/* button.css */\n.button {\n  font-size: 16px;\n}\n.mini {\n  font-size: 12px;\n}\n```\n\nIt's will transformed to something similar to:\n\n```css\n/* button.css */\n.button__button--d8fj3 {\n  font-size: 16px;\n}\n.button__mini--f90jc {\n  font-size: 12px;\n}\n```\n\nWhen importing the CSS Module from a JS Module, it exports an object with all mappings from local names to global names. Just like this:\n\n```javascript\nimport styles from './button.css'\n// styles = {\n//   button: 'button__button--d8fj3',\n//   mini: 'button__mini--f90jc'\n// }\n\nelement.innerHTML = '\u003cbutton class=\"' + styles.button + ' ' + styles.mini + '\" /\u003e'\n```\n\n## `vue-css-modules`: simplify mapping name\n\nHere's a button component with CSS Modules:\n\n```html\n\u003ctemplate\u003e\n  \u003cbutton :class=\"{\n    'global-button-class-name': true,\n    [styles.button]: true,\n    [styles.mini]: mini\n  }\"\u003eClick me\u003c/button\u003e\n\u003c/template\u003e\n\n\u003cscript\u003e\n  import styles from './button.css'\n\n  export default {\n    props: { mini: Boolean },\n    data: () =\u003e ({ styles })\n  }\n\u003c/script\u003e\n```\n\nSurely, CSS Modules is a good choice for Vue components. But here are a few disadvantages:\n\n- You have to pass `styles` object into `data` function.\n- You have to use `styles.localClassName` importing a global class name.\n- If there are other global class names, you have to put them together.\n- If you want to bind a class name to a component property value, you have to explicitly specify the property name, even if the class name is equals the property name.\n\nNow, you can use `vue-css-modules` to remake it:\n\n```html\n\u003ctemplate\u003e\n  \u003cbutton\n    class=\"global-button-class-name\"\n    styleName=\"button :mini\"\u003e\n    Click me\n  \u003c/button\u003e\n\u003c/template\u003e\n\n\u003cscript\u003e\n  import CSSModules from 'vue-css-modules'\n  import styles from './button.css'\n\n  export default {\n    mixins: [CSSModules(styles)],\n    props: { mini: Boolean }\n  }\n\u003c/script\u003e\n```\n\nUsing `vue-css-modules`:\n\n- You don't need pass `styles` object into `data` function, but the `CSSModules` mixin. 🌝\n- You can completely say byebye to `styles.localClassName`.\n- There is clear distinction between global CSS and CSS Modules.\n- You can use the `:` modifier to bind the property with the same name.\n\n## Modifiers\n\n### @button\n\n```html\n\u003cbutton styleName=\"@button\"\u003eButton\u003c/button\u003e\n```\n\nThis is the equivalent to:\n\n```html\n\u003cbutton styleName=\"button\" data-component-button=\"true\"\u003eButton\u003c/button\u003e\n```\n\nThis allows you to override component styles in context:\n\n```css\n.form [data-component-button] {\n  font-size: 20px;\n}\n```\n\n### $type\n\n```html\n\u003cbutton styleName=\"$type\"\u003eButton\u003c/button\u003e\n```\n\nThis is the equivalent to:\n\n```html\n\u003cbutton :styleName=\"type\"\u003eButton\u003c/button\u003e\n```\n\n### :mini\n\n```html\n\u003cbutton styleName=\":mini\"\u003eButton\u003c/button\u003e\n```\n\nThis is the equivalent to:\n\n```html\n\u003cbutton :styleName=\"mini ? 'mini' : ''\"\u003eButton\u003c/button\u003e\n```\n\n### disabled=isDisabled\n\n```html\n\u003cbutton styleName=\"disabled=isDisabled\"\u003eButton\u003c/button\u003e\n```\n\nThis is the equivalent to:\n\n```html\n\u003cbutton :styleName=\"isDisabled ? 'disabled' : ''\"\u003eButton\u003c/button\u003e\n```\n\n## Usage\n\n### In templates\n\n#### CSS Modules outside the template\n\n```html\n\u003ctemplate\u003e\n  \u003cbutton\n    class=\"global-button-class-name\"\n    styleName=\"button :mini\"\u003e\n    Click me\n  \u003c/button\u003e\n\u003c/template\u003e\n\n\u003cscript\u003e\n  import CSSModules from 'vue-css-modules'\n  import styles from './button.css'\n\n  export default {\n    mixins: [CSSModules(styles)],\n    props: { mini: Boolean }\n  }\n\u003c/script\u003e\n```\n\n#### CSS Modules inside the template\n\n```html\n\u003ctemplate\u003e\n  \u003cbutton\n    class=\"global-button-class-name\"\n    styleName=\"button :mini\"\u003e\n    Click me\n  \u003c/button\u003e\n\u003c/template\u003e\n\n\u003cscript\u003e\n  import CSSModules from 'vue-css-modules'\n\n  export default {\n    mixins: [CSSModules()],\n    props: { mini: Boolean }\n  }\n\u003c/script\u003e\n\n\u003cstyle module\u003e\n  .button {\n    font-size: 16px;\n  }\n  .mini {\n    font-size: 12px;\n  }\n\u003c/style\u003e\n```\n\n### In JSX\n\n```javascript\nimport CSSModules from 'vue-css-modules'\nimport styles from './button.css'\n\nexport default {\n  mixins: [CSSModules(styles)],\n  props: { mini: Boolean },\n  render() {\n    return (\n      \u003cbutton styleName=\"@button :mini\"\u003eClick me\u003c/button\u003e\n    )\n  }\n}\n```\n\n### In render functions\n\n```javascript\nimport CSSModules from 'vue-css-modules'\nimport styles from './button.css'\n\nexport default {\n  mixins: [CSSModules(styles)],\n  props: { mini: Boolean },\n  render(h) {\n    return h('button', {\n      styleName: '@button :mini'\n    }, 'Click me')\n  }\n}\n```\n\n## The implementation\n\n`vue-css-modules` extends `$createElement` method of the current component. It will use the value of `styleName` in `data` or `data.attrs` to look for CSS Modules in the associated styles object and will append the matching unique CSS class names to the `data.staticClass` value.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffjc0k%2Fvue-css-modules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffjc0k%2Fvue-css-modules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffjc0k%2Fvue-css-modules/lists"}