Skip to content

Commit 978f5ea

Browse files
authored
Add primitive radix dialog (#5848)
* add primitive radix dialog * precommit * Div * set where we import from
1 parent 09c33a8 commit 978f5ea

File tree

4 files changed

+183
-3
lines changed

4 files changed

+183
-3
lines changed

pyi_hashes.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444
"reflex/components/moment/moment.pyi": "93fa4c6009390fe9400197ab52735b84",
4545
"reflex/components/plotly/plotly.pyi": "4311a0aae2abcc9226abb6a273f96372",
4646
"reflex/components/radix/__init__.pyi": "5d8e3579912473e563676bfc71f29191",
47-
"reflex/components/radix/primitives/__init__.pyi": "68fcf93acb9a40d94561d375a3a5fdb1",
47+
"reflex/components/radix/primitives/__init__.pyi": "01c388fe7a1f5426a16676404344edf6",
4848
"reflex/components/radix/primitives/accordion.pyi": "9192299dac04a5172edd7b2f6b125f53",
49-
"reflex/components/radix/primitives/base.pyi": "336f5e26669a809454bb343e698ab563",
49+
"reflex/components/radix/primitives/base.pyi": "8918eb415aabd66736db1ba680de5eb9",
50+
"reflex/components/radix/primitives/dialog.pyi": "86b6498d67471de91f0854aec02b39e8",
5051
"reflex/components/radix/primitives/drawer.pyi": "c6ad2f60217fe25952f3a1ba88fbd72a",
5152
"reflex/components/radix/primitives/form.pyi": "8a5ec180a50acdc35dfe53e4e65c20e0",
5253
"reflex/components/radix/primitives/progress.pyi": "b26c99c1d827c0f599fff344746aeec3",

reflex/components/radix/primitives/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
_SUBMOD_ATTRS: dict[str, list[str]] = {
99
"".join(k.split("components.radix.primitives.")[-1]): v
1010
for k, v in RADIX_PRIMITIVES_MAPPING.items()
11-
}
11+
} | {"dialog": ["dialog"]}
1212

