better_ui 0.1.0 โ 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +199 -75
- data/app/assets/javascripts/better_ui/controllers/navbar_controller.js +138 -0
- data/app/assets/javascripts/better_ui/controllers/sidebar_controller.js +211 -0
- data/app/assets/javascripts/better_ui/controllers/toast_controller.js +161 -0
- data/app/assets/javascripts/better_ui/index.js +159 -0
- data/app/assets/javascripts/better_ui/toast_manager.js +77 -0
- data/app/assets/stylesheets/better_ui/application.css +25 -351
- data/app/components/better_ui/application/alert_component.html.erb +27 -0
- data/app/components/better_ui/application/alert_component.rb +202 -0
- data/app/components/better_ui/application/card_component.html.erb +24 -0
- data/app/components/better_ui/application/card_component.rb +53 -0
- data/app/components/better_ui/application/card_container_component.html.erb +8 -0
- data/app/components/better_ui/application/card_container_component.rb +14 -0
- data/app/components/better_ui/application/header_component.html.erb +88 -0
- data/app/components/better_ui/application/header_component.rb +188 -0
- data/app/components/better_ui/application/navbar_component.html.erb +294 -0
- data/app/components/better_ui/application/navbar_component.rb +249 -0
- data/app/components/better_ui/application/sidebar_component.html.erb +207 -0
- data/app/components/better_ui/application/sidebar_component.rb +318 -0
- data/app/components/better_ui/application/toast_component.html.erb +35 -0
- data/app/components/better_ui/application/toast_component.rb +223 -0
- data/app/components/better_ui/general/avatar_component.html.erb +19 -0
- data/app/components/better_ui/general/avatar_component.rb +171 -0
- data/app/components/better_ui/general/badge_component.html.erb +19 -0
- data/app/components/better_ui/general/badge_component.rb +123 -0
- data/app/components/better_ui/general/breadcrumb_component.html.erb +15 -0
- data/app/components/better_ui/general/breadcrumb_component.rb +130 -0
- data/app/components/better_ui/general/button_component.html.erb +34 -0
- data/app/components/better_ui/general/button_component.rb +162 -0
- data/app/components/better_ui/general/heading_component.html.erb +25 -0
- data/app/components/better_ui/general/heading_component.rb +148 -0
- data/app/components/better_ui/general/icon_component.html.erb +2 -0
- data/app/components/better_ui/general/icon_component.rb +100 -0
- data/app/components/better_ui/general/link_component.html.erb +17 -0
- data/app/components/better_ui/general/link_component.rb +132 -0
- data/app/components/better_ui/general/panel_component.html.erb +27 -0
- data/app/components/better_ui/general/panel_component.rb +103 -0
- data/app/components/better_ui/general/spinner_component.html.erb +15 -0
- data/app/components/better_ui/general/spinner_component.rb +79 -0
- data/app/components/better_ui/general/table_component.html.erb +73 -0
- data/app/components/better_ui/general/table_component.rb +167 -0
- data/app/components/better_ui/theme_helper.rb +171 -0
- data/app/controllers/better_ui/application_controller.rb +1 -0
- data/app/controllers/better_ui/docs_controller.rb +18 -25
- data/app/views/components/better_ui/general/table/_custom_body_row.html.erb +17 -0
- data/app/views/components/better_ui/general/table/_custom_footer_rows.html.erb +17 -0
- data/app/views/components/better_ui/general/table/_custom_header_rows.html.erb +12 -0
- data/app/views/layouts/component_preview.html.erb +32 -0
- data/config/initializers/lookbook.rb +23 -0
- data/config/routes.rb +6 -1
- data/lib/better_ui/engine.rb +18 -1
- data/lib/better_ui/version.rb +1 -1
- data/lib/better_ui.rb +4 -0
- data/lib/generators/better_ui/stylesheet_generator.rb +96 -0
- data/lib/generators/better_ui/templates/README +56 -0
- data/lib/generators/better_ui/templates/components/_avatar.scss +151 -0
- data/lib/generators/better_ui/templates/components/_badge.scss +142 -0
- data/lib/generators/better_ui/templates/components/_breadcrumb.scss +107 -0
- data/lib/generators/better_ui/templates/components/_button.scss +106 -0
- data/lib/generators/better_ui/templates/components/_card.scss +69 -0
- data/lib/generators/better_ui/templates/components/_heading.scss +180 -0
- data/lib/generators/better_ui/templates/components/_icon.scss +90 -0
- data/lib/generators/better_ui/templates/components/_link.scss +130 -0
- data/lib/generators/better_ui/templates/components/_panel.scss +144 -0
- data/lib/generators/better_ui/templates/components/_spinner.scss +132 -0
- data/lib/generators/better_ui/templates/components/_table.scss +105 -0
- data/lib/generators/better_ui/templates/components/_variables.scss +33 -0
- data/lib/generators/better_ui/templates/components_stylesheet.scss +66 -0
- metadata +135 -10
- data/app/helpers/better_ui/application_helper.rb +0 -183
- data/app/views/better_ui/docs/component.html.erb +0 -365
- data/app/views/better_ui/docs/index.html.erb +0 -100
- data/app/views/better_ui/docs/show.html.erb +0 -60
- data/app/views/layouts/better_ui/application.html.erb +0 -135
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 317837f4f80e12e6ab860a7a134e9f17e01f5db2d86f5b178a4c9a2bc9588f3a
|
4
|
+
data.tar.gz: 8815e3b40a461070b9cb178ee1a7e1e8abaccbf6cea7366ab5c079db174429f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fb96fb9537391b7448ffde247cd8d9ae4916dac0cfcd17c4efe912b061dfd05986368ebe6563549c58ce0909e9a509474ac885e136d133214f806437925726d
|
7
|
+
data.tar.gz: de95bf54dcd6fa022f5afa5c0867d65b7a78e8f24cab637aa7cf098f0bf01f5cabdac5e6c7b36ba1aba14833682b17174e1d229a4d428f5646d92ee876ad42f4
|
data/README.md
CHANGED
@@ -3,58 +3,66 @@
|
|
3
3
|
[](https://badge.fury.io/rb/better_ui)
|
4
4
|
[](LICENSE)
|
5
5
|
|
6
|
-
>
|
6
|
+
> Elegant, consistent, and easily integrable UI components for your Rails applications
|
7
7
|
|
8
|
-
BetterUI
|
8
|
+
BetterUI is a Rails gem that provides reusable UI components with built-in documentation. Designed to be lightweight, customizable, and easy to use, it helps you build beautiful and consistent interfaces without JavaScript.
|
9
9
|
|
10
|
-
## โจ
|
10
|
+
## โจ Key Features
|
11
11
|
|
12
|
-
- **Design
|
13
|
-
- **
|
14
|
-
- **
|
15
|
-
- **
|
16
|
-
- **
|
12
|
+
- **Pure Design** - Elegant, fully CSS UI components, no JavaScript
|
13
|
+
- **Easy Integration** - Mountable Rails engine that integrates seamlessly with your app
|
14
|
+
- **Built-in Documentation** - View examples and documentation directly in your browser
|
15
|
+
- **Highly Customizable** - Easily adapt the style to your brand
|
16
|
+
- **Modular Components** - Use only what you need
|
17
|
+
- **Component Previews** - View and interact with components using Lookbook
|
17
18
|
|
18
|
-
## ๐ฆ
|
19
|
+
## ๐ฆ Available Components
|
19
20
|
|
20
|
-
|
|
21
|
+
| Component | Description |
|
21
22
|
|------------|-------------|
|
22
|
-
| **Button** |
|
23
|
-
| **Alert** |
|
24
|
-
| **Card** |
|
25
|
-
| **Modal** |
|
26
|
-
| **Tabs** |
|
27
|
-
| **
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
23
|
+
| **Button** | Styled buttons with primary, secondary, success, danger variants |
|
24
|
+
| **Alert** | Informational, success, warning, and error messages |
|
25
|
+
| **Card** | Flexible containers with header, body, and footer |
|
26
|
+
| **Modal** | Modal dialog windows |
|
27
|
+
| **Tabs** | Tab navigation |
|
28
|
+
| **Navbar** | Responsive navigation bar with dropdown menu support |
|
29
|
+
| **Sidebar** | Side menu with hierarchical navigation support |
|
30
|
+
| **Toast** | Temporary notifications with timer and animations |
|
31
|
+
| **Header** | Page headers with title, subtitle, breadcrumb, and actions |
|
32
|
+
| **Badge** | Labels with support for icons, colors, and outline variants |
|
33
|
+
| **Spinner** | Loading indicators with various themes and sizes |
|
34
|
+
| **Avatar** | User avatars with support for images, initials, and online status |
|
35
|
+
| **Form Elements** | Styled input fields (coming soon) |
|
36
|
+
|
37
|
+
## ๐ Installation
|
38
|
+
|
39
|
+
Add this line to your application's Gemfile:
|
32
40
|
|
33
41
|
```ruby
|
34
42
|
gem 'better_ui', '~> 0.1.0'
|
35
43
|
```
|
36
44
|
|
37
|
-
|
45
|
+
And then execute:
|
38
46
|
|
39
47
|
```bash
|
40
48
|
$ bundle install
|
41
49
|
```
|
42
50
|
|
43
|
-
## โ๏ธ
|
51
|
+
## โ๏ธ Configuration
|
44
52
|
|
45
|
-
###
|
53
|
+
### Mount the Engine
|
46
54
|
|
47
|
-
|
55
|
+
Add this to your `config/routes.rb` file:
|
48
56
|
|
49
57
|
```ruby
|
50
58
|
Rails.application.routes.draw do
|
51
59
|
mount BetterUi::Engine => '/better_ui'
|
52
60
|
|
53
|
-
# ...
|
61
|
+
# ... your other routes
|
54
62
|
end
|
55
63
|
```
|
56
64
|
|
57
|
-
###
|
65
|
+
### Include the Assets
|
58
66
|
|
59
67
|
In `app/assets/stylesheets/application.css`:
|
60
68
|
|
@@ -64,33 +72,149 @@ In `app/assets/stylesheets/application.css`:
|
|
64
72
|
*/
|
65
73
|
```
|
66
74
|
|
67
|
-
## ๐ฎ
|
75
|
+
## ๐ฎ Usage
|
68
76
|
|
69
|
-
|
77
|
+
Once installed, you can start using the components:
|
70
78
|
|
71
79
|
```erb
|
72
80
|
<%# Button %>
|
73
|
-
<%= better_ui_button("
|
81
|
+
<%= better_ui_button("Save", type: "primary") %>
|
74
82
|
|
75
83
|
<%# Alert %>
|
76
|
-
<%= better_ui_alert("
|
84
|
+
<%= better_ui_alert("Operation completed", type: "success") %>
|
77
85
|
|
78
86
|
<%# Card %>
|
79
87
|
<%= better_ui_card do %>
|
80
|
-
<%= better_ui_card_header("
|
88
|
+
<%= better_ui_card_header("Card Title") %>
|
81
89
|
<%= better_ui_card_body do %>
|
82
|
-
<p>
|
90
|
+
<p>Card content...</p>
|
83
91
|
<% end %>
|
84
92
|
<% end %>
|
93
|
+
|
94
|
+
<%# Header with breadcrumb %>
|
95
|
+
<%= render BetterUi::Application::HeaderComponent.new(
|
96
|
+
title: "Dashboard",
|
97
|
+
subtitle: "Manage everything from here",
|
98
|
+
breadcrumbs: [
|
99
|
+
{ text: "Home", url: "/" },
|
100
|
+
{ text: "Dashboard" }
|
101
|
+
],
|
102
|
+
actions: [
|
103
|
+
{ content: button_html("New", "primary") }
|
104
|
+
]
|
105
|
+
) %>
|
106
|
+
```
|
107
|
+
|
108
|
+
Visit `/better_ui` in your application to see the complete documentation and examples.
|
109
|
+
|
110
|
+
### The Header Component
|
111
|
+
|
112
|
+
The Header component is designed to create complete page headers with numerous features:
|
113
|
+
|
114
|
+
```erb
|
115
|
+
<%= render BetterUi::Application::HeaderComponent.new(
|
116
|
+
title: {
|
117
|
+
text: "Settings",
|
118
|
+
icon: "settings"
|
119
|
+
},
|
120
|
+
subtitle: "Configure system preferences",
|
121
|
+
breadcrumbs: [
|
122
|
+
{ text: "Home", url: "/" },
|
123
|
+
{ text: "Admin", url: "/admin" },
|
124
|
+
{ text: "Settings" }
|
125
|
+
],
|
126
|
+
variant: :modern,
|
127
|
+
fixed: :top,
|
128
|
+
show_breadcrumbs: true,
|
129
|
+
actions: [
|
130
|
+
{ content: button_html("Save", "primary") },
|
131
|
+
{ content: button_html("Cancel", "secondary") }
|
132
|
+
]
|
133
|
+
) %>
|
85
134
|
```
|
86
135
|
|
87
|
-
|
136
|
+
The component supports:
|
137
|
+
- Title with optional integrated icon
|
138
|
+
- Descriptive subtitle
|
139
|
+
- Complete breadcrumbs with navigation links
|
140
|
+
- Contextual actions (buttons, menus, etc.)
|
141
|
+
- Style variants: modern, light, dark, primary, transparent
|
142
|
+
- Fixed positioning (top or bottom)
|
143
|
+
- Breadcrumb visibility control
|
144
|
+
|
145
|
+
### Component Previews with Lookbook
|
146
|
+
|
147
|
+
BetterUI integrates [Lookbook](https://github.com/allmarkedup/lookbook) to preview UI components:
|
148
|
+
|
149
|
+
1. Access `/better_ui/lookbook` in your application to view the component catalog
|
150
|
+
2. Explore available variants and configurations for each component
|
151
|
+
3. View source code and generated markup
|
152
|
+
4. Interact with components in real-time
|
153
|
+
|
154
|
+
Lookbook is only available in development and test environments.
|
155
|
+
|
156
|
+
## ๐จ Customization
|
157
|
+
|
158
|
+
### Generate a Custom Stylesheet
|
159
|
+
|
160
|
+
BetterUI includes a generator to create a base stylesheet for customizing components:
|
161
|
+
|
162
|
+
```bash
|
163
|
+
# Generate a stylesheet with the default "custom" prefix
|
164
|
+
rails generate better_ui:stylesheet
|
88
165
|
|
89
|
-
|
166
|
+
# Generate a stylesheet with a custom prefix
|
167
|
+
rails generate better_ui:stylesheet --prefix=my_theme
|
168
|
+
```
|
169
|
+
|
170
|
+
This will create:
|
171
|
+
1. A main SCSS file with imports for all components
|
172
|
+
2. Individual SCSS files for each component in the `app/assets/stylesheets/components/` directory
|
173
|
+
3. Special `*_overrides.scss` files for each component that allow you to override styles without modifying the original files
|
174
|
+
|
175
|
+
### Component Overrides System โจ
|
176
|
+
|
177
|
+
One of BetterUI's most powerful features is the automatic generation of override files:
|
178
|
+
|
179
|
+
```bash
|
180
|
+
# Generate override files for all components
|
181
|
+
rails generate better_ui:stylesheet
|
182
|
+
```
|
183
|
+
|
184
|
+
This creates special `*_overrides.scss` files that:
|
185
|
+
- Contain empty classes matching the original component files
|
186
|
+
- Allow for clean customization without modifying the original styles
|
187
|
+
- Support both standard and nested SCSS class structures
|
188
|
+
- Include helpful comments for easier modification
|
189
|
+
|
190
|
+
Example override file structure:
|
191
|
+
```scss
|
192
|
+
/* ==============================
|
193
|
+
* Button Component Overrides
|
194
|
+
* ==============================
|
195
|
+
* This file contains empty classes to override default styles.
|
196
|
+
* Add your customizations here.
|
197
|
+
*/
|
198
|
+
|
199
|
+
.bui-button-primary {
|
200
|
+
// Override styles for .bui-button-primary here
|
201
|
+
}
|
202
|
+
|
203
|
+
.bui-button-secondary {
|
204
|
+
// Override styles for .bui-button-secondary here
|
205
|
+
}
|
206
|
+
|
207
|
+
// Nested class overrides
|
208
|
+
.bui-button-with-icon {
|
209
|
+
.bui-icon {
|
210
|
+
// Override nested styles here
|
211
|
+
}
|
212
|
+
}
|
213
|
+
```
|
90
214
|
|
91
|
-
###
|
215
|
+
### Use the Initializer
|
92
216
|
|
93
|
-
|
217
|
+
Create a `config/initializers/better_ui.rb` file:
|
94
218
|
|
95
219
|
```ruby
|
96
220
|
BetterUi.configure do |config|
|
@@ -104,92 +228,92 @@ BetterUi.configure do |config|
|
|
104
228
|
end
|
105
229
|
```
|
106
230
|
|
107
|
-
###
|
231
|
+
### Customize Styles
|
108
232
|
|
109
|
-
|
233
|
+
Override CSS styles in your stylesheet:
|
110
234
|
|
111
235
|
```css
|
112
236
|
.better-ui-button-primary {
|
113
|
-
background-color: #8b5cf6; /*
|
237
|
+
background-color: #8b5cf6; /* Custom purple */
|
114
238
|
border-color: #8b5cf6;
|
115
239
|
}
|
116
240
|
```
|
117
241
|
|
118
|
-
## ๐
|
242
|
+
## ๐ Development
|
119
243
|
|
120
|
-
BetterUI
|
244
|
+
BetterUI uses an integrated dummy app for development and testing. Here's how to get started:
|
121
245
|
|
122
|
-
### Setup
|
246
|
+
### Initial Setup
|
123
247
|
|
124
248
|
```bash
|
125
|
-
#
|
249
|
+
# Clone the repository
|
126
250
|
git clone https://github.com/alessiobussolari/better_ui.git
|
127
251
|
cd better_ui
|
128
252
|
|
129
|
-
#
|
253
|
+
# Install dependencies
|
130
254
|
bin/setup
|
131
255
|
```
|
132
256
|
|
133
|
-
###
|
257
|
+
### Start the Test Application
|
134
258
|
|
135
|
-
|
259
|
+
To see components in action and work on documentation:
|
136
260
|
|
137
261
|
```bash
|
138
|
-
#
|
262
|
+
# Start the test server
|
139
263
|
cd test/dummy
|
140
264
|
bin/rails server
|
141
265
|
|
142
|
-
#
|
266
|
+
# Or from the main directory
|
143
267
|
bin/rails server -b 0.0.0.0
|
144
268
|
```
|
145
269
|
|
146
|
-
|
270
|
+
Visit `http://localhost:3000/better_ui` in your browser to see the documentation and test the components.
|
147
271
|
|
148
|
-
###
|
272
|
+
### Development Flow
|
149
273
|
|
150
|
-
1. **
|
151
|
-
2. **
|
152
|
-
3. **
|
153
|
-
4. **
|
154
|
-
5. **
|
155
|
-
6. **
|
274
|
+
1. **Create a branch**: `git checkout -b feature/new-component`
|
275
|
+
2. **Implement the component**: Add helpers in `app/helpers/better_ui/application_helper.rb`
|
276
|
+
3. **Add CSS**: Insert styles in `app/assets/stylesheets/better_ui/application.css`
|
277
|
+
4. **Document**: Create Markdown files in `docs/components/`
|
278
|
+
5. **Test**: Verify in the dummy app that everything works correctly
|
279
|
+
6. **Run tests**: `rake test`
|
156
280
|
|
157
|
-
### Build
|
281
|
+
### Build and Release
|
158
282
|
|
159
|
-
|
283
|
+
To build the gem locally:
|
160
284
|
|
161
285
|
```bash
|
162
286
|
bundle exec rake build
|
163
287
|
```
|
164
288
|
|
165
|
-
|
289
|
+
To install the development version:
|
166
290
|
|
167
291
|
```bash
|
168
292
|
bundle exec rake install
|
169
293
|
```
|
170
294
|
|
171
|
-
## ๐ค
|
295
|
+
## ๐ค Contributing
|
172
296
|
|
173
|
-
|
297
|
+
Contributions are welcome and appreciated! Here's how you can help:
|
174
298
|
|
175
|
-
1. **Fork**
|
176
|
-
2. **
|
177
|
-
3. **
|
178
|
-
4. **
|
179
|
-
5. **
|
180
|
-
6.
|
299
|
+
1. **Fork** the repository on GitHub
|
300
|
+
2. **Clone** your fork: `git clone https://github.com/YOUR-USERNAME/better_ui.git`
|
301
|
+
3. **Create** a branch for your feature: `git checkout -b feature/amazing-improvement`
|
302
|
+
4. **Commit** your changes: `git commit -am 'Add an amazing feature'`
|
303
|
+
5. **Push** to the branch: `git push origin feature/amazing-improvement`
|
304
|
+
6. Submit a **Pull Request**
|
181
305
|
|
182
|
-
###
|
306
|
+
### Guidelines
|
183
307
|
|
184
|
-
-
|
185
|
-
-
|
186
|
-
-
|
187
|
-
-
|
308
|
+
- Follow the existing code style
|
309
|
+
- Write tests for new features
|
310
|
+
- Update documentation
|
311
|
+
- Create components that work without JavaScript
|
188
312
|
|
189
|
-
## ๐
|
313
|
+
## ๐ License
|
190
314
|
|
191
|
-
BetterUI
|
315
|
+
BetterUI is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
192
316
|
|
193
317
|
---
|
194
318
|
|
195
|
-
|
319
|
+
Made with โค๏ธ by [Alessio Bussolari](https://github.com/alessiobussolari)
|
@@ -0,0 +1,138 @@
|
|
1
|
+
// Navbar controller per gestire il comportamento del menu mobile e dei dropdown
|
2
|
+
import { Controller } from "@hotwired/stimulus"
|
3
|
+
|
4
|
+
export default class extends Controller {
|
5
|
+
static targets = ["menu", "dropdown", "submenu"]
|
6
|
+
|
7
|
+
connect() {
|
8
|
+
// Verifica se siamo su mobile e aggiorna lo stato del menu
|
9
|
+
this.updateMenuState();
|
10
|
+
|
11
|
+
// Aggiungi un event listener per il resize della finestra
|
12
|
+
window.addEventListener("resize", this.updateMenuState.bind(this));
|
13
|
+
|
14
|
+
// Chiudi menu quando si clicca su un link (solo su mobile)
|
15
|
+
if (this.hasMenuTarget) {
|
16
|
+
const links = this.menuTarget.querySelectorAll("a");
|
17
|
+
links.forEach(link => {
|
18
|
+
link.addEventListener("click", () => {
|
19
|
+
// Se siamo su mobile, chiudi il menu
|
20
|
+
if (window.innerWidth < 768) {
|
21
|
+
this.closeMenu();
|
22
|
+
}
|
23
|
+
});
|
24
|
+
});
|
25
|
+
}
|
26
|
+
|
27
|
+
// Aggiungi listener per i click all'esterno del menu
|
28
|
+
document.addEventListener("click", this.handleClickOutside.bind(this))
|
29
|
+
}
|
30
|
+
|
31
|
+
disconnect() {
|
32
|
+
// Rimuovi event listener per il resize della finestra
|
33
|
+
window.removeEventListener("resize", this.updateMenuState.bind(this));
|
34
|
+
|
35
|
+
// Rimuovi listener al disconnette
|
36
|
+
document.removeEventListener("click", this.handleClickOutside.bind(this))
|
37
|
+
}
|
38
|
+
|
39
|
+
// Metodo per alternare l'apertura/chiusura del menu
|
40
|
+
toggleMenu(event) {
|
41
|
+
event.stopPropagation()
|
42
|
+
const isExpanded = this.menuTarget.classList.contains("hidden") === false
|
43
|
+
|
44
|
+
if (isExpanded) {
|
45
|
+
this.closeMenu()
|
46
|
+
} else {
|
47
|
+
this.openMenu()
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
// Metodo per chiudere il menu
|
52
|
+
closeMenu() {
|
53
|
+
this.menuTarget.classList.add("hidden")
|
54
|
+
|
55
|
+
// Aggiorna l'attributo aria-expanded
|
56
|
+
const button = this.element.querySelector("[aria-controls='navbar-menu']")
|
57
|
+
if (button) {
|
58
|
+
button.setAttribute("aria-expanded", "false")
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
// Metodo per aggiornare lo stato del menu in base alla dimensione della finestra
|
63
|
+
updateMenuState() {
|
64
|
+
if (this.hasMenuTarget) {
|
65
|
+
// Se siamo su desktop (md breakpoint - 768px)
|
66
|
+
if (window.innerWidth >= 768) {
|
67
|
+
// Assicurati che il menu sia visibile su desktop
|
68
|
+
this.menuTarget.classList.remove("hidden");
|
69
|
+
this.menuTarget.classList.add("md:block");
|
70
|
+
} else {
|
71
|
+
// Su mobile, nascondi il menu di default
|
72
|
+
this.menuTarget.classList.add("hidden");
|
73
|
+
}
|
74
|
+
|
75
|
+
// Aggiorna l'attributo aria-expanded
|
76
|
+
const button = this.element.querySelector("[aria-controls='navbar-menu']");
|
77
|
+
if (button) {
|
78
|
+
const isExpanded = window.innerWidth >= 768 ? "true" : "false";
|
79
|
+
button.setAttribute("aria-expanded", isExpanded);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
openMenu() {
|
85
|
+
this.menuTarget.classList.remove("hidden")
|
86
|
+
|
87
|
+
// Chiudi tutti i dropdown nel menu mobile
|
88
|
+
if (this.hasSubmenuTarget) {
|
89
|
+
this.submenuTargets.forEach(submenu => {
|
90
|
+
submenu.classList.add("hidden")
|
91
|
+
})
|
92
|
+
}
|
93
|
+
|
94
|
+
// Aggiorna stato del pulsante
|
95
|
+
event.currentTarget.setAttribute("aria-expanded", "true")
|
96
|
+
}
|
97
|
+
|
98
|
+
toggleDropdown(event) {
|
99
|
+
event.stopPropagation()
|
100
|
+
const button = event.currentTarget
|
101
|
+
const dropdownId = button.getAttribute("aria-controls")
|
102
|
+
const dropdown = document.getElementById(dropdownId)
|
103
|
+
|
104
|
+
if (dropdown) {
|
105
|
+
const isExpanded = button.getAttribute("aria-expanded") === "true"
|
106
|
+
|
107
|
+
if (isExpanded) {
|
108
|
+
dropdown.classList.add("hidden")
|
109
|
+
button.setAttribute("aria-expanded", "false")
|
110
|
+
} else {
|
111
|
+
// Chiudi tutti gli altri dropdown prima di aprire quello corrente
|
112
|
+
if (this.hasSubmenuTarget) {
|
113
|
+
this.submenuTargets.forEach(submenu => {
|
114
|
+
if (submenu.id !== dropdownId) {
|
115
|
+
submenu.classList.add("hidden")
|
116
|
+
|
117
|
+
// Trova e aggiorna il pulsante associato
|
118
|
+
const associatedButton = this.element.querySelector(`[aria-controls='${submenu.id}']`)
|
119
|
+
if (associatedButton) {
|
120
|
+
associatedButton.setAttribute("aria-expanded", "false")
|
121
|
+
}
|
122
|
+
}
|
123
|
+
})
|
124
|
+
}
|
125
|
+
|
126
|
+
dropdown.classList.remove("hidden")
|
127
|
+
button.setAttribute("aria-expanded", "true")
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
handleClickOutside(event) {
|
133
|
+
// Chiudi il menu se si fa clic all'esterno
|
134
|
+
if (this.element.contains(event.target) === false) {
|
135
|
+
this.closeMenu()
|
136
|
+
}
|
137
|
+
}
|
138
|
+
}
|