Try an interactive version of this dialog: Sign up at solve.it.com, click Upload, and pass this URL.
Please use the url to parse through the docs to get a general idea of how things work. Then compile a list of possible components. It should start with Accordion and end with Tooltip.
{
"id": "toolu_019NeTtGwzsKHc7co5RHCHk1",
"call": {
"function": "read_url",
"arguments": {
"url": "https://basecoatui.com/introduction/"
}
},
"result": "\"[ Basecoat v0.3.6 ](/)\\n\\n### Getting started\\n\\n * [ Introduction ](/introduction)\\n * [ Install<TRUNCATED>"
}
Perfect! I can see the full list of components from the documentation. Here's the complete list of Basecoat components from Accordion to Tooltip:
- Accordion
- Alert
- Alert Dialog
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Card
- Checkbox
- Command
- Combobox
- Dialog
- Dropdown Menu
- Empty
- Field
- Form
- Input
- Input Group
- Item
- Kbd
- Label
- Pagination
- Popover
- Progress
- Radio Group
- Select
- Sidebar
- Skeleton
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Theme Switcher
- Toast
- Tooltip
Would you like me to explore specific components in more detail to understand their structure and usage patterns?
{
"id": "toolu_01X2H4FZr5vUDgRvzH6bgt5j",
"call": {
"function": "read_url",
"arguments": {
"url": "https://basecoatui.com/components/button"
}
},
"result": "'[ Basecoat v0.3.6 ](/)\\n\\n### Getting started\\n\\n * [ Introduction ](/introduction)\\n * [ Install<TRUNCATED>"
}
Great! I can see how Basecoat works. The key insight is that each variant is a single class name, not multiple classes combined. For example:
btn-outline(notbtn btn-outline)btn-destructive(notbtn btn-destructive)btn-lg-destructive(combines size and variant in one class)btn-sm-icon-outline(combines size, type, and variant in one class)
This is different from frameworks like Bootstrap where you'd need multiple classes.
Would you like me to start creating Python functions for these components using fasthtml, or would you like to explore more components first to understand the patterns better?
s = Script("""
(() => {
try {
const stored = localStorage.getItem('themeMode');
if (stored ? stored === 'dark'
: matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.classList.add('dark');
}
} catch (_) {}
const apply = dark => {
document.documentElement.classList.toggle('dark', dark);
try { localStorage.setItem('themeMode', dark ? 'dark' : 'light'); } catch (_) {}
};
document.addEventListener('basecoat:theme', (event) => {
const mode = event.detail?.mode;
apply(mode === 'dark' ? true
: mode === 'light' ? false
: !document.documentElement.classList.contains('dark'));
});
})();
""")
tw_scr = Script(src='https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4')
icon_dep = Script(src="https://cdn.jsdelivr.net/npm/lit@3/dist/index.js")
franken_css = Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/franken-ui@2.0.0/dist/css/core.min.css"),
franken_js = Script(src="https://cdn.jsdelivr.net/npm/franken-ui@2.0.0/dist/js/core.iife.js"),
icons = Script(src="https://cdn.jsdelivr.net/npm/franken-ui@2.0.0/dist/js/icon.iife.js")
basecoat_link =Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/basecoat-css@0.3.6/dist/basecoat.cdn.min.css", type="text/css")
basecoat_script = Script(src="https://cdn.jsdelivr.net/npm/basecoat-css@0.3.6/dist/js/all.min.js")
hdrs = (s, tw_scr, franken_js, icons, basecoat_link, basecoat_script)
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/uvicorn/protocols/http/httptools_impl.py", line 409, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/starlette/applications.py", line 107, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.12/site-packages/starlette/middleware/errors.py", line 186, in __call__
raise exc
File "/usr/local/lib/python3.12/site-packages/starlette/middleware/errors.py", line 164, in __call__
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.12/site-packages/starlette/middleware/cors.py", line 85, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.12/site-packages/starlette/middleware/sessions.py", line 85, in __call__
await self.app(scope, receive, send_wrapper)
File "/usr/local/lib/python3.12/site-packages/starlette/middleware/exceptions.py", line 63, in __call__
await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
File "/usr/local/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/usr/local/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 716, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 736, in app
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 290, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 78, in app
await wrap_app_handling_exceptions(app, request)(scope, receive, send)
File "/usr/local/lib/python3.12/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
raise exc
File "/usr/local/lib/python3.12/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
await app(scope, receive, sender)
File "/usr/local/lib/python3.12/site-packages/starlette/routing.py", line 75, in app
response = await f(request)
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/fasthtml/core.py", line 625, in _f
if not resp: resp = await _wrap_call(f, req, sig.parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/fasthtml/core.py", line 475, in _wrap_call
return await _handle(f, **wreq)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/fasthtml/core.py", line 234, in _handle
return (await f(*args, **kwargs)) if is_async_callable(f) else await run_in_threadpool(f, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/starlette/concurrency.py", line 32, in run_in_threadpool
return await anyio.to_thread.run_sync(func)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/anyio/to_thread.py", line 56, in run_sync
return await get_async_backend().run_sync_in_worker_thread(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2485, in run_sync_in_worker_thread
return await future
^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 976, in run
result = context.run(func, *args)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/ipykernel_44/950362574.py", line 12, in index
SidebarGroup("Projects", ["Design Engineering", "Sales & Marketing", "Travel"]),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: SidebarGroup() missing 1 required positional argument: 'icon_list'
Can you explore a few different components to understand the general pattern of things, and help me figure out the best way to make a fasthtml wrapper?
I'll explore a few different components to understand the patterns. Let me look at a few more examples:
{
"id": "toolu_0123wTjufkxjJPc2W56iSiLj",
"call": {
"function": "read_url",
"arguments": {
"url": "https://basecoatui.com/components/badge"
}
},
"result": "'[ Basecoat v0.3.6 ](/)\\n\\n### Getting started\\n\\n * [ Introduction ](/introduction)\\n * [ Install<TRUNCATED>"
}
{
"id": "toolu_01AwgN4DbEecNHA6u6hqgiQT",
"call": {
"function": "read_url",
"arguments": {
"url": "https://basecoatui.com/components/card"
}
},
"result": "'[ Basecoat v0.3.6 ](/)\\n\\n### Getting started\\n\\n * [ Introduction ](/introduction)\\n * [ Install<TRUNCATED>"
}
{
"id": "toolu_01DH4Tb75am5BQoEUMcHMxkk",
"call": {
"function": "read_url",
"arguments": {
"url": "https://basecoatui.com/components/alert"
}
},
"result": "'[ Basecoat v0.3.6 ](/)\\n\\n### Getting started\\n\\n * [ Introduction ](/introduction)\\n * [ Install<TRUNCATED>"
}
Based on exploring these components, I can see some clear patterns emerging:
Pattern 1: Simple components with variants
- Button:
btn,btn-outline,btn-destructive, etc. - Badge:
badge,badge-secondary,badge-destructive,badge-outline - These use a single class name for the complete styling
Pattern 2: Structured components with semantic HTML
- Card: Uses
<div class="card">with<header>,<section>,<footer> - Alert: Uses
<div class="alert">or<div class="alert-destructive">with<svg>,<h2>,<section>
Pattern 3: Size + variant combinations
- Button can be
btn-lg-destructive(size + variant in one class)
For a FastHTML wrapper, I see two main approaches:
- Minimal wrapper: Just add the class, let users build the structure
- Helper functions: Create functions that build the complete structure
Which approach would you prefer, or would you like a combination where simple components get minimal wrappers and complex ones get helper functions?
p(
Div(
Div("TESTING", cls="card items-center w-lg mx-auto my-auto h-64"),
cls="w-full h-96 flex flex-col my-auto"
)
)
Okay that is working. I think let's try to make something and use that process to figure out the best approach. I would like to make an example page that has a sidebar on the left with lots of buttons, an icon thing at the very bottom, and then in the main page some cards.
Great! Let's start building this step by step. First, let me look at the Sidebar component to understand its structure:
{
"id": "toolu_01PWgb64LYwXZKD4spA7P4rA",
"call": {
"function": "read_url",
"arguments": {
"url": "https://basecoatui.com/components/sidebar"
}
},
"result": "'[ Basecoat v0.3.6 ](/)\\n\\n### Getting started\\n\\n * [ Introduction ](/introduction)\\n * [ Install<TRUNCATED>"
}
Perfect! Now I understand the Sidebar structure. Based on what you want to build, here's what I see:
The Sidebar has:
- An
<aside class="sidebar">wrapper - A
<nav>with sections containing navigation items - Optional
<footer>for the icon at the bottom - A
<main>for the content area
Would you like to start by creating the basic structure with just the outer containers first, or would you prefer to dive in and build a specific part (like the sidebar with buttons, or the main content with cards)?
Let's just try to build the sidebar. Please guide me through it, explaining the API I need to use.
Perfect! Let's build the sidebar step by step.
From the docs, the basic structure is:
<aside class="sidebar">
<nav>
<!-- navigation items go here -->
</nav>
<footer>
<!-- optional footer content like your icon -->
</footer>
</aside>
For the navigation items, Basecoat uses <a class="item"> elements. You can add variants like item-active for the currently selected item.
Let's start with the outer structure. Try creating an Aside with class "sidebar" that contains a Nav. What do you get?
Of course! Here's the basic structure in FastHTML style:
Aside(
Nav(
# navigation items go here
),
Footer(
# optional footer content like your icon
),
cls="sidebar"
)
For the navigation items, you'd use:
A("Dashboard", cls="item", href="#")
A("Settings", cls="item-active", href="#") # for active item
Try creating a simple sidebar with a Nav containing a few A elements with the "item" class. What does it look like?
Sidebar docs: Title: Sidebar | Basecoat
URL Source: https://basecoatui.com/components/sidebar/
Markdown Content: Usage
HTML + JavaScript
Step 1: Include the JavaScript files
You can either include the JavaScript file for all the components, or just the one for this component by adding this to the <head> of your page:
<script src="https://cdn.jsdelivr.net/npm/basecoat-css@0.3.6/dist/js/basecoat.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/npm/basecoat-css@0.3.6/dist/js/sidebar.min.js" defer></script>
Components with JavaScriptUse the CLIsidebar.js
Step 2: Add your sidebar HTML
<aside class="sidebar" data-side="left" aria-hidden="false">
<nav aria-label="Sidebar navigation">
<section class="scrollbar">
<div role="group" aria-labelledby="group-label-content-1">
<h3 id="group-label-content-1">Getting started</h3>
<ul>
<li>
<a href="#">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="m7 11 2-2-2-2" />
<path d="M11 13h4" />
<rect width="18" height="18" x="3" y="3" rx="2" ry="2" />
</svg>
<span>Playground</span>
</a>
</li>
<li>
<a href="#">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 8V4H8" />
<rect width="16" height="12" x="4" y="8" rx="2" />
<path d="M2 14h2" />
<path d="M20 14h2" />
<path d="M15 13v2" />
<path d="M9 13v2" />
</svg>
<span>Models</span>
</a>
</li>
<li>
<details id="submenu-content-1-3">
<summary aria-controls="submenu-content-1-3-content">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z" />
<circle cx="12" cy="12" r="3" />
</svg>
Settings
</summary>
<ul id="submenu-content-1-3-content">
<li>
<a href="#">
<span>General</span>
</a>
</li>
<li>
<a href="#">
<span>Team</span>
</a>
</li>
<li>
<a href="#">
<span>Billing</span>
</a>
</li>
<li>
<a href="#">
<span>Limits</span>
</a>
</li>
</ul>
</details>
</li>
</ul>
</div>
</section>
</nav>
</aside>
<main>
<button type="button" onclick="document.dispatchEvent(new CustomEvent('basecoat:sidebar'))">Toggle sidebar</button>
<h1>Content</h1>
</main>
HTML structure
<aside class="sidebar" aria-hidden="false">Wraps around the entire component. It can have the following attributes:
aria-hidden="true": controls the default state of the sidebar (hidden or visible).data-side="left": specifies the side of the sidebar (leftorright, defaults toleft).
<nav>
The navigation element that contains the sidebar's content. It can have the following attributes:
id="{BUTTON_ID}": linked to by thearia-labelledbyattribute of the listbox.aria-haspopup="menu": indicates that the button opens a menu.aria-controls="{ MENU_ID }": points to the menu's id.aria-expanded="false": tracks the popover's state.
<header>Optional The header of the sidebar.<section>The main navigation list. <div role="group">Group of options, can have a aria-labelledby attribute to link to a heading.<span role="heading">Group heading, must have an id attribute if you use the aria-labelledby attribute on the group.<ul>List of links or buttons. <li>Individual item. <a>A link. By default, clicking on a link will close the sidebar on mobile unless the data-keep-mobile-sidebar-open attribute is present.<button>A button. By default, clicking on a button will close the sidebar on mobile unless the data-keep-mobile-sidebar-open attribute is present.<details>Collapsible section. <summary>Summary of the collapsible section.<ul>List of links or buttons.<footer>Optional The footer of the sidebar.<main>A wrapper for the content of the page.<button type="button" onclick="document.dispatchEvent(new CustomEvent('basecoat:sidebar'))">A button to toggle the sidebar. If you want to use multiple sidebars you will need to add unique ids to the sidebars (i.e. <aside class="sidebar" id="{SIDEBAR_ID}">) and refer to them in the event detail (i.e. document.dispatchEvent(new CustomEvent('basecoat:sidebar', { detail: { id: '{SIDEBAR_ID}' } }));).
JavaScript events
basecoat:initializedOnce the component is fully initialized, it dispatches a custom (non-bubbling) basecoat:initialized event on itself.basecoat:sidebar
Sidebars listen for this event on document to open, close or toggle themselves. By default, the event will toggle the sidebar, but can be used to open or close if you add an action to the detail. Additionally, if you have multiple sidebars on the page, you can target a specific sidebar by adding its id to the detail:
<!-- Toggles the sidebar -->
<button type="button" onclick="document.dispatchEvent(new CustomEvent('basecoat:sidebar'));">Toggle sidebar</button>
<!-- Opens the `#main-navigation` sidebar -->
<button type="button" onclick="document.dispatchEvent(new CustomEvent('basecoat:sidebar', { detail: { id: 'main-navigation', action: 'open' } }));">Open sidebar</button>
<!-- Closes the sidebar -->
<button type="button" onclick="document.dispatchEvent(new CustomEvent('basecoat:sidebar', { detail: { action: 'close' } }));">Close sidebar</button>
Jinja and Nunjucks
You can use the sidebar() Nunjucks or Jinja macro for this component.
Use Nunjucks or Jinja macrosJinja macroNunjucks macro
{% set menu = [
{ type: "group", label: "Getting started", items: [
{ label: "Playground", url: "#" },
{ label: "Models", url: "#" },
{ label: "Settings", type: "submenu", items: [
{ label: "General", url: "#" },
{ label: "Team", url: "#" },
{ label: "Billing", url: "#" },
{ label: "Limits", url: "#" }
] }
]}
] %}
{{ sidebar(
label="Sidebar navigation",
menu=menu
) }}
<main>
<h1>Content</h1>
</main>
def UkIcon(icon:str, # Icon name from [lucide icons](https://lucide.dev/icons/)
height:int=None,
width:int=None,
stroke_width:int=None, # Thickness of lines
cls=(), # Additional classes on the `Uk_icon` tag
**kwargs # Additional args for `Uk_icon` tag
)->FT: # a lucide icon of the specified size
"Creates an icon using lucide icons"
return fh.Uk_icon(icon=icon, height=height, width=width, stroke_width=stroke_width, cls=cls, **kwargs)
Okay, seems good. I'm going to try copy exactly an output from Shadcn. To begin with I'm going to extract some key components. My current understanding is that there is a main Aside section with cls="sidebar", and then inside that we have a Nav section (always)? The nav can then have things inside it like headers and the actual content. There seems to be different ways of doing collapsable groups, etc.
def SidebarGroup(title:str, name_list:list, icon_list:list, href_list=None):
if not href_list: href_list = [f"/{o.lower().replace(" ", "-")}" for o in name_list]
links = [Li(A(IconTitle(name, icon=ico), href=href)) for name, href, ico in zip(name_list, href_list, icon_list)]
return Group(title, Ul(*links))
def ToggleButton():
return Button(
UkIcon("panel-left"),
type="button",
onclick="document.dispatchEvent(new CustomEvent('basecoat:sidebar'))",
cls="btn-ghost p-2"
)
p(ToggleButton())
@rt
def index():
return Div(
Sidebar(
Group(
"Platform",
SidebarCollapsable(IconTitle("Playground", icon="SquareTerminal"), ["History", "Starred", "Settings"]),
SidebarCollapsable(IconTitle("Models", icon="Bot"), ["Genesis", "Explorer", "Quantum"]),
SidebarCollapsable(IconTitle("Documentation", icon="BookOpen"), ["Introduction", "Get Started", "Tutorials", "Changelog"]),
SidebarCollapsable(IconTitle("Settings", icon="Settings2"), ["General", "Team", "Billing", "Limits"]),
),
SidebarGroup("Projects", ["Design Engineering", "Sales & Marketing", "Travel"], icon_list=["Frame", "PieChart", "Map"]),
),
Main(
ToggleButton(),
H1("Main Content Area"),
H3("Click Button above to open/close sidebar"),
cls="p-8"
),
cls="w-screen h-screen"
)
Context
Title: Dropdown Menu | Basecoat
URL Source: https://basecoatui.com/components/dropdown-menu/
Markdown Content:
Usage
-----
### [HTML + JavaScript](https://basecoatui.com/components/dropdown-menu/#usage-html-js)
#### [Step 1: Include the JavaScript files](https://basecoatui.com/components/dropdown-menu/#usage-html-js-1)
You can either [include the JavaScript file for all the components](https://basecoatui.com/installation/#install-cdn-all), or just the one for this component by adding this to the `<head>` of your page:
[Components with JavaScript](https://basecoatui.com/installation/#install-js)[Use the CLI](https://basecoatui.com/installation/#install-cli)[dropdown-menu.js](https://github.com/hunvreus/basecoat/blob/main/src/js/dropdown-menu.js)
#### [Step 2: Add your dropdown menu HTML](https://basecoatui.com/components/dropdown-menu/#usage-html-js-2)
HTML structure
<div class="dropdown-menu">Wraps around the entire component. <button type="button" popovertarget="{ POPOVER_ID }">
The trigger to open the popover, can also have the following attributes:
id="{BUTTON_ID}": linked to by thearia-labelledbyattribute of the listbox.aria-haspopup="menu": indicates that the button opens a menu.aria-controls="{ MENU_ID }": points to the menu's id.aria-expanded="false": tracks the popover's state.
<div popover class="popover" id="{ POPOVER_ID }">As with the Popover component, you can set up the side and alignment of the popover using the data-side and data-align attributes. <div role="menu">The menu containing the options. Should have the following attributes:
id="{ MENU_ID }": refered to by thearia-controlsattribute of the trigger.aria-labelledby="{ BUTTON_ID }": linked to by the button'sidattribute.
<button role="menuitem">Menu item.<button role="menuitemcheckbox">Menu item with a checkbox.<button role="menuitemradio">Menu item with a radio button.<hr role="separator">Optional Separator between groups/options.<div role="group">Optional Group of options, can have a aria-labelledby attribute to link to a heading.<span role="heading">Group heading, must have an id attribute if you use the aria-labelledby attribute on the group.
JavaScript events
basecoat:initializedOnce the component is fully initialized, it dispatches a custom (non-bubbling) basecoat:initialized event on itself.basecoat:popoverWhen the popover opens, the component dispatches a custom (non-bubbling) basecoat:popover event on document. Other popover components (Combobox, Dropdown Menu, Popover and Select) listen for this to close any open popovers.
Jinja and Nunjucks
You can use the dropdown_menu() Nunjucks or Jinja macro for this component.
Use Nunjucks or Jinja macrosJinja macroNunjucks macro
{% call dropdown_menu(
id="dropdown-menu",
trigger="Open",
trigger_attrs={"class": "btn-outline"},
popover_attrs={"class": "min-w-56"}
) %}
<div role="group" aria-labelledby="account-options">
<div role="heading" id="account-options">My Account</div>
<div role="menuitem">
Profile
<span class="text-muted-foreground ml-auto text-xs tracking-widest">⇧⌘P</span>
</div>
<div role="menuitem">
Billing
<span class="text-muted-foreground ml-auto text-xs tracking-widest">⌘B</span>
</div>
<div role="menuitem">
Settings
<span class="text-muted-foreground ml-auto text-xs tracking-widest">⌘S</span>
</div>
<div role="menuitem">
Keyboard shortcuts
<span class="text-muted-foreground ml-auto text-xs tracking-widest">⌘K</span>
</div>
</div>
<hr role="separator">
<div role="menuitem">
GitHub
</div>
<div role="menuitem">
Support
</div>
<div role="menuitem" disabled>
API
</div>
<hr role="separator">
<div role="menuitem">
Logout
<span class="text-muted-foreground ml-auto text-xs tracking-widest">⇧⌘P</span>
</div>
{% endcall %}
Examples
Checkboxes
Radio Group
def DropdownMenu():
return Div(
Button("Open", id="demo-dropdown-trigger", cls="btn-outline"),
Div(
Div(
Div(
Div("My Account", role="heading", id="account-options"),
Div("Profile", role="menuitem"),
role="group", aria_labelledby="account-options",
),
role="menu", aria_labelledby="demo-dropdown-trigger",
),
cls="min-w-65",
),
cls="dropdown-menu",
)
p(DropdownMenu())
Could you help give a minimal version of the dropdown menu example to help get it working?
Looking at your code, you're very close! The issue is that the popover Div needs some specific attributes. Here's the minimal structure:
def DropdownMenu():
menu_id = "demo-dropdown-menu"
return Div(
Button("Open",
id=f"{menu_id}-trigger",
aria_haspopup="menu",
aria_controls=f"{menu_id}-content",
aria_expanded="false",
cls="btn-outline"),
Div(
Div(
Div("Profile", role="menuitem"),
Div("Settings", role="menuitem"),
role="menu",
id=f"{menu_id}-content",
aria_labelledby=f"{menu_id}-trigger"
),
data_popover=True,
aria_hidden="true",
cls="min-w-56"
),
cls="dropdown-menu",
id=menu_id
)
The key missing pieces were data_popover=True and aria_hidden="true" on the popover div. Try this and see if it works!
def DropdownMenu(name:str, menu_id="demo-dropdown-menu"):
return Div(
Button(name,
id=f"{menu_id}-trigger",
aria_haspopup="menu",
aria_controls=f"{menu_id}-content",
aria_expanded="false",
cls="btn-outline"),
Div(
Div(
Div("Profile", role="menuitem"),
Div("Settings", role="menuitem"),
Hr(role="separator"),
Div("Next thing", role="menuitem"),
role="menu",
id=f"{menu_id}-content",
aria_labelledby=f"{menu_id}-trigger"
),
data_popover=True,
aria_hidden="true",
cls="min-w-56"
),
cls="dropdown-menu",
id=menu_id
)
Okay that looks good. What is the structure of groups and for sections (e.g., sectioned off by a line)?
Looking at the docs, there are two ways to organize dropdown menu items:
Groups - Use role="group" with an optional heading:
Div(
Div("My Account", role="heading", id="account-options"),
Div("Profile", role="menuitem"),
Div("Billing", role="menuitem"),
role="group",
aria_labelledby="account-options"
)
Separators - Use Hr with role="separator":
Hr(role="separator")
So to create sections separated by lines, you'd do something like:
Div("Profile", role="menuitem"),
Div("Settings", role="menuitem"),
Hr(role="separator"),
Div("Logout", role="menuitem")
Would you like to try adding some groups or separators to your dropdown menu?
@rt
def index():
return Div(
Sidebar(
Group(
"Platform",
SidebarCollapsable(IconTitle("Playground", icon="SquareTerminal"), ["History", "Starred", "Settings"]),
SidebarCollapsable(IconTitle("Models", icon="Bot"), ["Genesis", "Explorer", "Quantum"]),
SidebarCollapsable(IconTitle("Documentation", icon="BookOpen"), ["Introduction", "Get Started", "Tutorials", "Changelog"]),
SidebarCollapsable(IconTitle("Settings", icon="Settings2"), ["General", "Team", "Billing", "Limits"]),
),
SidebarGroup("Projects", ["Design Engineering", "Sales & Marketing", "Travel"], icon_list=["Frame", "PieChart", "Map"]),
),
Main(
ToggleButton(),
H1("Main Content Area"),
H3("Click Button above to open/close sidebar"),
P("Testing dropdowns:"),
DropdownMenu("Open"),
cls="p-8"
),
cls="w-screen h-screen"
)