Back
Forward
Reload
More tools
Save page as…
Developer tools
右クリックまたは長押しでポインターの位置に開くアクションメニューです。
コンテキストメニューは、Menu.Triggerの代わりにMenu.ContextTriggerによって開かれるMenuです。アイテム、グループ、チェックボックスおよびラジオアイテム、サブメニュー、キーボード操作はMenuと同じです。このメニューはMenu WAI-ARIAデザインパターンに準拠しています。
import { $, component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Menu } from '@leve-ui/menu';
import { LuChevronRight } from '@qwikest/icons/lucide';
import styles from './context.css?inline';
export default component$(() => {
useStyles$(styles);
const last = useSignal('Nothing yet');
const select$ = $((action: string) => {
last.value = action;
});
return (
<div class="menu-context">
<Menu.Root>
{/* Right-click (or long-press) anywhere in the area. Keyboard users press Shift+F10 on the button. */}
<Menu.ContextTrigger class="menu-context-area">
<span>Right-click here</span>
<button type="button" class="menu-context-button">
Or focus me and press Shift+F10
</button>
</Menu.ContextTrigger>
{/* There is no Trigger to take the name from, so name the menu with aria-label */}
<Menu.Content aria-label="Page" class="menu-context-content">
<Menu.Item onSelect$={() => select$('Back')} class="menu-context-item">
Back
</Menu.Item>
<Menu.Item disabled class="menu-context-item">
Forward
</Menu.Item>
<Menu.Item onSelect$={() => select$('Reload')} class="menu-context-item">
Reload
</Menu.Item>
<Menu.Separator class="menu-context-separator" />
<Menu.Sub>
<Menu.SubTrigger class="menu-context-item">
More tools
<LuChevronRight class="menu-context-icon" aria-hidden="true" />
</Menu.SubTrigger>
<Menu.SubContent alignOffset={-5} class="menu-context-content">
<Menu.Item onSelect$={() => select$('Save page as')} class="menu-context-item">
Save page as…
</Menu.Item>
<Menu.Item onSelect$={() => select$('Developer tools')} class="menu-context-item">
Developer tools
</Menu.Item>
</Menu.SubContent>
</Menu.Sub>
</Menu.Content>
</Menu.Root>
<p class="menu-context-status">Last action: {last.value}</p>
</div>
);
});.menu-context {
display: flex;
flex-direction: column;
gap: 12px;
font-family: inherit;
}
/* The area that opens the menu; the long press must not select text or show the iOS callout */
.menu-context-area {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
box-sizing: border-box;
width: 320px;
height: 160px;
border: 2px dashed light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
user-select: none;
-webkit-user-select: none;
-webkit-touch-callout: none;
}
.menu-context-area[data-state="open"] {
border-color: #009dfd;
}
.menu-context-button {
padding: 6px 10px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
font-size: 13px;
color: light-dark(#0f172a, #ffffff);
}
.menu-context-button:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
/* The Content is a popover in the top layer; the library places it at the pointer */
.menu-context-content {
box-sizing: border-box;
min-width: 180px;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 8px 24px rgb(15 23 42 / 0.18);
}
.menu-context-content:focus-visible {
outline: none;
}
.menu-context-item {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
cursor: default;
outline: none;
}
/* The focused item: arrow keys, typeahead and the pointer move focus */
.menu-context-item:focus {
background: #009dfd;
color: #ffffff;
}
.menu-context-item[data-disabled] {
opacity: 0.5;
}
/* The SubTrigger stays highlighted while its submenu is open */
.menu-context-item[data-state="open"] {
background: light-dark(#f1f5f9, #ffffff14);
}
.menu-context-separator {
height: 1px;
margin: 4px 0;
background: light-dark(#e2e8f0, #ffffff1f);
}
.menu-context-icon {
width: 14px;
height: 14px;
}
.menu-context-status {
margin: 0;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}個別のパッケージをインストールするか、@leve-ui/coreを使用してすべてのコンポーネントを一度に取得します。
pnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreimport { Menu } from '@leve-ui/menu'; // or '@leve-ui/core'
<Menu.Root>
<Menu.ContextTrigger />
<Menu.Content aria-label="...">
<Menu.Item />
</Menu.Content>
</Menu.Root>
| パーツ | 要素 | 説明 |
|---|---|---|
Menu.Root | <div> | 各パーツをラップし、開閉状態を保持します。 |
Menu.ContextTrigger | <div> | メニューを開く領域です。 |
Menu.Content | <div> | トップレイヤーにあるpopover="manual"要素のメニュー。 |
その他のパーツはMenuと同じです。Root内ではMenu.TriggerまたはMenu.ContextTriggerのいずれか一方のみを使用し、両方は使用しないでください。
Shift + F10またはContextMenuキーを押すと、その要素の下にメニューが開きます。領域自体はフォーカスを受け取らないため、フォーカス可能なコンテンツ(ファイルリスト、tabindex="0"付きのcanvasなど)を内部に配置してください。user-select: noneと-webkit-touch-callout: noneを追加してください。disabledを設定すると、領域はメニューを開かず、ブラウザのメニューが表示されます。| Prop | デフォルト | 型 |
|---|---|---|
| disabled | false | boolean 領域がメニューを開かないようにするかどうかです。代わりにブラウザ独自のコンテキストメニューが表示されます。 |
すべての<div>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-menu-context-trigger | 常に存在 |
data-state | "open" | "closed" |
data-disabled | 無効時に存在 |
Contentはside="bottom"およびalign="start"でポインターの位置に配置され、ビューポート内に収まるように反転またはシフトします。side、alignおよびオフセットの各propsはTriggerと同様に機能します。その他のパーツについてはMenuを参照してください。
Menu.ContextTriggerはroleをレンダリングしません。領域は独自のコンテンツであり、そのセマンティクスが保持されます。Menu.Contentにaria-label(またはaria-labelledby)を指定してください。Shift + F10またはContextMenuキーを押してメニューを開きます。領域内にフォーカス可能な要素があることを確認し、コンテキストメニューは見つけにくいため、可能であれば他の場所でも同じアクションを提供してください。| キー | 説明 |
|---|---|
Shift + F10 / ContextMenu | 領域内のフォーカスされた要素でメニューを開き、最初のアイテムにフォーカスを合わせます。 |
Escape | メニューを閉じ、開く前にフォーカスされていた要素にフォーカスを戻します。 |
メニュー内のキー操作はMenuと同じです。