Dropdown Menu

Style-agnostic dropdown menu component.

python 7 params

Preview

open
python
dropdown_menu(label='Actions', items=[{'label': 'Edit', 'event': 'edit_item'}, {'label': 'Duplicate', 'event': 'duplicate_item'}, {'divider': True}, {'label': 'Delete', 'event': 'delete_item', 'danger': True}], open=True)

Usage

views.py
python
from djust import LiveView
from djust.decorators import event_handler
from djust.components import DropdownMenu


class MyView(LiveView):
    template_name = "my_template.html"

    def mount(self, request, **kwargs):
        self.component = DropdownMenu(label='Actions', items=[{'label': 'Edit', 'event': 'edit_item'}, {'label': 'Duplicate', 'event': 'duplicate_item'}, {'divider': True}, {'label': 'Delete', 'event': 'delete_item', 'danger': True}], open=True)

    @event_handler()
    def toggle_menu(self, **kwargs):
        self.component.open = not self.component.open

    @event_handler()
    def edit_item(self, **kwargs):
        ...  # write to self.component; the re-render carries it

    @event_handler()
    def duplicate_item(self, **kwargs):
        ...  # write to self.component; the re-render carries it

    @event_handler()
    def delete_item(self, **kwargs):
        ...  # write to self.component; the re-render carries it
my_template.html
django
{{ component }}

Events this component sends to the view: toggle_menu.

Parameters

NameTypeDefault
labelstr'Menu'
itemslist | NoneNone
openboolFalse
toggle_eventstr — renames the event; identity is `name`'toggle_menu'
alignstr'left'
custom_classstr''
**kwargs— passed to `Component.__init__`: `name=` identifies the instance in the events it sends, `id=` sets `component.id`, and any other keyword is kept as state
Source & styles
FilePathNotes
moduledjust.components.components.dropdown_menuSubclass it, or pass custom_class, to change how it renders.
cssdjust_components/components.cssDefines 7 of this component's classes at line 1857, 1858, 1859, 1860, 1862, 1864, 1866. Override those rules, or the custom properties they read, in a stylesheet loaded after it.