Skip to content

Commit

Permalink
feat(web-core) update doc for toVariants utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ByScripts committed Sep 19, 2024
1 parent 903cd33 commit 39977c5
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions @xen-orchestra/web-core/docs/utils/to-variants.util.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ This utility is used to convert a set of props into a list of CSS variants class
- No class will be added for other _falsy_ values
- `<key>--<value>` class will be added for other values

For example :
## Basic usage

```ts
defineProps<{
const props = defineProps<{
label: string
color: 'blue' | 'red'
size: 'small' | 'large'
Expand All @@ -29,25 +29,32 @@ const variants = computed(() =>
If `color` is `'blue'`, `size` is `'small'`, and `disabled` is `false`,
then `variants` will be `['color--blue', 'size--small', 'disabled--0']`.

> [!TIP]
> You can obviously define your own variants, based or not on props.
## Advanced usage

Variants don't have to be based on props, you can define them the way you want.

Thanks to the way Vue works, they can also be mixed with other classes.

```ts
defineProps<{
const props = defineProps<{
label: string
color: 'blue' | 'red'
size: 'small' | 'large'
}>()

const isDisabled = inject(IK_DISABLED, ref(false))
const isDisabled = inject('isParentDisabled', ref(false))

const variants = computed(() =>
const classes = computed(() => [
'vts-my-component',
'typo',
props.size === 'small' ? 'p3-regular' : 'p2-medium',
{ disabled: isDisabled.value },
toVariants({
color: props.color,
size: props.size,
size: props.size.slice(0, 1),
state: isDisabled.value ? 'off' : 'on',
})
)
}),
])
```

Then if `isDisabled` is `true`, then `variants` will be `['color--blue', 'size--small', 'state--off']`,
else it will be `['color--blue', 'size--small', 'state--on']`.
`classes` applied to the component will then looks like `vts-my-component typo p3-regular disabled color-blue size-s state-off`

0 comments on commit 39977c5

Please sign in to comment.