Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

rescript-classnames

MinimaHQ3.2kMIT7.0.1

Reimplementation of classnames in ReScript

react, rescript, rescript-react, reason, reason-react, reasonml, ocaml, bucklescript, classnames

readme

rescript-classnames

version build license

Reimplementation of classnames in ReScript.

ShakaCode

If you are looking for help with the development and optimization of your project, ShakaCode can help you to take the reliability and performance of your app to the next level.

If you are a developer interested in working on ReScript / TypeScript / Rust / Ruby on Rails projects, we're hiring!

Installation

# yarn
yarn add rescript-classnames

# or npm
npm install --save rescript-classnames

Then add it to rescript.json:

"bs-dependencies": [
  "rescript-classnames"
]

API

You can use either Cn.make function:

Cn.make(["one", "two", "three"]) // => "one two three"

Or open Cx module and use cx alias:

open Cx

cx(["one", "two", "three"]) // => "one two three"

You can open Cx module globally via bsconfig.json and cx function will be available everywhere without a need to open Cx.

"bsc-flags": ["-open Cx"]

To conditionally render a classname, use an empty string to indicate an absence of it.

cx(["button", disabled ? "disabled" : ""])

Or use pattern matching to select the right classname for an input:

cx([
  "button",
  disabled ? "disabled" : "",
  switch color {
  | Green => "green"
  | Red => "red"
  },
])

Performance

First of all, if you are really concerned with performance, consider using string interpolation as it's the fastest possible way to render classnames.

`button ${disabled ? "disabled" : ""}`

Otherwise, rescript-classnames is reasonably fast.

js interpolation x 775,890,362 ops/sec ±1.46% (87 runs sampled)
rescript-classnames x 2,493,334 ops/sec ±0.64% (89 runs sampled)
classnames.js x 794,502 ops/sec ±0.62% (91 runs sampled)

P.S. To run benchmarks, change package-specs.module to commonjs in rescript.json.

License

See LICENSE.

Supporters

JetBrains <picture> <source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/4244251/184881147-0d077438-3978-40da-ace9-4f650d2efe2e.png"> <source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/4244251/184881152-9f2d8fba-88ac-4ba6-873b-22387f8711c5.png"> ScoutAPM </picture>
<picture> <source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/4244251/184881122-407dcc29-df78-4b20-a9ad-f597b56f6cdb.png"> <source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/4244251/184881129-e1edf4b7-3ae1-4ea8-9e6d-3595cf01609e.png"> BrowserStack </picture> Rails Autoscale Honeybadger

The following companies support our open source projects, and ShakaCode uses their products!

changelog

History

7.0.1

  • rescript is updated to v11.1.0.
  • rescript/core is updated to v1.3.0.

7.0.0

  • rescript is updated to v11.
  • [ BREAKING ] Old Reason API is removed.

6.0.0

  • Library transitioned to the ReScript syntax with the new name: rescript-classnames.
  • [ BREAKING ] We reduced the API surface by a lot. See re-classnames#api.
  • [ DEPRECATED ] Old api is deprecated and available under CnRe namespace.

5.0.1

  • Even faster :) Thanks, @Et7f3!

5.0.0

  • Perf! We're ~5.5 times faster than v4 and ~4 times faster than classnames.js counterpart.
  • [ NEW ] (+) infix operator
  • [ NEW ] append function
  • [ NEW ] fromList function
  • [ NEW ] none alias
  • [ NEW ] onOk combinator
  • [ NEW ] mapOk combinator
  • [ NEW ] onErr combinator
  • [ NEW ] mapErr combinator
  • [ DEPRECATED ] make. Use either (+) (recommended) or fromList (slower).
  • [ DEPRECATED ] ifTrue. Use on.
  • [ DEPRECATED ] ifSome. Use onSome.
  • [ DEPRECATED ] unpack. Use take.

4.1.0

  • bs-platform updated to v7.

4.0.0

  • [ BREAKING ] bs-platform updated to 5.0.0.

3.0.0

  • [ BREAKING ] API style is changed from data-last to data-first (use -> instead of |>)
  • [ BREAKING ] Cn.unwrap renamed to Cn.unpack (in Rust unwrap throws on None, while Cn just ignores it)
  • [ BREAKING ] ClassNames module is removed, use Cn

2.1.0

  • bs-platform updated to 4.0.2.

2.0.0

  • [ BREAKING ] Cn.ifBool renamed to Cn.ifTrue
  • [ BREAKING ] Cn.ifOpt renamed to Cn.unwrap
  • [ BREAKING ] Cn.mapOpt renamed to Cn.mapSome
  • Cn.ifSome added.

1.1.0

  • bs-platform updated to 3.0.0.

1.0.0

  • [ BREAKING ] bs-platform updated to 2.2.2.

0.0.1

Initial release.