Skip to content

AI-native design editor. Open-source Figma alternative.

1.4 KB
Raw Blame
import { computed, ref } from 'vue'

import type { Tool, EditorToolDef } from '@open-pencil/core'

const CATEGORY_COUNT = 3

export function isToolbarToolActive(tool: EditorToolDef, activeTool: Tool): boolean {
  return tool.key === activeTool || (tool.flyout?.includes(activeTool) ?? false)
}

export function getToolbarToolSelection(
  tool: EditorToolDef,
  activeTool: Tool,
  flyoutSelections?: ReadonlyMap<Tool, Tool>
): Tool {
  if (tool.flyout?.includes(activeTool)) return activeTool
  return flyoutSelections?.get(tool.key) ?? tool.key
}

/**
 * Returns responsive toolbar UI state for mobile category paging.
 *
 * This composable is presentation-oriented and complements {@link useToolbar}
 * when building toolbar shells.
 */
export function useToolbarState() {
  const mobileCategory = ref(0)
  const slideDirection = ref(1)

  const hasPrev = computed(() => mobileCategory.value > 0)
  const hasNext = computed(() => mobileCategory.value < CATEGORY_COUNT - 1)

  function goPrev() {
    if (!hasPrev.value) return
    slideDirection.value = -1
    mobileCategory.value--
  }

  function goNext() {
    if (!hasNext.value) return
    slideDirection.value = 1
    mobileCategory.value++
  }

  return {
    mobileCategory,
    slideDirection,
    hasPrev,
    hasNext,
    isActive: isToolbarToolActive,
    activeKeyForTool: getToolbarToolSelection,
    goPrev,
    goNext
  }
}