Skip to content

Commit

Permalink
Support optional classnames
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosant committed Feb 22, 2020
1 parent 4851fd1 commit ce3bdc5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ Using cn is pretty simple. It is inspired by the well known [Classnames](https:/
import cn from '@mariosant/cn';

const isActive = true;
const SomeComponent = () => <Button className={cn('btn', [isActive, 'active', 'inactive'])} .../>
const SomeComponent = () => <Button className={cn('btn', [isActive, 'active'])} .../> // classnames would be 'btn active'
```

Why the tuple syntax, you may ask. Well, tuple allows to pass an alternative classname, optionally.
Consider the following:

```javascript
import cn from '@mariosant/cn';

const isActive = false;
const SomeComponent = () => <Button className={cn('btn', [isActive, 'active', 'inactive'])} .../> // classnames would be 'btn inactive'
```

## Development
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type Tuple = [boolean, string, string];
type Tuple = [boolean, string, string?];

const handleTuple = ([conditional, yeap, nope]: Tuple) =>
const handleTuple = ([conditional, yeap, nope = '']: Tuple) =>
conditional ? yeap : nope;

const handleString = (c: string) => c;
Expand Down
5 changes: 5 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ test('outputs multiple classes', () => {
expect(className('test', 'work')).toEqual('test work');
});

test('works with simple conditions', () => {
expect(className([true, 'works'])).toEqual('works');
expect(className([false, 'works'])).toEqual('');
});

test('works with conditions', () => {
expect(className([true, 'works', 'does not work'])).toEqual('works');
expect(className([false, 'works', 'does not work'])).toEqual('does not work');
Expand Down

0 comments on commit ce3bdc5

Please sign in to comment.