Powered by AppSignal & Oban Pro

Light/Dark Themes using vars

examples/light_dark_vars.livemd

Light/Dark Themes using vars

Mix.install([
  {:phoenix_playground, "~> 0.1"},
  {:phoenix_html, "~> 4.3"},
  {:autumn, ">= 0.6.0"}
])

HtmlMultiThemes Formatter

Define CSS vars to resolve the colors and style based on @media prefers-color-scheme.

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"
        }
    )

    {: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;
      }

      /* Apply dark theme variables when in dark mode */
      @media (prefers-color-scheme: dark) {
        .athl,
        .athl span {
          color: var(--athl-dark) !important;
          background-color: var(--athl-dark-bg) !important;
          font-style: var(--athl-dark-font-style) !important;
          font-weight: var(--athl-dark-font-weight) !important;
          text-decoration: var(--athl-dark-text-decoration) !important;
        }
      }
    

    
      

Light/Dark Theme Demo with CSS Variables

Change your OS theme setting to light/dark to see the code highlighting change automatically.

This example uses CSS variables and @media prefers-color-scheme for manual theme control. Light theme uses inline styles, dark theme applies CSS variables via media query.

{Phoenix.HTML.raw(@code)} """
end end
PhoenixPlayground.start(live: SyntaxHighlighterLive, open_browser: true, port: 4000)