All files / util style-helpers.js

100% Statements 6/6
100% Branches 3/3
100% Functions 3/3
100% Lines 6/6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                8095x   11295x       14781x   14529x         122x                         122x  
import _ from 'lodash';
import classNames from 'classnames';
 
/**
 * bindClassNames
 *
 * Returns a version of the `classnames` functions where `&` is bound to a given
 * value. The returned functions can be further bound to more specific values for
 * `&` which allows your bound classnames to look closer to style selector.
 *
 * Examples:
 *   bindClassNames('lucid')('&-Button') === 'lucid-Button'
 *   bindClassNames('lucid').bind('&-Button')('&-active') === 'lucid-Button-active'
 */
export function bindClassNames(value='', variable=/&/g) {
	function cx(...args) {
		return _.map(
			classNames(...args).split(' '),
			(className) => className.replace(variable, value)
		).join(' ');
	}
 
	return _.assign(cx, {
		bind(nextValue=value, ...args) {
			return bindClassNames(nextValue.replace(variable, value), ...args);
		},
	});
}
 
export const NAMESPACE = 'lucid';
 
/**
 * Exports a lucid-bound version of classnames, which can be make more specific
 * to a component.
 *
 * Example:
 *   const cx = lucidClassNames.bind('&-Button')
 *
 *   cx('&',{
 *     '&-active': true
 *   }, ['custom-classname']) === 'lucid-Button lucid-Button-active custom-classname'
 */
export const lucidClassNames = bindClassNames(NAMESPACE);