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

Add support for toggling color mode ('night/day mode') #382

Merged
merged 11 commits into from
Jan 30, 2023
1 change: 1 addition & 0 deletions pynecone/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
from .model import Model, session
from .state import ComputedVar as var
from .state import State
from .style import toggle_color_mode
1 change: 1 addition & 0 deletions pynecone/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"next/router": {"useRouter"},
f"/{constants.STATE_PATH}": {"connect", "updateState", "E"},
"": {"focus-visible/dist/focus-visible"},
"@chakra-ui/react": {"useColorMode"},
}


Expand Down
3 changes: 3 additions & 0 deletions pynecone/compiler/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,6 @@ def format_state(

# Sockets.
SOCKET = "const socket = useRef(null)"

# Color toggle
COLORTOGGLE = "const { colorMode, toggleColorMode } = useColorMode()"
5 changes: 4 additions & 1 deletion pynecone/compiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Main,
Script,
Title,
ColorModeScript,
)
from pynecone.components.component import Component, CustomComponent, ImportDict
from pynecone.state import State
Expand Down Expand Up @@ -120,7 +121,8 @@ def compile_state(state: Type[State]) -> str:
router = templates.ROUTER
socket = templates.SOCKET
ready = templates.READY
return templates.join([synced_state, result, router, socket, ready])
color_toggle = templates.COLORTOGGLE
return templates.join([synced_state, result, router, socket, ready, color_toggle])


def compile_events(state: Type[State]) -> str:
Expand Down Expand Up @@ -209,6 +211,7 @@ def create_document_root(stylesheets: List[str]) -> Component:
return Html.create(
DocumentHead.create(*sheets),
Body.create(
ColorModeScript.create(),
Main.create(),
Script.create(),
),
Expand Down
2 changes: 1 addition & 1 deletion pynecone/components/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Base components."""

from .body import Body
from .document import DocumentHead, Html, Main, Script
from .document import DocumentHead, Html, Main, Script, ColorModeScript
from .head import Head
from .link import Link
from .meta import Description, Image, Title
13 changes: 13 additions & 0 deletions pynecone/components/base/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ class Script(NextDocumentLib):
"""The document main scripts."""

tag = "NextScript"


class ChakraUiReactLib(Component):
"""Chakra UI React document components."""

library = "@chakra-ui/react"


class ColorModeScript(ChakraUiReactLib):
"""Chakra color mode script."""

tag = "ColorModeScript"
initialColorMode = "light"
5 changes: 5 additions & 0 deletions pynecone/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def _create_event_chain(
Raises:
ValueError: If the value is not a valid event chain.
"""
# If it's a JS var, return it.
if isinstance(value, Var):
if value.is_local is False and value.is_string is False:
return value

# If it's an event chain var, return it.
if isinstance(value, Var):
if value.type_ is not EventChain:
Expand Down
9 changes: 9 additions & 0 deletions pynecone/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from pynecone.var import Var


def toggle_color_mode():
"""Toggle the color mode.

Returns:
Toggle color mode JS event as a variable
"""
return Var.create(value="toggleColorMode", is_local=False, is_string=False)


def convert(style_dict):
"""Format a style dictionary.

Expand Down