Skip to content

Commit

Permalink
Added array for false condition class, 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Amareis committed May 11, 2021
1 parent ca0e7ea commit 7748ff1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ import { style, composeStyles } from '@vanilla-extract/css'
import { vcn } from 'vanilla-classnames'

export const item = vcn(style({
//first, some base styles (but it can be omitted)
//first, some base styles (it can be omitted)
background: 'blue',
cursor: 'pointer',
}), {
//and then, dynamic variants
active: style({
background: 'green',
}),
//for composing multiply styles, use vanilla composeStyles
disabled: composeStyles(

//if pass array from two classes, then
disabled: [
//first will be applied on truthy condition
style({
background: 'none',
color: 'gray',
}),
}),
//and second - for falsy or omitted condition
style({
cursor: 'default',
cursor: 'pointer',
}),
),
],
})
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vanilla-classnames",
"version": "0.0.2",
"version": "0.1.0",
"description": "Better classnames utility for vanilla-extract",
"keywords": [
"css",
Expand Down
24 changes: 22 additions & 2 deletions src/vcn.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
type Rec = Record<string, string>
type E = string | [string, string]
type Rec = Record<string, E>
export type Selector<T extends Rec> = (vars?: Partial<Record<keyof T, unknown>>) => string

export function vcn<T extends Rec>(variants: T): Selector<T>
export function vcn<T extends Rec>(baseCn: string, variants: T): Selector<T>
export function vcn<T extends Rec>(...args: [T] | [string, T]): Selector<T> {
const baseCn = args.length === 1 ? '' : args[0]
const variants = args.length === 1 ? args[0] : args[1]

let neededKeys: Partial<Record<keyof T, false>> = {}

for (const key in variants) {
const v: E = variants[key]
if (typeof v !== 'string') {
neededKeys[key] = false
}
}

const f: Selector<T> = (vars = {}) => {
for (const k in neededKeys) {
if (!(k in vars)) {
vars[k] = false
}
}

let res = baseCn
for (const key in vars) {
const v: E = variants[key]
if (vars[key]) {
res += (res ? ' ' : '') + variants[key]
res += (res ? ' ' : '') + (typeof v === 'string' ? v : v[0])
} else if (typeof v !== 'string') {
res += (res ? ' ' : '') + v[1]
}
}
return res
Expand Down
21 changes: 21 additions & 0 deletions test/vcn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ describe('short form', () => {
expect(t({ active: 0, someClass: 1 })).toBe('s')
})
})

describe('with array', () => {
it('works', () => {
const t = vcn({
root: 'r',
active: ['a', 'i']
})
expect(t()).toBe('i')
expect(t({ active: 1 })).toBe('a')
expect(t({ active: 0 })).toBe('i')
expect(t({ active: 0, root: 0 })).toBe('i')
expect(t({ active: 0, root: 1 })).toBe('i r')
expect(t({ active: 1, root: 0 })).toBe('a')
expect(t({ active: 1, root: 1 })).toBe('a r')
expect(t({ root: 0, active: 0 })).toBe('i')
expect(t({ root: 0, active: 1 })).toBe('a')
expect(t({ root: 1, active: 0 })).toBe('r i')
expect(t({ root: 1, active: 1 })).toBe('r a')
expect(t({ root: 1 })).toBe('r i')
})
})

0 comments on commit 7748ff1

Please sign in to comment.