@component-anatomy/core 0.0.1
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/LICENSE +21 -0
- package/README.md +142 -0
- package/dist/controller.d.ts +3 -0
- package/dist/controller.d.ts.map +1 -0
- package/dist/index.cjs +486 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.iife.js +488 -0
- package/dist/index.iife.js.map +7 -0
- package/dist/index.js +463 -0
- package/dist/index.js.map +7 -0
- package/dist/overlay.d.ts +31 -0
- package/dist/overlay.d.ts.map +1 -0
- package/dist/registry.d.ts +25 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/theme.d.ts +30 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +59 -0
- package/src/controller.ts +200 -0
- package/src/index.ts +14 -0
- package/src/overlay.ts +231 -0
- package/src/registry.ts +77 -0
- package/src/theme.ts +118 -0
- package/src/types.ts +115 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Julien Déramond
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# @component-anatomy/core
|
|
2
|
+
|
|
3
|
+
> Framework-agnostic runtime for interactive component anatomy documentation.
|
|
4
|
+
|
|
5
|
+
This is the core package. It runs in any browser DOM — no build tools, no framework required.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @component-anatomy/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or via CDN (IIFE build):
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<script src="https://unpkg.com/@component-anatomy/core/dist/index.iife.js"></script>
|
|
17
|
+
<!-- ComponentAnatomy is now available as a global -->
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
Annotate DOM elements with `data-part`:
|
|
23
|
+
|
|
24
|
+
```html
|
|
25
|
+
<div class="slider">
|
|
26
|
+
<div class="track" data-part="track"></div>
|
|
27
|
+
<div class="thumb" data-part="thumb"></div>
|
|
28
|
+
</div>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Initialize the controller:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { createAnatomy } from '@component-anatomy/core';
|
|
35
|
+
|
|
36
|
+
const anatomy = createAnatomy({
|
|
37
|
+
root: document.querySelector('.slider'),
|
|
38
|
+
panel: document.querySelector('#anatomy-panel'),
|
|
39
|
+
parts: [
|
|
40
|
+
{
|
|
41
|
+
id: 'track',
|
|
42
|
+
name: 'Track',
|
|
43
|
+
description: 'The rail where the thumb slides.',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: 'thumb',
|
|
47
|
+
name: 'Thumb',
|
|
48
|
+
description: 'The draggable control handle.',
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### `createAnatomy(options): AnatomyController`
|
|
57
|
+
|
|
58
|
+
| Option | Type | Required | Description |
|
|
59
|
+
|--------|------|----------|-------------|
|
|
60
|
+
| `root` | `HTMLElement` | Yes | The component preview container |
|
|
61
|
+
| `panel` | `HTMLElement` | No | The documentation panel container |
|
|
62
|
+
| `parts` | `AnatomyPartDefinition[]` | No | Part definitions (auto-discovered from DOM if omitted) |
|
|
63
|
+
| `preset` | `'default' \| 'minimal' \| 'contrast' \| 'blueprint'` | No | Named visual preset |
|
|
64
|
+
| `theme` | `AnatomyTheme` | No | Theme token overrides, e.g. `{ accent: '#0d9488' }` |
|
|
65
|
+
| `overlay` | `OverlayOptions` | No | Overlay hooks: `label`, `padding`, `className`, `renderLabel`, `decorateOverlay` |
|
|
66
|
+
|
|
67
|
+
### `AnatomyController`
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
interface AnatomyController {
|
|
71
|
+
highlight(partId: string): void; // Programmatically highlight a part
|
|
72
|
+
unhighlight(): void; // Clear active highlight
|
|
73
|
+
refresh(): void; // Re-discover DOM parts after dynamic updates
|
|
74
|
+
destroy(): void; // Remove all listeners and overlays
|
|
75
|
+
on(event: AnatomyEvent, handler: AnatomyEventHandler): () => void;
|
|
76
|
+
getParts(): AnatomyPartDefinition[]; // Resolved (explicit or auto-discovered) parts
|
|
77
|
+
setTheme(theme: AnatomyTheme, preset?: AnatomyPresetName): void; // Runtime theme switch
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Theming
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
// One-line brand match — border, background wash and label derive from accent
|
|
85
|
+
createAnatomy({ root, panel, theme: { accent: '#0d9488' } });
|
|
86
|
+
|
|
87
|
+
// Named presets
|
|
88
|
+
createAnatomy({ root, panel, preset: 'blueprint' });
|
|
89
|
+
|
|
90
|
+
// Overlay hooks
|
|
91
|
+
createAnatomy({
|
|
92
|
+
root,
|
|
93
|
+
overlay: {
|
|
94
|
+
padding: 4,
|
|
95
|
+
renderLabel: ({ part, index }) => `${part.name} #${index + 1}`,
|
|
96
|
+
decorateOverlay: (box, ctx) => box.classList.add('glow'),
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Full token table: [customization guide](https://julien-deramond.github.io/component-anatomy/docs/customization).
|
|
102
|
+
|
|
103
|
+
### `AnatomyPartDefinition`
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
type AnatomyPartDefinition = {
|
|
107
|
+
id: string; // Stable machine ID, matches data-part value
|
|
108
|
+
name: string; // Human-readable display name
|
|
109
|
+
description?: string; // Markdown supported
|
|
110
|
+
};
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Events
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
anatomy.on('part:enter', (partId) => console.log('hovered:', partId));
|
|
117
|
+
anatomy.on('part:leave', (partId) => console.log('left:', partId));
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## CSS custom properties
|
|
121
|
+
|
|
122
|
+
Instances without a `preset`/`theme` pick up global variables — theme a whole site in CSS only:
|
|
123
|
+
|
|
124
|
+
```css
|
|
125
|
+
:root {
|
|
126
|
+
--ca-overlay-bg: rgba(99, 102, 241, 0.18);
|
|
127
|
+
--ca-overlay-border: rgba(99, 102, 241, 0.75);
|
|
128
|
+
--ca-overlay-border-width: 2px;
|
|
129
|
+
--ca-overlay-border-style: solid;
|
|
130
|
+
--ca-overlay-radius: 4px;
|
|
131
|
+
--ca-label-bg: rgba(79, 70, 229, 1);
|
|
132
|
+
--ca-label-fg: #fff;
|
|
133
|
+
--ca-label-font: ui-monospace, monospace;
|
|
134
|
+
--ca-label-font-size: 11px;
|
|
135
|
+
--ca-overlay-z: 9998;
|
|
136
|
+
--ca-transition: 150ms;
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,cAAc,EACd,iBAAiB,EAIlB,MAAM,YAAY,CAAC;AASpB,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,cAAc,GAAG,iBAAiB,CAqL3E"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
createAnatomy: () => createController,
|
|
24
|
+
presets: () => presets,
|
|
25
|
+
resolveThemeVars: () => resolveThemeVars
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
|
|
29
|
+
// src/registry.ts
|
|
30
|
+
var AnatomyRegistry = class {
|
|
31
|
+
constructor(root) {
|
|
32
|
+
this.observer = null;
|
|
33
|
+
this.root = root;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns a Map of partId → matching HTMLElements.
|
|
37
|
+
* Always queries the live DOM — never returns a stale cache.
|
|
38
|
+
*/
|
|
39
|
+
query() {
|
|
40
|
+
const map = /* @__PURE__ */ new Map();
|
|
41
|
+
const nodes = this.root.querySelectorAll("[data-part]");
|
|
42
|
+
nodes.forEach((el) => {
|
|
43
|
+
const id = el.dataset.part;
|
|
44
|
+
if (!id) return;
|
|
45
|
+
const existing = map.get(id) ?? [];
|
|
46
|
+
existing.push(el);
|
|
47
|
+
map.set(id, existing);
|
|
48
|
+
});
|
|
49
|
+
return map;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns all unique part IDs present in the DOM, in document order.
|
|
53
|
+
*/
|
|
54
|
+
partIds() {
|
|
55
|
+
const seen = /* @__PURE__ */ new Set();
|
|
56
|
+
const result = [];
|
|
57
|
+
this.root.querySelectorAll("[data-part]").forEach((el) => {
|
|
58
|
+
const id = el.dataset.part;
|
|
59
|
+
if (id && !seen.has(id)) {
|
|
60
|
+
seen.add(id);
|
|
61
|
+
result.push(id);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Watches the root for DOM mutations and calls the callback when [data-part]
|
|
68
|
+
* elements are added or removed. Returns a cleanup function.
|
|
69
|
+
*/
|
|
70
|
+
observe(callback) {
|
|
71
|
+
this.observer = new MutationObserver((mutations) => {
|
|
72
|
+
const relevant = mutations.some(
|
|
73
|
+
(m) => Array.from(m.addedNodes).concat(Array.from(m.removedNodes)).some(
|
|
74
|
+
(n) => n instanceof HTMLElement && (n.hasAttribute("data-part") || n.querySelector("[data-part]") !== null)
|
|
75
|
+
)
|
|
76
|
+
);
|
|
77
|
+
if (relevant) callback();
|
|
78
|
+
});
|
|
79
|
+
this.observer.observe(this.root, { childList: true, subtree: true });
|
|
80
|
+
return () => {
|
|
81
|
+
this.observer?.disconnect();
|
|
82
|
+
this.observer = null;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
destroy() {
|
|
86
|
+
this.observer?.disconnect();
|
|
87
|
+
this.observer = null;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// src/overlay.ts
|
|
92
|
+
var OVERLAY_CLASS = "ca-overlay";
|
|
93
|
+
var LABEL_CLASS = "ca-overlay-label";
|
|
94
|
+
var STYLE_ID = "ca-overlay-styles";
|
|
95
|
+
function injectStyles() {
|
|
96
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
97
|
+
const style = document.createElement("style");
|
|
98
|
+
style.id = STYLE_ID;
|
|
99
|
+
style.textContent = `
|
|
100
|
+
.${OVERLAY_CLASS} {
|
|
101
|
+
position: fixed;
|
|
102
|
+
pointer-events: none;
|
|
103
|
+
box-sizing: border-box;
|
|
104
|
+
border-radius: var(--ca-overlay-radius, 4px);
|
|
105
|
+
background: var(--ca-overlay-bg, rgba(99, 102, 241, 0.18));
|
|
106
|
+
outline: var(--ca-overlay-border-width, 2px)
|
|
107
|
+
var(--ca-overlay-border-style, solid)
|
|
108
|
+
var(--ca-overlay-border, rgba(99, 102, 241, 0.75));
|
|
109
|
+
outline-offset: 1px;
|
|
110
|
+
z-index: var(--ca-overlay-z, 9998);
|
|
111
|
+
transition: opacity var(--ca-transition, 150ms) ease;
|
|
112
|
+
}
|
|
113
|
+
.${OVERLAY_CLASS}.ca-overlay--hidden {
|
|
114
|
+
opacity: 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/* Name label chip \u2014 floats above the overlay */
|
|
118
|
+
.${LABEL_CLASS} {
|
|
119
|
+
position: fixed;
|
|
120
|
+
pointer-events: none;
|
|
121
|
+
z-index: var(--ca-overlay-z, 9999);
|
|
122
|
+
display: inline-flex;
|
|
123
|
+
align-items: center;
|
|
124
|
+
gap: 4px;
|
|
125
|
+
padding: 2px 7px 2px 5px;
|
|
126
|
+
border-radius: 4px;
|
|
127
|
+
background: var(--ca-label-bg, rgba(79, 70, 229, 1));
|
|
128
|
+
color: var(--ca-label-fg, #fff);
|
|
129
|
+
font-family: var(--ca-label-font, ui-monospace, 'Cascadia Code', 'Fira Mono', monospace);
|
|
130
|
+
font-size: var(--ca-label-font-size, 11px);
|
|
131
|
+
font-weight: 500;
|
|
132
|
+
letter-spacing: 0.02em;
|
|
133
|
+
white-space: nowrap;
|
|
134
|
+
line-height: 1.6;
|
|
135
|
+
box-shadow: 0 1px 4px rgba(0,0,0,0.25);
|
|
136
|
+
transition: opacity var(--ca-transition, 150ms) ease;
|
|
137
|
+
}
|
|
138
|
+
.${LABEL_CLASS}.ca-overlay--hidden {
|
|
139
|
+
opacity: 0;
|
|
140
|
+
}
|
|
141
|
+
.${LABEL_CLASS}::before {
|
|
142
|
+
content: '';
|
|
143
|
+
display: inline-block;
|
|
144
|
+
width: 5px;
|
|
145
|
+
height: 5px;
|
|
146
|
+
border-radius: 50%;
|
|
147
|
+
background: color-mix(in srgb, var(--ca-label-fg, #fff) 60%, transparent);
|
|
148
|
+
flex-shrink: 0;
|
|
149
|
+
}
|
|
150
|
+
`;
|
|
151
|
+
document.head.appendChild(style);
|
|
152
|
+
}
|
|
153
|
+
function applyVars(el, vars) {
|
|
154
|
+
for (const [prop, value] of Object.entries(vars)) {
|
|
155
|
+
el.style.setProperty(prop, value);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
var AnatomyOverlay = class {
|
|
159
|
+
constructor(vars = {}, options = {}) {
|
|
160
|
+
this.overlays = [];
|
|
161
|
+
this.labels = [];
|
|
162
|
+
this.activeElements = [];
|
|
163
|
+
this.activePart = null;
|
|
164
|
+
this.rafId = null;
|
|
165
|
+
this.vars = vars;
|
|
166
|
+
this.options = options;
|
|
167
|
+
injectStyles();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Replace the per-instance theme variables. Re-applies to any overlays
|
|
171
|
+
* currently on screen.
|
|
172
|
+
*/
|
|
173
|
+
setVars(vars) {
|
|
174
|
+
this.vars = vars;
|
|
175
|
+
if (this.overlays.length > 0 || this.labels.length > 0) {
|
|
176
|
+
[...this.overlays, ...this.labels].forEach((el) => {
|
|
177
|
+
el.removeAttribute("style");
|
|
178
|
+
applyVars(el, vars);
|
|
179
|
+
});
|
|
180
|
+
this._position();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Show highlight overlays + label chip over each of the given elements.
|
|
185
|
+
*/
|
|
186
|
+
show(elements, part) {
|
|
187
|
+
this.hide();
|
|
188
|
+
this.activeElements = elements;
|
|
189
|
+
this.activePart = part;
|
|
190
|
+
const showLabel = this.options.label !== false;
|
|
191
|
+
elements.forEach((element, index) => {
|
|
192
|
+
const ctx = { part, element, index };
|
|
193
|
+
const div = document.createElement("div");
|
|
194
|
+
div.className = `${OVERLAY_CLASS} ca-overlay--hidden`;
|
|
195
|
+
if (this.options.className) div.className += ` ${this.options.className}`;
|
|
196
|
+
div.setAttribute("aria-hidden", "true");
|
|
197
|
+
applyVars(div, this.vars);
|
|
198
|
+
document.body.appendChild(div);
|
|
199
|
+
this.overlays.push(div);
|
|
200
|
+
if (this.options.decorateOverlay) {
|
|
201
|
+
this.options.decorateOverlay(div, ctx);
|
|
202
|
+
}
|
|
203
|
+
if (showLabel) {
|
|
204
|
+
const label = document.createElement("div");
|
|
205
|
+
label.className = `${LABEL_CLASS} ca-overlay--hidden`;
|
|
206
|
+
label.setAttribute("aria-hidden", "true");
|
|
207
|
+
applyVars(label, this.vars);
|
|
208
|
+
const custom = this.options.renderLabel?.(ctx);
|
|
209
|
+
if (typeof custom === "string") label.textContent = custom;
|
|
210
|
+
else if (custom instanceof Node) label.appendChild(custom);
|
|
211
|
+
else label.textContent = part.name;
|
|
212
|
+
document.body.appendChild(label);
|
|
213
|
+
this.labels.push(label);
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
requestAnimationFrame(() => {
|
|
217
|
+
this._position();
|
|
218
|
+
this.overlays.forEach((o) => o.classList.remove("ca-overlay--hidden"));
|
|
219
|
+
this.labels.forEach((l) => l.classList.remove("ca-overlay--hidden"));
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Remove all overlays and labels from the DOM.
|
|
224
|
+
*/
|
|
225
|
+
hide() {
|
|
226
|
+
if (this.rafId !== null) {
|
|
227
|
+
cancelAnimationFrame(this.rafId);
|
|
228
|
+
this.rafId = null;
|
|
229
|
+
}
|
|
230
|
+
this.overlays.forEach((o) => o.remove());
|
|
231
|
+
this.labels.forEach((l) => l.remove());
|
|
232
|
+
this.overlays = [];
|
|
233
|
+
this.labels = [];
|
|
234
|
+
this.activeElements = [];
|
|
235
|
+
this.activePart = null;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Recompute overlay positions. Call on scroll or resize.
|
|
239
|
+
*/
|
|
240
|
+
reposition() {
|
|
241
|
+
if (this.overlays.length === 0) return;
|
|
242
|
+
if (this.rafId !== null) cancelAnimationFrame(this.rafId);
|
|
243
|
+
this.rafId = requestAnimationFrame(() => {
|
|
244
|
+
this._position();
|
|
245
|
+
this.rafId = null;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
destroy() {
|
|
249
|
+
this.hide();
|
|
250
|
+
}
|
|
251
|
+
_position() {
|
|
252
|
+
const LABEL_OFFSET = 6;
|
|
253
|
+
const LABEL_HEIGHT = 22;
|
|
254
|
+
const pad = this.options.padding ?? 0;
|
|
255
|
+
this.activeElements.forEach((el, i) => {
|
|
256
|
+
const overlay = this.overlays[i];
|
|
257
|
+
const label = this.labels[i];
|
|
258
|
+
if (!overlay) return;
|
|
259
|
+
const rect = el.getBoundingClientRect();
|
|
260
|
+
const top = rect.top - pad;
|
|
261
|
+
const left = rect.left - pad;
|
|
262
|
+
const width = rect.width + pad * 2;
|
|
263
|
+
const height = rect.height + pad * 2;
|
|
264
|
+
overlay.style.top = `${top}px`;
|
|
265
|
+
overlay.style.left = `${left}px`;
|
|
266
|
+
overlay.style.width = `${width}px`;
|
|
267
|
+
overlay.style.height = `${height}px`;
|
|
268
|
+
if (!label) return;
|
|
269
|
+
const labelTop = top - LABEL_HEIGHT - LABEL_OFFSET;
|
|
270
|
+
const finalTop = labelTop < 4 ? top + height + LABEL_OFFSET : labelTop;
|
|
271
|
+
label.style.top = `${finalTop}px`;
|
|
272
|
+
label.style.left = `${left}px`;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// src/theme.ts
|
|
278
|
+
var TOKEN_TO_VAR = {
|
|
279
|
+
overlayBg: "--ca-overlay-bg",
|
|
280
|
+
overlayBorder: "--ca-overlay-border",
|
|
281
|
+
overlayBorderWidth: "--ca-overlay-border-width",
|
|
282
|
+
overlayRadius: "--ca-overlay-radius",
|
|
283
|
+
labelBg: "--ca-label-bg",
|
|
284
|
+
labelFg: "--ca-label-fg",
|
|
285
|
+
labelFont: "--ca-label-font",
|
|
286
|
+
labelFontSize: "--ca-label-font-size",
|
|
287
|
+
zIndex: "--ca-overlay-z",
|
|
288
|
+
transitionMs: "--ca-transition"
|
|
289
|
+
};
|
|
290
|
+
var presets = {
|
|
291
|
+
/** The built-in indigo look. Empty on purpose: it lives in the stylesheet defaults. */
|
|
292
|
+
default: {},
|
|
293
|
+
/** Quiet: no fill, thin neutral border, subdued label. */
|
|
294
|
+
minimal: {
|
|
295
|
+
overlayBg: "transparent",
|
|
296
|
+
overlayBorder: "rgba(107, 114, 128, 0.9)",
|
|
297
|
+
overlayBorderWidth: "1px",
|
|
298
|
+
overlayRadius: "2px",
|
|
299
|
+
labelBg: "#374151",
|
|
300
|
+
labelFg: "#f9fafb"
|
|
301
|
+
},
|
|
302
|
+
/** High-visibility: strong yellow/black, thick border. WCAG-friendly. */
|
|
303
|
+
contrast: {
|
|
304
|
+
overlayBg: "rgba(250, 204, 21, 0.25)",
|
|
305
|
+
overlayBorder: "#000000",
|
|
306
|
+
overlayBorderWidth: "3px",
|
|
307
|
+
overlayRadius: "0px",
|
|
308
|
+
labelBg: "#000000",
|
|
309
|
+
labelFg: "#facc15"
|
|
310
|
+
},
|
|
311
|
+
/** Technical drawing look: blue dashed outline on a light wash. */
|
|
312
|
+
blueprint: {
|
|
313
|
+
overlayBg: "rgba(37, 99, 235, 0.08)",
|
|
314
|
+
overlayBorder: "#2563eb",
|
|
315
|
+
overlayBorderWidth: "1.5px",
|
|
316
|
+
overlayRadius: "0px",
|
|
317
|
+
overlayBorderStyle: "dashed",
|
|
318
|
+
labelBg: "#1d4ed8",
|
|
319
|
+
labelFg: "#ffffff"
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
function resolveThemeVars(preset, theme) {
|
|
323
|
+
const merged = {
|
|
324
|
+
...preset ? presets[preset] : void 0,
|
|
325
|
+
...theme
|
|
326
|
+
};
|
|
327
|
+
const vars = {};
|
|
328
|
+
if (merged.accent) {
|
|
329
|
+
vars["--ca-overlay-border"] = merged.accent;
|
|
330
|
+
vars["--ca-overlay-bg"] = `color-mix(in srgb, ${merged.accent} 15%, transparent)`;
|
|
331
|
+
vars["--ca-label-bg"] = merged.accent;
|
|
332
|
+
}
|
|
333
|
+
for (const [token, cssVar] of Object.entries(TOKEN_TO_VAR)) {
|
|
334
|
+
const value = merged[token];
|
|
335
|
+
if (value !== void 0) {
|
|
336
|
+
vars[cssVar] = typeof value === "number" ? token === "zIndex" ? String(value) : token === "transitionMs" ? `${value}ms` : `${value}px` : String(value);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (merged.overlayBorderStyle) {
|
|
340
|
+
vars["--ca-overlay-border-style"] = merged.overlayBorderStyle;
|
|
341
|
+
}
|
|
342
|
+
return vars;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// src/controller.ts
|
|
346
|
+
function idToName(id) {
|
|
347
|
+
return id.replace(/[-_]/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
|
|
348
|
+
}
|
|
349
|
+
function createController(options) {
|
|
350
|
+
const { root, panel } = options;
|
|
351
|
+
const registry = new AnatomyRegistry(root);
|
|
352
|
+
const overlay = new AnatomyOverlay(
|
|
353
|
+
resolveThemeVars(options.preset, options.theme),
|
|
354
|
+
options.overlay ?? {}
|
|
355
|
+
);
|
|
356
|
+
let parts = options.parts ?? registry.partIds().map((id) => ({ id, name: idToName(id) }));
|
|
357
|
+
function findPart(partId) {
|
|
358
|
+
return parts.find((p) => p.id === partId) ?? { id: partId, name: idToName(partId) };
|
|
359
|
+
}
|
|
360
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
361
|
+
function emit(event, partId) {
|
|
362
|
+
listeners.get(event)?.forEach((fn) => fn(partId));
|
|
363
|
+
}
|
|
364
|
+
const cleanupFns = [];
|
|
365
|
+
function highlight(partId, source = "panel") {
|
|
366
|
+
const elements = registry.query().get(partId) ?? [];
|
|
367
|
+
overlay.show(elements, findPart(partId));
|
|
368
|
+
if (panel) {
|
|
369
|
+
panel.querySelectorAll("[data-anatomy-item]").forEach((el) => {
|
|
370
|
+
if (el.dataset.anatomyItem === partId) {
|
|
371
|
+
el.setAttribute("data-active", "");
|
|
372
|
+
if (source === "preview") {
|
|
373
|
+
el.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
el.removeAttribute("data-active");
|
|
377
|
+
}
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
emit("part:enter", partId);
|
|
381
|
+
}
|
|
382
|
+
function unhighlight() {
|
|
383
|
+
overlay.hide();
|
|
384
|
+
if (panel) {
|
|
385
|
+
panel.querySelectorAll("[data-anatomy-item]").forEach((el) => {
|
|
386
|
+
el.removeAttribute("data-active");
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
emit("part:leave", "");
|
|
390
|
+
}
|
|
391
|
+
function attachElementListeners() {
|
|
392
|
+
const map = registry.query();
|
|
393
|
+
map.forEach((elements, partId) => {
|
|
394
|
+
elements.forEach((el) => {
|
|
395
|
+
const enter = () => highlight(partId, "preview");
|
|
396
|
+
const leave = () => unhighlight();
|
|
397
|
+
el.addEventListener("mouseenter", enter);
|
|
398
|
+
el.addEventListener("mouseleave", leave);
|
|
399
|
+
cleanupFns.push(() => {
|
|
400
|
+
el.removeEventListener("mouseenter", enter);
|
|
401
|
+
el.removeEventListener("mouseleave", leave);
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
function attachPanelListeners() {
|
|
407
|
+
if (!panel) return;
|
|
408
|
+
panel.querySelectorAll("[data-anatomy-item]").forEach((el) => {
|
|
409
|
+
const partId = el.dataset.anatomyItem ?? "";
|
|
410
|
+
const enter = () => highlight(partId, "panel");
|
|
411
|
+
const leave = () => unhighlight();
|
|
412
|
+
el.addEventListener("mouseenter", enter);
|
|
413
|
+
el.addEventListener("mouseleave", leave);
|
|
414
|
+
el.addEventListener("focus", enter);
|
|
415
|
+
el.addEventListener("blur", leave);
|
|
416
|
+
cleanupFns.push(() => {
|
|
417
|
+
el.removeEventListener("mouseenter", enter);
|
|
418
|
+
el.removeEventListener("mouseleave", leave);
|
|
419
|
+
el.removeEventListener("focus", enter);
|
|
420
|
+
el.removeEventListener("blur", leave);
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
const onScroll = () => overlay.reposition();
|
|
425
|
+
const onResize = () => overlay.reposition();
|
|
426
|
+
const resizeObserver = new ResizeObserver(() => overlay.reposition());
|
|
427
|
+
function setup() {
|
|
428
|
+
attachElementListeners();
|
|
429
|
+
attachPanelListeners();
|
|
430
|
+
window.addEventListener("scroll", onScroll, { passive: true, capture: true });
|
|
431
|
+
window.addEventListener("resize", onResize, { passive: true });
|
|
432
|
+
resizeObserver.observe(root);
|
|
433
|
+
const stopObserving = registry.observe(() => {
|
|
434
|
+
teardownListeners();
|
|
435
|
+
if (!options.parts) {
|
|
436
|
+
parts = registry.partIds().map((id) => ({ id, name: idToName(id) }));
|
|
437
|
+
}
|
|
438
|
+
attachElementListeners();
|
|
439
|
+
attachPanelListeners();
|
|
440
|
+
});
|
|
441
|
+
cleanupFns.push(stopObserving);
|
|
442
|
+
}
|
|
443
|
+
function teardownListeners() {
|
|
444
|
+
const observerCleanup = cleanupFns.pop();
|
|
445
|
+
cleanupFns.forEach((fn) => fn());
|
|
446
|
+
cleanupFns.length = 0;
|
|
447
|
+
if (observerCleanup) cleanupFns.push(observerCleanup);
|
|
448
|
+
}
|
|
449
|
+
setup();
|
|
450
|
+
return {
|
|
451
|
+
highlight,
|
|
452
|
+
unhighlight,
|
|
453
|
+
refresh() {
|
|
454
|
+
unhighlight();
|
|
455
|
+
teardownListeners();
|
|
456
|
+
if (!options.parts) {
|
|
457
|
+
parts = registry.partIds().map((id) => ({ id, name: idToName(id) }));
|
|
458
|
+
}
|
|
459
|
+
attachElementListeners();
|
|
460
|
+
attachPanelListeners();
|
|
461
|
+
},
|
|
462
|
+
destroy() {
|
|
463
|
+
unhighlight();
|
|
464
|
+
cleanupFns.forEach((fn) => fn());
|
|
465
|
+
cleanupFns.length = 0;
|
|
466
|
+
window.removeEventListener("scroll", onScroll, { capture: true });
|
|
467
|
+
window.removeEventListener("resize", onResize);
|
|
468
|
+
resizeObserver.disconnect();
|
|
469
|
+
overlay.destroy();
|
|
470
|
+
registry.destroy();
|
|
471
|
+
listeners.clear();
|
|
472
|
+
},
|
|
473
|
+
on(event, handler) {
|
|
474
|
+
if (!listeners.has(event)) listeners.set(event, /* @__PURE__ */ new Set());
|
|
475
|
+
listeners.get(event).add(handler);
|
|
476
|
+
return () => listeners.get(event)?.delete(handler);
|
|
477
|
+
},
|
|
478
|
+
getParts() {
|
|
479
|
+
return parts.slice();
|
|
480
|
+
},
|
|
481
|
+
setTheme(theme, preset) {
|
|
482
|
+
overlay.setVars(resolveThemeVars(preset, theme));
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
//# sourceMappingURL=index.cjs.map
|