|
| 1 | +"""Interactive components provided by base-ui components.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any, Literal, Optional |
| 6 | + |
| 7 | +from reflex import Component |
| 8 | +from reflex.components.component import ComponentNamespace |
| 9 | +from reflex.event import EventHandler |
| 10 | +from reflex.vars import Var |
| 11 | + |
| 12 | + |
| 13 | +class BaseUIComponent(Component): |
| 14 | + """Set of content sections to be displayed one at a time.""" |
| 15 | + |
| 16 | + library = "@base-ui-components/react@^1.0.0-alpha.5" |
| 17 | + |
| 18 | + |
| 19 | +class TabsBaseUIComponent(BaseUIComponent): |
| 20 | + """Tabs component.""" |
| 21 | + |
| 22 | + tag = "Tabs" |
| 23 | + |
| 24 | + |
| 25 | +class TabsRoot(TabsBaseUIComponent): |
| 26 | + """Groups the tabs and the corresponding panels.""" |
| 27 | + |
| 28 | + tag = "Tabs.Root" |
| 29 | + |
| 30 | + # The value of the tab that should be active when initially rendered |
| 31 | + default_value: Var[Any] = 0 |
| 32 | + |
| 33 | + # The controlled value of the tab that should be active |
| 34 | + value: Var[Any] |
| 35 | + |
| 36 | + # Event handler called when the value changes |
| 37 | + on_value_change: EventHandler |
| 38 | + |
| 39 | + # The orientation of the component |
| 40 | + orientation: Var[Literal["horizontal", "vertical"]] = "horizontal" |
| 41 | + |
| 42 | + # Class name |
| 43 | + class_name: Optional[Var[str] | str] |
| 44 | + |
| 45 | + |
| 46 | +class TabsList(TabsBaseUIComponent): |
| 47 | + """Groups the individual tab buttons.""" |
| 48 | + |
| 49 | + tag = "Tabs.List" |
| 50 | + |
| 51 | + # Whether to activate tabs on focus |
| 52 | + activate_on_focus: Var[bool] = True |
| 53 | + |
| 54 | + # Whether to loop through tabs |
| 55 | + loop: Var[bool] = True |
| 56 | + |
| 57 | + # Class name |
| 58 | + class_name: Optional[Var[str] | str] = ( |
| 59 | + "bg-slate-3 inline-flex gap-1 p-1 items-center justify-start rounded-[10px] relative z-0" |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +class TabsTab(TabsBaseUIComponent): |
| 64 | + """An individual interactive tab button.""" |
| 65 | + |
| 66 | + tag = "Tabs.Tab" |
| 67 | + |
| 68 | + # The value of the tab |
| 69 | + value: Var[Any] |
| 70 | + |
| 71 | + # Optional icon component |
| 72 | + icon: Optional[Component] = None |
| 73 | + |
| 74 | + # Class name |
| 75 | + class_name: Optional[Var[str] | str] = ( |
| 76 | + "h-7 px-1.5 rounded-md justify-center items-center gap-1.5 inline-flex text-sm font-medium text-slate-9 cursor-pointer z-[1] hover:text-slate-12 transition-color" |
| 77 | + ) |
| 78 | + |
| 79 | + @classmethod |
| 80 | + def create(cls, *children, icon=None, **props) -> Component: |
| 81 | + """Create the tab component with optional icon. |
| 82 | +
|
| 83 | + Args: |
| 84 | + *children: The children of the component. |
| 85 | + icon: Optional icon component to display. |
| 86 | + **props: The properties of the component. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + The tab Component. |
| 90 | +
|
| 91 | + """ |
| 92 | + if icon: |
| 93 | + children = (icon, *children) |
| 94 | + return super().create(*children, **props) |
| 95 | + |
| 96 | + |
| 97 | +class TabsIndicator(TabsBaseUIComponent): |
| 98 | + """Visual indicator for the active tab.""" |
| 99 | + |
| 100 | + tag = "Tabs.Indicator" |
| 101 | + |
| 102 | + # Whether to render before hydration |
| 103 | + render_before_hydration: Var[bool] = True |
| 104 | + |
| 105 | + # Class name |
| 106 | + class_name: Optional[Var[str] | str] = ( |
| 107 | + "absolute top-1/2 left-0 -z-1 h-7 w-[var(--active-tab-width)] -translate-y-1/2 translate-x-[var(--active-tab-left)] rounded-md bg-slate-1 shadow-small transition-all duration-200 ease-in-out" |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +class TabsPanel(TabsBaseUIComponent): |
| 112 | + """Content panel associated with a tab.""" |
| 113 | + |
| 114 | + tag = "Tabs.Panel" |
| 115 | + |
| 116 | + # The value that associates the panel with a tab |
| 117 | + value: Var[Any] |
| 118 | + |
| 119 | + # Whether to keep the panel mounted when inactive |
| 120 | + keep_mounted: Var[bool] = False |
| 121 | + |
| 122 | + # Class name |
| 123 | + class_name: Optional[Var[str] | str] |
| 124 | + |
| 125 | + |
| 126 | +class Tabs(ComponentNamespace): |
| 127 | + """Namespace for Tab components.""" |
| 128 | + |
| 129 | + root = __call__ = staticmethod(TabsRoot.create) |
| 130 | + list = staticmethod(TabsList.create) |
| 131 | + tab = staticmethod(TabsTab.create) |
| 132 | + panel = staticmethod(TabsPanel.create) |
| 133 | + indicator = staticmethod(TabsIndicator.create) |
| 134 | + |
| 135 | + |
| 136 | +tabs = Tabs() |
0 commit comments