Skip to content

Commit e36bd60

Browse files
docs(topnav): add top-nav-item documentation
1 parent 3b2f530 commit e36bd60

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

packages/top-nav/top-nav-item.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
## Overview
2+
3+
`<sp-top-nav-item>` represents individual navigation links within a [`<sp-top-nav>` component](/components/top-nav). Each item extends standard anchor functionality with Spectrum styling, automatic selection management, and enhanced accessibility features.
4+
5+
### Usage
6+
7+
[![See it on NPM!](https://img.shields.io/npm/v/@spectrum-web-components/top-nav?style=for-the-badge)](https://www.npmjs.com/package/@spectrum-web-components/top-nav)
8+
[![How big is this package in your project?](https://img.shields.io/bundlephobia/minzip/@spectrum-web-components/top-nav?style=for-the-badge)](https://bundlephobia.com/result?p=@spectrum-web-components/top-nav)
9+
[![Try it on Stackblitz](https://img.shields.io/badge/Try%20it%20on-Stackblitz-blue?style=for-the-badge)](https://stackblitz.com/edit/vitejs-vite-95xejnti)
10+
11+
```bash
12+
yarn add @spectrum-web-components/top-nav
13+
```
14+
15+
Import the side effectful registration of `<sp-top-nav>` and `<sp-top-nav-item>` as follows:
16+
17+
```js
18+
import '@spectrum-web-components/top-nav/sp-top-nav.js';
19+
import '@spectrum-web-components/top-nav/sp-top-nav-item.js';
20+
```
21+
22+
When looking to leverage the `TopNav` or `TopNavItem` base classes as a type and/or for extension purposes, do so via:
23+
24+
```js
25+
import { TopNav, TopNavItem } from '@spectrum-web-components/top-nav';
26+
```
27+
28+
### Anatomy
29+
30+
The `sp-top-nav-item` consists of the following parts:
31+
32+
- **Default slot**: text content for the navigation item
33+
- **Label**: Sets a visually-hidden `aria-label` on the item
34+
35+
### Options
36+
37+
<sp-tabs selected="default" auto label="Top nav item property options">
38+
<sp-tab value="default">Default</sp-tab>
39+
<sp-tab-panel value="default">
40+
41+
```html
42+
<sp-top-nav>
43+
<sp-top-nav-item href="#pam">Pam</sp-top-nav-item>
44+
<sp-top-nav-item href="#phyllis">Phyllis</sp-top-nav-item>
45+
<sp-top-nav-item href="#angela">Angela</sp-top-nav-item>
46+
<sp-top-nav-item href="#meredith">Meredith</sp-top-nav-item>
47+
</sp-top-nav>
48+
```
49+
50+
</sp-tab-panel>
51+
<sp-tab value="target">Target</sp-tab>
52+
<sp-tab-panel value="target">
53+
54+
The `target` property specifies where to display the linked URL, such as in a new tab (`_blank`), parent frame (`_parent`), same frame (`_self`), or top frame (`_top`).
55+
56+
```html
57+
<sp-top-nav>
58+
<sp-top-nav-item href="/components/top-nav" target="_blank">
59+
The Office
60+
</sp-top-nav-item>
61+
</sp-top-nav>
62+
```
63+
64+
</sp-tab-panel>
65+
<sp-tab value="download">Download</sp-tab>
66+
<sp-tab-panel value="download">
67+
68+
When set, the `download` property causes the browser to treat the linked URL as a downloadable file, rather than navigating to it. It works in conjunction with the `href` attribute to trigger file downloads when the top nav item is clicked.
69+
70+
```html
71+
<sp-top-nav>
72+
<sp-top-nav-item href="/components/top-nav" download>
73+
The Office
74+
</sp-top-nav-item>
75+
</sp-top-nav>
76+
```
77+
78+
</sp-tab-panel>
79+
<sp-tab value="rel">Rel</sp-tab>
80+
<sp-tab-panel value="rel">
81+
82+
The `rel` property defines the relationship between the current page and the linked URL using space-separated link types (like `nofollow`, `noreferrer`, or `external`). It provides semantic information to browsers and search engines about the nature of the link relationship.
83+
84+
```html
85+
<sp-top-nav>
86+
<sp-top-nav-item href="/components/top-nav" rel="noreferrer">
87+
The Office
88+
</sp-top-nav-item>
89+
</sp-top-nav>
90+
```
91+
92+
</sp-tab-panel>
93+
<sp-tab value="referrer-policy">Referrer policy</sp-tab>
94+
<sp-tab-panel value="referrer-policy">
95+
96+
Setting `referrer-policy` will control how much referrer information is sent when following the link, with options ranging from no referrer (`no-referrer`) to full URL (`unsafe-url`).
97+
98+
```html
99+
<sp-top-nav>
100+
<sp-top-nav-item href="/components/top-nav" referrerpolicy="no-referrer">
101+
The Office
102+
</sp-top-nav-item>
103+
</sp-top-nav>
104+
```
105+
106+
</sp-tab-panel>
107+
</sp-tabs>
108+
109+
### States
110+
111+
<sp-tabs selected="disabled" auto label="Top nav item states">
112+
<sp-tab value="disabled">Disabled</sp-tab>
113+
<sp-tab-panel value="disabled">
114+
115+
Adding the `disabled` attribute to a top nav item renders it non-interactive.
116+
117+
```html
118+
<sp-top-nav>
119+
<sp-top-nav-item href="#pam">Pam</sp-top-nav-item>
120+
<sp-top-nav-item href="#phyllis">Phyllis</sp-top-nav-item>
121+
<sp-top-nav-item href="#angela" disabled>Angela</sp-top-nav-item>
122+
<sp-top-nav-item href="#meredith">Meredith</sp-top-nav-item>
123+
</sp-top-nav>
124+
```
125+
126+
</sp-tab-panel>
127+
<sp-tab value="selected">Selected</sp-tab>
128+
<sp-tab-panel value="selected">
129+
130+
When a user selects a top nav item, the `selected` property is added.
131+
132+
The `selected` status of a top nav item will populate the parent `sp-top-nav` component's `selected` property with the resolved URL.
133+
134+
```html
135+
<sp-top-nav
136+
selected="https://opensource.adobe.com/spectrum-web-components/components/top-nav-item/#dwight"
137+
>
138+
<sp-top-nav-item href="#michael">Michael</sp-top-nav-item>
139+
<sp-top-nav-item href="#dwight" selected>Dwight</sp-top-nav-item>
140+
<sp-top-nav-item href="#kevin">Kevin</sp-top-nav-item>
141+
<sp-top-nav-item href="#jim">Jim</sp-top-nav-item>
142+
</sp-top-nav>
143+
```
144+
145+
</sp-tab-panel>
146+
</sp-tabs>
147+
148+
### Behaviors
149+
150+
Selection state is automatically managed by the parent `<sp-top-nav>` component. When selected, the `selected` property is set to `true` and visual styles will update. Only one item can be selected at a time within a navigation group
151+
152+
Clicking anywhere within the item area triggers navigation. Standard anchor behaviors apply to top nav items (new tab with Ctrl/Cmd+click, etc.).
153+
154+
### Accessibility
155+
156+
#### ARIA attributes
157+
158+
- Selected items automatically receive `aria-current="page"`
159+
- The `label` property can optionally set `aria-label` on the anchor element if screen reader text is significantly different from visible text
160+
- Top nav items renders as anchor elements for accessible screen reader navigation
161+
162+
#### Keyboard interaction
163+
164+
- `Tab` and `Shift+Tab` moves focus between items in a natural, logical order
165+
- `Enter` or `Space` trigger navigation
166+
167+
#### Best Practices
168+
169+
- **Use descriptive link text** that makes sense out of context
170+
- **Use the `label` property sparingly** for additional accessibility information when needed
171+
- **Avoid icon-only items** without accessible text or labels

0 commit comments

Comments
 (0)