🚀 Ship faster with premium components 🚀
Search documentation v1.1.0

Context Menu

Display a right-click menu with context-specific actions

Source Report Issue

The ContextMenu component can be used to create a right-click menu that you can use to allow your users to interact directly with elements in a way that matches their workflow. They:

Preview
Right-click here
  • Suggestions
  • Create issue
    (GitHub)
  • Knowledge base
    Learn more
  • Switch theme
  • Settings
  • Profile
  • Preferences
  • Sign out
  • Nothing found...
How to use ContextMenu in Astro
---
import { ContextMenu, List } from 'webcoreui/astro'
---
<ContextMenu className="ctx">
<span class="muted">Right-click here</span>
<List
itemGroups={[ ... ]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Search the app..."
noResultsLabel="Nothing found..."
slot="context"
/>
</ContextMenu>
How to use ContextMenu in Svelte
<script>
import { ContextMenu, List } from 'webcoreui/svelte'
</script>
<ContextMenu>
<span class="muted">Right-click here</span>
<List
itemGroups={[ ... ]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Search the app..."
noResultsLabel="Nothing found..."
slot="context"
/>
</ContextMenu>
How to use ContextMenu in React
import React from 'react'
import { ContextMenu, List } from 'webcoreui/react'
const Context = (
<List
itemGroups={[ ... ]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Search the app..."
noResultsLabel="Nothing found..."
/>
)
const Component = () => (
<ContextMenu context={Context}>
<span class="muted">Right-click here</span>
</ContextMenu>
)
export default Component
You can read more about how to use the List component on its documentation page.

To use the ContextMenu component, you need to wrap an area of your page into a ContextMenu that ensures elements within that section opens a context menu when a right-click is triggered. You can make any element a context menu by setting slot to context.

By default, the component will create a div. You can change this using the element prop, and also style it with CSS classes using the className prop:

How to set the default element and pass CSS classes
<ContextMenu element="section" className="ctx-area">
...
</ContextMenu>

Closing Context Programmatically

The context menu is automatically closed when the user clicks outside of it. However, you may also want to close your context menu when certain actions are triggered within the context menu. To do this, you can use the closeContext function that expects a query selector that targets the context slot:

How to close a context menu programmatically
<ContextMenu>
<span class="muted">Right-click here</span>
<List
itemGroups={[ ... ]}
showSearchBar={true}
showSearchBarIcon={true}
searchBarPlaceholder="Search the app..."
noResultsLabel="Nothing found..."
wrapperClassName="ctx-menu"
slot="context"
/>
</ContextMenu>
<script>
import { closeContext, on } from 'webcoreui'
on('.ctx-menu', 'click', () => closeContext('.ctx-menu'))
</script>

API

type ContextMenuProps = {
element?: string
className?: string
[key: string]: any
}
type SvelteContextMenuProps = {
children: Snippet
context: Snippet
} & ContextMenuProps
type ReactContextMenuProps = {
Element?: React.ElementType
children: React.ReactNode
context: React.ReactNode
} & Omit<ContextMenuProps, 'element'>
PropPurpose
element Sets the element of the context area. Defaults to div.
className Pass additional CSS classes to your context area.
Svelte & React PropPurpose
context Sets the element for your context menu.

Request improvement for this page