Light/Dark Themes using light-dark()
Mix.install([
{:phoenix_playground, "~> 0.1"},
{:phoenix_html, "~> 4.3"},
{:autumn, ">= 0.6.0"}
])
HtmlMultiThemes Formatter
Use the light-dark() CSS function to resolve the theme. It requires both light and dark theme defined in :themes option.
defmodule SyntaxHighlighterLive do
use Phoenix.LiveView
def mount(_params, _session, socket) do
code = ~S"""
@doc "Defines a public function with the given name and body."
defmacro def(call, expr \\ nil) do
assert_no_match_or_guard_scope(__CALLER__.context, "def/2")
define(:def, call, expr, __CALLER__)
end
"""
code = Autumn.highlight!(
code,
language: "elixir",
formatter:
{
:html_multi_themes,
themes: [light: "github_light", dark: "github_dark"],
default_theme: "light-dark()"
}
)
{:ok, assign(socket, code: code)}
end
def render(assigns) do
~H"""
/* Enable light/dark mode based on system preference */
html {
color-scheme: light dark;
}
body {
background: light-dark(#ffffff, #0d1117);
color: light-dark(#1f2328, #e6edf3);
min-height: 100vh;
font-family: ui-monospace,SFMono-Regular,"SF Mono",Menlo,Consolas,"Liberation Mono",monospace
}
Light/Dark Theme Demo
Change your OS theme setting to light/dark to see the code highlighting change automatically.
Uses CSS light-dark() function for automatic theme switching.
{Phoenix.HTML.raw(@code)}
"""
end
end
PhoenixPlayground.start(live: SyntaxHighlighterLive, open_browser: true, port: 4000)