1313
__getattr__, __dir__, __all__ = lazy_loader.attach(
1414
__name__,

reflex/components/radix/primitives/base.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""The base component for Radix primitives."""
22

3+
from typing import Any
4+
35
from reflex.components.component import Component
46
from reflex.components.tags.tag import Tag
57
from reflex.utils import format
@@ -24,3 +26,32 @@ def _render(self) -> Tag:
2426
class_name=f"{format.to_title_case(self.tag or '')} {self.class_name or ''}"
2527
)
2628
)
29+
30+
31+
class RadixPrimitiveTriggerComponent(RadixPrimitiveComponent):
32+
"""Base class for Trigger, Close, Cancel, and Accept components.
33+
34+
These components trigger some action in an overlay component that depends on the
35+
on_click event, and thus if a child is provided and has on_click specified, it
36+
will overtake the internal action, unless it is wrapped in some inert component,
37+
in this case, a Flex.
38+
"""
39+
40+
@classmethod
41+
def create(cls, *children: Any, **props: Any) -> Component:
42+
"""Create a new RadixPrimitiveTriggerComponent instance.
43+
44+
Args:
45+
children: The children of the component.
46+
props: The properties of the component.
47+
48+
Returns:
49+
The new RadixPrimitiveTriggerComponent instance.
50+
"""
51+
from reflex.components.el.elements.typography import Div
52+
53+
for child in children:
54+
if "on_click" in getattr(child, "event_triggers", {}):
55+
children = (Div.create(*children),)
56+
break
57+
return super().create(*children, **props)
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
"""Interactive components provided by @radix-ui/react-dialog."""
2+
3+
from typing import Any
4+
5+
from reflex.components.component import ComponentNamespace
6+
from reflex.components.el import elements
7+
from reflex.constants.compiler import MemoizationMode
8+
from reflex.event import EventHandler, no_args_event_spec, passthrough_event_spec
9+
from reflex.vars.base import Var
10+
11+
from .base import RadixPrimitiveComponent, RadixPrimitiveTriggerComponent
12+
13+
14+
class DialogElement(RadixPrimitiveComponent):
15+
"""Base class for all @radix-ui/react-dialog components."""
16+
17+
library = "@radix-ui/[email protected]"
18+
19+
20+
class DialogRoot(DialogElement):
21+
"""Root component for Dialog."""
22+
23+
tag = "Root"
24+
alias = "RadixPrimitiveDialogRoot"
25+
26+
# The controlled open state of the dialog.
27+
open: Var[bool]
28+
29+
# Fired when the open state changes.
30+
on_open_change: EventHandler[passthrough_event_spec(bool)]
31+
32+
# The open state of the dialog when it is initially rendered. Use when you do not need to control its open state.
33+
default_open: Var[bool]
34+
35+
# The modality of the dialog. When set to true, interaction with outside elements will be disabled and only dialog content will be visible to screen readers.
36+
modal: Var[bool]
37+
38+
39+
class DialogPortal(DialogElement):
40+
"""Portal component for Dialog."""
41+
42+
tag = "Portal"
43+
alias = "RadixPrimitiveDialogPortal"
44+
45+
# Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries. If used on this part, it will be inherited by Dialog.Overlay and Dialog.Content.
46+
force_mount: Var[bool]
47+
48+
# Specify a container element to portal the content into.
49+
container: Var[Any]
50+
51+
52+
class DialogOverlay(DialogElement):
53+
"""A layer that covers the inert portion of the view when the dialog is open."""
54+
55+
tag = "Overlay"
56+
alias = "RadixPrimitiveDialogOverlay"
57+
58+
# Change the default rendered element for the one passed as a child, merging their props and behavior.
59+
as_child: Var[bool]
60+
61+
# Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries. It inherits from Dialog.Portal.
62+
force_mount: Var[bool]
63+
64+
65+
class DialogTrigger(DialogElement, RadixPrimitiveTriggerComponent):
66+
"""Trigger an action or event, to open a Dialog modal."""
67+
68+
tag = "Trigger"
69+
alias = "RadixPrimitiveDialogTrigger"
70+
71+
# Change the default rendered element for the one passed as a child, merging their props and behavior.
72+
as_child: Var[bool]
73+
74+
_memoization_mode = MemoizationMode(recursive=False)
75+
76+
77+
class DialogContent(elements.Div, DialogElement):
78+
"""Content component to display inside a Dialog modal."""
79+
80+
tag = "Content"
81+
alias = "RadixPrimitiveDialogContent"
82+
83+
# Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries. It inherits from Dialog.Portal.
84+
force_mount: Var[bool]
85+
86+
# Change the default rendered element for the one passed as a child, merging their props and behavior.
87+
as_child: Var[bool]
88+
89+
# Fired when the dialog is opened.
90+
on_open_auto_focus: EventHandler[no_args_event_spec]
91+
92+
# Fired when the dialog is closed.
93+
on_close_auto_focus: EventHandler[no_args_event_spec]
94+
95+
# Fired when the escape key is pressed.
96+
on_escape_key_down: EventHandler[no_args_event_spec]
97+
98+
# Fired when the pointer is down outside the dialog.
99+
on_pointer_down_outside: EventHandler[no_args_event_spec]
100+
101+
# Fired when the pointer interacts outside the dialog.
102+
on_interact_outside: EventHandler[no_args_event_spec]
103+
104+
105+
class DialogTitle(DialogElement):
106+
"""Title component to display inside a Dialog modal."""
107+
108+
tag = "Title"
109+
alias = "RadixPrimitiveDialogTitle"
110+
111+
# Change the default rendered element for the one passed as a child, merging their props and behavior.
112+
as_child: Var[bool]
113+
114+
115+
class DialogDescription(DialogElement):
116+
"""Description component to display inside a Dialog modal."""
117+
118+
tag = "Description"
119+
alias = "RadixPrimitiveDialogDescription"
120+
121+
# Change the default rendered element for the one passed as a child, merging their props and behavior.
122+
as_child: Var[bool]
123+
124+
125+
class DialogClose(DialogElement, RadixPrimitiveTriggerComponent):
126+
"""Close button component to close an open Dialog modal."""
127+
128+
tag = "Close"
129+
alias = "RadixPrimitiveDialogClose"
130+
131+
# Change the default rendered element for the one passed as a child, merging their props and behavior.
132+
as_child: Var[bool]
133+
134+
135+
class Dialog(ComponentNamespace):
136+
"""Dialog components namespace."""
137+
138+
root = __call__ = staticmethod(DialogRoot.create)
139+
portal = staticmethod(DialogPortal.create)
140+
trigger = staticmethod(DialogTrigger.create)
141+
title = staticmethod(DialogTitle.create)
142+
overlay = staticmethod(DialogOverlay.create)
143+
content = staticmethod(DialogContent.create)
144+
description = staticmethod(DialogDescription.create)
145+
close = staticmethod(DialogClose.create)
146+
147+
148+
dialog = Dialog()

0 commit comments

Comments
 (0)