@itrocks/menu 0.0.5 → 0.0.6
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.
- package/README.md +220 -0
- package/package.json +9 -1
package/README.md
CHANGED
|
@@ -7,3 +7,223 @@
|
|
|
7
7
|
# menu
|
|
8
8
|
|
|
9
9
|
A flexible and accessible customizable main menu component with sidebar layout.
|
|
10
|
+
|
|
11
|
+
*This documentation was written by an artificial intelligence and may contain errors or approximations.
|
|
12
|
+
It has not yet been fully reviewed by a human. If anything seems unclear or incomplete,
|
|
13
|
+
please feel free to contact the author of this package.*
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i @itrocks/menu
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
`@itrocks/menu` provides a small set of classes you use to represent the
|
|
24
|
+
structure of your application menu. You typically:
|
|
25
|
+
|
|
26
|
+
1. Build a `Menu` instance from a simple configuration object.
|
|
27
|
+
2. Pass that instance to your view layer (for example the it.rocks
|
|
28
|
+
template engine) to render the navigation.
|
|
29
|
+
|
|
30
|
+
Each menu is composed of blocks (`MenuBlock`) that contain items
|
|
31
|
+
(`MenuItem`). Both blocks and items have a `title` and a CSS-friendly
|
|
32
|
+
`class` derived from that title.
|
|
33
|
+
|
|
34
|
+
### Minimal example
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { Menu } from '@itrocks/menu'
|
|
38
|
+
|
|
39
|
+
const menu = new Menu({
|
|
40
|
+
'General': {
|
|
41
|
+
'/': 'Home',
|
|
42
|
+
'/dashboard': 'Dashboard',
|
|
43
|
+
},
|
|
44
|
+
'Account': {
|
|
45
|
+
'/profile': 'My profile',
|
|
46
|
+
'/logout': 'Sign out',
|
|
47
|
+
},
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
// menu.blocks is now an array of MenuBlock instances
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
This configuration creates two blocks, **General** and **Account**, each
|
|
54
|
+
containing several navigation items.
|
|
55
|
+
|
|
56
|
+
### Complete example with template rendering
|
|
57
|
+
|
|
58
|
+
In a real it.rocks application, the framework builds a `Menu` instance
|
|
59
|
+
from your configuration and exposes it to the HTML templates that render
|
|
60
|
+
the main layout.
|
|
61
|
+
|
|
62
|
+
The provided `menu.html` file illustrates how a typical sidebar layout
|
|
63
|
+
is generated from a `Menu` instance:
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<nav class="app menu">
|
|
67
|
+
<ul>
|
|
68
|
+
<!-- for each block in menu.blocks -->
|
|
69
|
+
<li class="{class}">
|
|
70
|
+
<h3>{title.tr}</h3>
|
|
71
|
+
<ul>
|
|
72
|
+
<!-- for each item in block.items -->
|
|
73
|
+
<li>
|
|
74
|
+
<a href="app://(link)" target="main">{title.tr}</a>
|
|
75
|
+
</li>
|
|
76
|
+
</ul>
|
|
77
|
+
</li>
|
|
78
|
+
</ul>
|
|
79
|
+
</nav>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
You normally do not manipulate this template directly when using the
|
|
83
|
+
full it.rocks framework, but it shows the structure that will be
|
|
84
|
+
rendered: a navigation element containing an unordered list of blocks,
|
|
85
|
+
each with its own title and list of items.
|
|
86
|
+
|
|
87
|
+
If you want to reuse `@itrocks/menu` outside of the framework, you can
|
|
88
|
+
build a `Menu` instance as shown above and render it with your own
|
|
89
|
+
templating system by iterating over `menu.blocks` and `block.items`.
|
|
90
|
+
|
|
91
|
+
## API
|
|
92
|
+
|
|
93
|
+
### `class Menu`
|
|
94
|
+
|
|
95
|
+
Represents the whole application menu, made of multiple blocks of items.
|
|
96
|
+
|
|
97
|
+
#### Constructor
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
constructor(config?: Record<string, Record<string, string>>)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
If a configuration object is provided, it is immediately loaded using
|
|
104
|
+
`loadConfig`.
|
|
105
|
+
|
|
106
|
+
The configuration format is:
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
type MenuConfig = Record<
|
|
110
|
+
string, // block title
|
|
111
|
+
Record<string, string> // link -> item title
|
|
112
|
+
>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
- The **keys** of the outer object are block titles.
|
|
116
|
+
- For each block, the **keys** of the inner object are item links
|
|
117
|
+
(typically application routes), and the **values** are item titles
|
|
118
|
+
shown to the user.
|
|
119
|
+
|
|
120
|
+
#### Properties
|
|
121
|
+
|
|
122
|
+
- `blocks: MenuBlock[]` – list of blocks that make up the menu. The
|
|
123
|
+
order of the blocks follows the insertion order of the configuration
|
|
124
|
+
object.
|
|
125
|
+
|
|
126
|
+
#### Methods
|
|
127
|
+
|
|
128
|
+
##### `loadConfig(config: Record<string, Record<string, string>>): void`
|
|
129
|
+
|
|
130
|
+
Clears the current menu structure and rebuilds it from the provided
|
|
131
|
+
configuration object.
|
|
132
|
+
|
|
133
|
+
For each `[blockTitle, blockItems]` entry in `config` a new
|
|
134
|
+
`MenuBlock(blockTitle)` is created, and for each `[link, title]` in
|
|
135
|
+
`blockItems` a `MenuItem(title, link)` is added to the block.
|
|
136
|
+
|
|
137
|
+
You can call `loadConfig` again to replace the menu at runtime (for
|
|
138
|
+
example when switching tenant, language, or user role).
|
|
139
|
+
|
|
140
|
+
#### Example
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { Menu } from '@itrocks/menu'
|
|
144
|
+
|
|
145
|
+
const menu = new Menu()
|
|
146
|
+
|
|
147
|
+
menu.loadConfig({
|
|
148
|
+
'Admin': {
|
|
149
|
+
'/admin/users': 'Users',
|
|
150
|
+
'/admin/settings': 'Settings',
|
|
151
|
+
},
|
|
152
|
+
})
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### `class MenuBlock`
|
|
158
|
+
|
|
159
|
+
Represents a group of related menu items (for example a section in a
|
|
160
|
+
sidebar).
|
|
161
|
+
|
|
162
|
+
Blocks are created automatically by `Menu.loadConfig`, but you can also
|
|
163
|
+
instantiate them manually if you build the structure by hand.
|
|
164
|
+
|
|
165
|
+
#### Constructor
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
constructor(title: string)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
#### Properties
|
|
172
|
+
|
|
173
|
+
- `title: string` – human-readable title of the block, as it should
|
|
174
|
+
appear in the menu.
|
|
175
|
+
- `class: string` (getter) – CSS-friendly identifier derived from
|
|
176
|
+
`title`, lowercased with all non-alphanumeric characters replaced by
|
|
177
|
+
dashes (`-`). This is useful to style each block differently.
|
|
178
|
+
- `items: MenuItem[]` – list of items belonging to this block.
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
### `class MenuItem`
|
|
183
|
+
|
|
184
|
+
Represents a single clickable entry in a menu block.
|
|
185
|
+
|
|
186
|
+
Items are created automatically by `Menu.loadConfig`, but you can also
|
|
187
|
+
instantiate them manually.
|
|
188
|
+
|
|
189
|
+
#### Constructor
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
constructor(title: string, link: string)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### Properties
|
|
196
|
+
|
|
197
|
+
- `title: string` – label displayed to the user.
|
|
198
|
+
- `class: string` (getter) – CSS-friendly identifier derived from
|
|
199
|
+
`title`, lowercased with all non-alphanumeric characters replaced by
|
|
200
|
+
dashes (`-`). This lets you style certain items differently.
|
|
201
|
+
- `link: string` – navigation target associated with the item. In a
|
|
202
|
+
full it.rocks application this is typically a route handled by the
|
|
203
|
+
framework.
|
|
204
|
+
|
|
205
|
+
#### Example
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
import { Menu, MenuBlock, MenuItem } from '@itrocks/menu'
|
|
209
|
+
|
|
210
|
+
const menu = new Menu()
|
|
211
|
+
const adminBlock = new MenuBlock('Administration')
|
|
212
|
+
|
|
213
|
+
adminBlock.items.push(new MenuItem('Users', '/admin/users'))
|
|
214
|
+
adminBlock.items.push(new MenuItem('Settings', '/admin/settings'))
|
|
215
|
+
|
|
216
|
+
menu.blocks.push(adminBlock)
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Typical use cases
|
|
220
|
+
|
|
221
|
+
- Build the main sidebar or header menu of an it.rocks application from
|
|
222
|
+
a simple configuration object.
|
|
223
|
+
- Generate different menus depending on the current user role, tenant or
|
|
224
|
+
context by loading different configurations at runtime.
|
|
225
|
+
- Integrate a navigation structure into your own templating system by
|
|
226
|
+
iterating over `menu.blocks` and `block.items`.
|
|
227
|
+
- Style individual blocks and items through their automatically derived
|
|
228
|
+
`class` property, without hard-coding CSS class names in your
|
|
229
|
+
templates.
|
package/package.json
CHANGED
|
@@ -22,15 +22,23 @@
|
|
|
22
22
|
"!*.map"
|
|
23
23
|
],
|
|
24
24
|
"keywords": [
|
|
25
|
+
"bar",
|
|
25
26
|
"component",
|
|
26
27
|
"front-end",
|
|
28
|
+
"html",
|
|
27
29
|
"it.rocks",
|
|
28
30
|
"layout",
|
|
31
|
+
"main",
|
|
32
|
+
"main-menu",
|
|
29
33
|
"menu",
|
|
30
34
|
"navigation",
|
|
35
|
+
"navigation-bar",
|
|
31
36
|
"responsive",
|
|
32
37
|
"sidebar",
|
|
38
|
+
"sidebar-menu",
|
|
39
|
+
"standard",
|
|
33
40
|
"template",
|
|
41
|
+
"view",
|
|
34
42
|
"UI",
|
|
35
43
|
"UX",
|
|
36
44
|
"web"
|
|
@@ -46,5 +54,5 @@
|
|
|
46
54
|
"build:css": "sass --no-source-map src:css"
|
|
47
55
|
},
|
|
48
56
|
"types": "./cjs/menu.d.ts",
|
|
49
|
-
"version": "0.0.
|
|
57
|
+
"version": "0.0.6"
|
|
50
58
|
}
|