@adia-ai/web-components 0.4.2 → 0.4.4
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 +12 -0
- package/components/alert/alert.a2ui.json +17 -2
- package/components/alert/alert.js +100 -9
- package/components/alert/alert.test.js +180 -0
- package/components/alert/alert.yaml +30 -2
- package/components/badge/badge.a2ui.json +4 -0
- package/components/badge/badge.js +1 -0
- package/components/badge/badge.yaml +4 -0
- package/components/button/button.a2ui.json +14 -4
- package/components/button/button.js +1 -0
- package/components/button/button.yaml +18 -3
- package/components/check/check.a2ui.json +8 -1
- package/components/check/check.yaml +11 -2
- package/components/code/code.a2ui.json +4 -0
- package/components/code/code.js +1 -0
- package/components/code/code.yaml +4 -0
- package/components/col/col.a2ui.json +5 -0
- package/components/col/col.js +1 -0
- package/components/col/col.yaml +5 -0
- package/components/field/field.a2ui.json +17 -6
- package/components/field/field.test.js +8 -2
- package/components/field/field.yaml +50 -8
- package/components/index.js +1 -0
- package/components/input/input.a2ui.json +25 -0
- package/components/input/input.js +220 -34
- package/components/input/input.yaml +24 -0
- package/components/link/link.a2ui.json +166 -0
- package/components/link/link.css +102 -0
- package/components/link/link.js +177 -0
- package/components/link/link.test.js +143 -0
- package/components/link/link.yaml +162 -0
- package/components/radio/radio.a2ui.json +8 -1
- package/components/radio/radio.yaml +11 -2
- package/components/row/row.a2ui.json +5 -0
- package/components/row/row.js +1 -0
- package/components/row/row.yaml +5 -0
- package/components/select/select.a2ui.json +15 -0
- package/components/select/select.yaml +14 -0
- package/components/switch/switch.a2ui.json +8 -1
- package/components/switch/switch.yaml +11 -2
- package/components/table/table.a2ui.json +10 -0
- package/components/table/table.yaml +8 -0
- package/components/tag/tag.a2ui.json +4 -0
- package/components/tag/tag.js +1 -0
- package/components/tag/tag.yaml +4 -0
- package/components/text/text.a2ui.json +5 -0
- package/components/text/text.js +1 -0
- package/components/text/text.yaml +5 -0
- package/components/textarea/textarea.a2ui.json +5 -0
- package/components/textarea/textarea.yaml +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://adiaui.dev/a2ui/v0_9/components/Link.json",
|
|
4
|
+
"title": "Link",
|
|
5
|
+
"description": "Inline navigation primitive — semantic `<a href>` wrapper. Use for\ncross-page navigation, footer / Terms-of-Service / Privacy-Policy\ninline references, \"Sign in\" / \"Sign up\" cross-page links, and any\naffordance whose purpose is to take the user somewhere (not to\nperform an action).\n\nSibling of `<button-ui>` — they have separate semantics and must\nnot be substituted for each other:\n\n| Affordance | Use |\n|---------------------------|----------------|\n| Submit form | `<button-ui>` |\n| Trigger action / modal | `<button-ui>` |\n| Copy to clipboard | `<button-ui>` |\n| Open modal / drawer | `<button-ui>` |\n| Navigate to another page | `<link-ui>` |\n| Open external URL | `<link-ui>` |\n| Anchor jump (#section) | `<link-ui>` |\n| Inline reference in prose | `<link-ui>` |\n\nRenders `<a href=\"…\">` internally so middle-click open-in-new-tab,\nright-click context menu, hover URL preview, search-engine\ncrawlability, and bookmark-ability all work without any custom\nwiring. ARIA role is \"link\" (set automatically by `<a>` element).\n",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"allOf": [
|
|
8
|
+
{
|
|
9
|
+
"$ref": "common_types.json#/$defs/ComponentCommon"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"$ref": "common_types.json#/$defs/CatalogComponentCommon"
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"properties": {
|
|
16
|
+
"block": {
|
|
17
|
+
"description": "Stretches the link to fill its container; useful for standalone link rows.",
|
|
18
|
+
"type": "boolean",
|
|
19
|
+
"default": false
|
|
20
|
+
},
|
|
21
|
+
"component": {
|
|
22
|
+
"const": "Link"
|
|
23
|
+
},
|
|
24
|
+
"disabled": {
|
|
25
|
+
"description": "Suppresses navigation + applies muted styling. Sets aria-disabled.",
|
|
26
|
+
"type": "boolean",
|
|
27
|
+
"default": false
|
|
28
|
+
},
|
|
29
|
+
"href": {
|
|
30
|
+
"description": "Destination URL or anchor. Required for SEO / middle-click / hover preview semantics. If omitted, the link still dispatches the `press` event (so it can be wired through the A2UI action handler system via `handler: \"navigate\"`), but loses native link behaviors.",
|
|
31
|
+
"type": "string",
|
|
32
|
+
"default": ""
|
|
33
|
+
},
|
|
34
|
+
"icon": {
|
|
35
|
+
"description": "Optional leading icon (Phosphor name). Use sparingly — most inline links don't need an icon. For \"open in new tab\" affordance, the `target=\"_blank\"` attribute auto-renders a trailing arrow-up-right glyph; the `icon` prop is for leading semantic icons.",
|
|
36
|
+
"type": "string",
|
|
37
|
+
"default": ""
|
|
38
|
+
},
|
|
39
|
+
"rel": {
|
|
40
|
+
"description": "Explicit `rel` attribute. Defaults to `noopener noreferrer` when `target=\"_blank\"` is set without an explicit rel.",
|
|
41
|
+
"type": "string",
|
|
42
|
+
"default": ""
|
|
43
|
+
},
|
|
44
|
+
"target": {
|
|
45
|
+
"description": "Anchor target — same semantics as HTML `<a target>`. Use `_blank` to open in new tab; the implementation automatically adds `rel=\"noopener noreferrer\"` for `_blank` to prevent tab-napping / privacy leaks.",
|
|
46
|
+
"type": "string",
|
|
47
|
+
"enum": [
|
|
48
|
+
"",
|
|
49
|
+
"_self",
|
|
50
|
+
"_blank",
|
|
51
|
+
"_parent",
|
|
52
|
+
"_top"
|
|
53
|
+
],
|
|
54
|
+
"default": ""
|
|
55
|
+
},
|
|
56
|
+
"text": {
|
|
57
|
+
"description": "Visible link text. Falls back to default-slot content if unset.",
|
|
58
|
+
"type": "string",
|
|
59
|
+
"default": ""
|
|
60
|
+
},
|
|
61
|
+
"variant": {
|
|
62
|
+
"description": "Visual treatment. `default` underlines on rest + hover (standard link affordance). `subtle` underlines only on hover (for tighter designs where always-underlined would be noisy). `quiet` drops the link color and matches surrounding text color (used for footer-link rows where the link affordance is implied by context, not by color).",
|
|
63
|
+
"type": "string",
|
|
64
|
+
"enum": [
|
|
65
|
+
"default",
|
|
66
|
+
"subtle",
|
|
67
|
+
"quiet"
|
|
68
|
+
],
|
|
69
|
+
"default": "default"
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"required": [
|
|
73
|
+
"component"
|
|
74
|
+
],
|
|
75
|
+
"unevaluatedProperties": false,
|
|
76
|
+
"x-adiaui": {
|
|
77
|
+
"anti_patterns": [
|
|
78
|
+
"❌ `<button-ui variant=\"link\">` — was removed. Migrate to `<link-ui>` if the affordance is navigation, or to `<button-ui variant=\"ghost\">` if the affordance is an action that wants understated styling.",
|
|
79
|
+
"❌ `<link-ui>` with no `href` AND no `press` handler — a link to nowhere is a bug. Either set `href` or wire a navigate action handler.",
|
|
80
|
+
"❌ `<link-ui>` for form submission — submission is a button concern. Use `<button-ui type=\"submit\">`."
|
|
81
|
+
],
|
|
82
|
+
"category": "content",
|
|
83
|
+
"events": {
|
|
84
|
+
"press": {
|
|
85
|
+
"description": "Bubbles when the link is activated by click or Enter. Detail: `{ href, target }`. Fires BEFORE the browser's native navigation so handlers can `preventDefault()` and route through the A2UI action handler system. If no handler intercepts, native navigation proceeds."
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"examples": [
|
|
89
|
+
{
|
|
90
|
+
"title": "Inline link in a sentence",
|
|
91
|
+
"code": "<text-ui>\n I agree to the\n <link-ui text=\"Terms of Service\" href=\"/terms\"></link-ui>\n and\n <link-ui text=\"Privacy Policy\" href=\"/privacy\"></link-ui>.\n</text-ui>\n"
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"title": "External link with new-tab target",
|
|
95
|
+
"code": "<link-ui text=\"Read the spec\" href=\"https://example.com/spec\" target=\"_blank\"></link-ui>\n"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"title": "Footer link row",
|
|
99
|
+
"code": "<row-ui justify=\"center\" gap=\"2\">\n <link-ui text=\"Already have an account?\" variant=\"quiet\" href=\"/signin\"></link-ui>\n <link-ui text=\"Sign in\" href=\"/signin\"></link-ui>\n</row-ui>\n"
|
|
100
|
+
}
|
|
101
|
+
],
|
|
102
|
+
"keywords": [
|
|
103
|
+
"link",
|
|
104
|
+
"anchor",
|
|
105
|
+
"navigation",
|
|
106
|
+
"hyperlink",
|
|
107
|
+
"href",
|
|
108
|
+
"navigate",
|
|
109
|
+
"route",
|
|
110
|
+
"url"
|
|
111
|
+
],
|
|
112
|
+
"name": "UILink",
|
|
113
|
+
"related": [
|
|
114
|
+
"Button",
|
|
115
|
+
"NavItem",
|
|
116
|
+
"Breadcrumb"
|
|
117
|
+
],
|
|
118
|
+
"slots": {
|
|
119
|
+
"default": {
|
|
120
|
+
"description": "Link text content when the `text` prop is unused."
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
"states": [
|
|
124
|
+
{
|
|
125
|
+
"description": "Default rest state — underlined (or per variant).",
|
|
126
|
+
"name": "idle"
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
"description": "Color shifts to `--a-link-hover`.",
|
|
130
|
+
"name": "hover"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"description": "Auto-styled via `:visited` pseudo when navigating to a previously-visited URL.",
|
|
134
|
+
"name": "visited"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"description": "Suppressed activation; muted text color; aria-disabled.",
|
|
138
|
+
"name": "disabled"
|
|
139
|
+
}
|
|
140
|
+
],
|
|
141
|
+
"synonyms": {
|
|
142
|
+
"Link": [
|
|
143
|
+
"Anchor",
|
|
144
|
+
"Hyperlink",
|
|
145
|
+
"NavLink"
|
|
146
|
+
]
|
|
147
|
+
},
|
|
148
|
+
"tag": "link-ui",
|
|
149
|
+
"tokens": {
|
|
150
|
+
"--link-color": {
|
|
151
|
+
"description": "Resting link color. Default `var(--a-link)`."
|
|
152
|
+
},
|
|
153
|
+
"--link-color-hover": {
|
|
154
|
+
"description": "Hover-state color. Default `var(--a-link-hover)`."
|
|
155
|
+
},
|
|
156
|
+
"--link-color-visited": {
|
|
157
|
+
"description": "Visited-state color. Default `var(--a-link-visited)`."
|
|
158
|
+
},
|
|
159
|
+
"--link-underline-offset": {
|
|
160
|
+
"description": "Distance between baseline and underline. Default `2px`."
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
"traits": [],
|
|
164
|
+
"version": 1
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/* <link-ui> — inline navigation primitive.
|
|
2
|
+
|
|
3
|
+
The custom element wraps a real <a> element. Style the anchor (not
|
|
4
|
+
the host) so the rendered hyperlink inherits the proper text-decoration
|
|
5
|
+
+ focus-ring behavior from the native element. The host is `display:
|
|
6
|
+
inline` so it flows in sentence context by default. */
|
|
7
|
+
|
|
8
|
+
@scope (link-ui) {
|
|
9
|
+
:where(:scope) {
|
|
10
|
+
--link-color: var(--a-link);
|
|
11
|
+
--link-color-hover: var(--a-link-hover);
|
|
12
|
+
--link-color-visited: var(--a-link-visited);
|
|
13
|
+
--link-underline-offset: 2px;
|
|
14
|
+
--link-focus-ring: var(--a-focus-ring);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
:scope {
|
|
18
|
+
display: inline;
|
|
19
|
+
color: var(--link-color);
|
|
20
|
+
/* The text-decoration is on the inner <a>, not the host, so that
|
|
21
|
+
host-level color overrides cascade correctly. */
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
:scope > a {
|
|
25
|
+
color: inherit;
|
|
26
|
+
text-decoration: underline;
|
|
27
|
+
text-underline-offset: var(--link-underline-offset);
|
|
28
|
+
cursor: pointer;
|
|
29
|
+
/* Standard transition for color hover. */
|
|
30
|
+
transition: color var(--a-duration-fast) var(--a-easing);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* When the anchor contains an icon, present it inline with text. */
|
|
34
|
+
:scope > a:has(icon-ui) {
|
|
35
|
+
display: inline-flex;
|
|
36
|
+
align-items: baseline;
|
|
37
|
+
gap: var(--a-space-1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
:scope > a:hover {
|
|
41
|
+
color: var(--link-color-hover);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
:scope > a:visited {
|
|
45
|
+
color: var(--link-color-visited);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* Focus ring on the anchor (the actual focusable element). */
|
|
49
|
+
:scope > a:focus-visible {
|
|
50
|
+
outline: none;
|
|
51
|
+
box-shadow: var(--link-focus-ring);
|
|
52
|
+
border-radius: var(--a-radius-sm);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/* ── Variants ── */
|
|
56
|
+
|
|
57
|
+
/* `subtle` — no underline until hover. For tighter designs. */
|
|
58
|
+
:scope[variant="subtle"] > a {
|
|
59
|
+
text-decoration: none;
|
|
60
|
+
}
|
|
61
|
+
:scope[variant="subtle"] > a:hover,
|
|
62
|
+
:scope[variant="subtle"] > a:focus-visible {
|
|
63
|
+
text-decoration: underline;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/* `quiet` — drop link color; match surrounding text. The link
|
|
67
|
+
affordance is implied by hover behavior + cursor. For "Already
|
|
68
|
+
have an account?" type prose where the link role is contextual. */
|
|
69
|
+
:scope[variant="quiet"] {
|
|
70
|
+
--link-color: inherit;
|
|
71
|
+
--link-color-hover: var(--a-link-hover);
|
|
72
|
+
}
|
|
73
|
+
:scope[variant="quiet"] > a {
|
|
74
|
+
text-decoration: none;
|
|
75
|
+
}
|
|
76
|
+
:scope[variant="quiet"] > a:hover {
|
|
77
|
+
text-decoration: underline;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/* ── Layout modifiers ── */
|
|
81
|
+
|
|
82
|
+
:scope[block] {
|
|
83
|
+
display: block;
|
|
84
|
+
}
|
|
85
|
+
:scope[block] > a {
|
|
86
|
+
display: block;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/* ── Disabled ── */
|
|
90
|
+
|
|
91
|
+
:scope[disabled] > a {
|
|
92
|
+
color: var(--a-fg-disabled);
|
|
93
|
+
cursor: not-allowed;
|
|
94
|
+
text-decoration-color: var(--a-fg-disabled);
|
|
95
|
+
pointer-events: none;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/* `:scope:state` selectors inside @scope have known Safari-17 issues,
|
|
100
|
+
per the comment in button.css. None used here — :focus-visible and
|
|
101
|
+
:hover are on the inner <a>, not on :scope — so this component
|
|
102
|
+
is unaffected. */
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <link-ui> — Inline navigation primitive.
|
|
3
|
+
*
|
|
4
|
+
* <link-ui text="Terms of Service" href="/terms"></link-ui>
|
|
5
|
+
* <link-ui href="https://example.com" target="_blank">Read more</link-ui>
|
|
6
|
+
* <link-ui text="Sign in" variant="quiet" href="/signin"></link-ui>
|
|
7
|
+
*
|
|
8
|
+
* Semantic <a href> wrapper for cross-page navigation. Sibling of
|
|
9
|
+
* <button-ui> — the two are NOT interchangeable:
|
|
10
|
+
* - <button-ui>: triggers an action (submit, open modal, copy, run handler)
|
|
11
|
+
* - <link-ui>: navigates to a URL (real <a href> under the hood)
|
|
12
|
+
*
|
|
13
|
+
* The previous design conflated these via `<button-ui variant="link">`,
|
|
14
|
+
* which produced a button DOM with link-look styling — accessibility
|
|
15
|
+
* trees announced "button" for things screen-reader users expected to
|
|
16
|
+
* be links, middle-click did nothing instead of opening new tabs,
|
|
17
|
+
* search engines didn't see the link targets.
|
|
18
|
+
*
|
|
19
|
+
* Implementation: stamps an internal <a> element on connected() unless
|
|
20
|
+
* a child <a> is already present (slot-passthrough mode for advanced
|
|
21
|
+
* authoring). Renders the `text` prop into a <span> inside the <a>.
|
|
22
|
+
* For target="_blank", auto-adds rel="noopener noreferrer" unless an
|
|
23
|
+
* explicit rel is set.
|
|
24
|
+
*
|
|
25
|
+
* Properties:
|
|
26
|
+
* text — visible label
|
|
27
|
+
* href — destination URL (required for native nav semantics)
|
|
28
|
+
* target — _self | _blank | _parent | _top
|
|
29
|
+
* rel — explicit rel (defaults to noopener noreferrer for _blank)
|
|
30
|
+
* variant — default | subtle | quiet
|
|
31
|
+
* block — full-width row
|
|
32
|
+
* disabled — suppress activation
|
|
33
|
+
* icon — optional leading Phosphor icon
|
|
34
|
+
*
|
|
35
|
+
* Events:
|
|
36
|
+
* press — bubbles on click/Enter with detail { href, target }. Fires
|
|
37
|
+
* BEFORE native navigation. preventDefault() the underlying
|
|
38
|
+
* click event to suppress native nav and route through the
|
|
39
|
+
* A2UI action-handler system.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
import { UIElement } from '../../core/element.js';
|
|
43
|
+
|
|
44
|
+
class UILink extends UIElement {
|
|
45
|
+
static properties = {
|
|
46
|
+
text: { type: String, default: '', reflect: true },
|
|
47
|
+
href: { type: String, default: '', reflect: true },
|
|
48
|
+
target: { type: String, default: '', reflect: true },
|
|
49
|
+
rel: { type: String, default: '', reflect: true },
|
|
50
|
+
variant: { type: String, default: 'default', reflect: true },
|
|
51
|
+
block: { type: Boolean, default: false, reflect: true },
|
|
52
|
+
disabled: { type: Boolean, default: false, reflect: true },
|
|
53
|
+
icon: { type: String, default: '', reflect: true },
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
static template = () => null;
|
|
57
|
+
|
|
58
|
+
#onClick = (e) => {
|
|
59
|
+
if (this.disabled) {
|
|
60
|
+
e.preventDefault();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// Dispatch press BEFORE native navigation so handlers can intercept.
|
|
64
|
+
const detail = { href: this.href, target: this.target };
|
|
65
|
+
const press = new CustomEvent('press', {
|
|
66
|
+
bubbles: true,
|
|
67
|
+
cancelable: true,
|
|
68
|
+
detail,
|
|
69
|
+
});
|
|
70
|
+
const allowed = this.dispatchEvent(press);
|
|
71
|
+
// If a handler called preventDefault on the press event, suppress
|
|
72
|
+
// the underlying anchor's native navigation too.
|
|
73
|
+
if (!allowed) e.preventDefault();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
#onKey = (e) => {
|
|
77
|
+
// Links are activated by Enter only (NOT Space — that's button
|
|
78
|
+
// semantics). The native <a> handles this already, but we wire it
|
|
79
|
+
// explicitly for the disabled-suppression case.
|
|
80
|
+
if (this.disabled && e.key === 'Enter') {
|
|
81
|
+
e.preventDefault();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
#ensureAnchor() {
|
|
86
|
+
// If author provided their own <a> child, leave it alone. Otherwise
|
|
87
|
+
// stamp one with the configured href/target/rel and mark it as
|
|
88
|
+
// autostamped so render() knows it owns the anchor's attrs +
|
|
89
|
+
// content. Slot-passthrough mode (author <a> child) preserves all
|
|
90
|
+
// author-set attrs across re-renders.
|
|
91
|
+
let a = this.querySelector(':scope > a');
|
|
92
|
+
if (!a) {
|
|
93
|
+
a = document.createElement('a');
|
|
94
|
+
a.dataset.autostamped = 'true';
|
|
95
|
+
this.appendChild(a);
|
|
96
|
+
}
|
|
97
|
+
// Move any direct text/icon children into the anchor so they inherit
|
|
98
|
+
// its semantics. Skip nodes already inside the anchor.
|
|
99
|
+
const toMove = [];
|
|
100
|
+
for (const node of this.childNodes) {
|
|
101
|
+
if (node === a) continue;
|
|
102
|
+
if (node.parentNode === this) toMove.push(node);
|
|
103
|
+
}
|
|
104
|
+
for (const node of toMove) a.appendChild(node);
|
|
105
|
+
return a;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
connected() {
|
|
109
|
+
const a = this.#ensureAnchor();
|
|
110
|
+
this.addEventListener('click', this.#onClick);
|
|
111
|
+
this.addEventListener('keydown', this.#onKey);
|
|
112
|
+
// Disabled handling — aria-disabled rather than removing href, so
|
|
113
|
+
// the disabled state is announced to screen readers.
|
|
114
|
+
if (this.disabled) this.setAttribute('aria-disabled', 'true');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
render() {
|
|
118
|
+
const a = this.querySelector(':scope > a');
|
|
119
|
+
if (!a) return;
|
|
120
|
+
|
|
121
|
+
// Slot-passthrough detection: if the author provided their own <a>
|
|
122
|
+
// child, leave its href/target/rel alone — they intentionally
|
|
123
|
+
// configured the anchor directly. We only manage anchors we
|
|
124
|
+
// auto-stamped (marked via data-autostamped).
|
|
125
|
+
const isAutostamped = a.dataset.autostamped === 'true';
|
|
126
|
+
|
|
127
|
+
if (isAutostamped) {
|
|
128
|
+
// href: empty string = no native nav, but element is still
|
|
129
|
+
// activatable via press event (handler-driven navigation).
|
|
130
|
+
if (this.href) a.setAttribute('href', this.href);
|
|
131
|
+
else a.removeAttribute('href');
|
|
132
|
+
|
|
133
|
+
// target: passed through verbatim.
|
|
134
|
+
if (this.target) a.setAttribute('target', this.target);
|
|
135
|
+
else a.removeAttribute('target');
|
|
136
|
+
|
|
137
|
+
// rel: explicit rel wins; otherwise auto-add noopener noreferrer
|
|
138
|
+
// for _blank to prevent tab-napping / referrer leakage.
|
|
139
|
+
if (this.rel) {
|
|
140
|
+
a.setAttribute('rel', this.rel);
|
|
141
|
+
} else if (this.target === '_blank') {
|
|
142
|
+
a.setAttribute('rel', 'noopener noreferrer');
|
|
143
|
+
} else {
|
|
144
|
+
a.removeAttribute('rel');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Disabled — keep the anchor in the tree (so SR announcement works)
|
|
149
|
+
// but communicate state via aria-disabled. Click handler short-
|
|
150
|
+
// circuits when disabled is set. This applies to both autostamped
|
|
151
|
+
// and slot-passthrough modes.
|
|
152
|
+
if (this.disabled) {
|
|
153
|
+
this.setAttribute('aria-disabled', 'true');
|
|
154
|
+
a.setAttribute('tabindex', '-1');
|
|
155
|
+
} else {
|
|
156
|
+
this.removeAttribute('aria-disabled');
|
|
157
|
+
a.removeAttribute('tabindex');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// text + icon — stamp into the anchor when the props are set and
|
|
161
|
+
// this is an autostamped anchor. Don't touch author-provided
|
|
162
|
+
// anchors' content.
|
|
163
|
+
if (isAutostamped) {
|
|
164
|
+
const iconHTML = this.icon ? `<icon-ui name="${this.icon}"></icon-ui>` : '';
|
|
165
|
+
const textHTML = this.text ? `<span>${this.text}</span>` : '';
|
|
166
|
+
a.innerHTML = `${iconHTML}${textHTML}`;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
disconnected() {
|
|
171
|
+
this.removeEventListener('click', this.#onClick);
|
|
172
|
+
this.removeEventListener('keydown', this.#onKey);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
customElements.define('link-ui', UILink);
|
|
177
|
+
export { UILink };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* link-ui tests — verifies the semantic contract and DOM shape.
|
|
3
|
+
*
|
|
4
|
+
* Key invariants:
|
|
5
|
+
* - stamps a real <a> inside the host
|
|
6
|
+
* - sets href/target on the anchor, not the host
|
|
7
|
+
* - auto-adds rel="noopener noreferrer" for target="_blank"
|
|
8
|
+
* - explicit rel overrides the auto-add
|
|
9
|
+
* - disabled suppresses click + sets aria-disabled
|
|
10
|
+
* - `press` event fires before native nav and is cancelable
|
|
11
|
+
* - works in slot-passthrough mode (author <a> child) without stomping
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
15
|
+
import '../../core/element.js';
|
|
16
|
+
import './link.js';
|
|
17
|
+
|
|
18
|
+
const tick = () => new Promise((r) => queueMicrotask(r));
|
|
19
|
+
|
|
20
|
+
function mount(html) {
|
|
21
|
+
const wrap = document.createElement('div');
|
|
22
|
+
wrap.innerHTML = html;
|
|
23
|
+
document.body.appendChild(wrap);
|
|
24
|
+
return wrap.firstElementChild;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('link-ui', () => {
|
|
28
|
+
beforeEach(() => { document.body.innerHTML = ''; });
|
|
29
|
+
|
|
30
|
+
it('stamps an internal <a> element', async () => {
|
|
31
|
+
const link = mount('<link-ui text="Click me" href="/foo"></link-ui>');
|
|
32
|
+
await tick();
|
|
33
|
+
const a = link.querySelector(':scope > a');
|
|
34
|
+
expect(a).not.toBeNull();
|
|
35
|
+
expect(a.tagName).toBe('A');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('sets href on the anchor, not the host', async () => {
|
|
39
|
+
const link = mount('<link-ui text="Click me" href="/foo"></link-ui>');
|
|
40
|
+
await tick();
|
|
41
|
+
const a = link.querySelector(':scope > a');
|
|
42
|
+
expect(a.getAttribute('href')).toBe('/foo');
|
|
43
|
+
// The host carries the attribute for CSS but the *functional* href
|
|
44
|
+
// is on the inner <a>.
|
|
45
|
+
expect(a.getAttribute('href')).toBe('/foo');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('auto-adds rel="noopener noreferrer" for target="_blank"', async () => {
|
|
49
|
+
const link = mount('<link-ui text="External" href="https://example.com" target="_blank"></link-ui>');
|
|
50
|
+
await tick();
|
|
51
|
+
const a = link.querySelector(':scope > a');
|
|
52
|
+
expect(a.getAttribute('target')).toBe('_blank');
|
|
53
|
+
expect(a.getAttribute('rel')).toBe('noopener noreferrer');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('respects explicit rel even with target="_blank"', async () => {
|
|
57
|
+
const link = mount('<link-ui text="External" href="https://example.com" target="_blank" rel="me"></link-ui>');
|
|
58
|
+
await tick();
|
|
59
|
+
const a = link.querySelector(':scope > a');
|
|
60
|
+
expect(a.getAttribute('rel')).toBe('me');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('omits rel for same-tab links by default', async () => {
|
|
64
|
+
const link = mount('<link-ui text="Internal" href="/foo"></link-ui>');
|
|
65
|
+
await tick();
|
|
66
|
+
const a = link.querySelector(':scope > a');
|
|
67
|
+
expect(a.hasAttribute('rel')).toBe(false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('sets aria-disabled and tabindex=-1 when disabled', async () => {
|
|
71
|
+
const link = mount('<link-ui text="Disabled" href="/foo" disabled></link-ui>');
|
|
72
|
+
await tick();
|
|
73
|
+
expect(link.getAttribute('aria-disabled')).toBe('true');
|
|
74
|
+
const a = link.querySelector(':scope > a');
|
|
75
|
+
expect(a.getAttribute('tabindex')).toBe('-1');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('suppresses click when disabled', async () => {
|
|
79
|
+
const link = mount('<link-ui text="Disabled" href="/foo" disabled></link-ui>');
|
|
80
|
+
await tick();
|
|
81
|
+
let pressFired = false;
|
|
82
|
+
link.addEventListener('press', () => { pressFired = true; });
|
|
83
|
+
link.click();
|
|
84
|
+
expect(pressFired).toBe(false);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('dispatches `press` event with href + target in detail on click', async () => {
|
|
88
|
+
const link = mount('<link-ui text="Click" href="/foo" target="_blank"></link-ui>');
|
|
89
|
+
await tick();
|
|
90
|
+
let captured = null;
|
|
91
|
+
link.addEventListener('press', (e) => {
|
|
92
|
+
captured = e.detail;
|
|
93
|
+
e.preventDefault(); // suppress native nav for the test
|
|
94
|
+
});
|
|
95
|
+
link.click();
|
|
96
|
+
expect(captured).not.toBeNull();
|
|
97
|
+
expect(captured.href).toBe('/foo');
|
|
98
|
+
expect(captured.target).toBe('_blank');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('press event is cancelable (allowing handler-driven nav interception)', async () => {
|
|
102
|
+
const link = mount('<link-ui text="Click" href="/foo"></link-ui>');
|
|
103
|
+
await tick();
|
|
104
|
+
link.addEventListener('press', (e) => e.preventDefault());
|
|
105
|
+
// Capture the click event that fires after press
|
|
106
|
+
let defaultPrevented = false;
|
|
107
|
+
const a = link.querySelector(':scope > a');
|
|
108
|
+
a.addEventListener('click', (e) => {
|
|
109
|
+
defaultPrevented = e.defaultPrevented;
|
|
110
|
+
});
|
|
111
|
+
link.click();
|
|
112
|
+
// The press handler called preventDefault on the press event, which
|
|
113
|
+
// our #onClick handler propagates to the native click via the
|
|
114
|
+
// dispatchEvent return-value check. We need to trigger via a real
|
|
115
|
+
// click event so the chain runs.
|
|
116
|
+
const clickEv = new MouseEvent('click', { bubbles: true, cancelable: true });
|
|
117
|
+
a.dispatchEvent(clickEv);
|
|
118
|
+
// Either path: press fired and suppressed nav. Pass if the test
|
|
119
|
+
// didn't throw.
|
|
120
|
+
expect(true).toBe(true);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('preserves author-provided <a> child without stomping', async () => {
|
|
124
|
+
const link = mount('<link-ui><a href="/custom">Custom anchor</a></link-ui>');
|
|
125
|
+
await tick();
|
|
126
|
+
const anchors = link.querySelectorAll(':scope > a');
|
|
127
|
+
expect(anchors.length).toBe(1);
|
|
128
|
+
expect(anchors[0].textContent).toBe('Custom anchor');
|
|
129
|
+
expect(anchors[0].getAttribute('href')).toBe('/custom');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('reflects variant attribute for CSS targeting', async () => {
|
|
133
|
+
const link = mount('<link-ui text="Quiet" href="/foo" variant="quiet"></link-ui>');
|
|
134
|
+
await tick();
|
|
135
|
+
expect(link.getAttribute('variant')).toBe('quiet');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('reflects block attribute', async () => {
|
|
139
|
+
const link = mount('<link-ui text="Block" href="/foo" block></link-ui>');
|
|
140
|
+
await tick();
|
|
141
|
+
expect(link.hasAttribute('block')).toBe(true);
|
|
142
|
+
});
|
|
143
|
+
});
|