@@ -3,15 +3,23 @@ module Yoga.Block.Container.Style where
3import Yoga.Prelude.Style
3import Yoga.Prelude.Style
4import Color as Color
4import Color as Color
5import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
5import Data.Symbol (class IsSymbol, SProxy, reflectSymbol)
6+import Effect.Uncurried (EffectFn2, runEffectFn2)
6import Foreign.Object (Object)
7import Foreign.Object (Object)
7import Foreign.Object as Object
8import Foreign.Object as Object
8import Heterogeneous.Mapping (class HMapWithIndex, class MappingWithIndex, hmap, hmapWithIndex)
9import Heterogeneous.Mapping (class HMapWithIndex, class MappingWithIndex, hmap, hmapWithIndex)
9import Unsafe.Coerce (unsafeCoerce)
10import Unsafe.Coerce (unsafeCoerce)
11+import Web.DOM (Element)
12+import Web.DOM.Document (documentElement)
13+import Web.HTML (Window, window)
14+import Web.HTML.HTMLDocument as HTMLDocument
15+import Web.HTML.Window (document)
11data DarkOrLightMode
17data DarkOrLightMode
12 = DarkMode
18 = DarkMode
13 | LightMode
19 | LightMode
21+derive instance eqDarkOrLightMode ∷ Eq DarkOrLightMode
15lightModeStyle ∷ Style
23lightModeStyle ∷ Style
16lightModeStyle = unsafeCoerce lightModeVariables
24lightModeStyle = unsafeCoerce lightModeVariables
@@ -27,6 +35,35 @@ lightMode = mkGlobal (Just LightMode)
27global ∷ Style
35global ∷ Style
28global = mkGlobal Nothing
36global = mkGlobal Nothing
38+foreign import data ComputedStyle ∷ Type
40+foreign import getComputedStyleImpl ∷ EffectFn2 Element Window ComputedStyle
42+getComputedStyle ∷ Element -> Window -> Effect ComputedStyle
43+getComputedStyle = runEffectFn2 getComputedStyleImpl
45+foreign import getPropertyValueImpl ∷ EffectFn2 String ComputedStyle String -- Not sure it always returns a string
47+getPropertyValue ∷ String -> ComputedStyle -> Effect String
48+getPropertyValue = runEffectFn2 getPropertyValueImpl
50+getDarkOrLightMode ∷ Effect (Maybe DarkOrLightMode)
54+ htmlDoc <- document win # lift
55+ let doc = HTMLDocument.toDocument htmlDoc
56+ docElem ∷ Element <- documentElement doc # MaybeT
57+ computedStyle <- getComputedStyle docElem win # lift
58+ pv <- getPropertyValue "--theme-variant" computedStyle # lift
62+ if pv == "light" then
65+ Nothing # pure # MaybeT
30mkGlobal ∷ Maybe DarkOrLightMode -> Style
67mkGlobal ∷ Maybe DarkOrLightMode -> Style
31mkGlobal maybeMode =
68mkGlobal maybeMode =
@@ -40,7 +77,12 @@ mkGlobal maybeMode =
40 , transition: str "background,color 0.33s ease-in"
77 , transition: str "background,color 0.33s ease-in"
42 , ":root":
79 , ":root":
82+ { "@media (prefers-color-scheme: dark)":
83+ nest { "--theme-variant": str "dark" }
43− nested $ variables
85+ <> variables
44 <> fontVariables { main: "Inter", mono: "Victor Mono, Menlo, Consolas, Monaco, Liberation Mono, Lucida Console" }
86 <> fontVariables { main: "Inter", mono: "Victor Mono, Menlo, Consolas, Monaco, Liberation Mono, Lucida Console" }
@@ -358,6 +400,7 @@ variables =
358 , "--s3": str "calc(var(--s2) * var(--ratio))"
400 , "--s3": str "calc(var(--s2) * var(--ratio))"
359 , "--s4": str "calc(var(--s3) * var(--ratio))"
401 , "--s4": str "calc(var(--s3) * var(--ratio))"
360 , "--s5": str "calc(var(--s4) * var(--ratio))"
402 , "--s5": str "calc(var(--s4) * var(--ratio))"
403+ , "--theme-variant": str "light"
363fontVariables ∷ { main ∷ String, mono ∷ String } -> Style
406fontVariables ∷ { main ∷ String, mono ∷ String } -> Style