Skip to content

Commit 21ec8f9

Browse files
Merge pull request #2 from lukasborawski/issue-#2-variants-as-array
[#1] added the possibility to define variants as an array
2 parents 8983f84 + d734a57 commit 21ec8f9

File tree

12 files changed

+221
-55
lines changed

12 files changed

+221
-55
lines changed

CHANGELOG.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
### 0.1.2
2+
3+
- handling variants as an array of strings [#1]
4+
- add demo more examples
5+
- output terser
6+
- docs update
7+
18
### 0.1.1
2-
* removed unnecessary dependencies
3-
* add the demo missing types
4-
* docs update
9+
10+
- removed unnecessary dependencies
11+
- add the demo missing types
12+
- docs update
513

614
### 0.1.0
7-
* initial package state
15+
16+
- initial package state

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ export default defineComponent({
141141
</script>
142142
```
143143

144+
Use as an Array.
145+
146+
```vue
147+
<script lang="ts">
148+
import { buttonVariantsRef } from './variants.ts'
149+
150+
export default defineComponent({
151+
name: 'Button',
152+
setup() {
153+
const { defineVariant } = useVariant() as UseVariant
154+
155+
return {
156+
buttonVariant: defineVariant(['button', 'buttonPrimary'], buttonVariantsRef),
157+
}
158+
},
159+
})
160+
</script>
161+
```
162+
144163
Use straight without variant definitions.
145164

146165
```vue
@@ -190,7 +209,7 @@ Want to check or test it in action? Check out the simple app in the `demo` folde
190209

191210
---
192211

193-
**API Reference**: Check out the [types](src/types.d.ts) for API definitions.
212+
**API Reference**: Check out the [types](src/types.ts) for API definitions.
194213

195214
**Contribution**: Please add Pull Request to introduce some changes or fixes.
196215

__tests__/index.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useVariant } from '~/lib/index.es'
33
describe('vue-use-variant', () => {
44
const variantsDefinitions = {
55
button: 'button-class',
6-
buttonPrimary: 'button-class-primary'
6+
buttonPrimary: 'button-class-primary',
77
}
88

99
it('Renders class string with regular configuration', () => {
@@ -30,10 +30,10 @@ describe('vue-use-variant', () => {
3030
{
3131
value: {
3232
button: true,
33-
buttonPrimary: true
34-
}
33+
buttonPrimary: true,
34+
},
3535
},
36-
variantsDefinitions
36+
variantsDefinitions,
3737
)
3838
// @ts-ignore
3939
expect(variants).toBe('button-class button-class-primary')
@@ -48,6 +48,12 @@ describe('vue-use-variant', () => {
4848
// @ts-ignore
4949
expect(variants).toBe('button-class button-class-primary')
5050
})
51+
it('Renders class string with an array', () => {
52+
const { defineVariant } = useVariant()
53+
const variants = defineVariant(['button', 'buttonPrimary'], variantsDefinitions)
54+
// @ts-ignore
55+
expect(variants).toBe('button-class button-class-primary')
56+
})
5157
it('Fails when using wrong variants data', () => {
5258
// @ts-ignore
5359
global.console = { warn: jest.fn() }

demo/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
"scripts": {
55
"dev": "vite",
66
"build": "vue-tsc --noEmit && vite build",
7-
"serve": "vite preview"
7+
"serve": "vite preview",
8+
"clean": "rm -rf node_modules"
89
},
910
"dependencies": {
1011
"vue": "^3.2.13",
11-
"vue-use-variant": "0.1.0"
12+
"vue-use-variant": "../"
1213
},
1314
"devDependencies": {
1415
"@vitejs/plugin-vue": "^1.9.0",

demo/src/components/Button.vue

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,40 @@ interface Props {
99
buttonPrimary?: boolean
1010
}
1111
12-
const definitions = ref({
13-
button: 'button',
14-
buttonPrimary: 'px-4 py-2 m-2 rounded text-white bg-blue-600 font-helvetica font-bold hover:opacity-80 transition-all'
15-
})
1612
const props = withDefaults(defineProps<Props>(), {
1713
button: true,
18-
buttonPrimary: true
14+
buttonPrimary: true,
1915
})
2016
21-
const variantRegular = defineVariant({
22-
button: true,
23-
buttonPrimary: true
24-
}, {
17+
const definitions = {
2518
button: 'button',
2619
buttonPrimary:
27-
definitions.value.buttonPrimary
28-
})
29-
const variantRef = defineVariant({ button: true, buttonPrimary: true }, definitions)
30-
const variantProps = defineVariant({ ...props }, definitions)
20+
'px-4 py-2 m-2 rounded text-white bg-blue-600 font-helvetica font-bold hover:opacity-80 transition-all',
21+
}
22+
const definitionsRef = ref(definitions)
23+
24+
const variantRegularDefinitionsRaw = defineVariant(
25+
{
26+
button: true,
27+
buttonPrimary: true,
28+
},
29+
{
30+
button: 'button',
31+
buttonPrimary: definitionsRef.value.buttonPrimary,
32+
},
33+
)
34+
const variantRefDefinitionsRef = defineVariant(ref({ button: true, buttonPrimary: true }), definitionsRef)
35+
const variantPropsDefinitionsRef = defineVariant(props as any, definitionsRef)
36+
const variantArrayDefinitionsRef = defineVariant(['button', 'buttonPrimary'], definitionsRef)
37+
const variantArrayDefinitionsRaw = defineVariant(['button', 'buttonPrimary'], definitions)
38+
const variantRefArrayDefinitionsRef = defineVariant(ref(['button', 'buttonPrimary']), definitionsRef)
3139
</script>
3240

3341
<template>
34-
<button :class="variantRegular" type="button">Test Button</button>
35-
<button :class="variantRef" type="button">Test Button</button>
36-
<button :class="variantProps" type="button">Test Button</button>
42+
<button :class="variantRegularDefinitionsRaw" type="button">Test Button</button>
43+
<button :class="variantRefDefinitionsRef" type="button">Test Button</button>
44+
<button :class="variantPropsDefinitionsRef" type="button">Test Button</button>
45+
<button :class="variantArrayDefinitionsRef" type="button">Test Button</button>
46+
<button :class="variantArrayDefinitionsRaw" type="button">Test Button</button>
47+
<button :class="variantRefArrayDefinitionsRef" type="button">Test Button</button>
3748
</template>

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-use-variant",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Vue.js CSS class variant resolver. Presented as handy composable.",
55
"main": "lib/index.cjs.js",
66
"module": "lib/index.es.js",
@@ -12,13 +12,18 @@
1212
"test": "jest",
1313
"prepublish": "yarn build"
1414
},
15+
"files": [
16+
"lib"
17+
],
1518
"repository": {
1619
"type": "git",
1720
"url": "git+https://github.com/lukasborawski/vue-use-variant.git"
1821
},
1922
"keywords": [
2023
"vue",
2124
"vue3",
25+
"nuxt",
26+
"nuxt3",
2227
"composable",
2328
"tailwind",
2429
"javascript",
@@ -49,6 +54,7 @@
4954
"rollup": "^2.71.1",
5055
"rollup-plugin-dts": "^4.0.0",
5156
"rollup-plugin-typescript2": "^0.30.0",
57+
"rollup-plugin-terser": "^7.0.2",
5258
"ts-jest": "^27.0.5",
5359
"ts-node": "^10.2.1"
5460
}

rollup.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pkg from './package.json'
22
import typescript from 'rollup-plugin-typescript2'
33
import dts from 'rollup-plugin-dts'
4+
import { terser } from 'rollup-plugin-terser'
45

56
export default [
67
{
@@ -19,14 +20,15 @@ export default [
1920
],
2021
external: [...Object.keys(pkg.dependencies || {})],
2122
plugins: [
23+
terser(),
2224
typescript({
2325
// eslint-disable-next-line global-require
2426
typescript: require('typescript'),
2527
}),
2628
],
2729
},
2830
{
29-
input: 'src/types.d.ts',
31+
input: 'src/types.ts',
3032
output: [{ file: 'lib/index.d.ts', format: 'es' }],
3133
plugins: [dts()],
3234
},

src/demo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ import { UseVariant } from '~/lib'
44
const { defineVariant } = useVariant() as UseVariant
55

66
console.log(defineVariant({ value: { shadow: true } }, { shadow: 'shadow' }))
7+
console.log(defineVariant({ value: { shadow: true } }, { value: { shadow: 'shadow' } }))
8+
console.log(defineVariant({ shadow: true }, { shadow: 'shadow' }))
9+
console.log(defineVariant(['shadow'], { shadow: 'shadow' }))
10+
console.log(defineVariant({ value: ['shadow'] }, { shadow: 'shadow' }))

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { DefineVariant, ObjectRecord, UseVariantReturn } from '~/src/types'
1+
import { DefineVariant, ObjectRecord, UseVariantReturn } from './types'
22

33
export const useVariant: UseVariantReturn = (definitions) => {
44
const defineVariant: typeof DefineVariant = ($variants, $definitions) => {
5-
const _variants = $variants.value || $variants
5+
let _variants = ($variants as { value: ObjectRecord | string[] }).value || $variants
66
const _definitions = definitions?.value || definitions || $definitions?.value || $definitions || {}
77

88
try {
9+
if (Array.isArray(_variants)) _variants = Object.fromEntries(_variants.map((key) => [key, true]))
910
return Object.keys(_variants)
1011
.map((key) => {
1112
if (typeof (_variants as ObjectRecord)[key] === 'object') throw Error()

src/types.d.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)