@zynq/vue
v1.0.6
Published
Synchronize application state with the URL using Vue 3 composables and Vue Router
Maintainers
Readme
@zynq/vue
Synchronize application state with the URL in Vue 3 apps using composables and Vue Router. Keep filters, pagination, search, and other UI state in the query string so users can share and bookmark links.
Requirements
- Vue ^3.0.0
- Vue Router ^4.0.0
Installation
npm install @zynq/vue
# or
pnpm add @zynq/vue
# or
yarn add @zynq/vueInstall Vue and Vue Router if you don't have them yet:
npm install vue vue-routerQuick start
Define a schema with the
sbuilders and calluseZynq(schema)inside a component that has access to the router (e.g. under<RouterView>or whereuseRouter()works).The returned
stateis reactive and stays in sync with the URL: when the user changes the URL (or back/forward), state updates; when you changestate, the URL updates (viareplaceStateby default).
<script setup lang="ts">
import { useZynq, s } from '@zynq/vue';
const schema = {
name: s.string(),
count: s.number(0),
active: s.boolean(true),
};
const { state } = useZynq(schema);
</script>
<template>
<div>
<input v-model="state.name" placeholder="Name" />
<input v-model.number="state.count" type="number" />
<label>
<input v-model="state.active" type="checkbox" />
Active
</label>
</div>
</template>Visiting /?name=foo&count=10&active=false will set state to { name: 'foo', count: 10, active: false }. Changing state in the UI will update the query string without full page reloads.
API
useZynq(schema)
- Parameters:
schema– object of keys to codecs (e.g. froms.string(),s.number(),s.boolean()). - Returns:
{ state }– reactive object whose keys match the schema and stay synced with the URL query.
Must be called in a component that runs in a Vue Router context (so useRouter() and useRoute() are available).
Schema builders (s)
Included in this package for convenience:
s.string()– string (optional in URL)s.number(default?)– number with optional defaults.boolean(default?)– boolean with optional default
VueRouterAdapter
Adapter that implements Zynq’s router interface using Vue Router. Used internally by useZynq; you typically don’t need to use it directly unless you’re building custom integration.
Types
Schema– schema shapeInferState<S>– inferred state type from schemaS
Building
This package is built with tsup. From the repo root (monorepo) or package directory:
pnpm build
# or
pnpm --filter @zynq/vue buildOutput is emitted to dist/ (ESM, CJS, and type declarations). Vue and Vue Router are external and not bundled.
License
MIT
