pyjs 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. pyjs/__init__.py +6 -0
  2. pyjs/__main__.py +6 -0
  3. pyjs/cli.py +816 -0
  4. pyjs/decorators.py +170 -0
  5. pyjs/docs/__init__.py +6 -0
  6. pyjs/docs/builder.py +351 -0
  7. pyjs/docs/cli.py +55 -0
  8. pyjs/docs/config.py +45 -0
  9. pyjs/docs/favicon.svg +18 -0
  10. pyjs/docs/markdown.py +61 -0
  11. pyjs/docs/models.py +38 -0
  12. pyjs/docs/navigation.py +61 -0
  13. pyjs/docs/style.css +901 -0
  14. pyjs/docs/ui.py +349 -0
  15. pyjs/nativescript/__init__.py +77 -0
  16. pyjs/scaffolding/__init__.py +418 -0
  17. pyjs/scaffolding/templates/app/README.md.template +33 -0
  18. pyjs/scaffolding/templates/app/__PYJS_SOURCE_ROOT__/__PYJS_PACKAGE__/__PYJS_PACKAGE__.py.template +81 -0
  19. pyjs/scaffolding/templates/app/__PYJS_SOURCE_ROOT__/__PYJS_PACKAGE__/__init__.py.template +0 -0
  20. pyjs/scaffolding/templates/app/__PYJS_SOURCE_ROOT__/__PYJS_PACKAGE__/__main__.py.template +5 -0
  21. pyjs/scaffolding/templates/app/tests/test_app.py.template +22 -0
  22. pyjs/scaffolding/templates/app/tests/test_browser_app.py.template +19 -0
  23. pyjs/scaffolding/templates/app_test.py.template +8 -0
  24. pyjs/server.py +468 -0
  25. pyjs/skills/pyjs/SKILL.md +161 -0
  26. pyjs/state.py +101 -0
  27. pyjs/testing/__init__.py +3 -0
  28. pyjs/testing/android.py +248 -0
  29. pyjs/testing/artifacts.py +37 -0
  30. pyjs/testing/assertion_rewriter.py +125 -0
  31. pyjs/testing/builder.py +191 -0
  32. pyjs/testing/capture.py +133 -0
  33. pyjs/testing/client/__init__.py +1 -0
  34. pyjs/testing/client/app.py +718 -0
  35. pyjs/testing/client/assertions.py +49 -0
  36. pyjs/testing/client/runtime.py +123 -0
  37. pyjs/testing/client/sidebar.py +377 -0
  38. pyjs/testing/client/state.py +107 -0
  39. pyjs/testing/client/viewer.py +361 -0
  40. pyjs/testing/daemon.py +641 -0
  41. pyjs/testing/helpers.py +155 -0
  42. pyjs/testing/protocol.py +56 -0
  43. pyjs/testing/pytest_plugin.py +797 -0
  44. pyjs/testing/web.py +97 -0
  45. pyjs/testing/webgpu.py +27 -0
  46. pyjs/tooling/__init__.py +4 -0
  47. pyjs/tooling/android.py +138 -0
  48. pyjs/tooling/browser.py +676 -0
  49. pyjs/tooling/command.py +75 -0
  50. pyjs/tooling/nativescript.py +156 -0
  51. pyjs/tooling/node.py +170 -0
  52. pyjs/tooling/perf.py +163 -0
  53. pyjs/transpiler/__init__.py +23 -0
  54. pyjs/transpiler/_builtins.py +485 -0
  55. pyjs/transpiler/analyzer.py +1495 -0
  56. pyjs/transpiler/objects.py +1015 -0
  57. pyjs/transpiler/source.py +189 -0
  58. pyjs/transpiler/transpiler.py +748 -0
  59. pyjs/transpiler/utils.py +150 -0
  60. pyjs/web/__init__.py +5 -0
  61. pyjs/web/console.py +16 -0
  62. pyjs/web/dom.py +1529 -0
  63. pyjs/web/domx.py +474 -0
  64. pyjs/web/dragdrop.py +102 -0
  65. pyjs/web/events.py +1306 -0
  66. pyjs/web/files.py +203 -0
  67. pyjs/web/geometry.py +475 -0
  68. pyjs/web/html.py +927 -0
  69. pyjs/web/indexeddb.py +410 -0
  70. pyjs/web/javascript.py +112 -0
  71. pyjs/web/network.py +193 -0
  72. pyjs/web/ssr.py +70 -0
  73. pyjs/web/streams.py +17 -0
  74. pyjs/web/svg.py +684 -0
  75. pyjs/web/timers.py +29 -0
  76. pyjs/web/ui/__init__.py +18 -0
  77. pyjs/web/ui/accordion.py +166 -0
  78. pyjs/web/ui/alert.py +52 -0
  79. pyjs/web/ui/alert_dialog.py +102 -0
  80. pyjs/web/ui/aspect_ratio.py +12 -0
  81. pyjs/web/ui/attachment.py +80 -0
  82. pyjs/web/ui/avatar.py +105 -0
  83. pyjs/web/ui/badge.py +48 -0
  84. pyjs/web/ui/breadcrumb.py +85 -0
  85. pyjs/web/ui/bubble.py +38 -0
  86. pyjs/web/ui/button.py +86 -0
  87. pyjs/web/ui/calendar.py +124 -0
  88. pyjs/web/ui/card.py +58 -0
  89. pyjs/web/ui/carousel.py +99 -0
  90. pyjs/web/ui/chart.py +56 -0
  91. pyjs/web/ui/checkbox.py +31 -0
  92. pyjs/web/ui/collapsible.py +71 -0
  93. pyjs/web/ui/combobox.py +36 -0
  94. pyjs/web/ui/command.py +118 -0
  95. pyjs/web/ui/context_menu.py +65 -0
  96. pyjs/web/ui/date_picker.py +24 -0
  97. pyjs/web/ui/dialog.py +289 -0
  98. pyjs/web/ui/direction.py +9 -0
  99. pyjs/web/ui/drawer.py +72 -0
  100. pyjs/web/ui/dropdown_menu.py +239 -0
  101. pyjs/web/ui/empty.py +69 -0
  102. pyjs/web/ui/field.py +96 -0
  103. pyjs/web/ui/floating.py +90 -0
  104. pyjs/web/ui/hover_card.py +128 -0
  105. pyjs/web/ui/input.py +48 -0
  106. pyjs/web/ui/input_group.py +77 -0
  107. pyjs/web/ui/input_otp.py +76 -0
  108. pyjs/web/ui/item.py +78 -0
  109. pyjs/web/ui/kbd.py +26 -0
  110. pyjs/web/ui/label.py +14 -0
  111. pyjs/web/ui/marker.py +29 -0
  112. pyjs/web/ui/menubar.py +52 -0
  113. pyjs/web/ui/message.py +57 -0
  114. pyjs/web/ui/message_scroller.py +45 -0
  115. pyjs/web/ui/native_select.py +47 -0
  116. pyjs/web/ui/navigation_menu.py +110 -0
  117. pyjs/web/ui/pagination.py +63 -0
  118. pyjs/web/ui/popover.py +217 -0
  119. pyjs/web/ui/progress.py +44 -0
  120. pyjs/web/ui/radio_group.py +129 -0
  121. pyjs/web/ui/resizable.py +194 -0
  122. pyjs/web/ui/scroll_area.py +29 -0
  123. pyjs/web/ui/select.py +83 -0
  124. pyjs/web/ui/separator.py +24 -0
  125. pyjs/web/ui/sheet.py +77 -0
  126. pyjs/web/ui/sidebar.py +162 -0
  127. pyjs/web/ui/skeleton.py +15 -0
  128. pyjs/web/ui/slider.py +46 -0
  129. pyjs/web/ui/sonner.py +70 -0
  130. pyjs/web/ui/spinner.py +14 -0
  131. pyjs/web/ui/switch.py +35 -0
  132. pyjs/web/ui/table.py +84 -0
  133. pyjs/web/ui/tabs.py +167 -0
  134. pyjs/web/ui/text.py +15 -0
  135. pyjs/web/ui/textarea.py +28 -0
  136. pyjs/web/ui/toggle.py +109 -0
  137. pyjs/web/ui/tooltip.py +164 -0
  138. pyjs/web/ui/typography.py +79 -0
  139. pyjs/web/url.py +76 -0
  140. pyjs/web/webgpu.py +496 -0
  141. pyjs-0.0.1.dist-info/METADATA +441 -0
  142. pyjs-0.0.1.dist-info/RECORD +146 -0
  143. pyjs-0.0.1.dist-info/WHEEL +5 -0
  144. pyjs-0.0.1.dist-info/entry_points.txt +5 -0
  145. pyjs-0.0.1.dist-info/licenses/LICENSE +28 -0
  146. pyjs-0.0.1.dist-info/top_level.txt +1 -0
