cn function
Plain Dart class name concatenation helper for Bloom UI primitives.
Combines class names, filtering out null, false, empty, or whitespace-only
entries, and joins them with a single space. For known exclusive utility
groups, the last class with the same variant prefix wins and stays in its
original position relative to the remaining classes. This applies to
display, position, visibility, flex direction, text alignment, font weight,
font size, and matching value utilities in the padding, margin, width,
height, gap, and rounded families. For example, cn(['hidden', 'flex'])
returns 'flex', while cn(['p-2', 'px-4']) keeps both classes. Other
classes are never removed.
cn(['hidden', isOpen ? 'inline-flex' : null, 'p-2', 'p-4']);
// 'inline-flex p-4'
Implementation
String cn(List<Object?> classes) {
final results = <String?>[];
final lastIndexByGroup = <String, int>{};
for (final item in classes) {
if (item == null || item == false) continue;
final str = item.toString().trim();
if (str.isEmpty) continue;
for (final name in str.split(RegExp(r'\s+'))) {
if (name.isEmpty) continue;
final group = _exclusiveGroup(name);
if (group != null) {
final previousIndex = lastIndexByGroup[group];
if (previousIndex != null) results[previousIndex] = null;
lastIndexByGroup[group] = results.length;
}
results.add(name);
}
}
return results.whereType<String>().join(' ');
}