Skip to content

Commit 69a967b

Browse files
committed
ability to specify react version in template
1 parent b0c7f93 commit 69a967b

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import idom
1+
from idom import component, run, web
22

33

4-
mui = idom.web.module_from_template("react", "@material-ui/core@^5.0", fallback="⌛")
5-
Button = idom.web.export(mui, "Button")
6-
7-
idom.run(
8-
idom.component(
9-
lambda: Button({"color": "primary", "variant": "contained"}, "Hello World!")
10-
)
4+
mui = web.module_from_template(
5+
"react@^17.0.0",
6+
"@material-ui/[email protected]",
7+
fallback="⌛",
118
)
9+
Button = web.export(mui, "Button")
10+
11+
12+
@component
13+
def HelloWorld():
14+
return Button({"color": "primary", "variant": "contained"}, "Hello World!")
15+
16+
17+
run(HelloWorld)

docs/source/guides/escape-hatches/_examples/material_ui_button_on_click.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import idom
44

55

6-
mui = idom.web.module_from_template("react", "@material-ui/core@^5.0", fallback="⌛")
6+
mui = idom.web.module_from_template(
7+
"react@^17.0.0",
8+
"@material-ui/[email protected]",
9+
fallback="⌛",
10+
)
711
Button = idom.web.export(mui, "Button")
812

913

src/idom/web/module.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ def module_from_template(
122122
Using this option has negative performance consequences since all DOM
123123
elements must be changed on each render. See :issue:`461` for more info.
124124
"""
125+
template_name, _, template_version = template.partition("@")
126+
if template_version:
127+
template_version = "@" + template_version
128+
125129
# We do this since the package may be any valid URL path. Thus we may need to strip
126130
# object parameters or query information so we save the resulting template under the
127131
# correct file name.
@@ -131,7 +135,7 @@ def module_from_template(
131135
cdn = cdn.rstrip("/")
132136

133137
template_file_name = (
134-
template
138+
template_name
135139
+ (".default" if exports_default else "")
136140
+ module_name_suffix(package_name)
137141
)
@@ -140,7 +144,7 @@ def module_from_template(
140144
if not template_file.exists():
141145
raise ValueError(f"No template for {template_file_name!r} exists")
142146

143-
variables = {"PACKAGE": package, "CDN": cdn}
147+
variables = {"PACKAGE": package, "CDN": cdn, "VERSION": template_version}
144148
content = Template(template_file.read_text()).substitute(variables)
145149

146150
return module_from_string(

src/idom/web/templates/react.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export * from "$CDN/$PACKAGE";
22

3-
import * as React from "$CDN/react";
4-
import * as ReactDOM from "$CDN/react-dom";
3+
import * as React from "$CDN/react$VERSION";
4+
import * as ReactDOM from "$CDN/react-dom$VERSION";
55

66
export function bind(node, config) {
77
return {

0 commit comments

Comments
 (0)