LogoLogo
v1.1.1
v1.1.1
  • 🌸What is Petal Pro?
  • Guides
    • 🚀Creating a web app from start to finish
  • Fundamentals
    • 👥Users & Authentication
    • 📄Included Pages
    • 🧊Components
    • 🎨Branding
    • 🌱Seeding
    • 📄Layouts & Menus
    • 🛠️Background Tasks and Jobs
    • 🔧Util, DB & Helpers
    • 📧Emails
    • 🪝Javascript Hooks
    • 📚Extra Hex Libraries
    • 🏗️Generators & Page Builder
    • 🗣️Translations
Powered by GitBook
On this page
  • Layouts
  • Sidebar layout
  • Stacked layout
  • Menus

Was this helpful?

  1. Fundamentals

Layouts & Menus

PreviousSeedingNextBackground Tasks and Jobs

Last updated 3 years ago

Was this helpful?

Layouts

Sidebar layout

This is the typical web application layout. It allows for a large number of menu items on the sidebar.

<.layout
  current_page={@current}
  type="sidebar"
  current_user_name={user_name(@current_user)}
  main_menu_items={main_menu_items(@current_user)}
  user_menu_items={user_menu_items(@current_user)}
  home_path={home_path(@current_user)}
>
  <.container max_width="xl">
    <div>content</div>
  </.container>
</.layout>e

Stacked layout

A stacked layout with a navbar and then content.

<.layout
  current_page={@current}
  type="stacked"
  current_user_name={user_name(@current_user)}
  main_menu_items={main_menu_items(@current_user)}
  user_menu_items={user_menu_items(@current_user)}
  home_path={home_path(@current_user)}
>
  <.container max_width="xl">
    <div>content</div>
  </.container>
</.layout>e

Menus

In menus.ex there is a list of all the menu items. This can be useful when working with navbars and layouts, where you sometimes need to loop over menu items multiple times.

For all menu items, you must define a new get_link/2 function, which takes a name and current_user as the parameters. The name parameter is pattern matched and thus you statically include it in the function definition. A menu item has a name, label, path and icon. Here's an example of a menu item for the "Register" menu item:

def get_link(:register, _current_user) do
  %{
    name: :register,
    label: "Register",
    path: Routes.user_registration_path(Endpoint, :new),
    icon: :clipboard_list
  }
end

You can use the current_user param to conditionally display a menu item. For example, here we don't show the menu item unless the user is an admin.

def get_link(:admin_users = name, current_user) do
  if Helpers.is_admin?(current_user) do
    %{
      name: name,
      label: "Users",
      path: Routes.admin_users_path(Endpoint, :index),
      icon: :users
    }
  else
    nil
  end
end

You can build menu item lists to give to layouts. We define the two core menu lists in menus.ex:

📄
Sidebar layout in action
Sidebar layout on mobile (closed)
Sidebar layout on mobile (open)
Stacked layout
Stacked layout on mobile
Stacked layout on mobile with menu open
How layouts use menu lists