# frozen_string_literal: true module Maquina module Navbar # == Desktop profile menu at Navbar # # Renders dropdown profile menu on desktop. # # Requires Rails Helper method to get access to options for this menu. Method `profile_menu_options` example: # # def profile_menu_options # if Current.user.present? # { # profile: profiles_path # signout: { method: :delete, path: session_path } # } # else # { # signin: new_session_path # } # end # end # # `ProfileMenu` uses I18n to translate menu options. Translation example: # # menu: # profile: # profile: My profile # signout: Close session # signin: Login # # `ProfileMenu` is initialized by default to render links for desktop. To render mobile links, # initialize `ProfileMenu` as follow: # # Views::Navbar::ProfileMenu.new(desktop: false) # class ProfileMenu < Phlex::HTML include ApplicationView delegate :t, :profile_menu_options, to: :helpers def initialize(desktop: true) @desktop = desktop end def view_template div(**menu_attributes) do secure_menu_options.each_pair do |option, path| link_path, attributes = path_options(path) render Maquina::Navbar::ProfileMenuItemLink.new(option, link_path, attributes, desktop: @desktop) end end end private def path_options(options) attrs = {} path = "" if options.is_a?(Hash) path = options[:path] attrs[:method] = options[:method] else path = options end [path, attrs] end def secure_menu_options profile_menu_options rescue NoMethodError => ex Rails.logger.error "[#{self.class}] Please implement helper method :profile_menu_options :: #{ex.message}" {} end def menu_attributes attrs = {class: "mt-3 space-y-1"} if @desktop attrs = { class: "hidden origin-top-right absolute right-0 mt-2 w-48 rounded-md shadow-lg py-1 bg-white ring-1 ring-black ring-opacity-5 focus:outline-none", role: "menu", "aria-orientation": "vertical", "aria-labelledby": "user-menu-button", tabindex: "-1" }.merge(**data_attributes) end attrs end def data_attributes { data: { "popup-menu-target": "menu", "transition-enter": "transition ease-out duration-100", "transition-enter-active": "transform opacity-0 scale-95", "transition-enter-to": "transform opacity-100 scale-100", "transition-leave": "transition ease-in duration-75", "transition-leave-active": "transform opacity-100 scale-100", "transition-leave-to": "transform opacity-0 scale-95" } } end end end end