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.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +199 -75
  3. data/app/assets/javascripts/better_ui/controllers/navbar_controller.js +138 -0
  4. data/app/assets/javascripts/better_ui/controllers/sidebar_controller.js +211 -0
  5. data/app/assets/javascripts/better_ui/controllers/toast_controller.js +161 -0
  6. data/app/assets/javascripts/better_ui/index.js +159 -0
  7. data/app/assets/javascripts/better_ui/toast_manager.js +77 -0
  8. data/app/assets/stylesheets/better_ui/application.css +25 -351
  9. data/app/components/better_ui/application/alert_component.html.erb +27 -0
  10. data/app/components/better_ui/application/alert_component.rb +202 -0
  11. data/app/components/better_ui/application/card_component.html.erb +24 -0
  12. data/app/components/better_ui/application/card_component.rb +53 -0
  13. data/app/components/better_ui/application/card_container_component.html.erb +8 -0
  14. data/app/components/better_ui/application/card_container_component.rb +14 -0
  15. data/app/components/better_ui/application/header_component.html.erb +88 -0
  16. data/app/components/better_ui/application/header_component.rb +188 -0
  17. data/app/components/better_ui/application/navbar_component.html.erb +294 -0
  18. data/app/components/better_ui/application/navbar_component.rb +249 -0
  19. data/app/components/better_ui/application/sidebar_component.html.erb +207 -0
  20. data/app/components/better_ui/application/sidebar_component.rb +318 -0
  21. data/app/components/better_ui/application/toast_component.html.erb +35 -0
  22. data/app/components/better_ui/application/toast_component.rb +223 -0
  23. data/app/components/better_ui/general/avatar_component.html.erb +19 -0
  24. data/app/components/better_ui/general/avatar_component.rb +171 -0
  25. data/app/components/better_ui/general/badge_component.html.erb +19 -0
  26. data/app/components/better_ui/general/badge_component.rb +123 -0
  27. data/app/components/better_ui/general/breadcrumb_component.html.erb +15 -0
  28. data/app/components/better_ui/general/breadcrumb_component.rb +130 -0
  29. data/app/components/better_ui/general/button_component.html.erb +34 -0
  30. data/app/components/better_ui/general/button_component.rb +162 -0
  31. data/app/components/better_ui/general/heading_component.html.erb +25 -0
  32. data/app/components/better_ui/general/heading_component.rb +148 -0
  33. data/app/components/better_ui/general/icon_component.html.erb +2 -0
  34. data/app/components/better_ui/general/icon_component.rb +100 -0
  35. data/app/components/better_ui/general/link_component.html.erb +17 -0
  36. data/app/components/better_ui/general/link_component.rb +132 -0
  37. data/app/components/better_ui/general/panel_component.html.erb +27 -0
  38. data/app/components/better_ui/general/panel_component.rb +103 -0
  39. data/app/components/better_ui/general/spinner_component.html.erb +15 -0
  40. data/app/components/better_ui/general/spinner_component.rb +79 -0
  41. data/app/components/better_ui/general/table_component.html.erb +73 -0
  42. data/app/components/better_ui/general/table_component.rb +167 -0
  43. data/app/components/better_ui/theme_helper.rb +171 -0
  44. data/app/controllers/better_ui/application_controller.rb +1 -0
  45. data/app/controllers/better_ui/docs_controller.rb +18 -25
  46. data/app/views/components/better_ui/general/table/_custom_body_row.html.erb +17 -0
  47. data/app/views/components/better_ui/general/table/_custom_footer_rows.html.erb +17 -0
  48. data/app/views/components/better_ui/general/table/_custom_header_rows.html.erb +12 -0
  49. data/app/views/layouts/component_preview.html.erb +32 -0
  50. data/config/initializers/lookbook.rb +23 -0
  51. data/config/routes.rb +6 -1
  52. data/lib/better_ui/engine.rb +18 -1
  53. data/lib/better_ui/version.rb +1 -1
  54. data/lib/better_ui.rb +4 -0
  55. data/lib/generators/better_ui/stylesheet_generator.rb +96 -0
  56. data/lib/generators/better_ui/templates/README +56 -0
  57. data/lib/generators/better_ui/templates/components/_avatar.scss +151 -0
  58. data/lib/generators/better_ui/templates/components/_badge.scss +142 -0
  59. data/lib/generators/better_ui/templates/components/_breadcrumb.scss +107 -0
  60. data/lib/generators/better_ui/templates/components/_button.scss +106 -0
  61. data/lib/generators/better_ui/templates/components/_card.scss +69 -0
  62. data/lib/generators/better_ui/templates/components/_heading.scss +180 -0
  63. data/lib/generators/better_ui/templates/components/_icon.scss +90 -0
  64. data/lib/generators/better_ui/templates/components/_link.scss +130 -0
  65. data/lib/generators/better_ui/templates/components/_panel.scss +144 -0
  66. data/lib/generators/better_ui/templates/components/_spinner.scss +132 -0
  67. data/lib/generators/better_ui/templates/components/_table.scss +105 -0
  68. data/lib/generators/better_ui/templates/components/_variables.scss +33 -0
  69. data/lib/generators/better_ui/templates/components_stylesheet.scss +66 -0
  70. metadata +135 -10
  71. data/app/helpers/better_ui/application_helper.rb +0 -183
  72. data/app/views/better_ui/docs/component.html.erb +0 -365
  73. data/app/views/better_ui/docs/index.html.erb +0 -100
  74. data/app/views/better_ui/docs/show.html.erb +0 -60
  75. 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: 8c59e86ef83ec560909908edbbbecd0c33a3663df87de94e99fff0475be5b23d
