# check out https://solara.dev/ for documentation
# or https://github.com/widgetti/solara/
# And check out https://py.cafe/maartenbreddels for more examples
# based on https://vuejs.org/guide/built-ins/transition
# and/or https://v2.vuejs.org/v2/guide/transitions
import solara
from pathlib import Path
clicks = solara.reactive(0)
@solara.component_vue("transition.vue")
def Transition(show_first=True, children=[], name="", mode="", duration=200):
pass # just a dummy function
@solara.component
def TransitionRotate(axis: str, show_first=True, children=[], duration=1.2):
uid = solara.use_unique_key()[:6]
name = f"rotate-{axis}-{uid}"
duration_ms = duration * 1000
css_code = f"""
.{name}-enter-active, .{name}-leave-active {{
transition: all {duration}s ease-out;
backface-visibility: hidden;
}}
.{name}-enter, .{name}-leave-to {{
transform: rotate{axis}(90deg);
}}
.{name}-enter-to, .{name}-leave {{
transform: rotate{axis}(0deg);
}}
"""
solara.Style(css_code)
Transition(show_first=show_first == 0, name=name, duration=duration_ms, children=children, mode="out-in")
@solara.component
def TransitionSlide(axis: str="X", show_first=True, children=[], duration=1.2, translate_enter="50px", translate_leave="-50px"):
uid = solara.use_unique_key()[:6]
name = f"slide-{axis}-{uid}"
duration_ms = duration * 1000
css_code = f"""
.{name}-enter-active, .{name}-leave-active {{
transition: all {duration}s ease-out;
}}
.{name}-enter {{
transform: translate{axis}({translate_enter});
opacity: 0;
}}
.{name}-leave-to {{
transform: translate{axis}({translate_leave});
opacity: 0;
}}
.{name}-enter-to, .{name}-leave {{
transform: translate{axis}(0px);
}}
"""
solara.Style(css_code)
Transition(show_first=show_first, name=name, duration=duration_ms, children=children, mode="out-in")
@solara.component
def Page():
print("The component render function gets called")
# change this code, and see the output refresh
color = "green"
if clicks.value >= 5:
color = "red"
def increment():
clicks.value += 1
print("clicks", clicks) # noqa
solara.Button(label=f"Clicked: {clicks}", on_click=increment, color=color)
solara.Style(Path("fade.css"))
# why overlap?
with TransitionRotate("X", show_first=(clicks.value % 2) == 0, duration=0.2):
with solara.Card("Even") as el1:
solara.Text("This number is even")
with solara.Card("Odd") as el2:
solara.Text("This number is even")
with solara.Card("List"):
TodoItem("Write", True)
TodoItem("Read", False)
RemovableCard()
RemovableCard()
solara.v.use_event(el1, "click", lambda *_ignore: increment())
solara.v.use_event(el2, "click", lambda *_ignore: increment())
# solara.Text("this renders below")
@solara.component
def RemovableCard():
show = solara.use_reactive(True)
with TransitionSlide("X", show_first=show.value, duration=0.5, translate_leave="100px"):
with solara.Card("Some report"):
with solara.Column():
solara.Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
solara.Markdown("*Close the card to see it animate away*")
with solara.CardActions():
solara.v.Spacer()
solara.Button("Close", on_click=lambda: show.set(False), text=True)
@solara.component
def TodoItem(text, default_value):
done = solara.use_reactive(default_value)
print("Todo", text, done.value)
with solara.Row():
solara.Switch(label=text, value=done)
solara.v.Spacer()
with TransitionSlide("X", show_first=done.value, duration=0.2):
solara.v.Icon(children=["mdi-check"], color="success")