pyjs/web/ui/popover.py ADDED
@@ -0,0 +1,217 @@
1
+ from pyjs import js
2
+ from pyjs.web.dom import Element, document
3
+ from pyjs.web.domx import ProxyElement, button, ref, tag, tw
4
+ from pyjs.web.events import Event, KeyboardEvent
5
+ from pyjs.web.ui import Widget
6
+ from pyjs.web.ui.floating import FloatingPanel, FloatingPosition
7
+
8
+
9
+ @js
10
+ class PopoverTrigger(ProxyElement):
11
+ def __init__(self, *children):
12
+ self.element = button(
13
+ tw(
14
+ "inline-flex h-9 items-center justify-center rounded-md border border-slate-300 "
15
+ "bg-white px-4 text-sm font-medium text-slate-950 shadow-sm transition-colors "
16
+ "hover:bg-slate-100 focus-visible:outline-none focus-visible:ring-2 "
17
+ "focus-visible:ring-slate-400"
18
+ ),
19
+ {
20
+ "aria-expanded": "false",
21
+ "aria-haspopup": "dialog",
22
+ "data-slot": "popover-trigger",
23
+ "type": "button",
24
+ },
25
+ *children,
26
+ )
27
+
28
+ def set_expanded(self, expanded: bool):
29
+ self.element.setAttribute("aria-expanded", "true" if expanded else "false")
30
+ self.element.setAttribute("data-state", "open" if expanded else "closed")
31
+
32
+
33
+ @js
34
+ class PopoverClose(ProxyElement):
35
+ def __init__(self, *children):
36
+ self.element = button(
37
+ tw(
38
+ "inline-flex h-9 items-center justify-center rounded-md border border-slate-300 "
39
+ "bg-white px-3 text-sm font-medium text-slate-950 shadow-sm hover:bg-slate-100"
40
+ ),
41
+ {"data-slot": "popover-close", "type": "button"},
42
+ *children,
43
+ )
44
+
45
+
46
+ @js
47
+ def PopoverHeader(*children):
48
+ return tag(
49
+ "div",
50
+ tw("grid gap-1.5"),
51
+ {"data-slot": "popover-header"},
52
+ *children,
53
+ )
54
+
55
+
56
+ @js
57
+ def PopoverTitle(*children):
58
+ return tag(
59
+ "h3",
60
+ tw("font-medium leading-none text-slate-950"),
61
+ {"data-slot": "popover-title"},
62
+ *children,
63
+ )
64
+
65
+
66
+ @js
67
+ def PopoverDescription(*children):
68
+ return tag(
69
+ "p",
70
+ tw("text-sm leading-5 text-slate-500"),
71
+ {"data-slot": "popover-description"},
72
+ *children,
73
+ )
74
+
75
+
76
+ @js
77
+ class PopoverContent(FloatingPanel):
78
+ def __init__(self, *children, label: str = "Popover"):
79
+ super().__init__(
80
+ tw(
81
+ "w-72 rounded-md border border-slate-200 bg-white p-4 text-slate-950 "
82
+ "shadow-md outline-none"
83
+ ),
84
+ {
85
+ "aria-label": label,
86
+ "data-slot": "popover-content",
87
+ "role": "dialog",
88
+ },
89
+ *children,
90
+ )
91
+ self.style.width = "288px"
92
+
93
+
94
+ @js
95
+ class PopoverClickListener:
96
+ def __init__(self, popover: Popover):
97
+ self.popover = popover
98
+
99
+ @js(include=True)
100
+ def handleEvent(self, event: Event):
101
+ self.popover.handle_document_click(event)
102
+
103
+
104
+ @js
105
+ class PopoverKeyListener:
106
+ def __init__(self, popover: Popover):
107
+ self.popover = popover
108
+
109
+ @js(include=True)
110
+ def handleEvent(self, event: KeyboardEvent):
111
+ self.popover.handle_keydown(event)
112
+
113
+
114
+ @js
115
+ class Popover(Widget):
116
+ def __init__(
117
+ self,
118
+ trigger: PopoverTrigger,
119
+ content: PopoverContent,
120
+ *,
121
+ side: str = "bottom",
122
+ align: str = "center",
123
+ side_offset: int = 4,
124
+ align_offset: int = 0,
125
+ ):
126
+ super().__init__()
127
+ self.trigger = ref(trigger)
128
+ self.content = ref(content)
129
+ self.setAttribute("data-side", side)
130
+ self.setAttribute("data-align", align)
131
+ self.setAttribute("data-side-offset", str(side_offset))
132
+ self.setAttribute("data-align-offset", str(align_offset))
133
+
134
+ self.trigger.element.addEventListener("click", self.toggle)
135
+ self.content.addEventListener("click", self.handle_content_click)
136
+ self.add(
137
+ tw("inline-block"),
138
+ {"data-slot": "popover", "data-state": "closed"},
139
+ self.trigger,
140
+ self.content,
141
+ )
142
+
143
+ def initialize(self):
144
+ self.is_open = False
145
+ self.click_listener = PopoverClickListener(self)
146
+ self.key_listener = PopoverKeyListener(self)
147
+
148
+ def open(self) -> FloatingPosition:
149
+ side = self.getAttribute("data-side")
150
+ align = self.getAttribute("data-align")
151
+ side_offset = self.getAttribute("data-side-offset")
152
+ align_offset = self.getAttribute("data-align-offset")
153
+ resolved_side = "bottom"
154
+ resolved_align = "center"
155
+ resolved_side_offset = 4
156
+ resolved_align_offset = 0
157
+ if side is not None:
158
+ resolved_side = side
159
+ if align is not None:
160
+ resolved_align = align
161
+ if side_offset is not None:
162
+ resolved_side_offset = int(side_offset)
163
+ if align_offset is not None:
164
+ resolved_align_offset = int(align_offset)
165
+ position = self.content.open(
166
+ self.trigger.element,
167
+ side=resolved_side,
168
+ align=resolved_align,
169
+ side_offset=resolved_side_offset,
170
+ align_offset=resolved_align_offset,
171
+ )
172
+ self.is_open = True
173
+ self.setAttribute("data-state", "open")
174
+ self.trigger.set_expanded(True)
175
+ document.addEventListener("click", self.click_listener)
176
+ document.addEventListener("keydown", self.key_listener)
177
+ return position
178
+
179
+ def close(self, restore_focus: bool = False):
180
+ if not self.is_open:
181
+ return
182
+ document.removeEventListener("click", self.click_listener)
183
+ document.removeEventListener("keydown", self.key_listener)
184
+ self.content.close()
185
+ self.is_open = False
186
+ self.setAttribute("data-state", "closed")
187
+ self.trigger.set_expanded(False)
188
+ if restore_focus:
189
+ self.trigger.element.focus()
190
+
191
+ def toggle(self, event: Event):
192
+ if self.is_open:
193
+ self.close()
194
+ else:
195
+ self.open()
196
+
197
+ def handle_content_click(self, event: Event):
198
+ target: Element = event.target
199
+ if target.closest("[data-slot=popover-close]") is not None:
200
+ self.close(restore_focus=True)
201
+
202
+ def handle_document_click(self, event: Event):
203
+ if not self.is_open:
204
+ return
205
+ target: Element = event.target
206
+ if self.contains(target) or self.content.contains(target):
207
+ return
208
+ self.close()
209
+
210
+ def handle_keydown(self, event: KeyboardEvent):
211
+ if event.key == "Escape" and self.is_open:
212
+ event.preventDefault()
213
+ self.close(restore_focus=True)
214
+
215
+ @js(include=True)
216
+ def disconnectedCallback(self):
217
+ self.close()
@@ -0,0 +1,44 @@
1
+ from pyjs import js
2
+ from pyjs.web.domx import ref, tag, tw
3
+ from pyjs.web.ui import Widget
4
+
5
+
6
+ @js
7
+ class ProgressBar(Widget):
8
+ def __init__(self, value: int = 0, label: str = "Progress"):
9
+ super().__init__()
10
+ self.indicator = ref(
11
+ tag(
12
+ "div",
13
+ tw("h-full bg-slate-950 transition-all"),
14
+ {"data-slot": "progress-indicator"},
15
+ )
16
+ )
17
+ self.add(
18
+ tw("relative h-2 w-full overflow-hidden rounded-full bg-slate-200"),
19
+ {
20
+ "aria-label": label,
21
+ "aria-valuemax": "100",
22
+ "aria-valuemin": "0",
23
+ "data-slot": "progress",
24
+ "role": "progressbar",
25
+ },
26
+ self.indicator,
27
+ )
28
+ self.set_value(value)
29
+
30
+ def set_value(self, value: int):
31
+ if value < 0:
32
+ value = 0
33
+ elif value > 100:
34
+ value = 100
35
+ value_text = str(value)
36
+ self.setAttribute("aria-valuenow", value_text)
37
+ self.setAttribute("value", value_text)
38
+ self.indicator.setAttribute("style", "width: " + value_text + "%")
39
+
40
+ def current_value(self) -> int:
41
+ value = self.getAttribute("value")
42
+ if value is None:
43
+ return 0
44
+ return int(value)
@@ -0,0 +1,129 @@
1
+ from pyjs import js
2
+ from pyjs.web.dom import Element
3
+ from pyjs.web.domx import input, ref, tag, tw
4
+ from pyjs.web.events import Event
5
+ from pyjs.web.ui import Widget
6
+
7
+
8
+ @js
9
+ class RadioGroupItem(Widget):
10
+ def __init__(self, value: str, *children, checked: bool = False):
11
+ super().__init__()
12
+ self.control = ref(
13
+ input(
14
+ tw(
15
+ "size-4 shrink-0 cursor-pointer accent-slate-950 focus-visible:outline-none "
16
+ "focus-visible:ring-2 focus-visible:ring-slate-400 disabled:opacity-50"
17
+ ),
18
+ {
19
+ "data-slot": "radio-group-control",
20
+ "type": "radio",
21
+ "value": value,
22
+ },
23
+ )
24
+ )
25
+ self.add(
26
+ tw("block"),
27
+ {
28
+ "data-slot": "radio-group-item",
29
+ "data-state": "checked" if checked else "unchecked",
30
+ "data-value": value,
31
+ },
32
+ tag(
33
+ "label",
34
+ tw("flex cursor-pointer items-center gap-2 text-sm text-slate-950"),
35
+ self.control,
36
+ *children,
37
+ ),
38
+ )
39
+ self.set_checked(checked)
40
+
41
+ def get_value(self) -> str:
42
+ value = self.getAttribute("data-value")
43
+ if value is None:
44
+ return ""
45
+ return value
46
+
47
+ def is_checked(self) -> bool:
48
+ return self.control.checked
49
+
50
+ def set_checked(self, checked: bool):
51
+ self.control.checked = checked
52
+ self.setAttribute("data-state", "checked" if checked else "unchecked")
53
+ if checked:
54
+ self.control.setAttribute("checked", "")
55
+ else:
56
+ self.control.removeAttribute("checked")
57
+
58
+ def set_name(self, name: str):
59
+ self.control.setAttribute("name", name)
60
+
61
+
62
+ @js
63
+ class RadioGroup(Widget):
64
+ def __init__(
65
+ self,
66
+ *items: RadioGroupItem,
67
+ value: str = "",
68
+ name: str = "",
69
+ orientation: str = "vertical",
70
+ ):
71
+ super().__init__()
72
+ values: list[str] = []
73
+ selected_value = value
74
+ group_name = name
75
+ if group_name == "":
76
+ group_name = self.get_component_id()
77
+ for item in items:
78
+ item_value = item.get_value()
79
+ for existing in values:
80
+ if existing == item_value:
81
+ raise ValueError(
82
+ "RadioGroupItem values must be unique within a RadioGroup"
83
+ )
84
+ values.append(item_value)
85
+ item.set_name(group_name)
86
+ if value == "" and item.is_checked():
87
+ selected_value = item_value
88
+ elif value != "":
89
+ item.set_checked(item_value == value)
90
+ self.add(
91
+ tw(
92
+ "grid gap-3 data-[orientation=horizontal]:flex "
93
+ "data-[orientation=horizontal]:items-center"
94
+ ),
95
+ {
96
+ "aria-orientation": orientation,
97
+ "data-orientation": orientation,
98
+ "data-slot": "radio-group",
99
+ "data-value": selected_value,
100
+ "role": "radiogroup",
101
+ },
102
+ *items,
103
+ )
104
+ self.addEventListener("change", self.changed)
105
+
106
+ def get_items(self) -> list[RadioGroupItem]:
107
+ items: list[RadioGroupItem] = self.children
108
+ return items
109
+
110
+ def current_value(self) -> str:
111
+ value = self.getAttribute("data-value")
112
+ if value is None:
113
+ return ""
114
+ return value
115
+
116
+ def select(self, value: str, focus: bool = False):
117
+ for item in self.get_items():
118
+ checked = item.get_value() == value
119
+ item.set_checked(checked)
120
+ if checked and focus:
121
+ item.control.focus()
122
+ self.setAttribute("data-value", value)
123
+
124
+ def changed(self, event: Event):
125
+ target: Element = event.target
126
+ for item in self.get_items():
127
+ if item.control.contains(target):
128
+ self.select(item.get_value())
129
+ return
@@ -0,0 +1,194 @@
1
+ from pyjs import js
2
+ from pyjs.web.dom import Element, window
3
+ from pyjs.web.domx import ProxyElement, div, tw
4
+ from pyjs.web.events import Event, KeyboardEvent, PointerEvent
5
+ from pyjs.web.ui import Widget
6
+
7
+
8
+ @js
9
+ class ResizablePanel(ProxyElement):
10
+ def __init__(self, *children, initial_size: int = 50):
11
+ self.element = div(
12
+ tw("min-h-0 min-w-0 overflow-auto"),
13
+ {
14
+ "data-size": str(initial_size),
15
+ "data-slot": "resizable-panel",
16
+ "style": "flex-basis: " + str(initial_size) + "%",
17
+ },
18
+ *children,
19
+ )
20
+
21
+ def current_size(self) -> float:
22
+ value = self.getAttribute("data-size")
23
+ if value is None:
24
+ return 0.0
25
+ return float(value)
26
+
27
+ def set_size(self, size: float):
28
+ self.setAttribute("data-size", str(size))
29
+ self.setAttribute("style", "flex-basis: " + str(size) + "%")
30
+
31
+
32
+ @js
33
+ class ResizableHandle(ProxyElement):
34
+ def __init__(self, with_handle: bool = False):
35
+ self.element = div(
36
+ tw(
37
+ "relative flex w-1 shrink-0 cursor-col-resize items-center justify-center "
38
+ "bg-slate-200 outline-none focus-visible:ring-2 focus-visible:ring-slate-400 "
39
+ "data-[direction=vertical]:h-1 data-[direction=vertical]:w-full "
40
+ "data-[direction=vertical]:cursor-row-resize"
41
+ ),
42
+ {
43
+ "aria-label": "Resize panels",
44
+ "aria-valuemax": "90",
45
+ "aria-valuemin": "10",
46
+ "data-slot": "resizable-handle",
47
+ "role": "separator",
48
+ "tabindex": "0",
49
+ },
50
+ "⋮" if with_handle else "",
51
+ )
52
+
53
+
54
+ @js
55
+ class ResizableMoveListener:
56
+ def __init__(self, group: ResizablePanelGroup):
57
+ self.group = group
58
+
59
+ @js(include=True)
60
+ def handleEvent(self, event: PointerEvent):
61
+ self.group.pointer_moved(event)
62
+
63
+
64
+ @js
65
+ class ResizableUpListener:
66
+ def __init__(self, group: ResizablePanelGroup):
67
+ self.group = group
68
+
69
+ @js(include=True)
70
+ def handleEvent(self, event: PointerEvent):
71
+ self.group.pointer_released(event)
72
+
73
+
74
+ @js
75
+ class ResizablePanelGroup(Widget):
76
+ def __init__(
77
+ self,
78
+ *children: ProxyElement,
79
+ direction: str = "horizontal",
80
+ ):
81
+ super().__init__()
82
+ self.panels: list[ResizablePanel] = []
83
+ self.handles: list[ResizableHandle] = []
84
+ for child in children:
85
+ if isinstance(child, ResizablePanel):
86
+ self.panels.append(child)
87
+ if isinstance(child, ResizableHandle):
88
+ child.setAttribute("data-direction", direction)
89
+ self.handles.append(child)
90
+ self.drag_index = -1
91
+ self.drag_start = 0
92
+ self.before_start = 0.0
93
+ self.after_start = 0.0
94
+ self.move_listener = ResizableMoveListener(self)
95
+ self.up_listener = ResizableUpListener(self)
96
+ self.add(
97
+ tw(
98
+ "flex min-h-48 w-full overflow-hidden rounded-lg border border-slate-200 "
99
+ "data-[direction=vertical]:flex-col"
100
+ ),
101
+ {
102
+ "data-direction": direction,
103
+ "data-slot": "resizable-panel-group",
104
+ },
105
+ *children,
106
+ )
107
+ self.addEventListener("keydown", self.keydown)
108
+ self.addEventListener("pointerdown", self.pointer_pressed)
109
+ self.update_handle_values()
110
+
111
+ def is_vertical(self) -> bool:
112
+ return self.getAttribute("data-direction") == "vertical"
113
+
114
+ def resize_pair(self, index: int, before_size: float, after_size: float):
115
+ if before_size < 10:
116
+ before_size = 10
117
+ after_size = 90
118
+ elif after_size < 10:
119
+ before_size = 90
120
+ after_size = 10
121
+ self.panels[index].set_size(before_size)
122
+ self.panels[index + 1].set_size(after_size)
123
+ self.update_handle_values()
124
+
125
+ def update_handle_values(self):
126
+ for index in range(len(self.handles)):
127
+ self.handles[index].setAttribute(
128
+ "aria-valuenow",
129
+ str(round(self.panels[index].current_size())),
130
+ )
131
+
132
+ def handle_index(self, target: Element) -> int:
133
+ for index in range(len(self.handles)):
134
+ if self.handles[index].element is target:
135
+ return index
136
+ return -1
137
+
138
+ def keydown(self, event: KeyboardEvent):
139
+ target: Element = event.target
140
+ handle = target.closest("[data-slot=resizable-handle]")
141
+ if handle is None:
142
+ return
143
+ delta = 0
144
+ if event.key == "ArrowLeft" or event.key == "ArrowUp":
145
+ delta = -2
146
+ elif event.key == "ArrowRight" or event.key == "ArrowDown":
147
+ delta = 2
148
+ if delta == 0:
149
+ return
150
+ index = self.handle_index(handle)
151
+ if index < 0:
152
+ return
153
+ event.preventDefault()
154
+ self.resize_pair(
155
+ index,
156
+ self.panels[index].current_size() + delta,
157
+ self.panels[index + 1].current_size() - delta,
158
+ )
159
+
160
+ def pointer_pressed(self, event: PointerEvent):
161
+ target: Element = event.target
162
+ handle = target.closest("[data-slot=resizable-handle]")
163
+ if handle is None:
164
+ return
165
+ index = self.handle_index(handle)
166
+ if index < 0:
167
+ return
168
+ event.preventDefault()
169
+ self.drag_index = index
170
+ self.drag_start = event.clientY if self.is_vertical() else event.clientX
171
+ self.before_start = self.panels[index].current_size()
172
+ self.after_start = self.panels[index + 1].current_size()
173
+ window.addEventListener("pointermove", self.move_listener)
174
+ window.addEventListener("pointerup", self.up_listener)
175
+
176
+ def pointer_moved(self, event: PointerEvent):
177
+ if self.drag_index < 0:
178
+ return
179
+ bounds = self.getBoundingClientRect()
180
+ current = event.clientY if self.is_vertical() else event.clientX
181
+ total = bounds.height if self.is_vertical() else bounds.width
182
+ if total <= 0:
183
+ return
184
+ delta = (current - self.drag_start) / total * 100
185
+ self.resize_pair(
186
+ self.drag_index,
187
+ self.before_start + delta,
188
+ self.after_start - delta,
189
+ )
190
+
191
+ def pointer_released(self, event: Event):
192
+ window.removeEventListener("pointermove", self.move_listener)
193
+ window.removeEventListener("pointerup", self.up_listener)
194
+ self.drag_index = -1
@@ -0,0 +1,29 @@
1
+ from pyjs import js
2
+ from pyjs.web.domx import div, tw
3
+
4
+
5
+ @js
6
+ def ScrollArea(*children, height: str = "12rem"):
7
+ return div(
8
+ tw(
9
+ "relative overflow-auto rounded-md focus-visible:outline-none focus-visible:ring-2"
10
+ ),
11
+ {
12
+ "data-slot": "scroll-area",
13
+ "style": "max-height: " + height,
14
+ "tabindex": "0",
15
+ },
16
+ *children,
17
+ )
18
+
19
+
20
+ @js
21
+ def ScrollBar(orientation: str = "vertical"):
22
+ return div(
23
+ tw("pointer-events-none absolute opacity-0"),
24
+ {
25
+ "aria-hidden": "true",
26
+ "data-orientation": orientation,
27
+ "data-slot": "scroll-bar",
28
+ },
29
+ )
pyjs/web/ui/select.py ADDED
@@ -0,0 +1,83 @@
1
+ from pyjs import js
2
+ from pyjs.web.domx import span, tw
3
+ from pyjs.web.ui.dropdown_menu import (
4
+ DropdownMenu,
5
+ DropdownMenuContent,
6
+ DropdownMenuItem,
7
+ DropdownMenuLabel,
8
+ DropdownMenuSeparator,
9
+ DropdownMenuTrigger,
10
+ )
11
+
12
+
13
+ @js
14
+ class SelectTrigger(DropdownMenuTrigger):
15
+ def __init__(self, placeholder: str = "Select an option"):
16
+ super().__init__(
17
+ span({"data-slot": "select-value"}, placeholder),
18
+ span(tw("ml-2 text-xs text-slate-500"), {"aria-hidden": "true"}, "▼"),
19
+ )
20
+ self.setAttribute("data-slot", "select-trigger")
21
+
22
+ def set_value_text(self, value: str):
23
+ value_element = self.element.querySelector("[data-slot=select-value]")
24
+ if value_element is None:
25
+ raise Exception("SelectTrigger requires an element with data-slot=select-value")
26
+ value_element.textContent = value
27
+
28
+
29
+ @js
30
+ class SelectItem(DropdownMenuItem):
31
+ def __init__(self, value: str, label: str, *children):
32
+ super().__init__(value, label, *children)
33
+ self.setAttribute("data-slot", "select-item")
34
+ self.setAttribute("data-label", label)
35
+
36
+
37
+ @js
38
+ class SelectContent(DropdownMenuContent):
39
+ def __init__(self, *children):
40
+ super().__init__(*children)
41
+ self.setAttribute("data-slot", "select-content")
42
+
43
+
44
+ @js
45
+ def SelectLabel(*children):
46
+ label = DropdownMenuLabel(*children)
47
+ label.setAttribute("data-slot", "select-label")
48
+ return label
49
+
50
+
51
+ @js
52
+ def SelectSeparator():
53
+ separator = DropdownMenuSeparator()
54
+ separator.setAttribute("data-slot", "select-separator")
55
+ return separator
56
+
57
+
58
+ @js
59
+ class Select(DropdownMenu):
60
+ def __init__(
61
+ self,
62
+ trigger: SelectTrigger,
63
+ content: SelectContent,
64
+ *,
65
+ value: str = "",
66
+ ):
67
+ self.select_trigger = trigger
68
+ super().__init__(trigger, content)
69
+ self.setAttribute("data-slot", "select")
70
+ if value != "":
71
+ self.select(value)
72
+
73
+ def select(self, value: str):
74
+ self.selected_value = value
75
+ for item in self.content.items:
76
+ selected = item.value == value
77
+ item.setAttribute("aria-selected", "true" if selected else "false")
78
+ if selected:
79
+ label = item.getAttribute("data-label")
80
+ if label is not None:
81
+ self.select_trigger.set_value_text(label)
82
+ if self.is_open:
83
+ self.close()