You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.7 KiB

3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. Classnames
  2. ===========
  3. [![Version](http://img.shields.io/npm/v/classnames.svg)](https://www.npmjs.org/package/classnames)
  4. [![Build Status](https://travis-ci.org/JedWatson/classnames.svg?branch=master)](https://travis-ci.org/JedWatson/classnames)
  5. [![Supported by Thinkmill](https://thinkmill.github.io/badge/heart.svg)](http://thinkmill.com.au/?utm_source=github&utm_medium=badge&utm_campaign=classnames)
  6. A simple JavaScript utility for conditionally joining classNames together.
  7. Install with [npm](https://www.npmjs.com/), [Bower](https://bower.io/), or [Yarn](https://yarnpkg.com/):
  8. npm:
  9. ```sh
  10. npm install classnames --save
  11. ```
  12. Bower:
  13. ```sh
  14. bower install classnames --save
  15. ```
  16. Yarn (note that `yarn add` automatically saves the package to the `dependencies` in `package.json`):
  17. ```sh
  18. yarn add classnames
  19. ```
  20. Use with [Node.js](https://nodejs.org/en/), [Browserify](http://browserify.org/), or [webpack](https://webpack.github.io/):
  21. ```js
  22. var classNames = require('classnames');
  23. classNames('foo', 'bar'); // => 'foo bar'
  24. ```
  25. Alternatively, you can simply include `index.js` on your page with a standalone `<script>` tag and it will export a global `classNames` method, or define the module if you are using RequireJS.
  26. ### Project philosophy
  27. We take the stability and performance of this package seriously, because it is run millions of times a day in browsers all around the world. Updates are thoroughly reviewed for performance impacts before being released, and we have a comprehensive test suite.
  28. Classnames follows the [SemVer](http://semver.org/) standard for versioning.
  29. There is also a [Changelog](https://github.com/JedWatson/classnames/blob/master/HISTORY.md).
  30. ## Usage
  31. The `classNames` function takes any number of arguments which can be a string or object.
  32. The argument `'foo'` is short for `{ foo: true }`. If the value associated with a given key is falsy, that key won't be included in the output.
  33. ```js
  34. classNames('foo', 'bar'); // => 'foo bar'
  35. classNames('foo', { bar: true }); // => 'foo bar'
  36. classNames({ 'foo-bar': true }); // => 'foo-bar'
  37. classNames({ 'foo-bar': false }); // => ''
  38. classNames({ foo: true }, { bar: true }); // => 'foo bar'
  39. classNames({ foo: true, bar: true }); // => 'foo bar'
  40. // lots of arguments of various types
  41. classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
  42. // other falsy values are just ignored
  43. classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'
  44. ```
  45. Arrays will be recursively flattened as per the rules above:
  46. ```js
  47. var arr = ['b', { c: true, d: false }];
  48. classNames('a', arr); // => 'a b c'
  49. ```
  50. ### Dynamic class names with ES2015
  51. If you're in an environment that supports [computed keys](http://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer) (available in ES2015 and Babel) you can use dynamic class names:
  52. ```js
  53. let buttonType = 'primary';
  54. classNames({ [`btn-${buttonType}`]: true });
  55. ```
  56. ### Usage with React.js
  57. This package is the official replacement for `classSet`, which was originally shipped in the React.js Addons bundle.
  58. One of its primary use cases is to make dynamic and conditional `className` props simpler to work with (especially more so than conditional string manipulation). So where you may have the following code to generate a `className` prop for a `<button>` in React:
  59. ```js
  60. var Button = React.createClass({
  61. // ...
  62. render () {
  63. var btnClass = 'btn';
  64. if (this.state.isPressed) btnClass += ' btn-pressed';
  65. else if (this.state.isHovered) btnClass += ' btn-over';
  66. return <button className={btnClass}>{this.props.label}</button>;
  67. }
  68. });
  69. ```
  70. You can express the conditional classes more simply as an object:
  71. ```js
  72. var classNames = require('classnames');
  73. var Button = React.createClass({
  74. // ...
  75. render () {
  76. var btnClass = classNames({
  77. btn: true,
  78. 'btn-pressed': this.state.isPressed,
  79. 'btn-over': !this.state.isPressed && this.state.isHovered
  80. });
  81. return <button className={btnClass}>{this.props.label}</button>;
  82. }
  83. });
  84. ```
  85. Because you can mix together object, array and string arguments, supporting optional `className` props is also simpler as only truthy arguments get included in the result:
  86. ```js
  87. var btnClass = classNames('btn', this.props.className, {
  88. 'btn-pressed': this.state.isPressed,
  89. 'btn-over': !this.state.isPressed && this.state.isHovered
  90. });
  91. ```
  92. ### Alternate `dedupe` version
  93. There is an alternate version of `classNames` available which correctly dedupes classes and ensures that falsy classes specified in later arguments are excluded from the result set.
  94. This version is slower (about 5x) so it is offered as an opt-in.
  95. To use the dedupe version with Node.js, Browserify, or webpack:
  96. ```js
  97. var classNames = require('classnames/dedupe');
  98. classNames('foo', 'foo', 'bar'); // => 'foo bar'
  99. classNames('foo', { foo: false, bar: true }); // => 'bar'
  100. ```
  101. For standalone (global / AMD) use, include `dedupe.js` in a `<script>` tag on your page.
  102. ### Alternate `bind` version (for [css-modules](https://github.com/css-modules/css-modules))
  103. If you are using [css-modules](https://github.com/css-modules/css-modules), or a similar approach to abstract class "names" and the real `className` values that are actually output to the DOM, you may want to use the `bind` variant.
  104. _Note that in ES2015 environments, it may be better to use the "dynamic class names" approach documented above._
  105. ```js
  106. var classNames = require('classnames/bind');
  107. var styles = {
  108. foo: 'abc',
  109. bar: 'def',
  110. baz: 'xyz'
  111. };
  112. var cx = classNames.bind(styles);
  113. var className = cx('foo', ['bar'], { baz: true }); // => "abc def xyz"
  114. ```
  115. Real-world example:
  116. ```js
  117. /* components/submit-button.js */
  118. import { Component } from 'react';
  119. import classNames from 'classnames/bind';
  120. import styles from './submit-button.css';
  121. let cx = classNames.bind(styles);
  122. export default class SubmitButton extends Component {
  123. render () {
  124. let text = this.props.store.submissionInProgress ? 'Processing...' : 'Submit';
  125. let className = cx({
  126. base: true,
  127. inProgress: this.props.store.submissionInProgress,
  128. error: this.props.store.errorOccurred,
  129. disabled: this.props.form.valid,
  130. });
  131. return <button className={className}>{text}</button>;
  132. }
  133. };
  134. ```
  135. ## Polyfills needed to support older browsers
  136. #### `classNames >=2.0.0`
  137. `Array.isArray`: see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill.
  138. `Object.keys`: see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) for details about unsupported older browsers (e.g. <= IE8) and a simple polyfill. This is only used in `dedupe.js`.
  139. ## License
  140. [MIT](LICENSE). Copyright (c) 2017 Jed Watson.