Surface and Moon Setup
The official documentation of Moon Design provides the steps to install it, however we are going to make some modifications in order to get it ready for production. This modifications are mainly related to relative links in the Config file
Add Moon Design dependency
We will using thelatest version of the Hex package of Moon
In your mix.ex
file add the following under tour deps
{:moon, "~> 2.81.0"}
We also need to add the latest version of Sourceror due to a bug in the older version of Surface use in moon
{:sourceror, "~> 0.12.0"}
And run mix deps.get
Oups , we have some conflict. This happens because we are using a newer version of Phoenix Lets downgrade some deps to fit Moon requirements.
{:phoenix_html, "~> 3.0"},
{:phoenix_live_view, "~> 0.18.3"},
{:phoenix_live_dashboard, "~> 0.7.2"},
Run again mix deps.get
Mix Compile
Create a new file in config/surface.ex
and copy the content of deps/moon/config/surface.exs
into it
We can now edit our config/config.exs
file
Add the following
import_config "surface.exs"
config :surface, :components, []
Modify the Node Path to the config
env: %{"NODE_PATH" => "#{Path.expand("../deps", __DIR__)}:./node_modules"}
You can now run mix compile
(be aware, it will take some time)
Mix Surface Init
We now need to configure Surface because moon depend on it and because we love it.
mix surface.init
Layout
Lets setup our layout with Surface emebed templates
The original layout folder is inside components, however the module name does not include components. The layout folder should potentially be on the root of our live_air_web
folder
So lets move the layouts
folder and layout.ex
to live_air_web
folder
We will now edit layout.ex
Remove
embed_templates "layouts/*"
Add
embed_sface("layouts/master.sface")
embed_sface("layouts/root.sface")
Replace the html.heex
template by the Surface alternative .sface
In the root.sface
<!DOCTYPE html>
<html lang="en" class="[scrollbar-gutter:stable]">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="csrf-token" content={get_csrf_token()} />
<title>{assigns[:page_title] || dgettext("layouts", "Flytis, booking simply")}</title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>
<body class="bg-white antialiased">
{@inner_content}
</body>
</html>
In the app.sface
Replace <%= @inner_content %>
by {@inner_content}
and replace <%= Application.spec(:phoenix, :vsn) %>
by {Application.spec(:phoenix, :vsn) }
Moon Icons and Assets
We add the Moon icons to our endpoint.ex
file
plug Plug.Static,
at: "/moon/assets",
from: :moon,
gzip: false,
only: ~w(assets themes images fonts svgs favicon.ico robots.txt),
cache_control_for_etags: "public, max-age=86400"
plug Plug.Static,
at: "/moon_icons/",
from: :moon_icons,
gzip: true,
cache_control_for_etags: "public, max-age=86400"
App JS
Replace
by
Moon Theme CSS
We theme add our them css
Lets create a live_air/assets/css/theme/moon.css
file and copy the content of live_air/deps/moon/priv/static/themes/moon.css
in to it
We also need to add our theme class to our html
Open live_air/lib/live_air_web/layouts/root.sface
and replace the body class
<body class="theme-moon-light">
{@inner_content}
</body>
Assets Package JSON
Create assets.package.json
with the following content
{
"name": "assets",
"version": "1.0.0",
"description": "",
"main": "tailwind.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"deploy": "cd .. && mix assets.deploy && rm -f _build/esbuild",
"build": "tailwindcss -i css/app.css -o ../priv/static/assets/app.css --postcss",
"watch": "tailwindcss -i css/app.css -o ../priv/static/assets/app.css --postcss --watch"
},
"dependencies": {
"@popperjs/core": "^2.11.6",
"@tailwindcss/forms": "^0.4.0",
"autoprefixer": "^10.4.2",
"postcss": "^8.4.5",
"postcss-import": "^14.0.2",
"scroll-into-view-if-needed": "^3.0.10",
"smooth-scroll-into-view-if-needed": "^2.0.0",
"tailwindcss": "^3.3.2"
},
"author": "",
"license": "ISC",
"devDependencies": {
"postcss-nesting": "^12.0.1"
}
}
Please note the additional deploy line. This is needed later by Gigalixir for deployment
Post CSS
Create a live_air/assets/postcss.config.js
file with the following content
module.exports = {
plugins: {
'postcss-import': {},
tailwindcss: {},
autoprefixer: {}
}
}
Tailwind Config
Once again for deployment purposes, we need to import moon tailwind config directly and not from the deps folder.
1 Create live_air/assets/js/moon
folder and copy the followinf the folling files
live_air/deps/moon/assets/js/bottomsheet.config.js
live_air/deps/moon/assets/js/breadcrumb.config.js
live_air/deps/moon/assets/js/dropdown.config.js
live_air/deps/moon/assets/ds-moon-preset.js
live_air/deps/moon/assets/colors.json
2 Edit your Tailwind config file to combin Moon and Phoenix setup
Your final file should looki like this
Mix Assets Setup
In your file edit the following
You can now run mix.assets.setup