{"id":19174398,"url":"https://github.com/wavevision/class-name","last_synced_at":"2026-06-20T06:30:17.297Z","repository":{"id":44847709,"uuid":"233036768","full_name":"wavevision/class-name","owner":"wavevision","description":"🖍 Create and format BEM class names for React components","archived":false,"fork":false,"pushed_at":"2023-01-07T13:35:40.000Z","size":1008,"stargazers_count":1,"open_issues_count":8,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-24T17:05:41.387Z","etag":null,"topics":["bem","classname","classnames","css","format-bem","react","react-components","typescript","utility"],"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/wavevision.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":"2020-01-10T11:51:49.000Z","updated_at":"2021-07-19T10:52:17.000Z","dependencies_parsed_at":"2023-02-07T07:39:28.680Z","dependency_job_id":null,"html_url":"https://github.com/wavevision/class-name","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wavevision%2Fclass-name","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wavevision%2Fclass-name/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wavevision%2Fclass-name/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wavevision%2Fclass-name/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wavevision","download_url":"https://codeload.github.com/wavevision/class-name/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240254182,"owners_count":19772386,"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":["bem","classname","classnames","css","format-bem","react","react-components","typescript","utility"],"created_at":"2024-11-09T10:17:38.939Z","updated_at":"2026-06-20T06:30:17.215Z","avatar_url":"https://github.com/wavevision.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003ca href=\"https://github.com/wavevision\"\u003e\u003cimg alt=\"Wavevision s.r.o.\" src=\"https://wavevision.com/images/wavevision-logo.png\" width=\"120\" /\u003e\u003c/a\u003e\u003c/p\u003e\n\u003ch1 align=\"center\"\u003eClass Name\u003c/h1\u003e\n\n[![CI](https://github.com/wavevision/class-name/workflows/CI/badge.svg)](https://github.com/wavevision/class-name/actions/workflows/ci.yml)\n[![Coverage Status](https://coveralls.io/repos/github/wavevision/class-name/badge.svg?branch=master)](https://coveralls.io/github/wavevision/class-name?branch=master)\n[![npm](https://img.shields.io/npm/v/@wavevision/class-name)](https://www.npmjs.com/package/@wavevision/class-name)\n\nCreate and format BEM class names for React components. The formatter uses [simplified](https://github.com/inuitcss) BEM syntax.\n\n## Installation\n\nVia [Yarn](https://yarnpkg.com)\n\n```bash\nyarn add @wavevision/class-name\n```\n\nor [npm](https://npmjs.com)\n\n```bash\nnpm install --save @wavevision/class-name\n```\n\n## Usage\n\nSimple React component\n\n```typescript jsx\nimport React, { useState, FunctionComponent } from 'react';\nimport className, { USE_VALUE } from '@wavevision/class-name';\n\ninterface ComponentProps {\n  align: string;\n  booleanProp: boolean;\n  nullableProp: string | null;\n  stringProp: string;\n}\n\ninterface ComponentState {\n  visible: boolean;\n}\n\n// Define base class name with props and state behaving as modifiers\nconst componentClassName = className\u003cComponentProps, ComponentState\u003e(\n  'component-class',\n  () =\u003e ({\n    // if booleanProp value is truthy, 'booleanProp' will be used as modifier\n    booleanProp: true,\n    // if stringProp value is truthy then the value will be used\n    stringProp: USE_VALUE,\n    // use callback for custom modifiers, string returned will be used\n    customModifier: ({ props }) =\u003e (props.nullableProp ? 'custom' : null),\n    // if a non-string truthy value is returned, key will be used\n    anotherModifier: ({ state }) =\u003e state.visible,\n  }),\n);\n\n// We can also have modifiers defined only if some condition is met\nconst anotherClassName = className\u003cComponentProps, ComponentState\u003e(\n  'another-class',\n  ({ props, state }) =\u003e {\n    if (props.nullableProp !== null) {\n      // the whole set of modifiers will be created only if nullableProp is not null\n      return { stringProps: USE_VALUE, customModifier: () =\u003e true };\n    }\n    if (state.visible) {\n      // this set will be created only if state.visible is true\n      return { customModifier: () =\u003e true };\n    }\n  },\n);\n\nconst Component: FunctionComponent\u003cComponentProps\u003e = props =\u003e {\n  const [visible] = useState\u003cComponentState['visible']\u003e(false);\n  const className = componentClassName({ props, state: { visible } });\n  const nextClassName = anotherClassName({ props, state: { visible } });\n  return (\n    \u003cdiv className={className.block('inline-modifier')}\u003e\n      \u003cdiv className={nextClassName.block()} /\u003e\n      \u003cdiv\n        className={className.compose(\n          className.element('child'),\n          // extra class name with optional prefix (e.g. Bootstrap text utility)\n          className.extra(props.align, 'text'),\n        )}\n      /\u003e\n      // modifiers can be nullable and will be used only if not null\n      \u003cdiv className={className.element('element', props.nullableProp)} /\u003e\n      \u003cdiv className={className.element('another', 'element-modifier')} /\u003e\n    \u003c/div\u003e\n  );\n};\n```\n\nwill output following when rendered\n\n```typescript jsx\n\u003cComponent\n  align=\"right\"\n  booleanProp={true}\n  nullableProp={null}\n  stringProp=\"something\"\n/\u003e\n```\n\n```html\n\u003cdiv\n  class=\"component-class component-class--boolean-prop component-class--something component-class--inline-modifier\"\n\u003e\n  \u003cdiv class=\"another-class\"\u003e\u003c/div\u003e\n  \u003cdiv class=\"component-class__child text-right\"\u003e\u003c/div\u003e\n  \u003cdiv class=\"component-class__element\"\u003e\u003c/div\u003e\n  \u003cdiv\n    class=\"component-class__another component-class__another--element-modifier\"\n  \u003e\u003c/div\u003e\n\u003c/div\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwavevision%2Fclass-name","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwavevision%2Fclass-name","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwavevision%2Fclass-name/lists"}