Dioxus Components
repository·main·Indexed 43 days ago
https://github.com/dioxuslabs/dioxus-componentsA shadcn-style component library for the Dioxus framework. It provides accessible, customizable UI components built on top of dioxus-primitives, an unstyled core library focusing on structure and ARIA accessibility. Includes a CLI for adding styled components to projects and the dioxus-attributes proc macro for creating dynamic attribute lists.
What's inside Dioxus Components
- Dioxus Primitives is a library of accessible, unstyled, and foundational components for Dioxus. Inspired by Radix Primitives, it focuses on defining the structure and behavior (including ARIA accessibility) of components without imposing any visual styles. This allows developers to implement their own design systems and CSS while benefiting from robust, accessible component logic.
Structure of the Button component
mainThe Button component is used to trigger actions or events. It renders a standard HTML
buttonelement with specific Dioxus attributes for styling and sizing.To customize the button, use the following data attributes:
data-style: Controls the visual style (e.g.,"outline").data-size: Controls the size (e.g.,"default").class: Use"dx-button"to apply the base component styles.
button { // Global html attributes class: "dx-button", "data-style": "outline", "data-size": "default", // Children {children} }Structure the Navbar component
mainThe
Navbarcomponent is built using a hierarchical structure of sub-components to create a navigation bar with collapsible menus.Navbar: The top-level wrapper for the entire menu bar.NavbarNav: Represents an individual menu. It requires anindexto determine display order.NavbarTrigger: The interactive element (e.g., a button) that opens the menu.NavbarContent: The container for the menu items that appear when the trigger is activated.NavbarItem: Individual selectable items within the menu. Each item has avalueand anon_selectcallback.
Navbar { NavbarNav { index: 0, NavbarTrigger { "Menu Label" } NavbarContent { NavbarItem { value: "item_1", on_select: |value: String| { println!("Selected: {}", value); }, } } } }How Dioxus Components and Primitives work together
mainThe library is split into two layers to provide both functionality and design flexibility:
dioxus-primitives: An unstyled core library that provides the interaction logic and accessibility features. This is the foundation for all components.- Styled Components: The
dx componentsCLI adds styled versions of components (based on shadcn styles) to your project. These are built on top of the unstyled primitives and serve as a starting point for your own design system.
This architecture allows you to use pre-styled components for speed or build your own custom design system using the unstyled primitives.
How the Tabs component structure works
mainThe
Tabscomponent manages a tabbed interface by coordinatingTabList,TabTrigger, andTabContentcomponents. The component uses avalueproperty to link triggers to their corresponding content. When aTabTriggerwith a specificvalueis activated, theTabContentwith the matchingvalueis displayed.Component Hierarchy
Tabs: The root container that wraps all triggers and content.TabList: A container withinTabsthat holds allTabTriggerelements.TabTrigger: The clickable element that switches the active tab. It requires a uniquevalueand anindexfor focus order.TabContent: The container for the content associated with a tab. Itsvaluemust match aTabTrigger'svalueto be visible.
Tabs { TabList { TabTrigger { index: 0, value: "tab1", {children} } } TabContent { index: 0, value: "tab1", {children} } }Understand the Select component structure
mainThe
Selectcomponent is composed of several nested parts that define its behavior and layout:Select<T>: The top-level container. It is generic over the typeTof the value being selected. It requires avalueand anon_value_changecallback.SelectGroup: A container used to categorize options.SelectGroupLabel: A component used to provide a title or label for aSelectGroup.SelectOption<T>: An individual selectable item within a group. It requires anindex, avalueof typeT, and the display text for the option.
Structure the Toolbar component
mainThe
Toolbarcomponent acts as a container for various toolbar items. It is designed to be accessible and supports a hierarchical structure usingToolbarButtonandToolbarSeparatorcomponents.Component Hierarchy
Toolbar: The root container. Requires anaria_labelfor accessibility.ToolbarButton: Individual interactive elements within the toolbar. Each button requires anindexto manage focus order.ToolbarSeparator: Visual or structural dividers between items.
Toolbar { aria_label: "Toolbar Title", ToolbarButton { index: 0, on_click: |_: ()| { /* action */ }, {children} } ToolbarSeparator { horizontal: true, decorative: false, } }Structure the Accordion component
mainThe
Accordioncomponent is used to display collapsible content panels. To use it correctly, you must follow a specific nesting hierarchy:Accordion: The top-level wrapper that must contain all accordion items.AccordionItem: Represents an individual collapsible section. It requires anindexproperty (starting from0) to control its placement.AccordionTrigger: The interactive element used to expand or collapse the item.AccordionContent: The container for the content that is revealed when the item is expanded.
// The accordion component must wrap all accordion items. Accordion { // Each accordion item contains both a trigger and the expanded contents of the item. AccordionItem { // Each item must have an index starting from 0 to control where the item is placed. index: 0, // The trigger is used to expand or collapse the item. AccordionTrigger {} // The content that is shown when the item is expanded. AccordionContent {} } }Structure of the Sheet component
mainThe
Sheetcomponent is a panel that slides in from the edge of the screen (Top, Right, Bottom, or Left). It is composed of several sub-components to manage its layout and lifecycle:Sheet: The root container. Use thedata-sideattribute to specify the entry edge (defaults toRight).SheetContentClose: A component used within the sheet content to handle closing.SheetHeader: A container for the title and description.SheetTitle: The heading text for the sheet.SheetDescription: Supporting text for the sheet.SheetFooter: A container for actions at the bottom of the sheet.SheetClose: A trigger that closes the sheet when interacted with.
Sheet { open: open(), // Which edge to slide in from. Available sides: Top, Right (default), Bottom, Left. "data-side": SheetSide::Right.as_str(), SheetContentClose {} SheetHeader { SheetTitle { "Edit Profile" } SheetDescription { "Make changes to your profile here." } } SheetFooter { SheetClose { "Close" } } }Understand the Sidebar component structure
mainThe
Sidebarcomponent is built using a hierarchical structure managed by aSidebarProvider. The provider handles the state for open/closed status, side placement, and collapsible modes, and it also enables the⌘/Ctrl+Bkeyboard shortcut to toggle the sidebar.Component Hierarchy
SidebarProvider: The root wrapper that supplies state and keyboard shortcuts.Sidebar: The main container. Configurable viaside,variant, andcollapsibleprops.SidebarHeader: Contains the header area, typically housing aSidebarTrigger.SidebarContent: The main scrollable area for navigation.SidebarGroup: A logical grouping of menu items.SidebarGroupLabel: A label for the group.SidebarGroupAction: An action associated with the group.SidebarGroupContent: Wraps theSidebarMenu.SidebarMenu: The list of items.SidebarMenuItem: An individual item or a container for a submenu.SidebarMenuButton: The primary interactive element. Supportsis_activestate and an optionaltooltip.SidebarMenuAction: A trailing action (e.g., an icon) that can be configured toshow_on_hover.SidebarMenuBadge: An optional badge for the item.
SidebarMenuSub: A nested menu structure.SidebarMenuSubItem: A container for submenu items.SidebarMenuSubButton: The interactive button/link within a submenu.
SidebarFooter: The bottom area of the sidebar, often used for user profiles or settings.
SidebarRail: An optional draggable resize handle placed between the rail and the main content.SidebarInset: The main content area that sits adjacent to the sidebar.
SidebarProvider { Sidebar { side: SidebarSide::Left, variant: SidebarVariant::Sidebar, collapsible: SidebarCollapsible::Offcanvas, SidebarHeader { SidebarTrigger {} } SidebarContent { SidebarGroup { SidebarGroupLabel { "Label" } SidebarGroupContent { SidebarMenu { SidebarMenuItem { SidebarMenuButton { is_active: true, tooltip: rsx!("Tooltip text"), Icon {} span { "Menu Item" } } } } } } } } SidebarRail {} SidebarInset { /* Main Content */ } }How the Menubar component structure works
mainThe
Menubarcomponent is composed of several nested sub-components that manage the hierarchy and interaction of the menu system:Menubar: The root container that wraps the entire menu bar.MenubarMenu: Represents an individual collapsible menu. It requires anindexto determine its display order.MenubarTrigger: The interactive element (e.g., a button) that users click to open the menu. It wraps the label or content of the trigger.MenubarContent: The container for the menu's dropdown items, visible only when the menu is opened.MenubarItem: An individual selectable option within the menu. It uses avaluestring and anon_selectcallback to communicate which item was chosen.
How CardHeader layout changes with CardAction
mainTheCardHeadercomponent adapts its layout based on its children. If you include aCardActioncomponent inside aCardHeader, the header automatically switches from a standard layout to a two-column grid layout to accommodate the action elements (such as buttons) alongside the title and description.