문서 메뉴

UIContextMenu

항목을 우클릭하거나 키보드로 맥락 메뉴를 엽니다.

기본 사용법

대상 파일의 이름 변경과 복제

파일에서 우클릭하거나 Shift+F10을 눌러 메뉴를 여세요. 이름 변경, 복제, 삭제는 메뉴를 연 파일에 적용됩니다.

  • 배포 체크리스트.md4 KB
  • 업데이트 안내.txt12 KB
<script setup lang="ts">
import { UIButton, UIContextMenu, UIInput } from "@connextable/ui";
import { nextTick, ref, useId } from "vue";

const initialFiles = [
  { id: 1, name: "배포 체크리스트.md", size: "4 KB" },
  { id: 2, name: "업데이트 안내.txt", size: "12 KB" },
];
const commands = [
  { id: "rename", parentID: null, label: "이름 변경" },
  { id: "duplicate", parentID: null, label: "복제" },
  {
    id: "delete",
    parentID: null,
    label: "삭제",
    tone: "danger",
    separatorBefore: true,
  },
] as const;
const files = ref([...initialFiles]);
const nameInputID = useId();
const editing = ref<number>();
const draft = ref("");
const status = ref("");
let nextID = 3;

function invoke(command: string, file: (typeof initialFiles)[number]) {
  if (command === "rename") {
    editing.value = file.id;
    draft.value = file.name;
    void nextTick(() => document.getElementById(nameInputID)?.focus());
  } else if (command === "duplicate") {
    const extension = file.name.lastIndexOf(".");
    const name =
      extension > 0
        ? `${file.name.slice(0, extension)} 사본${file.name.slice(extension)}`
        : `${file.name} 사본`;
    files.value.push({ ...file, id: nextID++, name });
    status.value = `${name}을 추가했습니다.`;
  } else if (command === "delete") {
    files.value = files.value.filter((entry) => entry.id !== file.id);
    if (editing.value === file.id) editing.value = undefined;
    status.value = `${file.name}을 제거했습니다.`;
  }
}

function rename() {
  const name = draft.value.trim();
  if (!name) return;
  files.value = files.value.map((file) =>
    file.id === editing.value ? { ...file, name } : file,
  );
  editing.value = undefined;
  status.value = `${name}으로 변경했습니다.`;
}
</script>

<template>
  <div class="file-example">
    <ul class="file-example__list" aria-label="예제 파일">
      <li v-for="file in files" :key="file.id">
        <UIContextMenu
          :items="commands"
          :label="`${file.name} 작업`"
          @invoke="invoke($event, file)"
        >
          <template #trigger>
            <div
              tabindex="0"
              aria-keyshortcuts="Shift+F10"
              class="file-example__row"
            >
              <span>{{ file.name }}</span>
              <small>{{ file.size }}</small>
            </div>
          </template>
        </UIContextMenu>
      </li>
    </ul>
    <form
      v-if="editing !== undefined"
      class="file-example__actions"
      @submit.prevent="rename"
      @keydown.esc="editing = undefined"
    >
      <UIInput :id="nameInputID" v-model="draft" aria-label="새 파일 이름" />
      <UIButton type="submit" size="sm" :disabled="!draft.trim()">
        저장
      </UIButton>
      <UIButton
        type="button"
        size="sm"
        variant="ghost"
        @click="editing = undefined"
      >
        취소
      </UIButton>
    </form>
    <UIButton
      v-if="!files.length"
      size="sm"
      variant="outline"
      @click="
        files = [...initialFiles];
        status = '';
      "
    >
      예제 파일 복원
    </UIButton>
  </div>
</template>

<style scoped>
.file-example {
  display: grid;
  gap: var(--ui-space-sm);
  inline-size: min(100%, 24rem);
  min-inline-size: 0;
}

.file-example__list {
  display: grid;
  gap: var(--ui-space-2xs);
  margin: 0;
  padding: 0;
  list-style: none;
}

.file-example__row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  justify-items: start;
  gap: var(--ui-space-sm);
  padding: var(--ui-space-sm) var(--ui-space-md);
  border: 1px solid var(--ui-color-border);
  border-radius: var(--ui-radius-sm);
  background: var(--ui-color-surface);
}

.file-example__row:hover {
  background: var(--ui-color-surface-hover);
}

.file-example__row span {
  min-inline-size: 0;
  overflow-wrap: anywhere;
}

.file-example small {
  color: var(--ui-color-text-muted);
}

.file-example__actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--ui-space-xs);
}
</style>

설정과 동작

명령 앞에 아이콘 표시

회의 기록에서 우클릭하거나 Shift+F10을 눌러 각 명령의 아이콘을 봅니다.

키보드로 메뉴 열기

영역에 포커스를 두고 Shift+F10으로 메뉴를 엽니다.

삭제할 수 없는 항목

복제는 실행할 수 있고 삭제는 방향키 탐색에서 건너뜁니다.

