Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype tailwind support #476

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mesop/component_helpers/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class Style:
bottom: int | str | None = None
box_shadow: str | None = None
box_sizing: str | None = None
classes: list[str] | None = None
color: str | None = None
column_gap: int | str | None = None
columns: int | str | None = None
Expand Down Expand Up @@ -469,6 +470,7 @@ def to_style_proto(s: Style) -> pb.Style:
bottom=_px_str(s.bottom),
box_shadow=s.box_shadow,
box_sizing=s.box_sizing,
classes=s.classes,
color=s.color,
column_gap=_px_str(s.column_gap),
columns=_int_str(s.columns),
Expand Down
1 change: 1 addition & 0 deletions mesop/examples/web_component/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ py_library(
"//mesop/examples/web_component/quickstart",
"//mesop/examples/web_component/shared_js_module",
"//mesop/examples/web_component/slot",
"//mesop/examples/web_component/tailwind",
],
)
3 changes: 3 additions & 0 deletions mesop/examples/web_component/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
from mesop.examples.web_component.slot import (
slot_app as slot_app,
)
from mesop.examples.web_component.tailwind import (
tailwind_app as tailwind_app,
)
14 changes: 14 additions & 0 deletions mesop/examples/web_component/tailwind/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("//build_defs:defaults.bzl", "py_library")

package(
default_visibility = ["//build_defs:mesop_examples"],
)

py_library(
name = "tailwind",
srcs = glob(["*.py"]),
data = glob(["*.js"]),
deps = [
"//mesop",
],
)
10 changes: 10 additions & 0 deletions mesop/examples/web_component/tailwind/tailwind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// const policy = window.trustedTypes.createPolicy('lit-html', {
// createScriptURL: (input) => input,
// });

// // Use the policy to create a Trusted Script URL
// const scriptURL = policy.createScriptURL('https://cdn.tailwindcss.com');

// const script = document.createElement('script');
// script.src = scriptURL;
// document.body.appendChild(script);
17 changes: 17 additions & 0 deletions mesop/examples/web_component/tailwind/tailwind_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mesop as me
import mesop.labs as mel


@mel.web_component(path="./tailwind.js")
def tailwind():
pass


@me.page(
path="/web_component/tailwind/tailwind_app",
)
def page():
me.text("Loaded")
tailwind()
with me.box(style=me.Style(classes=["text-lg", "font-medium"])):
me.text("hi")
3 changes: 2 additions & 1 deletion mesop/protos/ui.proto
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ message UserDefinedType {
repeated Arg args = 1;
}

// Next id: 67
// Next id: 68
message Style {
optional string align_content = 32;
optional string align_items = 1;
Expand All @@ -274,6 +274,7 @@ message Style {
optional string bottom = 53;
optional string box_shadow = 29;
optional string box_sizing = 47;
repeated string classes = 67;
optional string color = 4;
optional string column_gap = 33;
optional string columns = 5;
Expand Down
4 changes: 4 additions & 0 deletions mesop/web/src/app/editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<link rel="stylesheet" href="./styles.css" />
<!-- Inject stylesheet (if needed) -->
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon" />
<script
src="https://cdn.tailwindcss.com"
nonce="$$INSERT_CSP_NONCE$$"
></script>
</head>
<body>
<mesop-editor-app ngCspNonce="$$INSERT_CSP_NONCE$$"
Expand Down
9 changes: 9 additions & 0 deletions mesop/web/src/component_renderer/component_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export class ComponentRenderer {

computeStyles() {
this.elementRef.nativeElement.style = this.getStyle();
this.elementRef.nativeElement.classList = this.getClasses();
}

createComponentRef() {
Expand Down Expand Up @@ -386,6 +387,14 @@ export class ComponentRenderer {
);
}

getClasses(): string {
const classes = this.component.getStyle()?.getClassesList();
if (!classes || !classes.length) {
return '';
}
return classes.join(' ');
}

@HostListener('click', ['$event'])
onClick(event: Event) {
if (!this._boxType) {
Expand Down
Loading