Skip to content

Commit

Permalink
elia/menu-item: [wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Dec 19, 2023
1 parent 3fe73ad commit 0d20d5a
Show file tree
Hide file tree
Showing 12 changed files with 2,803 additions and 27 deletions.
2,782 changes: 2,782 additions & 0 deletions admin/app/assets/builds/solidus_admin/tailwind.css

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def initialize(
)
@logo_path = logo_path
@items = items.map do |attrs|
children = attrs[:children].to_a.map { SolidusAdmin::MainNavItem.new(**_1, top_level: false) }
SolidusAdmin::MainNavItem.new(**attrs, children: children, top_level: true)
children = attrs[:children].to_a.map { SolidusAdmin::MenuItem.new(**_1, top_level: false) }
SolidusAdmin::MenuItem.new(**attrs, children: children, top_level: true)
end
@store = store
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class SolidusAdmin::Layout::Navigation::Item::Component < SolidusAdmin::BaseComponent
with_collection_parameter :item

# @param item [SolidusAdmin::MainNavItem]
# @param item [SolidusAdmin::MenuItem]
# @param fullpath [String] the current path
# @param url_helpers [#solidus_admin, #spree] context for generating paths
def initialize(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
en:
solidus_admin:
main_nav:
menu_item:
orders: Orders
products: Products
option_types: Option Types
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Customizing the main navigation

You are allowed to add your custom links to the main navigation. To do so, you can access `SolidusAdmin::Config.main_nav` in an initializer:
You are allowed to add your custom links to the main navigation. To do so, you can access `SolidusAdmin::Config.menu_items` in an initializer:

```ruby
# config/initializers/solidus_admin.rb
Expand All @@ -13,7 +13,7 @@ SolidusAdmin::Config.menu_items << {
```

- The key you provide will be used to translate the link's label under the
`solidus_admin.main_nav.#{key}` key.
`solidus_admin.menu_item.#{key}` key.
- Icon needs to be an icon name from [Remixicon](https://remixicon.com/).
- Position tells Solidus where to place the link in the main navigation. The
default items are placed with 10 points of difference between them.
Expand Down
8 changes: 1 addition & 7 deletions admin/docs/customizing_view_components.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ All the components Solidus Admin uses are located in the [`app/components`](../a
- They are grouped in sidecar directories, where the main component file and
all its related files (assets, i18n files, etc.) live together.

For instance, the component for the main navigation is located in
[`app/components/solidus_admin/main_nav/component.rb`](../app/components/solidus_admin/main_nav/component.rb).

Solidus Admin components are designed to be easily customizable by the host
application. Because of that, if you look at how they are designed, you'll find
a series of patterns are followed consistently:

- Components are always resolved from a global registry in
`SolidusAdmin::Config.components` instead of being referenced by their constant. For
example, we call `component('main_nav')` instead of referencing
`SolidusAdmin::MainNav::Component` directly. As you'll see later, this makes
it easy to replace or tweak a component.
`SolidusAdmin::Config.components` instead of being referenced by their constant.
- It's possible to override the registry locally inside a component by redefining
the `#component` method. This is useful when you need to use a component that
is not registered in the global registry or need to address some edge case.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SolidusAdmin::Config.configure do |config|
# regeneration of the importmap.
# config.importmap_cache_sweepers << Rails.root.join("app/javascript/my_admin_components")

# If you want to avoid defining menu_item customizations twice while migrating to SolidusAdmin
# If you want to avoMenuItem menu_item customizations twice while migrating to SolidusAdmin
# you can import menu_items from the backend by uncommenting the following line,
# but you will need to
<%- if defined? Spree::Backend -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module SolidusAdmin
# Encapsulates the data for a main navigation item.
class MainNavItem
class MenuItem
# @!attribute [r] key
# @return [String] a unique identifier for this item
attr_reader :key
Expand Down Expand Up @@ -45,7 +45,7 @@ def initialize(
end

def name
I18n.t("solidus_admin.main_nav.#{key}", default: key.to_s.humanize)
I18n.t("solidus_admin.menu_item.#{key}", default: key.to_s.humanize)
end

# @return [Boolean] whether this item has any children
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class SolidusAdmin::Layout::Navigation::ComponentPreview < ViewComponent::Previe
# The item component is used to render main navigation items, which are
# rendered within the sidebar.
#
# It needs to be passed a {SolidusAdmin::MainNavItem} instance, which
# It needs to be passed a {SolidusAdmin::MenuItem} instance, which
# represents the data for a main navigation item.
#
# ```ruby
# item = SolidusAdmin::MainNavItem.new(
# item = SolidusAdmin::MenuItem.new(
# key: :overview,
# position: 80
# )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SolidusAdmin::Layout::Navigation::Item::ComponentPreview < ViewComponent::
# @param key text { description: "ID also used for i18n" }
# @param icon text { description: "RemixIcon name (https://remixicon.com/)" }
def overview(active: false, key: "orders", icon: "inbox-line")
item = SolidusAdmin::MainNavItem.new(
item = SolidusAdmin::MenuItem.new(
key: key,
icon: icon,
position: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def url_helpers(solidus_admin: {}, spree: {})
end

it "renders the item" do
item = SolidusAdmin::MainNavItem.new(key: "orders", route: :orders_path, position: 1)
item = SolidusAdmin::MenuItem.new(key: "orders", route: :orders_path, position: 1)
component = described_class.new(
item: item,
url_helpers: url_helpers(solidus_admin: { orders_path: "/admin/foo" })
Expand All @@ -27,8 +27,8 @@ def url_helpers(solidus_admin: {}, spree: {})
end

it "renders nested items" do
item = SolidusAdmin::MainNavItem.new(key: "products", route: :products_path, position: 1, children: [
SolidusAdmin::MainNavItem.new(key: "option_types", route: :option_types_path, position: 1, top_level: false)
item = SolidusAdmin::MenuItem.new(key: "products", route: :products_path, position: 1, children: [
SolidusAdmin::MenuItem.new(key: "option_types", route: :option_types_path, position: 1, top_level: false)
])
component = described_class.new(
item: item,
Expand All @@ -41,8 +41,8 @@ def url_helpers(solidus_admin: {}, spree: {})
end

it "syles top level items differently from nested items" do
item = SolidusAdmin::MainNavItem.new(key: "products", route: :products_path, position: 1, children: [
SolidusAdmin::MainNavItem.new(key: "option_types", route: :option_types_path, position: 1, top_level: false)
item = SolidusAdmin::MenuItem.new(key: "products", route: :products_path, position: 1, children: [
SolidusAdmin::MenuItem.new(key: "option_types", route: :option_types_path, position: 1, top_level: false)
])
component = described_class.new(
item: item,
Expand All @@ -59,9 +59,9 @@ def url_helpers(solidus_admin: {}, spree: {})
end

it "syles active items differently from the others" do
inactive_item = SolidusAdmin::MainNavItem
inactive_item = SolidusAdmin::MenuItem
.new(key: "orders", route: :orders_path, position: 1)
active_item = SolidusAdmin::MainNavItem
active_item = SolidusAdmin::MenuItem
.new(key: "products", route: :products_path, position: 1)
inactive_component = described_class.new(
item: inactive_item,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

RSpec.describe SolidusAdmin::MainNavItem do
RSpec.describe SolidusAdmin::MenuItem do
def url_helpers(solidus_admin: {}, spree: {})
double(
solidus_admin: double(**solidus_admin),
Expand Down

0 comments on commit 0d20d5a

Please sign in to comment.