실전 예제

문서 탭 고정과 복제

탭에서 메뉴를 열어 고정하면 목록 앞에 표시됩니다. 탭을 복제하거나 닫을 수도 있습니다.

표의 열 정렬과 숨기기

재고나 단가 머리글에서 메뉴를 열면 해당 열을 기준으로 정렬하거나 숨길 수 있습니다.

받은 메일 정리

메일을 읽음으로 바꾸거나 중요 표시를 붙이고 받은편지함에서 보관할 수 있습니다.

보드 카드 옮기기

카드에서 메뉴를 열고 ‘다른 열로 이동’의 하위 메뉴에서 목적지를 고릅니다.

문장 서식 적용과 초기화

문장에서 메뉴를 열어 굵게 또는 기울임을 적용해 보세요. 서식 지우기는 구분선 아래에 있습니다.

구성 컴포넌트

구조와 배치

UIContextMenuSeparator

항목 또는 조작 영역을 시각적으로 구분합니다.

문장 서식 적용과 초기화 예제

팝업과 위치

UIContextMenuContent

트리거 또는 현재 값에 연결된 콘텐츠를 담습니다.

키보드로 메뉴 열기 예제
UIContextMenuSubContent

상위 메뉴 항목에서 열린 하위 명령을 담습니다.

보드 카드 옮기기 예제

탐색과 동작

UIContextMenuTrigger

컨트롤을 실행하거나 콘텐츠를 여는 버튼입니다.

키보드로 메뉴 열기 예제
UIContextMenuItem

루트에 등록된 값 하나를 표시하고 상호작용을 연결합니다.

키보드로 메뉴 열기 예제

주요 API

속성과 모델

items[].icon타입Component

이름 앞에 표시할 Vue 아이콘 컴포넌트입니다.

명령 앞에 아이콘 표시 예제
items타입readonly Command[]

우클릭 메뉴에 표시할 명령을 지정합니다. 항목에 icon을 넣으면 명령 이름 앞에 표시합니다.

명령 앞에 아이콘 표시 예제
UIContextMenuItem.icon

명령 이름 앞에 표시할 Vue 아이콘 컴포넌트입니다.

UIContextMenuItem.tone

danger로 지정하면 삭제 같은 명령이 위험 색상으로 표시됩니다.

슬롯

UIContextMenuItem #leading

상태에 따라 바뀌는 아이콘이나 사용자 정의 아이콘을 넣습니다.

이벤트와 명령

@invoke타입(_value: string) => any

사용자가 실행한 명령의 id를 받습니다.

키보드로 메뉴 열기 예제
전체 속성, 이벤트와 슬롯

UIContextMenu

disabled
boolean | undefined
label
string | undefined
open
boolean | undefined
@update:open
(_value: boolean) => any
shift
boolean | undefined
default-open
boolean | undefined
text-value
((id: string) => string) | undefined
policies
MenuPolicies<string> | undefined
side
RectSide | undefined
align
"start" | "center" | "end" | undefined
side-offset
number | undefined
collision-padding
PositionPadding | undefined
collision-boundary
PositionBoundary | undefined
avoid-collisions
boolean | undefined
flip
boolean | undefined
hide-when-detached
boolean | undefined
strategy
PositionStrategy | undefined
tracking
PositionTracking | undefined
position
boolean | undefined
default-highlighted-value
string | null | undefined
disabled-items
readonly string[] | undefined
@invoke
(_value: string) => any
items필수
readonly Command[]
trigger-label
string | undefined
#default
(props: MenuRootSlotProps) => VNodeChild
#trigger
(state: MenuRootSlotProps) => VNodeChild

UIContextMenuTrigger

as
PrimitiveAs | undefined
as-child
boolean | undefined
#default
(props: MenuRootSlotProps) => VNodeChild

UIContextMenuContent

as
PrimitiveAs | undefined
as-child
boolean | undefined
#default
(props: MenuRootSlotProps) => VNodeChild

UIContextMenuItem

value필수
string
disabled
boolean | undefined
as
PrimitiveAs | undefined
as-child
boolean | undefined
prefetch
boolean | undefined
prefetch-on
PrefetchTrigger | Partial<Record<PrefetchTrigger, boolean>> | undefined
no-prefetch
boolean | undefined
icon
Component | undefined
tone
"danger" | undefined
href
string | undefined
to
string | RouteLocationAsRelativeGeneric | RouteLocationAsPathGeneric | undefined
external
boolean | undefined
replace
boolean | undefined
#default
(state: MenuItemSlotProps) => unknown
#leading
(state: MenuItemSlotProps) => unknown
#trailing
(state: MenuItemSlotProps) => unknown

UIContextMenuSubContent

as
PrimitiveAs | undefined
as-child
boolean | undefined
for필수
string
#default
(props: MenuRootSlotProps) => VNodeChild

UIContextMenuSeparator

as
PrimitiveAs | undefined
as-child
boolean | undefined