Composables

useSidebar

Control one Sidebar instance or share responsive sidebar state.

useSidebar connects external controls to a Sidebar. Pass its component ref to control that specific instance. Pass an options object when your application owns the state.

Import the composable and its types from akaza-ui; the Nuxt module registers components, not composable auto-imports.

Usage

useSidebar has two overloads. Pass a Sidebar component ref to control one rendered instance, or pass an options object when the application owns the state.

function useSidebar(target: SidebarTarget): UseSidebarTargetReturn;
function useSidebar(options?: UseSidebarOptions): UseSidebarReturn;

Component ref mode

Use this mode for external controls, multiple sidebars, or access to the rendered sidebar's responsive and size state.

<script setup lang="ts">
import type { SidebarController } from "akaza-ui";
import { Sidebar, useSidebar } from "akaza-ui";
import { useTemplateRef } from "vue";

const primaryRef = useTemplateRef<SidebarController>("primary");
const inspectorRef = useTemplateRef<SidebarController>("inspector");

const primary = useSidebar(primaryRef);
const inspector = useSidebar(inspectorRef);
</script>

<template>
  <button type="button" @click="primary.toggle">Toggle navigation</button>
  <button type="button" @click="inspector.toggle">Toggle inspector</button>

  <Sidebar ref="primary" collapsible="icon">...</Sidebar>
  <Sidebar ref="inspector" side="right" collapsible="offcanvas">...</Sidebar>
</template>

Each controller delegates only to its referenced instance. Calls made before that instance mounts are ignored.

Controlled state mode

Use this mode when state must exist before the component mounts or must be shared with storage, routing, or other application logic. Pass the same refs to Sidebar models.

<script setup lang="ts">
import { Sidebar, useSidebar } from "akaza-ui";
import { ref } from "vue";

const open = ref(false);
const collapsed = ref(false);
const { isMobile, state, toggle } = useSidebar({
  open,
  collapsed,
  breakpoint: "(max-width: 767px)",
});
</script>

<template>
  <button type="button" @click="toggle">Toggle sidebar</button>
  <output>{{ state }}</output>

  <Sidebar
    v-model:open="open"
    v-model:collapsed="collapsed"
    :mobile="isMobile"
    collapsible="icon"
  >
    ...
  </Sidebar>
</template>

This mode updates the supplied refs directly. The composable does not emit component events; the connected Sidebar models receive the resulting values.

API Reference

Options

These options apply to controlled state mode.

OptionTypeDefaultDescription
openRef<boolean>Internal false refMobile dialog visibility.
collapsedRef<boolean>Internal false refDesktop collapse state.
breakpointMaybeRefOrGetter<string>"(max-width: 767px)"Media query used to calculate isMobile.
mobileMaybeRefOrGetter<boolean | undefined>Media query resultOverrides responsive detection. Useful for container layouts, SSR-owned state, and tests.

Return value

PropertyTypeAvailableDescription
openRef<boolean>Both modesWritable mobile visibility. In component ref mode, writes use the component's cancelable change path.
collapsedRef<boolean>Both modesWritable desktop collapse state. In component ref mode, writes use the component's cancelable change path.
isMobileComputedRef<boolean>Both modesCurrent responsive mode.
stateComputedRef<"open" | "closed" | "expanded" | "collapsed">Both modesNormalized responsive state.
sizeComputedRef<number>Component refCurrent expanded width in pixels. Read-only; use v-model:size for arbitrary size changes.
openMobile()() => voidBoth modesOpen mobile dialog state.
closeMobile()() => voidBoth modesClose mobile dialog state.
toggleMobile()() => voidBoth modesToggle mobile dialog state regardless of responsive mode.
expand()() => voidBoth modesExpand desktop state.
collapse()() => voidBoth modesCollapse desktop state. The component ignores this when collapsible is false.
toggleCollapsed()() => voidBoth modesToggle desktop collapse state regardless of responsive mode.
toggle()() => voidBoth modesToggle mobile visibility when mobile; otherwise toggle desktop collapse.
resetSize()() => voidComponent refRestore the component's defaultSize.

When no mobile override is supplied, controlled mode uses matchMedia through VueUse. During SSR it starts in desktop mode and updates after hydration. Supply an SSR-safe mobile value when initial responsive markup must be deterministic.

Component ref mode delegates through Sidebar events. open-change, collapsed-change, and size-change listeners can still cancel changes. Actions accept (reasonOrEvent?: string | Event, event?: Event) in addition to no arguments. Bind directly, for example @click="sidebar.toggle", to retain the original event and the mobile dialog's return-focus target. Calling sidebar.toggle() is a programmatic action without a triggering event. Standalone state mode does not emit component events.

Exported types

TypeDescription
SidebarControllerPublic Sidebar instance contract used with useTemplateRef.
SidebarControllerActionEvent-safe action signature, accepting an optional reason or native event.
SidebarTargetRead-only ref containing a SidebarController, null, or undefined.
UseSidebarOptionsControlled state mode options.
UseSidebarReturnShared state and actions returned by controlled state mode.
UseSidebarTargetReturnComponent ref return type, including size and resetSize.

Rendering and ownership

The composable does not render a sidebar, trap focus, or lock scrolling by itself. Sidebar supplies those behaviors and styling hooks. State mode needs a connected Sidebar, as shown above.

Keep storage, route watching, and keyboard shortcuts in your application or configure the component's existing props. There is no global sidebar singleton.