npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@togglely/sdk-vue

v1.2.6

Published

Vue SDK for Togglely - Feature toggles with composables

Downloads

105

Readme

Togglely Vue SDK

Vue 3 composables, directives, and plugin for Togglely feature flag management.

Installation

npm install @togglely/sdk-vue

@togglely/sdk-core is included as a dependency -- you do not need to install it separately.

Quick Start

// main.ts
import { createApp } from 'vue';
import { createTogglely } from '@togglely/sdk-vue';
import App from './App.vue';

const app = createApp(App);

app.use(createTogglely({
  apiKey: 'your-api-key',
  project: 'my-project',
  environment: 'production',
  baseUrl: 'https://togglely.io',
}));

app.mount('#app');
<!-- MyComponent.vue -->
<script setup>
import { useToggle, useStringToggle, useNumberToggle } from '@togglely/sdk-vue';

const isEnabled = useToggle('new-feature', false);
const message = useStringToggle('welcome-message', 'Hello!');
const limit = useNumberToggle('max-items', 10);
</script>

<template>
  <div v-if="isEnabled">
    <h1>{{ message }}</h1>
    <p>Max items: {{ limit }}</p>
  </div>
  <div v-else>
    Feature not available
  </div>
</template>

Plugin Installation

createTogglely(options): Plugin

Creates a Vue plugin that initializes a TogglelyClient and provides it to the entire app via inject.

interface TogglelyPluginOptions extends TogglelyConfig {
  initialToggles?: Record<string, any>;  // Pre-fetched toggles for SSR
}

app.use(createTogglely({
  // --- Required ---
  apiKey: 'your-api-key',
  project: 'my-project',
  environment: 'production',
  baseUrl: 'https://togglely.io',

  // --- Optional ---
  tenantId: 'brand-a',
  offlineJsonPath: '/toggles.json',
  offlineToggles: { 'feature-x': { value: true, enabled: true } },
  initialToggles: serverFetchedToggles,
  refreshStrategy: 'manual',
  refreshIntervalMs: 30000,
  timeout: 5000,
}));

The client is also available as this.$togglely in the Options API.

Composables API Reference

useToggle(key: string, defaultValue?: boolean): Ref<boolean>

Reactive boolean toggle. Returns a Ref<boolean> that updates automatically when toggles change.

const isEnabled = useToggle('new-feature', false);
// Use in template: v-if="isEnabled"

useStringToggle(key: string, defaultValue?: string): Ref<string>

Reactive string toggle.

const message = useStringToggle('welcome-message', 'Hello!');
// Use in template: {{ message }}

useNumberToggle(key: string, defaultValue?: number): Ref<number>

Reactive number toggle.

const limit = useNumberToggle('max-items', 10);

useJSONToggle<T>(key: string, defaultValue?: T): Ref<T>

Reactive JSON toggle.

interface ThemeConfig { primary: string; mode: 'light' | 'dark' }
const theme = useJSONToggle<ThemeConfig>('theme-config', { primary: '#000', mode: 'light' });

useToggles(): DeepReadonly<Ref<Record<string, ToggleValue>>>

Returns all cached toggles as a read-only reactive ref.

const allToggles = useToggles();

useTogglelyClient(): TogglelyClient

Access the underlying client for direct API calls.

const client = useTogglelyClient();
await client.refresh();

useTogglelyState(): DeepReadonly<Ref<TogglelyState>>

Reactive SDK state (isReady, isOffline, lastError, lastFetch). Updates on every event.

<script setup>
import { useTogglelyState } from '@togglely/sdk-vue';
const state = useTogglelyState();
</script>

<template>
  <div v-if="!state.isReady">Loading toggles...</div>
  <div v-else-if="state.isOffline">Offline mode</div>
</template>

useTogglelyReady(): Ref<boolean>

Shorthand for the isReady state.

useTogglelyOffline(): Ref<boolean>

Shorthand for the isOffline state.

useTogglelyContext(): { setContext, clearContext, getContext }

Manage the targeting context.

const { setContext, clearContext, getContext } = useTogglelyContext();

setContext({ userId: '123', country: 'DE' });
const ctx = getContext(); // { userId: '123', country: 'DE' }
clearContext();

Directives

v-feature-toggle

Show or hide elements based on a feature toggle. Register the directive globally:

import { vFeatureToggle } from '@togglely/sdk-vue';
app.directive('feature-toggle', vFeatureToggle);

Simple usage (pass toggle key as a string):

<template>
  <div v-feature-toggle="'new-feature'">
    Only visible when 'new-feature' is enabled
  </div>
</template>

With options:

<template>
  <div v-feature-toggle="{ toggle: 'premium', defaultValue: false }">
    Premium content
  </div>
</template>

The directive sets display: none when the toggle is disabled and restores it when enabled. It subscribes to update events and cleans up on unmount.

FeatureToggle Component (Recipe)

The SDK does not ship a .vue component, but you can create one easily:

<!-- FeatureToggle.vue -->
<script setup>
import { useToggle } from '@togglely/sdk-vue';

const props = defineProps({
  name: { type: String, required: true },
  defaultValue: { type: Boolean, default: false },
});

const isEnabled = useToggle(props.name, props.defaultValue);
</script>

<template>
  <slot v-if="isEnabled" />
  <slot v-else name="fallback" />
</template>

Usage:

<FeatureToggle name="new-dashboard">
  <NewDashboard />
  <template #fallback>
    <OldDashboard />
  </template>
</FeatureToggle>

SSR Integration (Nuxt)

Nuxt Plugin

// plugins/togglely.ts
import { createTogglely } from '@togglely/sdk-vue';

export default defineNuxtPlugin(async (nuxtApp) => {
  // Optionally fetch toggles server-side
  let initialToggles = {};
  if (import.meta.server) {
    const config = useRuntimeConfig();
    const response = await $fetch(
      `${config.togglelyBaseUrl}/sdk/flags/${config.togglelyProject}/${config.togglelyEnv}`,
      { headers: { Authorization: `Bearer ${config.togglelyApiKey}` } }
    );
    initialToggles = response;
  }

  nuxtApp.vueApp.use(createTogglely({
    apiKey: useRuntimeConfig().public.togglelyApiKey,
    project: useRuntimeConfig().public.togglelyProject,
    environment: useRuntimeConfig().public.togglelyEnv,
    baseUrl: useRuntimeConfig().public.togglelyBaseUrl,
    initialToggles,
  }));
});

Nuxt Runtime Config

// nuxt.config.ts
export default defineNuxtConfig({
  runtimeConfig: {
    togglelyApiKey: '',       // server-only
    togglelyBaseUrl: '',
    togglelyProject: '',
    togglelyEnv: '',
    public: {
      togglelyApiKey: '',     // also available client-side
      togglelyProject: '',
      togglelyEnv: '',
      togglelyBaseUrl: '',
    },
  },
});

Build-Time JSON Generation

{
  "scripts": {
    "build": "togglely-pull --apiKey=$TOGGLELY_APIKEY --project=my-project --environment=production --output=./public/toggles.json && vite build"
  }
}
app.use(createTogglely({
  ...config,
  offlineJsonPath: '/toggles.json',
}));

Re-exports

The Vue SDK re-exports from @togglely/sdk-core:

  • TogglelyClient
  • createOfflineTogglesScript
  • togglesToEnvVars
  • Types: ToggleContext, TogglelyConfig, TogglelyState

License

MIT