ember-attacher 5.0.0-beta.1 → 5.0.0-beta.3

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 ADDED
@@ -0,0 +1,421 @@
1
+ # ember-attacher
2
+
3
+ [![npm version](https://badge.fury.io/js/ember-attacher.svg)](http://badge.fury.io/js/ember-attacher)
4
+ [![Download count all time](https://img.shields.io/npm/dt/ember-attacher.svg)](https://www.npmjs.com/package/ember-attacher)
5
+ [![npm](https://img.shields.io/npm/dm/ember-attacher.svg)](https://www.npmjs.com/package/ember-attacher)
6
+ [![Ember Observer Score](http://emberobserver.com/badges/ember-attacher.svg)](http://emberobserver.com/addons/ember-attacher)
7
+ [![GitHub CI](https://github.com/tylerturdenpants/ember-attacher/actions/workflows/ci.yml/badge.svg)](https://github.com/tylerturdenpants/ember-attacher/actions/workflows/ci.yml)
8
+
9
+ ## Compatibility
10
+
11
+ * Ember.js v4.12 LTS through v6.x and v7.x (including Embroider + Vite and classic builds)
12
+ * Ember CLI v4.12 or above
13
+ * Node.js v20 LTS or above
14
+ * Full TypeScript & Glint `.d.ts` declarations (`ember-attacher`, `ember-attacher/template-registry`, `ember-attacher/test-support`)
15
+
16
+ Tooltips and popovers for Ember powered by `@floating-ui/dom` and native Top-Layer `[popover="manual"]` hosts.
17
+
18
+ ### `<template>` (`.gjs` / `.gts`)
19
+
20
+ ```gts
21
+ import { AttachPopover, AttachTooltip, offset, tooltip } from 'ember-attacher';
22
+
23
+ const offsetMiddleware = [offset(12)];
24
+
25
+ <template>
26
+ <button type="button" {{tooltip "Quick singleton tooltip" placement="top"}}>
27
+ Singleton modifier
28
+ </button>
29
+
30
+ <AttachTooltip @placement="top-start">
31
+ <:target as |target|>
32
+ <button type="button" {{target}}>Hover me</button>
33
+ </:target>
34
+ <:content>
35
+ I'm a rich <strong>HTML</strong> tooltip!
36
+ </:content>
37
+ </AttachTooltip>
38
+
39
+ <button id="menu-trigger" type="button">
40
+ Click me
41
+ </button>
42
+ <AttachPopover
43
+ @target="#menu-trigger"
44
+ @class="ember-attacher-popover"
45
+ @middleware={{offsetMiddleware}}
46
+ >
47
+ I'm an interactive popover!
48
+ </AttachPopover>
49
+ </template>
50
+ ```
51
+
52
+ ### Loose templates (`.hbs`)
53
+
54
+ ```hbs
55
+ {{!-- External target --}}
56
+ <button id="external-target" type="button">Target</button>
57
+ <AttachPopover @target="#external-target">
58
+ I'm attached to #external-target!
59
+ </AttachPopover>
60
+
61
+ {{!-- Native HTML invokers ([popovertarget] / [interestfor]) --}}
62
+ <button type="button" interestfor="hint-tooltip">Details</button>
63
+ <AttachTooltip @id="hint-tooltip">
64
+ Declarative tooltip via interestfor
65
+ </AttachTooltip>
66
+ ```
67
+
68
+ See [the example site](https://tylerturdenpants.github.io/ember-attacher/) for a live gallery of placements and animations.
69
+
70
+ ## Installation
71
+
72
+ ```bash
73
+ ember install ember-attacher
74
+ ```
75
+
76
+ If you're upgrading to **5.0** from 4.x, see the [5.0 upgrade guide](https://github.com/tylerturdenpants/ember-attacher/blob/master/addon/docs/upgrade-guide-5.0.md).
77
+ If you're upgrading from **3.x to 4.x**, see the [4.1 upgrade guide](https://github.com/tylerturdenpants/ember-attacher/blob/master/addon/docs/upgrade-guide-4.1.md).
78
+ If you're upgrading from **1.x to 2.x**, see the [2.0 upgrade guide](https://github.com/tylerturdenpants/ember-attacher/blob/master/addon/docs/upgrade-guide-2.0.md).
79
+
80
+ ## Components & Modifiers
81
+
82
+ ### `{{tooltip}}` modifier
83
+
84
+ `{{tooltip "Text" class="hint"}}` attaches a tooltip to a void or simple element such as `<button>` or `<input>`. Every `{{tooltip}}` in the document shares one singleton `.ember-attacher` DOM node with a `300ms` warm delay-group handoff across adjacent tooltips.
85
+
86
+ ### `<AttachTooltip/>`
87
+
88
+ A target-attached tooltip component (default `ariaRole="tooltip"`).
89
+
90
+ * Must be bound to a Target (`@target`, `<:target>`, `{{a.target}}`, `a.setTarget()`, `[popovertarget]`, or `[interestfor]`). Asserts in debug if no target binding is provided (nullable reactive `@target={{this.target}}` is supported).
91
+ * Has the default class `'ember-attacher-floating ember-attacher-tooltip'` (configurable via `tooltipClass`).
92
+ * Hoverable by default (`@interactive={{true}}`) per WCAG 1.4.13 with an automatic `safePolygon` diagonal pointer corridor (pass `@interactive={{false}}` to opt out).
93
+ * Smart Tooltip ARIA: binds `aria-labelledby` upfront on nameless triggers (such as icon buttons), suppresses duplicate `aria-describedby` when tooltip text matches the trigger's accessible name, and otherwise binds open-only token-safe `aria-describedby`.
94
+
95
+ ### `<AttachPopover/>`
96
+
97
+ An interactive popover component (default `ariaRole="dialog"`, `showOn="click"`, `hideOn="click clickout escapekey"`, `interactive=true`, `lazyRender=true`).
98
+
99
+ * Must be bound to a Target (`@target`, `<:target>`, `{{a.target}}`, `a.setTarget()`, or `[popovertarget]`).
100
+ * Has **no default visual look** (tooltip styles are not applied automatically); pass `@class="ember-attacher-popover"` to opt into the packaged styling or supply your own class.
101
+ * Renders `<div class="ember-attacher" popover="manual">` in place in 1 pass and promotes to the browser Top Layer on open (`@renderInPlace={{true}}` opts out of Top Layer).
102
+ * While mounted, sets `aria-haspopup` (matching `@ariaRole` or `'dialog'`), `aria-controls`, and `aria-expanded` on the Target when not already provided by the caller.
103
+ * Restores focus to the Target when dismissed via `Escape` while focus is inside the popover.
104
+
105
+ ### `<Attacher as |a|>`
106
+
107
+ The headless primitive yielding `{ target, attachment, arrow, setTarget, show, hide, toggle, update, isShown, shouldRender, data, placement, middlewareData }`. Use `{{a.target data}}` to share one overlay across list items, `{{a.attachment}}` on your own floating element, and `{{a.arrow}}` on an optional arrow element.
108
+
109
+ ## Options
110
+
111
+ Below are the 5.0 defaults (shared defaults from `addon/src/defaults.ts`; `<AttachPopover>` overrides `showOn: 'click'`, `hideOn: 'click clickout escapekey'`, and `lazyRender: true`):
112
+
113
+ ```typescript
114
+ {
115
+ // Animation used to present and dismiss the attachment:
116
+ // 'fade' | 'fill' | 'none' | 'perspective' | 'scale' | 'shift'
117
+ animation: 'fill',
118
+
119
+ // Whether an arrow element is rendered and positioned by Floating UI.
120
+ arrow: false,
121
+
122
+ // Keeps position updated while open via Floating UI autoUpdate().
123
+ // Pass `true` or Floating UI AutoUpdateOptions (e.g. `{ ancestorScroll: false }`), or `false`.
124
+ autoUpdate: true,
125
+
126
+ // Class(es) applied to the inner attachment container.
127
+ class: null,
128
+
129
+ // Delay in milliseconds before the attachment hides.
130
+ hideDelay: 0,
131
+
132
+ // Duration in milliseconds of the hide transition.
133
+ hideDuration: 300,
134
+
135
+ // Space-delimited events that hide the attachment:
136
+ // 'click' | 'clickout' | 'mouseleave' | 'blur' | 'focusout' | 'escapekey' | null
137
+ // (<AttachPopover> defaults to 'click clickout escapekey')
138
+ hideOn: 'mouseleave blur escapekey',
139
+
140
+ // Keeps attachment open while pointer/focus enters it or traverses the safePolygon corridor.
141
+ interactive: true,
142
+
143
+ // Controlled visibility flag.
144
+ isShown: false,
145
+
146
+ // Defers rendering inner content until the attachment is first triggered.
147
+ // (<AttachPopover> defaults to true)
148
+ lazyRender: false,
149
+
150
+ // Floating UI middleware array merged and deduplicated by `m.name`.
151
+ // Default `flip()` and `shift({ limiter: limitShift(), padding: 5 })` apply unless overridden.
152
+ // Pass `hide()` when you want attachments to hide once their target scrolls out of a clipping container.
153
+ // Re-exported builders from 'ember-attacher':
154
+ // { arrow, autoPlacement, flip, hide, inline, limitShift, offset, shift, size }
155
+ middleware: null,
156
+
157
+ // Invoked with the new visibility boolean when the attachment shows or hides.
158
+ onChange: null,
159
+
160
+ // Initial Floating UI placement:
161
+ // 'top' | 'top-start' | 'top-end' | 'right' | 'right-start' | 'right-end'
162
+ // | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end'
163
+ placement: 'top',
164
+
165
+ // Renders in place without promoting to the browser Top Layer ([popover="manual"]).
166
+ renderInPlace: false,
167
+
168
+ // Delay in milliseconds before the attachment shows.
169
+ showDelay: 0,
170
+
171
+ // Duration in milliseconds of the show transition.
172
+ showDuration: 300,
173
+
174
+ // Space-delimited events that show the attachment:
175
+ // 'mouseenter' | 'focus' | 'click' | null
176
+ // (<AttachPopover> defaults to 'click')
177
+ showOn: 'mouseenter focus',
178
+
179
+ // CSS positioning strategy used by Floating UI ('absolute' | 'fixed').
180
+ strategy: 'absolute',
181
+
182
+ // Attach Target (`Element | CSS selector | VirtualElement`).
183
+ target: null,
184
+
185
+ // Registers target/document event listeners in the capture phase.
186
+ useCapture: false,
187
+ }
188
+ ```
189
+
190
+ ## Recipes
191
+
192
+ ### Pinning and flipping within a custom boundary
193
+
194
+ Pass Floating UI `flip` and `shift` options with a custom `boundary` element via `@middleware` to keep an attachment constrained inside a scroll area or layout panel:
195
+
196
+ ```gts
197
+ import { tracked } from '@glimmer/tracking';
198
+ import { modifier } from 'ember-modifier';
199
+ import { AttachPopover, flip, shift } from 'ember-attacher';
200
+
201
+ export default class BoundaryDemo {
202
+ @tracked containerEl = null;
203
+
204
+ registerContainer = modifier((element) => {
205
+ this.containerEl = element;
206
+ });
207
+
208
+ get boundaryMiddleware() {
209
+ if (!this.containerEl) return [];
210
+ return [
211
+ flip({ boundary: this.containerEl }),
212
+ shift({ boundary: this.containerEl, padding: 8 }),
213
+ ];
214
+ }
215
+
216
+ <template>
217
+ <div {{this.registerContainer}}>
218
+ <button id="panel-btn" type="button">Options</button>
219
+ <AttachPopover @target="#panel-btn" @middleware={{this.boundaryMiddleware}}>
220
+ Stays within containerEl
221
+ </AttachPopover>
222
+ </div>
223
+ </template>
224
+ }
225
+ ```
226
+
227
+ ### Coordinate / virtual targets (`VirtualElement`, no DOM trigger)
228
+
229
+ Pass any `{ getBoundingClientRect() }` object to `@target` with `@showOn={{null}}` and controlled `@isShown` (or `a.show(virtualTarget)`). Add `@autoUpdate={{hash animationFrame=true}}` when tracking moving coordinates or externally animated elements:
230
+
231
+ ```gts
232
+ import { hash } from '@ember/helper';
233
+ import { AttachPopover } from 'ember-attacher';
234
+
235
+ const cursorTarget = {
236
+ getBoundingClientRect() {
237
+ return { x: 160, y: 120, top: 120, left: 160, right: 160, bottom: 120, width: 0, height: 0 };
238
+ },
239
+ };
240
+
241
+ <template>
242
+ <AttachPopover
243
+ @target={{cursorTarget}}
244
+ @showOn={{null}}
245
+ @isShown={{true}}
246
+ @autoUpdate={{hash animationFrame=true}}
247
+ >
248
+ Positioned at virtual coordinates
249
+ </AttachPopover>
250
+ </template>
251
+ ```
252
+
253
+ ### Portal / body-level stacking with `{{#in-element}}` + `@renderInPlace`
254
+
255
+ By default, components promote in place to the browser Top Layer (`popover="manual"`). When you want explicit DOM container placement and `z-index` stacking instead, opt out of Top Layer with `@renderInPlace={{true}}` inside `{{#in-element}}`:
256
+
257
+ ```hbs
258
+ {{#in-element this.portalContainer insertBefore=null}}
259
+ <AttachPopover
260
+ @target="#trigger"
261
+ @renderInPlace={{true}}
262
+ @class="my-z-indexed-popover"
263
+ >
264
+ Rendered inside portalContainer with traditional CSS stacking
265
+ </AttachPopover>
266
+ {{/in-element}}
267
+ ```
268
+
269
+ ### Re-binding externally replaced DOM elements (`@target` / `a.setTarget`)
270
+
271
+ String selectors in `@target` resolve once when `@target` mounts or changes. If a third-party widget (such as a calendar or data grid) tears down and recreates target nodes without re-rendering your template, pass a tracked element via `@target={{this.targetEl}}` (or use `<AttachPopover @target={{null}} as |a|>` and call `a.setTarget(el, data)`):
272
+
273
+ ```gts
274
+ import { tracked } from '@glimmer/tracking';
275
+ import { AttachPopover } from 'ember-attacher';
276
+
277
+ export default class ExternalWidgetOverlay {
278
+ @tracked targetEl = null;
279
+ @tracked activeEvent = null;
280
+
281
+ // Hook into widget mount / click callbacks:
282
+ onWidgetItemMounted = (el, eventData) => {
283
+ this.targetEl = el;
284
+ this.activeEvent = eventData;
285
+ };
286
+
287
+ <template>
288
+ <AttachPopover @target={{this.targetEl}}>
289
+ {{this.activeEvent.title}}
290
+ </AttachPopover>
291
+ </template>
292
+ }
293
+ ```
294
+
295
+ ## User-defined defaults
296
+
297
+ User-defined defaults can be configured in the consuming app's `config/environment.js`:
298
+
299
+ ```javascript
300
+ // config/environment.js
301
+ module.exports = function (environment) {
302
+ let ENV = {
303
+ emberAttacher: {
304
+ showDuration: 200,
305
+ hideDuration: 200,
306
+ tooltip: {
307
+ animation: 'fade',
308
+ arrow: true,
309
+ },
310
+ popover: {
311
+ animation: 'shift',
312
+ arrow: false,
313
+ },
314
+ },
315
+ };
316
+ return ENV;
317
+ };
318
+ ```
319
+
320
+ See [defaults.ts](https://github.com/tylerturdenpants/ember-attacher/blob/master/addon/src/defaults.ts) for all configurable keys.
321
+
322
+ ## Styles
323
+
324
+ `ember-attacher` provides CSS for the default tooltip class, `ember-attacher-tooltip` (`ember-attacher/styles`, also auto-injected via `ember-addon.implicit-styles` when the host app depends on `ember-attacher` directly). If an intermediate addon only lists `ember-attacher` as a `peerDependency`, the host app must also depend on `ember-attacher` directly or `import 'ember-attacher/styles';`. `<AttachPopover>` has no packaged look unless you opt in with `@class="ember-attacher-popover"` (same tokens as the tooltip; not applied automatically).
325
+
326
+ Default token values use `:where()` so an app class can override them even if addon `implicit-styles` load last.
327
+
328
+ ```css
329
+ .ember-attacher-tooltip,
330
+ .ember-attacher-popover {
331
+ --ember-attacher-fill: #333;
332
+ --ember-attacher-text: #fff;
333
+ --ember-attacher-radius: 4px;
334
+ --ember-attacher-border: var(--ember-attacher-fill);
335
+ --ember-attacher-border-width: 0px; /* set to 1px+ when you need a visible border */
336
+ --ember-attacher-arrow-size: 10px;
337
+ --ember-attacher-arrow-width: var(--ember-attacher-arrow-size);
338
+ --ember-attacher-arrow-height: var(--ember-attacher-arrow-size);
339
+ --ember-attacher-padding: 0.5rem 1rem;
340
+ --ember-attacher-shadow: none;
341
+ --ember-attacher-max-width: 400px;
342
+ --ember-attacher-font-size: inherit;
343
+ --ember-attacher-line-height: inherit;
344
+ --ember-attacher-text-align: center;
345
+ }
346
+ ```
347
+
348
+ `.ember-attacher-light-theme` reassigns those same tokens (fill `#f3f6f9`, text `#203d5d`, border `#ced2e2`, `border-width: 1px`, and box shadow).
349
+
350
+ Outer wrapper tokens on `.ember-attacher`:
351
+ * `--ember-attacher-distance` (default `10px`): inner animation gap.
352
+ * `--ember-attacher-z-index` (default `9999`): stacking context (used for non-Top-Layer / `@renderInPlace` hosts).
353
+ * `--ember-attacher-transition-duration` (default `0ms`): driven via CSSOM from `@showDuration` / `@hideDuration` (strict CSP `style-src` compatible without `'unsafe-inline'`). Spatial transforms opt in under `@media (prefers-reduced-motion: no-preference)`, and `@media (prefers-reduced-motion: reduce)` caps opacity transitions to `min(var(--ember-attacher-transition-duration, 0ms), 100ms)`.
354
+
355
+ Set outer tokens on `.ember-attacher` or an ancestor container. `--ember-attacher-offset` and `--ember-attacher-arrow-rotate` remain internal.
356
+
357
+ ## Architecture & Animations
358
+
359
+ Attachments use two nested elements rendered directly in place (1 pass):
360
+
361
+ * **Outer host** (`.ember-attacher[popover="manual"]`, see [attacher-overlay.gts](https://github.com/tylerturdenpants/ember-attacher/blob/master/addon/src/lib/attacher-overlay.gts)): promoted to the Top Layer via `showPopover()`, positioned relative to the Target by `@floating-ui/dom`, and annotated with `data-placement` / `x-placement` plus `data-open` / `data-closing`.
362
+ * **Inner content container**: hosts visual styles, the optional `.ember-attacher-arrow`, and `.ember-attacher-fill-circle` backdrop. Keeping inner animations decoupled from Floating UI's outer coordinates avoids layout interference while Top-Layer transitions animate entry via `@starting-style` and exit via `transition-behavior: allow-discrete` + Web Animations (`getAnimations({ subtree: true })`).
363
+
364
+ See [ember-attacher.css](https://github.com/tylerturdenpants/ember-attacher/blob/master/addon/src/styles/ember-attacher.css) for the full placement and animation definitions.
365
+
366
+ ## Testing
367
+
368
+ Use the `isVisible()` helper from `'ember-attacher/test-support'`:
369
+
370
+ ```javascript
371
+ import { click, find, render } from '@ember/test-helpers';
372
+ import { isVisible } from 'ember-attacher/test-support';
373
+
374
+ test('example', async function (assert) {
375
+ await render(hbs`
376
+ <button id="toggle" type="button">
377
+ Click me, captain!
378
+ </button>
379
+
380
+ <AttachPopover
381
+ @id="attachment"
382
+ @target="#toggle"
383
+ @hideOn="click"
384
+ @showOn="click"
385
+ >
386
+ Click-toggled popover
387
+ </AttachPopover>
388
+ `);
389
+
390
+ const attachment = find('#attachment');
391
+
392
+ assert.false(isVisible(attachment), 'Initially hidden');
393
+
394
+ await click('#toggle');
395
+
396
+ // You can also pass in a selector
397
+ assert.true(isVisible('#attachment'), 'Now shown');
398
+ });
399
+ ```
400
+
401
+ `import { isVisible } from 'ember-attacher'` is also supported for convenience.
402
+
403
+ To check that an open attachment actually sits next to its target, use `assertAttached()`. It pushes one QUnit result listing every problem it finds: not visible, wrong `data-placement`, wrong side, gap larger than `maxGap` (default `24`px), no cross-axis overlap with the target, arrow not pointing at the target, overflowing the viewport, or covered by another element.
404
+
405
+ ```javascript
406
+ import { triggerEvent } from '@ember/test-helpers';
407
+ import { assertAttached } from 'ember-attacher/test-support';
408
+
409
+ await triggerEvent('#trigger', 'mouseenter');
410
+ assertAttached(assert, '#tip', '#trigger', { placement: 'bottom' });
411
+ ```
412
+
413
+ ## Development setup
414
+
415
+ See the [Contributing](https://github.com/tylerturdenpants/ember-attacher/blob/master/CONTRIBUTING.md) guide for details.
416
+
417
+ ## Credits
418
+
419
+ * [tippy.js](https://github.com/atomiks/tippyjs), the library that inspired ember-attacher.
420
+ * [floating-ui](https://github.com/floating-ui/floating-ui), the library that powers positioning.
421
+ * [ember-tooltips](https://github.com/sir-dunxalot/ember-tooltips), the addon that influenced much of ember-attacher's initial API.