Checkbox component
The Checkbox
component designed with Vue 3 and Tailwind CSS. possibility to add an Label or customize with a custom Slot.
Basic Usage
Import and use Checkbox
in your Vue components as follows:
vue
<template>
<Checkbox
id="checkBox1"
v-model="checkbox"
label="Checkbox item"
value="Value 1"
name="Custom Checkbox"
/>
</template>
<script setup>
import { Checkbox } from '@robuust-digital/vue-components/core';
</script>
Props
Below are the props supported by Checkbox
, which allow you to customize its appearance and behavior:
Prop | Type | Default | Description |
---|---|---|---|
name | String | Custom checkbox | The name attribute for a checkbox field is used to identify the checkbox within a form. |
id | String | ID | The id attribute for a checkbox is used to uniquely identify the checkbox element. |
label | String | `` | The label prop provides the text for the label. |
value | String | `` | The value prop provides the value of the checkbox. |
Slots
Below is example to edit the custom slot of Checkbox
:
Checkbox with a custom slot
This is a description for the checkbox item.
Show code
vue
<template>
<Checkbox
id="checkBox2"
v-model="checkbox"
type="checkbox"
value="Value 2"
name="Custom Checkbox"
>
<label for="checkBox2"> Checkbox item with a <strong class="text-red-500">custom</strong> slot</label>
<p class="text-slate-600">
This is a description for the checkbox item.
</p>
</Checkbox>
</template>
Examples
Default checkbox
Show code
vue
<template>
<Checkbox
id="checkBox1"
v-model="checkbox"
label="Checkbox item"
value="Value 1"
name="Custom Checkbox"
/>
</template>
Refs
The Checkbox
component's native HTML <input type="checkbox">
element can be accessed using a template ref combined with a ref. This allows you to call native checkbox methods or set properties directly:
vue
<template>
<Checkbox ref="checkboxRef" />
</template>
<script setup>
import { ref, onMounted } from 'vue';
const checkboxRef = ref();
onMounted(() => {
// Access the native checkbox element
const nativeCheckbox = checkboxRef.value.$refs.input.$el;
nativeCheckbox.focus(); // Focus the checkbox
});
</script>
CSS Customization ⚡️
To customize the checkbox
styles global
css
:root {
/* Base Checkbox variables */
--rvc-checkbox-input-box-shadow: var(--rvc-base-box-shadow);
--rvc-checkbox-input-border-color: var(--rvc-base-border-color);
--rvc-checkbox-gap: calc(var(--spacing) * 2);
--rvc-checkbox-font-size: var(--text-base);
--rvc-checkbox-font-weight: var(--font-weight-medium);
--rvc-checkbox-color: var(--color-slate-950);
--rvc-checkbox-input-bg-color: var(--rvc-base-input-bg-color);
--rvc-checkbox-input-bg-color-disabled: var(--rvc-base-input-bg-color-disabled);
--rvc-checkbox-input-disabled-opacity: var(--rvc-base-input-disabled-opacity);
--rvc-checkbox-input-size: calc(var(--spacing) * 4);
--rvc-checkbox-input-border-radius: var(--radius-sm);
--rvc-checkbox-input-border-width: 1px;
--rvc-checkbox-input-checked-bg-color: var(--color-indigo-600);
--rvc-checkbox-input-checked-border-color: var(--color-indigo-600);
--rvc-checkbox-input-checked-icon-color: var(--color-white);
}