Menu Item
<p-menu-item> | PMenuItem
Menu items provide options for the user to pick from in a menu.
<p-menu style="max-width: 200px;"> <p-menu-item>Option 1</p-menu-item> <p-menu-item>Option 2</p-menu-item> <p-menu-item>Option 3</p-menu-item> <p-divider></p-divider> <p-menu-item type="checkbox" checked>Checkbox</p-menu-item> <p-menu-item disabled>Disabled</p-menu-item> <p-divider></p-divider> <p-menu-item> Prefix Icon <p-icon slot="prefix" name="gift"></p-icon> </p-menu-item> <p-menu-item> Suffix Icon <p-icon slot="suffix" name="heart"></p-icon> </p-menu-item> </p-menu>
import PDivider from 'pure-uikit/dist/react/divider'; import PIcon from 'pure-uikit/dist/react/icon'; import PMenu from 'pure-uikit/dist/react/menu'; import PMenuItem from 'pure-uikit/dist/react/menu-item'; const App = () => ( <PMenu style={{ maxWidth: '200px' }}> <PMenuItem>Option 1</PMenuItem> <PMenuItem>Option 2</PMenuItem> <PMenuItem>Option 3</PMenuItem> <PDivider /> <PMenuItem type="checkbox" checked> Checkbox </PMenuItem> <PMenuItem disabled>Disabled</PMenuItem> <PDivider /> <PMenuItem> Prefix Icon <PIcon slot="prefix" name="gift" /> </PMenuItem> <PMenuItem> Suffix Icon <PIcon slot="suffix" name="heart" /> </PMenuItem> </PMenu> );
Examples
Prefix & Suffix
Add content to the start and end of menu items using the prefix
and suffix
slots.
<p-menu style="max-width: 200px;"> <p-menu-item> <p-icon slot="prefix" name="house"></p-icon> Home </p-menu-item> <p-menu-item> <p-icon slot="prefix" name="envelope"></p-icon> Messages <p-badge slot="suffix" variant="primary" pill>12</p-badge> </p-menu-item> <p-divider></p-divider> <p-menu-item> <p-icon slot="prefix" name="gear"></p-icon> Settings </p-menu-item> </p-menu>
import PBadge from 'pure-uikit/dist/react/badge'; import PDivider from 'pure-uikit/dist/react/divider'; import PIcon from 'pure-uikit/dist/react/icon'; import PMenu from 'pure-uikit/dist/react/menu'; import PMenuItem from 'pure-uikit/dist/react/menu-item'; const App = () => ( <PMenu style={{ maxWidth: '200px' }}> <PMenuItem> <PIcon slot="prefix" name="house" /> Home </PMenuItem> <PMenuItem> <PIcon slot="prefix" name="envelope" /> Messages <PBadge slot="suffix" variant="primary" pill> 12 </PBadge> </PMenuItem> <PDivider /> <PMenuItem> <PIcon slot="prefix" name="gear" /> Settings </PMenuItem> </PMenu> );
Disabled
Add the disabled
attribute to disable the menu item so it cannot be selected.
<p-menu style="max-width: 200px;"> <p-menu-item>Option 1</p-menu-item> <p-menu-item disabled>Option 2</p-menu-item> <p-menu-item>Option 3</p-menu-item> </p-menu>
import PMenu from 'pure-uikit/dist/react/menu'; import PMenuItem from 'pure-uikit/dist/react/menu-item'; const App = () => ( <PMenu style={{ maxWidth: '200px' }}> <PMenuItem>Option 1</PMenuItem> <PMenuItem disabled>Option 2</PMenuItem> <PMenuItem>Option 3</PMenuItem> </PMenu> );
Loading
Use the loading
attribute to indicate that a menu item is busy. Like a disabled menu item,
clicks will be suppressed until the loading state is removed.
<p-menu style="max-width: 200px;"> <p-menu-item>Option 1</p-menu-item> <p-menu-item loading>Option 2</p-menu-item> <p-menu-item>Option 3</p-menu-item> </p-menu>
import PMenu from 'pure-uikit/dist/react/menu'; import PMenuItem from 'pure-uikit/dist/react/menu-item'; const App = () => ( <PMenu style={{ maxWidth: '200px' }}> <PMenuItem>Option 1</PMenuItem> <PMenuItem loading>Option 2</PMenuItem> <PMenuItem>Option 3</PMenuItem> </PMenu> );
Checkbox Menu Items
Set the type
attribute to checkbox
to create a menu item that will toggle on and
off when selected. You can use the checked
attribute to set the initial state.
Checkbox menu items are visually indistinguishable from regular menu items. Their ability to be toggled is primarily inferred from context, much like you’d find in the menu of a native app.
<p-menu style="max-width: 200px;"> <p-menu-item type="checkbox">Autosave</p-menu-item> <p-menu-item type="checkbox" checked>Check Spelling</p-menu-item> <p-menu-item type="checkbox">Word Wrap</p-menu-item> </p-menu>
import PMenu from 'pure-uikit/dist/react/menu'; import PMenuItem from 'pure-uikit/dist/react/menu-item'; const App = () => ( <PMenu style={{ maxWidth: '200px' }}> <PMenuItem type="checkbox">Autosave</PMenuItem> <PMenuItem type="checkbox" checked> Check Spelling </PMenuItem> <PMenuItem type="checkbox">Word Wrap</PMenuItem> </PMenu> );
Value & Selection
The value
attribute can be used to assign a hidden value, such as a unique identifier, to a
menu item. When an item is selected, the p-select
event will be emitted and a reference to the
item will be available at event.detail.item
. You can use this reference to access the selected
item’s value, its checked state, and more.
<p-menu class="menu-value" style="max-width: 200px;"> <p-menu-item value="opt-1">Option 1</p-menu-item> <p-menu-item value="opt-2">Option 2</p-menu-item> <p-menu-item value="opt-3">Option 3</p-menu-item> <p-divider></p-divider> <p-menu-item type="checkbox" value="opt-4">Checkbox 4</p-menu-item> <p-menu-item type="checkbox" value="opt-5">Checkbox 5</p-menu-item> <p-menu-item type="checkbox" value="opt-6">Checkbox 6</p-menu-item> </p-menu> <script> const menu = document.querySelector('.menu-value'); menu.addEventListener('p-select', event => { const item = event.detail.item; // Log value if (item.type === 'checkbox') { console.log(`Selected value: ${item.value} (${item.checked ? 'checked' : 'unchecked'})`); } else { console.log(`Selected value: ${item.value}`); } }); </script>
import PMenu from 'pure-uikit/dist/react/menu'; import PMenuItem from 'pure-uikit/dist/react/menu-item'; const App = () => { function handleSelect(event) { const item = event.detail.item; // Toggle checked state item.checked = !item.checked; // Log value console.log(`Selected value: ${item.value}`); } return ( <PMenu style={{ maxWidth: '200px' }} onPSelect={handleSelect}> <PMenuItem value="opt-1">Option 1</PMenuItem> <PMenuItem value="opt-2">Option 2</PMenuItem> <PMenuItem value="opt-3">Option 3</PMenuItem> </PMenu> ); };
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/menu-item/menu-item.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/menu-item/menu-item.js';
To import this component using a bundler:
import 'pure-uikit/dist/components/menu-item/menu-item.js';
To import this component as a React component:
import PMenuItem from 'pure-uikit/dist/react/menu-item';
Slots
Name | Description |
---|---|
(default) | The menu item’s label. |
prefix
|
Used to prepend an icon or similar element to the menu item. |
suffix
|
Used to append an icon or similar element to the menu item. |
submenu
|
Used to denote a nested menu. |
Learn more about using slots.
Properties
Name | Description | Reflects | Type | Default |
---|---|---|---|---|
type
|
The type of menu item to render. To use checked , this value must be set to
checkbox .
|
"normal" | "checkbox"
|
"normal"
|
|
checked
|
Draws the item in a checked state. |
|
boolean
|
false
|
value
|
A unique value to store in the menu item. This can be used as a way to identify menu items when selected. |
string
|
""
|
|
loading
|
Draws the menu item in a loading state. |
|
boolean
|
false
|
disabled
|
Draws the menu item in a disabled state, preventing selection. |
|
boolean
|
false
|
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Methods
Name | Description | Arguments |
---|---|---|
getTextLabel() |
Returns a text label based on the contents of the menu item’s default slot. | - |
Learn more about methods.
Custom Properties
Name | Description | Default |
---|---|---|
--submenu-offset |
The distance submenus shift to overlap the parent menu. | -2px |
Learn more about customizing CSS custom properties.
Parts
Name | Description |
---|---|
base |
The component’s base wrapper. |
checked-icon |
The checked icon, which is only visible when the menu item is checked. |
prefix |
The prefix container. |
label |
The menu item label. |
suffix |
The suffix container. |
spinner |
The spinner that shows when the menu item is in the loading state. |
spinner__base |
The spinner’s base part. |
submenu-icon |
The submenu icon, visible only when the menu item has a submenu (not yet implemented). |
Learn more about customizing CSS parts.
Dependencies
This component automatically imports the following dependencies.
<p-icon>
<p-popup>
<p-spinner>