ember-attacher 5.0.0-beta.1 → 5.0.0-beta.2
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 +416 -0
- package/dist/{attacher-core-CtbFbuD_.js → attacher-core-DIAMEQR6.js} +69 -3
- package/dist/attacher-core-DIAMEQR6.js.map +1 -0
- package/dist/{attacher-overlay-BoI7s8AI.js → attacher-overlay-BCAfauc_.js} +2 -2
- package/dist/{attacher-overlay-BoI7s8AI.js.map → attacher-overlay-BCAfauc_.js.map} +1 -1
- package/dist/components/attach-popover.js +1 -1
- package/dist/components/attach-tooltip.js +2 -2
- package/dist/components/attacher.js +1 -1
- package/dist/lib/dom.d.ts +8 -0
- package/dist/modifiers/tooltip.js +1 -1
- package/dist/test-support/assert-attached.d.ts +27 -0
- package/dist/test-support/index.d.ts +1 -0
- package/dist/test-support/index.js +95 -1
- package/dist/test-support/index.js.map +1 -1
- package/package.json +4 -2
- package/dist/attacher-core-CtbFbuD_.js.map +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
# ember-attacher
|
|
2
|
+
|
|
3
|
+
[](http://badge.fury.io/js/ember-attacher)
|
|
4
|
+
[](https://www.npmjs.com/package/ember-attacher)
|
|
5
|
+
[](https://www.npmjs.com/package/ember-attacher)
|
|
6
|
+
[](http://emberobserver.com/addons/ember-attacher)
|
|
7
|
+
[](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
|
+
* 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).
|
|
101
|
+
* While mounted, sets `aria-haspopup` (matching `@ariaRole` or `'dialog'`), `aria-controls`, and `aria-expanded` on the Target when not already provided by the caller.
|
|
102
|
+
* Restores focus to the Target when dismissed via `Escape` while focus is inside the popover.
|
|
103
|
+
|
|
104
|
+
### `<Attacher as |a|>`
|
|
105
|
+
|
|
106
|
+
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.
|
|
107
|
+
|
|
108
|
+
## Options
|
|
109
|
+
|
|
110
|
+
Below are the 5.0 defaults (shared defaults from `addon/src/defaults.ts`; `<AttachPopover>` overrides `showOn: 'click'`, `hideOn: 'click clickout escapekey'`, and `lazyRender: true`):
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
{
|
|
114
|
+
// Animation used to present and dismiss the attachment:
|
|
115
|
+
// 'fade' | 'fill' | 'none' | 'perspective' | 'scale' | 'shift'
|
|
116
|
+
animation: 'fill',
|
|
117
|
+
|
|
118
|
+
// Whether an arrow element is rendered and positioned by Floating UI.
|
|
119
|
+
arrow: false,
|
|
120
|
+
|
|
121
|
+
// Keeps position updated while open via Floating UI autoUpdate().
|
|
122
|
+
// Pass `true` or Floating UI AutoUpdateOptions (e.g. `{ ancestorScroll: false }`), or `false`.
|
|
123
|
+
autoUpdate: true,
|
|
124
|
+
|
|
125
|
+
// Class(es) applied to the inner attachment container.
|
|
126
|
+
class: null,
|
|
127
|
+
|
|
128
|
+
// Delay in milliseconds before the attachment hides.
|
|
129
|
+
hideDelay: 0,
|
|
130
|
+
|
|
131
|
+
// Duration in milliseconds of the hide transition.
|
|
132
|
+
hideDuration: 300,
|
|
133
|
+
|
|
134
|
+
// Space-delimited events that hide the attachment:
|
|
135
|
+
// 'click' | 'clickout' | 'mouseleave' | 'blur' | 'focusout' | 'escapekey' | null
|
|
136
|
+
// (<AttachPopover> defaults to 'click clickout escapekey')
|
|
137
|
+
hideOn: 'mouseleave blur escapekey',
|
|
138
|
+
|
|
139
|
+
// Keeps attachment open while pointer/focus enters it or traverses the safePolygon corridor.
|
|
140
|
+
interactive: true,
|
|
141
|
+
|
|
142
|
+
// Controlled visibility flag.
|
|
143
|
+
isShown: false,
|
|
144
|
+
|
|
145
|
+
// Defers rendering inner content until the attachment is first triggered.
|
|
146
|
+
// (<AttachPopover> defaults to true)
|
|
147
|
+
lazyRender: false,
|
|
148
|
+
|
|
149
|
+
// Floating UI middleware array merged and deduplicated by `m.name`.
|
|
150
|
+
// Default `flip()` and `shift({ limiter: limitShift(), padding: 5 })` apply unless overridden.
|
|
151
|
+
// Re-exported builders from 'ember-attacher':
|
|
152
|
+
// { arrow, autoPlacement, flip, hide, inline, limitShift, offset, shift, size }
|
|
153
|
+
middleware: null,
|
|
154
|
+
|
|
155
|
+
// Invoked with the new visibility boolean when the attachment shows or hides.
|
|
156
|
+
onChange: null,
|
|
157
|
+
|
|
158
|
+
// Initial Floating UI placement:
|
|
159
|
+
// 'top' | 'top-start' | 'top-end' | 'right' | 'right-start' | 'right-end'
|
|
160
|
+
// | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end'
|
|
161
|
+
placement: 'top',
|
|
162
|
+
|
|
163
|
+
// Renders in place without promoting to the browser Top Layer ([popover="manual"]).
|
|
164
|
+
renderInPlace: false,
|
|
165
|
+
|
|
166
|
+
// Delay in milliseconds before the attachment shows.
|
|
167
|
+
showDelay: 0,
|
|
168
|
+
|
|
169
|
+
// Duration in milliseconds of the show transition.
|
|
170
|
+
showDuration: 300,
|
|
171
|
+
|
|
172
|
+
// Space-delimited events that show the attachment:
|
|
173
|
+
// 'mouseenter' | 'focus' | 'click' | null
|
|
174
|
+
// (<AttachPopover> defaults to 'click')
|
|
175
|
+
showOn: 'mouseenter focus',
|
|
176
|
+
|
|
177
|
+
// CSS positioning strategy used by Floating UI ('absolute' | 'fixed').
|
|
178
|
+
strategy: 'absolute',
|
|
179
|
+
|
|
180
|
+
// Attach Target (`Element | CSS selector | VirtualElement`).
|
|
181
|
+
target: null,
|
|
182
|
+
|
|
183
|
+
// Registers target/document event listeners in the capture phase.
|
|
184
|
+
useCapture: false,
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Recipes
|
|
189
|
+
|
|
190
|
+
### Pinning and flipping within a custom boundary
|
|
191
|
+
|
|
192
|
+
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:
|
|
193
|
+
|
|
194
|
+
```gts
|
|
195
|
+
import { tracked } from '@glimmer/tracking';
|
|
196
|
+
import { modifier } from 'ember-modifier';
|
|
197
|
+
import { AttachPopover, flip, shift } from 'ember-attacher';
|
|
198
|
+
|
|
199
|
+
export default class BoundaryDemo {
|
|
200
|
+
@tracked containerEl = null;
|
|
201
|
+
|
|
202
|
+
registerContainer = modifier((element) => {
|
|
203
|
+
this.containerEl = element;
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
get boundaryMiddleware() {
|
|
207
|
+
if (!this.containerEl) return [];
|
|
208
|
+
return [
|
|
209
|
+
flip({ boundary: this.containerEl }),
|
|
210
|
+
shift({ boundary: this.containerEl, padding: 8 }),
|
|
211
|
+
];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
<template>
|
|
215
|
+
<div {{this.registerContainer}}>
|
|
216
|
+
<button id="panel-btn" type="button">Options</button>
|
|
217
|
+
<AttachPopover @target="#panel-btn" @middleware={{this.boundaryMiddleware}}>
|
|
218
|
+
Stays within containerEl
|
|
219
|
+
</AttachPopover>
|
|
220
|
+
</div>
|
|
221
|
+
</template>
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Coordinate / virtual targets (`VirtualElement`, no DOM trigger)
|
|
226
|
+
|
|
227
|
+
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:
|
|
228
|
+
|
|
229
|
+
```gts
|
|
230
|
+
import { hash } from '@ember/helper';
|
|
231
|
+
import { AttachPopover } from 'ember-attacher';
|
|
232
|
+
|
|
233
|
+
const cursorTarget = {
|
|
234
|
+
getBoundingClientRect() {
|
|
235
|
+
return { x: 160, y: 120, top: 120, left: 160, right: 160, bottom: 120, width: 0, height: 0 };
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
<template>
|
|
240
|
+
<AttachPopover
|
|
241
|
+
@target={{cursorTarget}}
|
|
242
|
+
@showOn={{null}}
|
|
243
|
+
@isShown={{true}}
|
|
244
|
+
@autoUpdate={{hash animationFrame=true}}
|
|
245
|
+
>
|
|
246
|
+
Positioned at virtual coordinates
|
|
247
|
+
</AttachPopover>
|
|
248
|
+
</template>
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Portal / body-level stacking with `{{#in-element}}` + `@renderInPlace`
|
|
252
|
+
|
|
253
|
+
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}}`:
|
|
254
|
+
|
|
255
|
+
```hbs
|
|
256
|
+
{{#in-element this.portalContainer insertBefore=null}}
|
|
257
|
+
<AttachPopover
|
|
258
|
+
@target="#trigger"
|
|
259
|
+
@renderInPlace={{true}}
|
|
260
|
+
@class="my-z-indexed-popover"
|
|
261
|
+
>
|
|
262
|
+
Rendered inside portalContainer with traditional CSS stacking
|
|
263
|
+
</AttachPopover>
|
|
264
|
+
{{/in-element}}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### Re-binding externally replaced DOM elements (`@target` / `a.setTarget`)
|
|
268
|
+
|
|
269
|
+
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)`):
|
|
270
|
+
|
|
271
|
+
```gts
|
|
272
|
+
import { tracked } from '@glimmer/tracking';
|
|
273
|
+
import { AttachPopover } from 'ember-attacher';
|
|
274
|
+
|
|
275
|
+
export default class ExternalWidgetOverlay {
|
|
276
|
+
@tracked targetEl = null;
|
|
277
|
+
@tracked activeEvent = null;
|
|
278
|
+
|
|
279
|
+
// Hook into widget mount / click callbacks:
|
|
280
|
+
onWidgetItemMounted = (el, eventData) => {
|
|
281
|
+
this.targetEl = el;
|
|
282
|
+
this.activeEvent = eventData;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
<template>
|
|
286
|
+
<AttachPopover @target={{this.targetEl}}>
|
|
287
|
+
{{this.activeEvent.title}}
|
|
288
|
+
</AttachPopover>
|
|
289
|
+
</template>
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## User-defined defaults
|
|
294
|
+
|
|
295
|
+
User-defined defaults can be configured in the consuming app's `config/environment.js`:
|
|
296
|
+
|
|
297
|
+
```javascript
|
|
298
|
+
// config/environment.js
|
|
299
|
+
module.exports = function (environment) {
|
|
300
|
+
let ENV = {
|
|
301
|
+
emberAttacher: {
|
|
302
|
+
showDuration: 200,
|
|
303
|
+
hideDuration: 200,
|
|
304
|
+
tooltip: {
|
|
305
|
+
animation: 'fade',
|
|
306
|
+
arrow: true,
|
|
307
|
+
},
|
|
308
|
+
popover: {
|
|
309
|
+
animation: 'shift',
|
|
310
|
+
arrow: false,
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
return ENV;
|
|
315
|
+
};
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
See [defaults.ts](https://github.com/tylerturdenpants/ember-attacher/blob/master/addon/src/defaults.ts) for all configurable keys.
|
|
319
|
+
|
|
320
|
+
## Styles
|
|
321
|
+
|
|
322
|
+
`ember-attacher` provides CSS for the default tooltip class, `ember-attacher-tooltip` (`ember-attacher/styles`; also injected via `implicit-styles`). `<AttachPopover>` has no packaged look unless you opt in with `@class="ember-attacher-popover"` (same tokens as the tooltip; not applied automatically).
|
|
323
|
+
|
|
324
|
+
Default token values use `:where()` so an app class can override them even if addon `implicit-styles` load last.
|
|
325
|
+
|
|
326
|
+
```css
|
|
327
|
+
.ember-attacher-tooltip,
|
|
328
|
+
.ember-attacher-popover {
|
|
329
|
+
--ember-attacher-fill: #333;
|
|
330
|
+
--ember-attacher-text: #fff;
|
|
331
|
+
--ember-attacher-radius: 4px;
|
|
332
|
+
--ember-attacher-border: var(--ember-attacher-fill);
|
|
333
|
+
--ember-attacher-border-width: 0px; /* set to 1px+ when you need a visible border */
|
|
334
|
+
--ember-attacher-arrow-size: 10px;
|
|
335
|
+
--ember-attacher-padding: 0.5rem 1rem;
|
|
336
|
+
--ember-attacher-shadow: none;
|
|
337
|
+
--ember-attacher-max-width: 400px;
|
|
338
|
+
--ember-attacher-font-size: inherit;
|
|
339
|
+
--ember-attacher-text-align: center;
|
|
340
|
+
}
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
`.ember-attacher-light-theme` reassigns those same tokens (fill `#f3f6f9`, text `#203d5d`, border `#ced2e2`, `border-width: 1px`, and box shadow).
|
|
344
|
+
|
|
345
|
+
Outer wrapper tokens on `.ember-attacher`:
|
|
346
|
+
* `--ember-attacher-distance` (default `10px`): inner animation gap.
|
|
347
|
+
* `--ember-attacher-z-index` (default `9999`): stacking context (used for non-Top-Layer / `@renderInPlace` hosts).
|
|
348
|
+
* `--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)`.
|
|
349
|
+
|
|
350
|
+
Set outer tokens on `.ember-attacher` or an ancestor container. `--ember-attacher-offset` and `--ember-attacher-arrow-rotate` remain internal.
|
|
351
|
+
|
|
352
|
+
## Architecture & Animations
|
|
353
|
+
|
|
354
|
+
Attachments use two nested elements rendered directly in place (1 pass):
|
|
355
|
+
|
|
356
|
+
* **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`.
|
|
357
|
+
* **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 })`).
|
|
358
|
+
|
|
359
|
+
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.
|
|
360
|
+
|
|
361
|
+
## Testing
|
|
362
|
+
|
|
363
|
+
Use the `isVisible()` helper from `'ember-attacher/test-support'`:
|
|
364
|
+
|
|
365
|
+
```javascript
|
|
366
|
+
import { click, find, render } from '@ember/test-helpers';
|
|
367
|
+
import { isVisible } from 'ember-attacher/test-support';
|
|
368
|
+
|
|
369
|
+
test('example', async function (assert) {
|
|
370
|
+
await render(hbs`
|
|
371
|
+
<button id="toggle" type="button">
|
|
372
|
+
Click me, captain!
|
|
373
|
+
</button>
|
|
374
|
+
|
|
375
|
+
<AttachPopover
|
|
376
|
+
@id="attachment"
|
|
377
|
+
@target="#toggle"
|
|
378
|
+
@hideOn="click"
|
|
379
|
+
@showOn="click"
|
|
380
|
+
>
|
|
381
|
+
Click-toggled popover
|
|
382
|
+
</AttachPopover>
|
|
383
|
+
`);
|
|
384
|
+
|
|
385
|
+
const attachment = find('#attachment');
|
|
386
|
+
|
|
387
|
+
assert.false(isVisible(attachment), 'Initially hidden');
|
|
388
|
+
|
|
389
|
+
await click('#toggle');
|
|
390
|
+
|
|
391
|
+
// You can also pass in a selector
|
|
392
|
+
assert.true(isVisible('#attachment'), 'Now shown');
|
|
393
|
+
});
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
`import { isVisible } from 'ember-attacher'` is also supported for convenience.
|
|
397
|
+
|
|
398
|
+
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.
|
|
399
|
+
|
|
400
|
+
```javascript
|
|
401
|
+
import { triggerEvent } from '@ember/test-helpers';
|
|
402
|
+
import { assertAttached } from 'ember-attacher/test-support';
|
|
403
|
+
|
|
404
|
+
await triggerEvent('#trigger', 'mouseenter');
|
|
405
|
+
assertAttached(assert, '#tip', '#trigger', { placement: 'bottom' });
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
## Development setup
|
|
409
|
+
|
|
410
|
+
See the [Contributing](https://github.com/tylerturdenpants/ember-attacher/blob/master/CONTRIBUTING.md) guide for details.
|
|
411
|
+
|
|
412
|
+
## Credits
|
|
413
|
+
|
|
414
|
+
* [tippy.js](https://github.com/atomiks/tippyjs), the library that inspired ember-attacher.
|
|
415
|
+
* [floating-ui](https://github.com/floating-ui/floating-ui), the library that powers positioning.
|
|
416
|
+
* [ember-tooltips](https://github.com/sir-dunxalot/ember-tooltips), the addon that influenced much of ember-attacher's initial API.
|
|
@@ -356,6 +356,69 @@ function elementContainsEvent(element, event) {
|
|
|
356
356
|
}
|
|
357
357
|
return Boolean(typeof Node !== 'undefined' && event?.target instanceof Node && element.contains(event.target));
|
|
358
358
|
}
|
|
359
|
+
function getEventClientPoint(event) {
|
|
360
|
+
if (!event) {
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
const touch = event.changedTouches?.[0];
|
|
364
|
+
if (touch) {
|
|
365
|
+
return {
|
|
366
|
+
x: touch.clientX,
|
|
367
|
+
y: touch.clientY
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
if ('clientX' in event && 'clientY' in event) {
|
|
371
|
+
const mouseEvent = event;
|
|
372
|
+
const x = mouseEvent.clientX;
|
|
373
|
+
const y = mouseEvent.clientY;
|
|
374
|
+
// Keyboard/synthetic clicks without pointer coordinates default to (0, 0) with detail === 0.
|
|
375
|
+
if (x === 0 && y === 0 && !mouseEvent.detail) {
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
x,
|
|
380
|
+
y
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
function isPointWithinElementBoxes(element, x, y) {
|
|
386
|
+
const rect = element.getBoundingClientRect();
|
|
387
|
+
if (rect.width === 0 && rect.height === 0) {
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
if (isPointInRect(x, y, rect)) {
|
|
391
|
+
return true;
|
|
392
|
+
}
|
|
393
|
+
for (const child of element.children) {
|
|
394
|
+
const childRect = child.getBoundingClientRect();
|
|
395
|
+
if ((childRect.width > 0 || childRect.height > 0) && isPointInRect(x, y, childRect)) {
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Determine whether a click/touch event landed inside a floating host element.
|
|
404
|
+
*
|
|
405
|
+
* Unlike `elementContainsEvent`, direct hits targeting a Top-Layer `[popover]`'s
|
|
406
|
+
* `::backdrop` (`event.target === element` with pointer coordinates outside the
|
|
407
|
+
* popover's rendered boxes) return `false` so clickout dismisses the popover.
|
|
408
|
+
*/
|
|
409
|
+
function floatingElementContainsEvent(element, event) {
|
|
410
|
+
if (!elementContainsEvent(element, event)) {
|
|
411
|
+
return false;
|
|
412
|
+
}
|
|
413
|
+
if (!element || event?.target !== element) {
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
const point = getEventClientPoint(event);
|
|
417
|
+
if (!point) {
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
return isPointWithinElementBoxes(element, point.x, point.y);
|
|
421
|
+
}
|
|
359
422
|
const CORRIDOR_BUFFER = 2;
|
|
360
423
|
function isPointInRect(x, y, rect) {
|
|
361
424
|
return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
|
|
@@ -538,7 +601,7 @@ function descendantAttachmentContainsEvent(parent, event) {
|
|
|
538
601
|
continue;
|
|
539
602
|
}
|
|
540
603
|
if (isDescendantOf(candidate, parent)) {
|
|
541
|
-
if (
|
|
604
|
+
if (floatingElementContainsEvent(candidate._floatingElement, event) || elementContainsEvent(candidate._listenerTarget, event)) {
|
|
542
605
|
return true;
|
|
543
606
|
}
|
|
544
607
|
}
|
|
@@ -547,6 +610,9 @@ function descendantAttachmentContainsEvent(parent, event) {
|
|
|
547
610
|
const path = typeof event?.composedPath === 'function' ? event.composedPath() : null;
|
|
548
611
|
if (path) {
|
|
549
612
|
for (const node of path) {
|
|
613
|
+
if (node instanceof Element && node === event?.target && !floatingElementContainsEvent(node, event)) {
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
550
616
|
let currentParent = floatingElementParentMap.get(node);
|
|
551
617
|
const visited = new Set();
|
|
552
618
|
while (currentParent && !visited.has(currentParent)) {
|
|
@@ -2426,7 +2492,7 @@ let AttacherCore = (_class = class AttacherCore {
|
|
|
2426
2492
|
if (elementContainsEvent(target, event) || descendantAttachmentContainsEvent(this, event)) {
|
|
2427
2493
|
return;
|
|
2428
2494
|
}
|
|
2429
|
-
if (!this._opt('interactive') || !
|
|
2495
|
+
if (!this._opt('interactive') || !floatingElementContainsEvent(this._floatingElement, event)) {
|
|
2430
2496
|
this._hideAfterDelay();
|
|
2431
2497
|
}
|
|
2432
2498
|
}
|
|
@@ -2607,4 +2673,4 @@ let AttacherCore = (_class = class AttacherCore {
|
|
|
2607
2673
|
}), _class);
|
|
2608
2674
|
|
|
2609
2675
|
export { AttacherCore as A, cancelScheduled as c, resolveTooltipClass as r, scheduleMicrotask as s };
|
|
2610
|
-
//# sourceMappingURL=attacher-core-
|
|
2676
|
+
//# sourceMappingURL=attacher-core-DIAMEQR6.js.map
|