11 · Navegação

ContextMenu

clique com o botão direito · na posição do ponteiro · atalhos · submenu · perigoso por último

Menu aberto com o botão direito (ou Shift+F10) sobre uma linha, card ou área, na posição do ponteiro.

Botão direito sobre a linha

Pedido #2490Mercadinho Bom PreçoR$ 1200,90
Pedido #2491Mercadinho Bom PreçoR$ 1237,90
Pedido #2492Mercadinho Bom PreçoR$ 1274,90
Pedido #2493Mercadinho Bom PreçoR$ 1311,90
.heex
<.context_menu id="ctx1" class="relative h-[300px] max-w-[560px]">
  <div class="overflow-hidden rounded-lg border border-line bg-panel shadow-xs">
    <div class="flex h-11 items-center gap-2.5 border-b border-line px-3.5"><b class="w-[110px] text-[13.5px]">Pedido #2490</b><span class="flex-1 text-[13px] text-muted">Mercadinho Bom Preço</span><span class="font-mono text-[13px]">R$ 1200,90</span></div>
    <div class="flex h-11 items-center gap-2.5 border-b border-line px-3.5 bg-hover"><b class="w-[110px] text-[13.5px]">Pedido #2491</b><span class="flex-1 text-[13px] text-muted">Mercadinho Bom Preço</span><span class="font-mono text-[13px]">R$ 1237,90</span></div>
    <div class="flex h-11 items-center gap-2.5 border-b border-line px-3.5"><b class="w-[110px] text-[13.5px]">Pedido #2492</b><span class="flex-1 text-[13px] text-muted">Mercadinho Bom Preço</span><span class="font-mono text-[13px]">R$ 1274,90</span></div>
    <div class="flex h-11 items-center gap-2.5 border-b border-line px-3.5"><b class="w-[110px] text-[13.5px]">Pedido #2493</b><span class="flex-1 text-[13px] text-muted">Mercadinho Bom Preço</span><span class="font-mono text-[13px]">R$ 1311,90</span></div>
  </div>
  <:item icon="external" kbd="↵">Abrir</:item>
  <:item icon="edit" kbd="F2">Renomear</:item>
  <:item icon="link" kbd="⌘ L">Copiar link</:item>
  <:item icon="trash" kbd="⌫" tone="danger" divider>Excluir</:item>
</.context_menu>

com submenu

.heex
<div class="flex flex-wrap items-start gap-1">
  <.menu_panel width="w-[230px]">
    <.dropdown_item shortcut="↵"><:icon><.icon name="external" /></:icon>Abrir</.dropdown_item>
    <.dropdown_item shortcut="F2"><:icon><.icon name="edit" /></:icon>Renomear</.dropdown_item>
    <.dropdown_item shortcut="⌘ L"><:icon><.icon name="link" /></:icon>Copiar link</.dropdown_item>
    <.dropdown_item><:icon><.icon name="folder" /></:icon>Mover para<:trailing><.icon name="chevron-right" size="sm" /></:trailing></.dropdown_item>
    <.dropdown_separator />
    <.dropdown_item danger shortcut="⌫"><:icon><.icon name="trash" /></:icon>Excluir</.dropdown_item>
  </.menu_panel>
  <.menu_panel class="mt-[118px]">
    <.dropdown_item><:icon><.icon name="folder" /></:icon>Financeiro</.dropdown_item>
    <.dropdown_item><:icon><.icon name="folder" /></:icon>Arquivo morto</.dropdown_item>
    <.dropdown_item><:icon><.icon name="plus" /></:icon>Nova pasta…</.dropdown_item>
  </.menu_panel>
</div>

Uso

<.context_menu id="row-menu" target="#orders tr">
  <:item icon="huge-edit-02" shortcut="F2">Renomear</:item>
  <:item icon="huge-delete-02" danger>Excluir</:item>
</.context_menu>

Regras

  • Abre onde o ponteiro está; se não couber, vira para o outro lado.
  • Mesmo conteúdo do ⋯ da linha: o menu de contexto é um atalho, nunca o único caminho.
  • Esc fecha; setas navegam.

Referência da API

Todos os function components, atributos, valores e slots que este componente cobre.

A menu that opens under the pointer on a right click.

<.context_menu id="file-menu">
  <.card variant="panel">Right click here</.card>
  <:item phx-click="rename" icon="edit" kbd="F2">Rename</:item>
  <:item phx-click="copy">Copy</:item>
  <:item phx-click="delete" tone="danger" divider icon="trash">Delete</:item>
</.context_menu>

The hook LedgerUI.ContextMenu (priv/static/ledger_ui/hooks/context_menu.js) opens the menu at the pointer (kept inside the window, in the top layer), moves the focus to the first item and closes it on a click outside, Escape, a scroll and when an item is chosen. The arrows, Home, End and letters move between the items.

Styled only with Tailwind utilities (the popup parts are those of LedgerUI.Components.Dropdown); every element carries data-part (context-menu, pop, pop-item, pop-separator, pop-end, pop-kbd).

Besides the README attrs the :item slot takes icon (a name) and kbd (a shortcut shown as text).

<.context_menu>

Attr Type Default Values / notes
id (required) :string
label :string "Actions" Accessible name of the menu.
class :any nil
placement :string "bottom-start" bottom-start, bottom-end, top-start, top-end - Where the menu sits against the pointer (bottom-start: below, from the pointer to the right).

Any other HTML/phx attribute goes to the root element.

Slots:

  • <:inner_block> (required) - The area that answers to the right click.
  • <:item> - attrs: tone, divider, disabled, icon, kbd, phx-click, phx-value-id, phx-target
  • <:trigger> - The area that answers to the right click, in place of the inner block (the vitrine's form).