4
- data.tar.gz: dc7a3b578f0b2cc70f3e25c843dbc918723bf4d0b7d9bdb4b511e2ad073cc5fd
3
+ metadata.gz: 317837f4f80e12e6ab860a7a134e9f17e01f5db2d86f5b178a4c9a2bc9588f3a
4
+ data.tar.gz: 8815e3b40a461070b9cb178ee1a7e1e8abaccbf6cea7366ab5c079db174429f4
5
5
  SHA512:
6
- metadata.gz: 66fdc7a9f14ff11b0d79caaff30d1b35ba4878dbbfcbddca7571684eb3628c62288a4fff4a33416b285c479ab96ca22ed271949184c472cb36f4131270a15552
7
- data.tar.gz: abaf0df7af66450518ab1620ca0fe165e411c2b96a6accfc3a49acb91a93b82c64c7093bacbb7e5e272040275b96063dee0253608a08f406d2e1122ff78cb8c3
6
+ metadata.gz: 0fb96fb9537391b7448ffde247cd8d9ae4916dac0cfcd17c4efe912b061dfd05986368ebe6563549c58ce0909e9a509474ac885e136d133214f806437925726d
7
+ data.tar.gz: de95bf54dcd6fa022f5afa5c0867d65b7a78e8f24cab637aa7cf098f0bf01f5cabdac5e6c7b36ba1aba14833682b17174e1d229a4d428f5646d92ee876ad42f4
data/README.md CHANGED
@@ -3,58 +3,66 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/better_ui.svg)](https://badge.fury.io/rb/better_ui)
4
4
  [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
5
5
 
6
- > Componenti UI eleganti, coerenti e facilmente integrabili per le tue applicazioni Rails
6
+ > Elegant, consistent, and easily integrable UI components for your Rails applications
7
7
 
8
- BetterUI รจ una gemma Rails che fornisce componenti UI riutilizzabili con documentazione integrata. Progettata per essere leggera, personalizzabile e facile da usare, ti aiuta a costruire interfacce belle e consistenti senza JavaScript.
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
- ## โœจ Caratteristiche principali
10
+ ## โœจ Key Features
11
11
 
12
- - **Design puro** - Componenti UI eleganti e completamente CSS, senza JavaScript
13
- - **Facile integrazione** - Engine Rails montabile che si integra perfettamente con la tua app
14
- - **Documentazione integrata** - Visualizza esempi e documentazione direttamente nel browser
15
- - **Altamente personalizzabile** - Adatta facilmente lo stile al tuo brand
16
- - **Componenti modulari** - Usa solo ciรฒ di cui hai bisogno
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
- ## ๐Ÿ“ฆ Componenti disponibili
19
+ ## ๐Ÿ“ฆ Available Components
19
20
 
20
- | Componente | Descrizione |
21
+ | Component | Description |
21
22
  |------------|-------------|
22
- | **Button** | Pulsanti stilizzati con varianti primary, secondary, success, danger |
23
- | **Alert** | Messaggi informativi, successi, avvisi ed errori |
24
- | **Card** | Contenitori flessibili con header, body e footer |
25
- | **Modal** | Finestre di dialogo modali |
26
- | **Tabs** | Navigazione a schede |
27
- | **Form Elements** | Campi di input stilizzati (in arrivo) |
28
-
29
- ## ๐Ÿš€ Installazione
30
-
31
- Aggiungi questa riga al Gemfile della tua applicazione:
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
- E poi esegui:
45
+ And then execute:
38
46
 
39
47
  ```bash
40
48
  $ bundle install
41
49
  ```
42
50
 
43
- ## โš™๏ธ Configurazione
51
+ ## โš™๏ธ Configuration
44
52
 
45
- ### Monta l'engine
53
+ ### Mount the Engine
46
54
 
47
- Aggiungi questo al tuo file `config/routes.rb`:
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
- # ... altre tue route
61
+ # ... your other routes
54
62
  end
55
63
  ```
56
64
 
57
- ### Includi gli assets
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
- ## ๐ŸŽฎ Utilizzo
75
+ ## ๐ŸŽฎ Usage
68
76
 
69
- Una volta installato, puoi iniziare ad usare i componenti:
77
+ Once installed, you can start using the components:
70
78
 
71
79
  ```erb
72
80
  <%# Button %>
73
- <%= better_ui_button("Salva", type: "primary") %>
81
+ <%= better_ui_button("Save", type: "primary") %>
74
82
 
75
83
  <%# Alert %>
76
- <%= better_ui_alert("Operazione completata", type: "success") %>
84
+ <%= better_ui_alert("Operation completed", type: "success") %>
77
85
 
78
86
  <%# Card %>
79
87
  <%= better_ui_card do %>
80
- <%= better_ui_card_header("Titolo Card") %>
88
+ <%= better_ui_card_header("Card Title") %>
81
89
  <%= better_ui_card_body do %>
82
- <p>Contenuto della card...</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
- Visita `/better_ui` nella tua applicazione per vedere la documentazione completa e gli esempi.
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
- ## ๐ŸŽจ Personalizzazione
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
- ### Usa l'inizializzatore
215
+ ### Use the Initializer
92
216
 
93
- Crea un file `config/initializers/better_ui.rb`:
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
- ### Personalizza gli stili
231
+ ### Customize Styles
108
232
 
109
- Sovrascrivi gli stili CSS nel tuo foglio di stile:
233
+ Override CSS styles in your stylesheet:
110
234
 
111
235
  ```css
112
236
  .better-ui-button-primary {
113
- background-color: #8b5cf6; /* Viola personalizzato */
237
+ background-color: #8b5cf6; /* Custom purple */
114
238
  border-color: #8b5cf6;
115
239
  }
116
240
  ```
117
241
 
118
- ## ๐Ÿ›  Sviluppo
242
+ ## ๐Ÿ›  Development
119
243
 
120
- BetterUI utilizza una app dummy integrata per lo sviluppo e i test. Ecco come iniziare:
244
+ BetterUI uses an integrated dummy app for development and testing. Here's how to get started:
121
245
 
122
- ### Setup iniziale
246
+ ### Initial Setup
123
247
 
124
248
  ```bash
125
- # Clona il repository
249
+ # Clone the repository
126
250
  git clone https://github.com/alessiobussolari/better_ui.git
127
251
  cd better_ui
128
252
 
129
- # Installa le dipendenze
253
+ # Install dependencies
130
254
  bin/setup
131
255
  ```
132
256
 
133
- ### Avvia l'applicazione di test
257
+ ### Start the Test Application
134
258
 
135
- Per vedere i componenti in azione e lavorare sulla documentazione:
259
+ To see components in action and work on documentation:
136
260
 
137
261
  ```bash
138
- # Avvia il server di test
262
+ # Start the test server
139
263
  cd test/dummy
140
264
  bin/rails server
141
265
 
142
- # Oppure dalla directory principale
266
+ # Or from the main directory
143
267
  bin/rails server -b 0.0.0.0
144
268
  ```
145
269
 
146
- Visita `http://localhost:3000/better_ui` nel tuo browser per vedere la documentazione e testare i componenti.
270
+ Visit `http://localhost:3000/better_ui` in your browser to see the documentation and test the components.
147
271
 
148
- ### Flusso di sviluppo
272
+ ### Development Flow
149
273
 
150
- 1. **Crea un branch**: `git checkout -b feature/nuovo-componente`
151
- 2. **Implementa il componente**: Aggiungi helper in `app/helpers/better_ui/application_helper.rb`
152
- 3. **Aggiungi CSS**: Inserisci gli stili in `app/assets/stylesheets/better_ui/application.css`
153
- 4. **Documenta**: Crea file Markdown in `docs/components/`
154
- 5. **Testa**: Verifica nell'app dummy che tutto funzioni correttamente
155
- 6. **Esegui i test**: `rake test`
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 e rilascio
281
+ ### Build and Release
158
282
 
159
- Per buildare la gemma localmente:
283
+ To build the gem locally:
160
284
 
161
285
  ```bash
162
286
  bundle exec rake build
163
287
  ```
164
288
 
165
- Per installare la versione in sviluppo:
289
+ To install the development version:
166
290
 
167
291
  ```bash
168
292
  bundle exec rake install
169
293
  ```
170
294
 
171
- ## ๐Ÿค Contribuire
295
+ ## ๐Ÿค Contributing
172
296
 
173
- I contributi sono benvenuti e apprezzati! Ecco come puoi aiutare:
297
+ Contributions are welcome and appreciated! Here's how you can help:
174
298
 
175
- 1. **Fork** il repository su GitHub
176
- 2. **Clona** il tuo fork: `git clone https://github.com/TUO-USERNAME/better_ui.git`
177
- 3. **Crea** un branch per la tua feature: `git checkout -b feature/incredibile-miglioramento`
178
- 4. **Committa** i tuoi cambiamenti: `git commit -am 'Aggiunge una funzionalitร  incredibile'`
179
- 5. **Pusha** al branch: `git push origin feature/incredibile-miglioramento`
180
- 6. Invia una **Pull Request**
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
- ### Linee guida
306
+ ### Guidelines
183
307
 
184
- - Segui lo stile di codice esistente
185
- - Scrivi test per le nuove funzionalitร 
186
- - Aggiorna la documentazione
187
- - Crea componenti che funzionano senza JavaScript
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
- ## ๐Ÿ“„ Licenza
313
+ ## ๐Ÿ“„ License
190
314
 
191
- BetterUI รจ disponibile come open source sotto i termini della [Licenza MIT](https://opensource.org/licenses/MIT).
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
- Realizzato con โค๏ธ da [Alessio Bussolari](https://github.com/alessiobussolari)
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
+ }