Avenra
Liquid GlassExamples
Liquid GlassExamples

Vue 3

Using Liquid Glass with Vue 3 Composition API.

Vue 3

In Vue 3, use the Composition API hooks onMounted and onUnmounted to manage your Liquid Glass components.

Reusable Component Example

LiquidSwitch.vue:

<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue';
import { createLiquidSwitch } from '@avenra/liquid-glass';
import '@avenra/liquid-glass/styles';

const props = defineProps({
  modelValue: Boolean
});

const emit = defineEmits(['update:modelValue']);

const container = ref(null);
const handle = ref(null);

onMounted(() => {
  // 1. Create component
  handle.value = createLiquidSwitch(container.value, {
    checked: props.modelValue
  });

  // 2. Sync state back to parent
  handle.value.on('change', ({ checked }) => {
    emit('update:modelValue', checked);
  });
});

// 3. Sync external changes to component
watch(() => props.modelValue, (newVal) => {
  if (handle.value) handle.value.checked = newVal;
});

// 4. Cleanup
onUnmounted(() => {
  if (handle.value) handle.value.destroy();
});
</script>

<template>
  <div ref="container"></div>
</template>

Usage

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

const isEnabled = ref(true);
</script>

<template>
  <LiquidSwitch v-model="isEnabled" />
  <p>Status: {{ isEnabled ? 'ON' : 'OFF' }}</p>
</template>

On this page