组件工坊:Reflex 组件库 100 例
·
课程概览
布局
·
Box
·
Flex
·
Stack
·
Grid
·
Container
·
Section
·
Center
·
Spacer
·
Separator
·
Card
·
Inset
·
Aspect Ratio
·
Fragment
排版
·
Text
·
Heading
·
Link
·
Code
·
Markdown
·
Blockquote
·
Quote
·
Strong
·
Em
·
Kbd
表单
·
Button
·
Input
·
Text Area
·
Checkbox
·
Switch
·
Radio Group
·
Select
·
Slider
·
Form
·
Upload
数据展示
·
Badge
·
Avatar
·
Icon
·
Callout
·
List
·
Data List
·
Code Block
·
Progress
·
Spinner
·
Scroll Area
·
Moment
展开与切换
·
Tabs
·
Accordion
·
Segmented Control
浮层
·
Tooltip
·
Popover
·
Hover Card
·
Dialog
·
Alert Dialog
·
Drawer
·
Dropdown Menu
·
Context Menu
·
Toast
动态渲染
·
Cond
·
Match
·
Foreach
·
Auto Scroll
媒体
·
Image
·
Audio
·
Video
表格与数据网格
·
Table
·
Data Table
·
Data Editor
图表
·
Area Chart
·
Bar Chart
·
Line Chart
·
Composed Chart
·
Pie Chart
·
Radar Chart
·
Radial Bar Chart
·
Scatter Chart
·
Funnel Chart
·
Treemap
·
Sankey Chart
·
Error Bar
图表部件
·
Axis
·
Cartesian Grid
·
Chart Tooltip
·
Legend
·
Label
·
Reference
·
Brush
其他图表库
·
Plotly
·
Pyplot
HTML 元素
·
HTML
·
HTML Text
·
HTML Layout
·
HTML Forms
·
HTML Media
·
HTML Tables
·
SVG
其他工具
·
Theme
·
Skeleton
·
Clipboard
·
Html Embed
·
Script
·
Memo
rx.context_menu.rootrx.context_menu.triggerrx.context_menu.contentrx.context_menu.itemrx.context_menu.separatorrx.context_menu.subrx.context_menu.sub_triggerrx.context_menu.sub_content官方文档
工坊订单
在文件列表中右键点击文件名时,就在原处弹出菜单,可以选择加星标、复制和删除。
上下文菜单是右键点击元素(触屏上为长按)时在鼠标位置弹出的菜单。适合把能「对这个东西」做的操作集中起来,比如文件、表格的行或图片。部件与下拉菜单相同。
context_menu.root:包裹所有部件。context_menu.trigger:包裹接收右键点击的元素。context_menu.content:打开时弹出的面板。context_menu.item:一个菜单项。用 shortcut 附上快捷键文字。context_menu.separator:菜单项之间的分隔线。context_menu.sub:包裹子菜单。里面放 context_menu.sub_trigger 和 context_menu.sub_content。rx.context_menu.root(
rx.context_menu.trigger(
rx.button("Right click me"),
),
rx.context_menu.content(
rx.context_menu.item("Edit", shortcut="⌘ E"),
rx.context_menu.item("Duplicate", shortcut="⌘ D"),
rx.context_menu.separator(),
rx.context_menu.item("Archive", shortcut="⌘ N"),
rx.context_menu.sub(
rx.context_menu.sub_trigger("More"),
rx.context_menu.sub_content(
rx.context_menu.item("Move to project…"),
rx.context_menu.item("Move to folder…"),
rx.context_menu.separator(),
rx.context_menu.item("Advanced options…"),
),
),
rx.context_menu.separator(),
rx.context_menu.item("Share"),
rx.context_menu.item("Add to favorites"),
rx.context_menu.separator(),
rx.context_menu.item("Delete", shortcut="⌘ ⌫", color="red"),
),
)右键点击虚线框试试。菜单面板的外观设置在 content 上,禁止右键打开的 disabled 设置在 trigger 上。开启 disabled 后,会弹出浏览器默认的右键菜单。
试着改属性
Right-click here
variant
size
color
high_contrast
disabled
modal
rx.context_menu.root(
rx.context_menu.trigger(
rx.center(
rx.text("Right-click here", size="2", color_scheme="gray"),
width="260px",
height="110px",
border=f"2px dashed {rx.color('gray', 7)}",
border_radius="8px",
),
disabled=False,
),
rx.context_menu.content(
rx.context_menu.item("Copy", shortcut="⌘ C"),
rx.context_menu.item("Paste", shortcut="⌘ V"),
rx.context_menu.separator(),
rx.context_menu.item("Delete", shortcut="⌘ ⌫", color_scheme="red"),
variant="solid",
size="2",
color_scheme="indigo",
high_contrast=False,
),
modal=True,
)| 部件 | 属性 | 取值 | 用途 |
|---|---|---|---|
root | modal | True / False | 打开期间阻止外部操作 |
root | on_open_change | 事件 | 打开或关闭时触发,传入新的打开状态(bool) |
trigger | disabled | True / False | 不允许右键打开 |
content | variant · size | "solid" "soft" · "1" "2" | 强调方式、尺寸 |
content | color_scheme · high_contrast | 颜色名 · bool | 菜单项的着色、对比度 |
content | loop · side · align · side_offset · align_offset | - | 键盘循环、位置微调 |
content | avoid_collisions · collision_padding · sticky · hide_when_detached | - | 屏幕边缘处理 |
item | shortcut · color_scheme · disabled · text_value | - | 快捷键文字、颜色、禁止选择、键入文字查找时使用的文本 |
item | on_select | 事件 | 选中菜单项时 |
sub | open · default_open · on_open_change | - | 子菜单的打开状态 |
sub_trigger | disabled · text_value | - | 打开子菜单的菜单项 |
sub_content | loop · side_offset · align_offset 等 | - | 子面板的位置 |
与下拉菜单不同,root 上没有 open,菜单只能通过右键点击打开。content 和 sub_content 有 on_escape_key_down、on_pointer_down_outside、on_focus_outside、on_interact_outside 事件。
context_menu.item 必须是 context_menu.content 的直接子组件。如果为了按条件切换菜单项而用 rx.vstack 之类的容器把它们包起来,菜单就识别不了这些菜单项。官方文档禁止的写法如下。
rx.context_menu.content(
rx.cond(
State.count % 2 == 0,
rx.vstack( # 不能用容器包起来
rx.context_menu.item("Even Option 1"),
rx.context_menu.item("Even Option 2"),
),
rx.vstack(
rx.context_menu.item("Odd Option A"),
rx.context_menu.item("Odd Option B"),
),
)
)如果想根据 State 切换菜单项,就把列表写成计算变量,再用 rx.foreach 直接渲染菜单项。rx.foreach 不会额外生成容器。在下面点一下 count 按钮,再右键点击看看。
Selected: none
class ContextMenuOptionsState(rx.State):
count: int = 0
selected: str = "none"
@rx.event
def increment(self):
self.count += 1
@rx.event
def set_selected(self, value: str):
self.selected = value
@rx.var
def options(self) -> list[str]:
if self.count % 2 == 0:
return ["Even Option 1", "Even Option 2", "Even Option 3"]
return ["Odd Option A", "Odd Option B", "Odd Option C"]
rx.flex(
rx.context_menu.root(
rx.context_menu.trigger(rx.button("Right click me")),
rx.context_menu.content(
rx.foreach(
ContextMenuOptionsState.options,
lambda option: rx.context_menu.item(option, on_select=ContextMenuOptionsState.set_selected(option)),
),
),
),
rx.button("count = ", ContextMenuOptionsState.count, variant="soft", on_click=ContextMenuOptionsState.increment),
rx.text("Selected: ", ContextMenuOptionsState.selected, size="2"),
spacing="3",
align="center",
wrap="wrap",
)context_menu.root 的 on_open_change 在打开时传入 True,关闭时传入 False。
Opened or closed: 0 times
Context menu open: no
class ContextMenuEventState(rx.State):
num_opens: int = 0
opened: bool = False
@rx.event
def count_opens(self, value: bool):
self.opened = value
self.num_opens += 1
rx.flex(
rx.text("Opened or closed: ", ContextMenuEventState.num_opens, " times"),
rx.text("Context menu open: ", rx.cond(ContextMenuEventState.opened, "yes", "no")),
rx.context_menu.root(
rx.context_menu.trigger(rx.button("Right click me", variant="soft")),
rx.context_menu.content(
rx.context_menu.item("Copy"),
rx.context_menu.item("Paste"),
),
on_open_change=ContextMenuEventState.count_opens,
),
direction="column",
spacing="3",
align="start",
)和下拉菜单一样,把对话框放进菜单项里,菜单关闭时它也会随之消失。应当把对话框放在菜单外面,菜单项只负责改变 State 值。每个对话框在这个值等于自己的名称时打开,关闭时由 on_open_change 清空这个值。右键点击下面的图标试试。
class ContextMenuDialogState(rx.State):
which_dialog_open: str = ""
@rx.event
def set_which_dialog_open(self, value: str):
self.which_dialog_open = value
@rx.event
def delete(self):
yield rx.toast("Deleted item")
@rx.event
def save_settings(self):
yield rx.toast("Saved settings")
rx.vstack(
rx.context_menu.root(
rx.context_menu.trigger(rx.icon_button(rx.icon("ellipsis-vertical"), variant="soft", aria_label="Right-click for menu")),
rx.context_menu.content(
rx.context_menu.item("Delete", on_click=ContextMenuDialogState.set_which_dialog_open("delete")),
rx.context_menu.item("Settings", on_click=ContextMenuDialogState.set_which_dialog_open("settings")),
),
),
rx.cond(
ContextMenuDialogState.which_dialog_open,
rx.text(ContextMenuDialogState.which_dialog_open, " dialog is open"),
),
rx.alert_dialog.root(
rx.alert_dialog.content(
rx.alert_dialog.title("Are you Sure?"),
rx.alert_dialog.description(
"This action cannot be undone. Are you sure you want to delete this item?",
margin_bottom="20px",
),
rx.hstack(
rx.alert_dialog.action(
rx.button("Delete", color_scheme="red", on_click=ContextMenuDialogState.delete),
),
rx.spacer(),
rx.alert_dialog.cancel(rx.button("Cancel")),
),
),
open=ContextMenuDialogState.which_dialog_open == "delete",
on_open_change=ContextMenuDialogState.set_which_dialog_open(""),
),
rx.dialog.root(
rx.dialog.content(
rx.dialog.title("Settings"),
rx.dialog.description("Set your settings in this settings dialog.", margin_bottom="20px"),
rx.dialog.close(
rx.button("Close", on_click=ContextMenuDialogState.save_settings),
),
),
open=ContextMenuDialogState.which_dialog_open == "settings",
on_open_change=ContextMenuDialogState.set_which_dialog_open(""),
),
align="center",
)用 rx.foreach 渲染每一行文件,每一行都包上上下文菜单。把该文件名传给菜单项的 on_select,这样一个事件处理器就能在任何一行上工作。
notes.txt
budget.xlsx
photo.png
class ContextMenuFileState(rx.State):
files: list[str] = ["notes.txt", "budget.xlsx", "photo.png"]
starred: list[str] = []
@rx.event
def toggle_star(self, name: str):
if name in self.starred:
self.starred = [f for f in self.starred if f != name]
else:
self.starred = self.starred + [name]
@rx.event
def duplicate(self, name: str):
stem, dot, ext = name.rpartition(".")
self.files = self.files + [f"{stem} copy{dot}{ext}"]
@rx.event
def remove(self, name: str):
self.files = [f for f in self.files if f != name]
self.starred = [f for f in self.starred if f != name]
@rx.event
def reset_files(self):
self.files = ["notes.txt", "budget.xlsx", "photo.png"]
self.starred = []
rx.vstack(
rx.foreach(
ContextMenuFileState.files,
lambda name: rx.context_menu.root(
rx.context_menu.trigger(
rx.flex(
rx.icon("file"),
rx.text(name),
rx.spacer(),
rx.cond(ContextMenuFileState.starred.contains(name), rx.icon("star", color=rx.color("amber", 9))),
spacing="2",
align="center",
width="100%",
padding="6px 10px",
border_radius="6px",
_hover={"background": rx.color("gray", 3)},
),
),
rx.context_menu.content(
rx.context_menu.item("Star / unstar", on_select=ContextMenuFileState.toggle_star(name)),
rx.context_menu.item("Duplicate", shortcut="⌘ D", on_select=ContextMenuFileState.duplicate(name)),
rx.context_menu.separator(),
rx.context_menu.item("Delete", shortcut="⌘ ⌫", color_scheme="red", on_select=ContextMenuFileState.remove(name)),
),
),
),
rx.button("Reset", size="1", variant="soft", on_click=ContextMenuFileState.reset_files),
width="100%",
max_width="320px",
spacing="1",
)工坊笔记
右键点击(长按)打开。部件与下拉菜单相同:root · trigger · content · item · separator · sub。
item 必须是 content 的直接子组件。动态菜单项用 rx.foreach 直接渲染。
root 上没有 open。想知道是否打开,就用 on_open_change。
trigger 设置 disabled=True 时,会弹出浏览器的默认菜单。
右键点击是看不见的功能。同样的操作也要通过按钮或下拉菜单提供。
订阅新课时和新课程通知
本课程新增课时或推出后续课程时,我们会发邮件通知你。
问答
关于本课有任何疑问都可以在这里留言。所有语言版本页面的提问会汇集在一起。