nb-toggle

This is a toggle component for Vue.js 3+.

Loading component...

Installation

Yarn
yarn add @vlalg-nimbus/nb-selects
NPM
npm install @vlalg-nimbus/nb-selects

Usage

Vue 3
import { createApp } from 'vue'
import App from './App.vue'

import NbSelectComponents from '@vlalg-nimbus/nb-selects'
import "@vlalg-nimbus/nb-selects/dist/style.css";

const app = createApp(App)
app.use(NbSelectComponents)
app.mount('#app')
Nuxt 3
import NbSelectComponents from '@vlalg-nimbus/nb-selects'
import "@vlalg-nimbus/nb-selects/dist/style.css";

export default defineNuxtPlugin(context => {
  context.vueApp.use(NbSelectComponents)
})

To use, simply call the component, in this case it will be NbToggle or nb-toggle.

Mode 1
<template>
  <NbToggle />
</template>
Mode 2
<template>
  <nb-toggle />
</template>
Mode 3
<template>
  <nb-toggle></nb-toggle>
</template>

Preview & Playground

Select the component you want to edit/test

Loading Sandbox...

Props

Items with an (*) mean they are required

nameValue typeDefaultDescription
nbId (*)StringSets the id attribute to differentiate from other components
valueBooleanfalseDefines the initial state of the toggle (checked/unchecked)
displayString'ib'Defines the display type. Accepts ib (inline-block) and b (block)
disabledBooleanfalseDefines if the toggle is disabled
tabIndexNumber0Defines the tab index. Set this property to make the toggle focusable by the keyboard.
hasTabIndexEnterBooleantrueEnables the toggle to be activated with the Enter key when focused.
hasTabIndexSpaceBooleantrueEnables the toggle to be activated with the Space key when focused.
ariaLabelString'Toggle'Defines the aria-label attribute for screen readers.
ariaAttrsObject{}Allows passing custom aria attributes as an object. Keys will automatically receive the aria- prefix. Example: { 'describedby': 'help-id', 'required': 'true' }
themeString'light'Defines the theme. Accepts dark and light. When theme is set, uses theme-specific color props.
lightSwitchColorString'#f5f5f5'Defines the switch background color for light theme. Accepts Hex color only
lightSwitchColorActiveString'#e0e0e0'Defines the switch background color when active for light theme. Accepts Hex color only
lightThumbColorString'#ffffff'Defines the thumb (circle) color for light theme. Accepts Hex color only
lightThumbColorActiveString'#ffffff'Defines the thumb (circle) color when active for light theme. Accepts Hex color only
lightDisabledBgColorString'#dfdfd9'Defines the disabled background color for light theme. Accepts Hex color only
darkSwitchColorString'#2d2d2d'Defines the switch background color for dark theme. Accepts Hex color only
darkSwitchColorActiveString'#3d3d3d'Defines the switch background color when active for dark theme. Accepts Hex color only
darkThumbColorString'#e0e0e0'Defines the thumb (circle) color for dark theme. Accepts Hex color only
darkThumbColorActiveString'#ffffff'Defines the thumb (circle) color when active for dark theme. Accepts Hex color only
darkDisabledBgColorString'rgba(40, 42, 54, 1)'Defines the disabled background color for dark theme. Accepts rgba or Hex color

Events

nameReturn typeDescription
current-valueBooleanFired when an update to the value is made, returns the current value.
changedBooleanFired when an update to the value is made, returns the current value.
clickednothingFired when the toggle is clicked, returns nothing.

Example

Light Theme (default)

<template>
  <NbToggle
    nb-id="toggle-light"
    theme="light"
    :value="toggleValue"
    @changed="handleChange($event)"
    @current-value="handleCurrentValue($event)"
    @clicked="logEvent()"
  />
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompToggle',
});

const toggleValue = ref(false);

const handleChange = (value) => {
  toggleValue.value = value;
  console.log('Toggle changed:', value);
};

const handleCurrentValue = (value) => {
  console.log('Current value:', value);
};

const logEvent = () => {
  console.log('Toggle clicked');
};
</script>

Dark Theme

<template>
  <NbToggle
    nb-id="toggle-dark"
    theme="dark"
    :value="toggleValue"
    @changed="handleChange($event)"
    @current-value="handleCurrentValue($event)"
    @clicked="logEvent()"
  />
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompToggleDark',
});

const toggleValue = ref(true);

const handleChange = (value) => {
  toggleValue.value = value;
  console.log('Toggle changed:', value);
};

const handleCurrentValue = (value) => {
  console.log('Current value:', value);
};

const logEvent = () => {
  console.log('Toggle clicked');
};
</script>

Light Theme (Custom Colors)

<template>
  <NbToggle
    nb-id="toggle-light-custom"
    theme="light"
    :value="toggleValue"
    light-switch-color="#dbeafe"
    light-switch-color-active="#3b82f6"
    light-thumb-color="#bfdbfe"
    light-thumb-color-active="#ffffff"
    @changed="handleChange($event)"
  />
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompToggleLightCustom',
});

const toggleValue = ref(false);

const handleChange = (value) => {
  toggleValue.value = value;
  console.log('Toggle changed:', value);
};
</script>

Dark Theme (Custom Colors)

<template>
  <NbToggle
    nb-id="toggle-dark-custom"
    theme="dark"
    :value="toggleValue"
    dark-switch-color="#8b5cf6"
    dark-switch-color-active="#6d28d9"
    dark-thumb-color="#a78bfa"
    dark-thumb-color-active="#f3f4f6"
    @changed="handleChange($event)"
  />
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompToggleDarkCustom',
});

const toggleValue = ref(true);

const handleChange = (value) => {
  toggleValue.value = value;
  console.log('Toggle changed:', value);
};
</script>

Disabled State

<template>
  <div>
    <NbToggle
      nb-id="toggle-disabled-off"
      theme="light"
      :value="false"
      :disabled="true"
    />
    <NbToggle
      nb-id="toggle-disabled-on"
      theme="light"
      :value="true"
      :disabled="true"
    />
  </div>
</template>

<script setup>
defineOptions({
  name: 'CompToggleDisabled',
});
</script>

Inline Usage

<template>
  <p>
    Text before
    <NbToggle
      nb-id="toggle-inline"
      theme="light"
      display="ib"
      :value="toggleValue"
      @changed="handleChange($event)"
    />
    text after
  </p>
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompToggleInline',
});

const toggleValue = ref(false);

const handleChange = (value) => {
  toggleValue.value = value;
};
</script>

Block Display

<template>
  <NbToggle
    nb-id="toggle-block"
    theme="light"
    display="b"
    :value="toggleValue"
    @changed="handleChange($event)"
  />
</template>

<script setup>
import { ref } from 'vue';

defineOptions({
  name: 'CompToggleBlock',
});

const toggleValue = ref(false);

const handleChange = (value) => {
  toggleValue.value = value;
};
</script>