Skip to content

Commit f7ae4e1

Browse files
committed
Start sketching out item docs
1 parent 9a2c03e commit f7ae4e1

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

docs/advanced-features/navigation-api.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,118 @@ The `NavigationMenu` class represents a navigation menu. It contains a collectio
157157

158158
The `NavigationItem` class represents a single item in a navigation menu. It contains information such as the destination link or route, a label, and priority for ordering in the menu.
159159

160+
Here is the constructor signature and a quick reference of the methods available on the `NavigationItem` class, and their types. Keep on reading to see more detailed examples and explanations.
161+
162+
```php
163+
use Hyde\Framework\Features\Navigation\NavigationItem;
164+
165+
$item = new NavigationItem(Route|string $destination, string $label, int $priority = 500);
166+
167+
$item->getLink(): string; // Returns the resolved link route or destination URL.
168+
$item->getLabel(): string; // Returns the label of the item.
169+
$item->isActive(): bool; // Returns true if the item is the page being rendered.
170+
$item->getPriority(): int; // Returns the priority of the item.
171+
$item->getPage(): ?HydePage; // Returns the underlying Page instance, if there is one.
172+
```
173+
174+
### Creating Navigation Items
175+
176+
There are several ways to create navigation items using the `NavigationItem` class.
177+
178+
#### Direct instantiation
179+
180+
You can create instances directly by passing either a URL or a Route instance to the constructor.
181+
182+
The first parameter is the destination, the second is the label, and the third optional parameter is the priority.
183+
184+
```php
185+
$item = new NavigationItem('index', 'Home');
186+
$item = new NavigationItem(Routes::get('index'), 'Home');
187+
$item = new NavigationItem('https://example.com', 'External Link');
188+
```
189+
190+
#### Setting Priority
191+
192+
You can set the priority of the item, which determines its position in the menu.
193+
194+
```php
195+
$item = new NavigationItem(Routes::get('index'), 'Home', 25);
196+
```
197+
198+
#### Static Creation Method
199+
200+
You can use the static `create` method, which can automatically fill in the label and priority from a Route.
201+
202+
```php
203+
$item = NavigationItem::create(Routes::get('index'));
204+
```
205+
206+
You can also pass a custom label and priority to the `create` method to override the defaults.
207+
208+
```php
209+
$item = NavigationItem::create(Routes::get('index'), 'Custom Label', 50);
210+
```
211+
212+
#### Using Route Keys and URLs
213+
214+
The `create` method works with route keys and URLs.
215+
216+
```php
217+
$item = NavigationItem::create('index');
218+
$item = NavigationItem::create('https://example.com');
219+
```
220+
221+
Unless you pass a custom label to URL items, the label will be the URL itself.
222+
223+
```php
224+
$item = NavigationItem::create('https://example.com');
225+
```
226+
227+
### Navigation Item Methods
228+
229+
### Getting Label and Link
230+
231+
```php
232+
$item = NavigationItem::create('index');
233+
234+
// Get the label of the item.
235+
$item->getLabel(); // Returns 'Home'
236+
237+
// Get the link of the item.
238+
$item->getLink(); // Returns 'index.html'
239+
```
240+
241+
You can also get the link by casting the item to a string.
242+
243+
```php
244+
(string) $item; // Returns 'index.html'
245+
```
246+
247+
### Getting Priority
248+
249+
```php
250+
$item->getPriority(); // Returns 0
251+
```
252+
253+
### Getting Page Instance
254+
255+
You can get the underlying Page instance of the item, if it exists.
256+
257+
```php
258+
$item->getPage(); // Returns instance of BladePage or null
259+
```
260+
261+
### Checking Active State
262+
263+
You can check if the item is active (i.e., the current page being rendered).
264+
265+
```php
266+
$item->isActive(); // Returns false
267+
```
268+
269+
This concludes the documentation for the `NavigationItem` class.
270+
271+
160272
## NavigationGroup
161273

162274
The `NavigationGroup` class represents a group of items in a navigation menu. It contains a label, priority, and a collection of navigation items.

0 commit comments

Comments
 (0)