Skip to content

Commit

Permalink
introduce gui.styled() components
Browse files Browse the repository at this point in the history
v0.5.2
  • Loading branch information
devxpy committed Jan 7, 2025
1 parent f6719b8 commit ab9e246
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function CodeEditor({
};

return (
<div className="mb-4 code-editor-wrapper position-relative">
<div className="gui-input code-editor-wrapper position-relative">
<InputLabel
label={label}
help={help}
Expand Down
7 changes: 7 additions & 0 deletions app/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function RenderedTreeNode({
children={children}
onChange={onChange}
state={state}
className={props.className}
/>
);
case "nav-tab-content":
Expand Down Expand Up @@ -526,10 +527,12 @@ export function RenderedChildren({
children,
onChange,
state,
className,
}: {
children: Array<TreeNode>;
onChange: OnChange;
state: Record<string, any>;
className?: string;
}) {
let elements = children.map((node, idx) => {
let key;
Expand All @@ -538,6 +541,10 @@ export function RenderedChildren({
} else {
key = `idx:${idx}`;
}
let prevClassName = node.props.className || "";
if (className && !prevClassName.includes(className)) {
node.props.className = `${className} ${prevClassName}`;
}
return (
<RenderedTreeNode
key={key}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gooey-gui",
"version": "0.5.0",
"version": "0.5.2",
"sideEffects": false,
"scripts": {
"build": "remix build",
Expand Down
9 changes: 9 additions & 0 deletions py/gooey_gui/components/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ def _node(nodename: str, **props):
return core.NestingCtx(node)


def styled(css: str) -> core.RenderTreeNode:
css = dedent(css).strip()
className = "gui-" + core.md5_values(css)
selector = "." + className
css = css.replace("&", selector)
core.add_styles(className, css)
return _node("", className=className)


def text(body: str, **props):
core.RenderTreeNode(
name="pre",
Expand Down
9 changes: 8 additions & 1 deletion py/gooey_gui/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@
realtime_clear_subs,
md5_values,
)
from .renderer import RenderTreeNode, NestingCtx, renderer, route, current_root_ctx
from .renderer import (
RenderTreeNode,
NestingCtx,
renderer,
route,
current_root_ctx,
add_styles,
)
from .state import (
get_session_state,
set_session_state,
Expand Down
13 changes: 13 additions & 0 deletions py/gooey_gui/core/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def current_root_ctx() -> "NestingCtx":
return threadlocal.root_ctx


def add_styles(className: str, css: str):
threadlocal.styles[className] = css


class RenderTreeNode(BaseModel):
name: str
props: ReactHTMLProps = {}
Expand Down Expand Up @@ -108,17 +112,26 @@ def renderer(
set_query_params(query_params or {})
realtime_clear_subs()
threadlocal.use_state_count = 0
threadlocal.styles = {}
while True:
try:
root = RenderTreeNode(name="root")
threadlocal.root_ctx = NestingCtx(root)
with threadlocal.root_ctx:
styles_node = RenderTreeNode(
name="tag",
props=dict(__reactjsxelement="style"),
).mount()
try:
ret = render()
except StopException:
ret = None
except RedirectException as e:
return RedirectResponse(e.url, status_code=e.status_code)
if threadlocal.styles:
styles_node.props["dangerouslySetInnerHTML"] = {
"__html": "\n".join(threadlocal.styles.values())
}
if isinstance(ret, Response):
return ret
return JSONResponse(
Expand Down

0 comments on commit ab9e246

Please sign in to comment.