clsx for dart

Build Pub License GitHub stars style: very good analysis

An unofficial Dart port of the popular JavaScript utility clsx for constructing className strings conditionally.

Features

This Dart package replicates the core functionality of the JavaScript clsx library, allowing you to:

  • Conditionally join strings as CSS class names.
  • Support nested lists and maps for dynamic class application.
  • Ignore null or empty values.

Usage

Here's a simple example of how to use clsx in Dart:

import 'package:clsx/clsx.dart';

void main() {
  String activeClass = 'active';
  bool isDisabled = false;

  final buttonClasses = clsx([
    'btn',
    activeClass,
    {'btn-disabled': isDisabled},
    ['extra-class', null],
    {'another-class': true},
  ]);

  print(buttonClasses); // Output: 'btn active extra-class another-class'
}

Additional information

This package is an unofficial port of the JavaScript clsx library developed by Luke Edwards. The goal is to provide a similar, convenient utility for Dart developers.

For more information about the original JavaScript library, please refer to its repository. Contributions, bug reports, and feature requests for this Dart port are welcome. Please file issues on the project's repository.

Libraries

clsx
An unofficial Dart port of the popular JavaScript utility clsx for constructing className strings conditionally.