Vue 3 composable for cycling through UI variants and toggling feature flags via global keyboard shortcuts.
Just copy useVariants.ts
import { createVariants } from "@/composables/useVariants";
export const { provideAppVariants, useAppVariants } = createVariants({
TaskCard: { key: "1", variants: ["A", "B", "C"] as const },
DebugMode: { key: "2", variants: [false, true] as const },
});
// The first element in the `variants` array will be used as the default value.import { provideAppVariants } from "@/variants.ts";
provideAppVariants();Note: Due to how Vue's
provide/injectworks,injectonly resolves values from ancestor components. SinceApp.vueis the root component, it cannot useuseAppVariants()— callinginjectthere will find no provider above it.If you need to access variants inside
App.vueitself, use the return value ofprovideAppVariants()instead:// App.vue import { provideAppVariants } from "@/variants.ts"; import { watchEffect } from "vue"; const { variants } = provideAppVariants(); watchEffect(() => { document.documentElement.classList.toggle("debug", variants.DebugMode); // works correctly });
<script setup lang="ts">
import { useAppVariants } from "@/variants";
import CardVariantA from "./CardVariantA.vue";
import CardVariantB from "./CardVariantB.vue";
import CardVariantC from "./CardVariantC.vue";
const { variants } = useAppVariants();
</script>
<template>
<div>
<h2>My tasks</h2>
<!-- Cycle through three UI variants using the "1" key -->
<CardVariantA v-if="variants.TaskCard === 'A'" />
<CardVariantB v-else-if="variants.TaskCard === 'B'" />
<CardVariantC v-else-if="variants.TaskCard === 'C'" />
<!-- Toggle debug panel using the "2" key -->
<div v-if="variants.DebugMode" class="debug-panel">Debug enabled</div>
</div>
</template>