-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
239 lines (164 loc) · 5.58 KB
/
README
File metadata and controls
239 lines (164 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Easy Menu
Simple menu bar and dropdown DSL for Rails views.
Easy Menu provides a small Ruby DSL for building menu bars, grouped dropdowns, and menu actions in helpers or views. It ships with default styles and JavaScript behavior designed for traditional server-rendered Rails applications.
Supports Prototype and jQuery integrations used by legacy projects.
## Features
- Ruby DSL for menu bars and dropdown menus
- Nested menu groups and separators
- Selection and disabled states
- Right and left aligned menu content
- Configurable global and per-menu behavior
- Rails engine assets (styles and JavaScript)
## Installation
Add to your Gemfile:
```ruby
gem 'easy_menu'
```
Then run:
```bash
bundle install
```
## Quick Start
Build a menu bar in a helper:
```ruby
def users_menu
MenuBar.new(self) do |mb|
mb.menu_bar_item(link_to('All Users', users_path)).selected(params[:action] == 'index')
mb.menu_bar_item(link_to('Invite', new_user_invitation_path))
mb.menu('More') do |menu|
menu.menu_item(link_to('Roles', roles_path))
menu.menu_item(link_to('Permissions', permissions_path))
end
end
end
```
Render it in a view:
```erb
<%= users_menu %>
```
## Core DSL
### MenuBar
Create a menu bar:
```ruby
MenuBar.new(self, theme: :default_theme) do |mb|
# items
end
```
Common methods:
- `menu_bar_item(content, options = {})`
- `menu(button_text, options = {}) { |menu| ... }`
- `group(options = {}) { |group| ... }`
- `menu_bar_content(content = nil, options = {}, &block)`
- `menu_bar_input(content, options = {})`
- `separator(options = {})`
### Menu
Inside `menu(...)` blocks:
- `menu_item(content, options = {})`
- `group(title, options = {}) { |group| ... }`
- `menu_content(content = nil, options = {}, &block)`
- `separator(options = {})`
### Item States
Most items support:
- `.selected(true_or_false)`
- `.disabled(true_or_false = true, click_blocker_html_options = {})`
- `.disable_when(dom_element, dom_event, js_condition, click_blocker_html_options = {})`
## Alignment and Ordering
Menu item alignment is set on the content wrapper:
```ruby
mb.menu_bar_item(link_to('Sign Out', destroy_user_session_path), align: :right)
```
### Right-Aligned Insert Strategy
Easy Menu supports two right-aligned insertion strategies:
- `:legacy_prepend` (default)
- Preserves historical float-right behavior.
- Right-aligned items are prepended internally.
- `:preserve_definition_order`
- Right-aligned items keep the same order they are declared in Ruby.
- Useful for flexbox-based layouts.
Set per menu bar:
```ruby
MenuBar.new(self, right_aligned_insert_strategy: :preserve_definition_order) do |mb|
mb.menu_bar_item(link_to('My Account', my_account_path), align: :right)
mb.menu_bar_item(link_to('Sign Out', destroy_user_session_path), align: :right)
end
```
Set globally:
```ruby
MenuBar.config[:right_aligned_insert_strategy] = :preserve_definition_order
```
### Explicit Indexing
`index` always takes precedence over insert strategy:
```ruby
mb.menu_bar_item(link_to('First', first_path), index: 0)
```
## Configuration
Configuration is available through `MenuBar.config` and defaults from `EasyMenu::Configuration::Default`.
Examples:
- Element wrappers (`:menu_bar_element`, `:menu_item_element`, ...)
- CSS class names (`:menu_bar_class`, `:menu_item_class`, ...)
- State classes (`:selected_class`, `:disabled_class`)
- Right alignment strategy (`:right_aligned_insert_strategy`)
Per-menu overrides can be passed using `config:`:
```ruby
MenuBar.new(self, config: { menu_bar_element: :nav }) do |mb|
mb.menu_bar_item('Example')
end
```
## HTML Options and Data Attributes
Most wrapper/item calls accept standard HTML options:
- `id`
- `class`
- `title`
- `style`
- `data`
- `data-*` keys
## Styling and JavaScript Notes
- The gem provides default styles (`easy_menu.css.scss`) and behavior hooks.
- `menu_bar_content_with_menu_class` defaults to `with_menu no_js`.
- The `no_js` class is meant to be removed client-side when JavaScript is active.
Projects commonly layer app-specific CSS on top of Easy Menu's baseline classes.
## Common Patterns
### Search Form Content in a Menu Bar
```ruby
mb.group do |group|
group.menu_bar_content do
form_tag(search_path, method: :get) { text_field_tag(:q) }
end
group.menu_bar_item(link_to('Clear', search_path)) if params[:q].present?
end
```
### Grouped Dropdown Menu
```ruby
mb.menu('Admin') do |menu|
menu.group('Users') do |group|
group.menu_item(link_to('List', users_path))
group.menu_item(link_to('Invite', new_user_invitation_path))
end
menu.separator
menu.group('System') do |group|
group.menu_item(link_to('Configuration', system_configuration_path))
end
end
```
## Migration Notes for Flex Layouts
If your app moved menu bars from float-based CSS to flexbox:
- Keep default `:legacy_prepend` unless you verify visual order is correct.
- Prefer `:preserve_definition_order` for flex layouts where declaration order should match render order.
- If needed, enable per-menu first, then move to global config.
## Troubleshooting
### Right-aligned items appear reversed
Use:
```ruby
MenuBar.new(self, right_aligned_insert_strategy: :preserve_definition_order)
```
### Unexpected classes or wrappers
Check `MenuBar.config` overrides and project CSS specificity.
### Empty separators at the end
`remove_dangling_separators` defaults to `true` and removes trailing separators.
## Development
- This gem is a Rails engine.
- Main DSL entry point: `lib/menu_bar.rb`
- Config defaults: `lib/easy_menu_configuration.rb`
- Shared helper behavior: `lib/easy_menu_helpers.rb`
## License
MIT. See `MIT-LICENSE`.