Skip to content

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

3.3 KB
Raw Blame
<script setup lang="ts">
import { ToolbarRoot } from 'reka-ui'

import type { EditorToolDef, Tool } from '@open-pencil/core'
import {
  getToolbarToolSelection,
  isToolbarToolActive,
  ToolbarItem
} from '@open-pencil/vue/primitives/Toolbar'
import { toolbarToolTestId } from '@open-pencil/vue/testing'

import type { ToolbarAction, ToolbarItem as ToolbarLayoutItem } from '@/app/editor/toolbar/layout'
import { toolTip } from '@/app/editor/toolbar/shortcuts'
import ShapeToolButton from '@/components/toolbar/ShapeToolButton.vue'
import ToolButton from '@/components/toolbar/ToolButton.vue'
import ToolFlyout from '@/components/toolbar/ToolFlyout.vue'
import type { ToolbarUI, ToolIconMap, ToolLabels } from '@/components/toolbar/types'
import Tip from '@/components/ui/overlay/Tip.vue'

const { items, activeTool, flyoutSelections, toolIcons, toolLabels, toolShortcuts, ui } =
  defineProps<{
    items: ToolbarLayoutItem[]
    activeTool: Tool
    flyoutSelections: ReadonlyMap<Tool, Tool>
    toolIcons: ToolIconMap
    toolLabels: ToolLabels
    toolShortcuts: Readonly<Record<Tool, string>>
    ui?: ToolbarUI
  }>()

const emit = defineEmits<{
  setTool: [tool: Tool]
}>()

/** The tool a flyout's button shows: the active one if it is in the flyout, else the last used. */
function selectedTool(tool: EditorToolDef) {
  return getToolbarToolSelection(tool, activeTool, flyoutSelections)
}

defineSlots<{
  /** A button that runs a command, such as placing an icon, rather than picking a tool. */
  action(props: { action: ToolbarAction }): unknown
  /** Pickers whose buttons are hidden, anchored to the toolbar's top edge. */
  hidden(): unknown
}>()
</script>

<template>
  <div
    data-canvas-obstacle
    class="absolute bottom-4 left-1/2 z-10 flex max-w-full -translate-x-1/2 items-center"
  >
    <ToolbarRoot
      data-test-id="toolbar"
      class="flex gap-0.5 overflow-x-auto rounded-xl bg-panel p-1 shadow-[0_8px_30px_rgb(0_0_0/0.4)] [&>*]:shrink-0"
    >
      <template v-for="item in items" :key="item.kind === 'tool' ? item.tool.key : item.action">
        <slot v-if="item.kind === 'action'" name="action" :action="item.action" />

        <ShapeToolButton v-else-if="item.tool.key === 'SHAPE_WITH_TEXT'" :ui="ui" />

        <Tip
          v-else-if="item.tool.flyout"
          :label="
            toolTip(toolLabels[selectedTool(item.tool)], toolShortcuts[selectedTool(item.tool)])
          "
        >
          <ToolFlyout
            :tool="item.tool"
            :active-tool="activeTool"
            :selected-tool="selectedTool(item.tool)"
            :tool-icons="toolIcons"
            :tool-labels="toolLabels"
            :tool-shortcuts="toolShortcuts"
            :ui="ui"
            @select="emit('setTool', $event)"
          />
        </Tip>

        <ToolbarItem v-else v-slot="{ active, actions }" :tool="item.tool.key">
          <Tip :label="toolTip(toolLabels[item.tool.key], toolShortcuts[item.tool.key])">
            <ToolButton
              :data-test-id="toolbarToolTestId(item.tool.key)"
              :icon="toolIcons[item.tool.key]"
              :label="toolLabels[item.tool.key]"
              :active="active || isToolbarToolActive(item.tool, activeTool)"
              :ui="ui"
              @click="actions.select"
            />
          </Tip>
        </ToolbarItem>
      </template>
    </ToolbarRoot>
    <slot name="hidden" />
  </div>
</template>