Skip to content

Commit 41fffe6

Browse files
thavocadog0eeg0ee
authored
Add support for toggling color mode ('night/day mode') (#382)
Co-authored-by: g0ee <[email protected]> Co-authored-by: g0ee <[email protected]>
1 parent 4c7d923 commit 41fffe6

File tree

8 files changed

+37
-2
lines changed

8 files changed

+37
-2
lines changed

pynecone/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
from .model import Model, session
1616
from .state import ComputedVar as var
1717
from .state import State
18+
from .style import toggle_color_mode

pynecone/compiler/compiler.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"next/router": {"useRouter"},
1818
f"/{constants.STATE_PATH}": {"connect", "updateState", "E"},
1919
"": {"focus-visible/dist/focus-visible"},
20+
"@chakra-ui/react": {"useColorMode"},
2021
}
2122

2223

pynecone/compiler/templates.py

+3
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,6 @@ def format_state(
197197

198198
# Sockets.
199199
SOCKET = "const socket = useRef(null)"
200+
201+
# Color toggle
202+
COLORTOGGLE = "const { colorMode, toggleColorMode } = useColorMode()"

pynecone/compiler/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Main,
1818
Script,
1919
Title,
20+
ColorModeScript,
2021
)
2122
from pynecone.components.component import Component, CustomComponent, ImportDict
2223
from pynecone.state import State
@@ -120,7 +121,8 @@ def compile_state(state: Type[State]) -> str:
120121
router = templates.ROUTER
121122
socket = templates.SOCKET
122123
ready = templates.READY
123-
return templates.join([synced_state, result, router, socket, ready])
124+
color_toggle = templates.COLORTOGGLE
125+
return templates.join([synced_state, result, router, socket, ready, color_toggle])
124126

125127

126128
def compile_events(state: Type[State]) -> str:
@@ -209,6 +211,7 @@ def create_document_root(stylesheets: List[str]) -> Component:
209211
return Html.create(
210212
DocumentHead.create(*sheets),
211213
Body.create(
214+
ColorModeScript.create(),
212215
Main.create(),
213216
Script.create(),
214217
),

pynecone/components/base/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Base components."""
22

33
from .body import Body
4-
from .document import DocumentHead, Html, Main, Script
4+
from .document import DocumentHead, Html, Main, Script, ColorModeScript
55
from .head import Head
66
from .link import Link
77
from .meta import Description, Image, Title

pynecone/components/base/document.py

+13
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ class Script(NextDocumentLib):
3131
"""The document main scripts."""
3232

3333
tag = "NextScript"
34+
35+
36+
class ChakraUiReactLib(Component):
37+
"""Chakra UI React document components."""
38+
39+
library = "@chakra-ui/react"
40+
41+
42+
class ColorModeScript(ChakraUiReactLib):
43+
"""Chakra color mode script."""
44+
45+
tag = "ColorModeScript"
46+
initialColorMode = "light"

pynecone/components/component.py

+5
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ def _create_event_chain(
162162
Raises:
163163
ValueError: If the value is not a valid event chain.
164164
"""
165+
# If it's a JS var, return it.
166+
if isinstance(value, Var):
167+
if value.is_local is False and value.is_string is False:
168+
return value
169+
165170
# If it's an event chain var, return it.
166171
if isinstance(value, Var):
167172
if value.type_ is not EventChain:

pynecone/style.py

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
from pynecone.var import Var
77

88

9+
def toggle_color_mode():
10+
"""Toggle the color mode.
11+
12+
Returns:
13+
Toggle color mode JS event as a variable
14+
"""
15+
return Var.create(value="toggleColorMode", is_local=False, is_string=False)
16+
17+
918
def convert(style_dict):
1019
"""Format a style dictionary.
1120

0 commit comments

Comments
 (0)