summaryrefslogtreecommitdiffstats
path: root/afm-client/bower_components/tether/docs/intro.md
blob: 3681d297debae63a9a0eaa204e366078d1c35c2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<script src="tether.js"></script>
<script src="js/markAttachment.js"></script>
<script src="docs/js/intro.js"></script>
<link rel="stylesheet" href="docs/css/intro.css"></link>

Tether
======

Tether is a JavaScript library for efficiently making an absolutely positioned
element stay next to another element on the page. For example, you might
want a tooltip or dialog to open, and remain, next to the relevant item
on the page.

Tether includes the ability to constrain the element within the viewport, its
scroll parent, any other element on the page, or a fixed bounding box.  When it
exceeds those constraints it can be pinned to the edge, flip to the other
side of its target, or hide itself.

Tether optimizes its location placement to result in the minimum amount of
'jankyness' as the page is scrolled and resized.  The page can maintain 60fps
scrolling even with dozens or hundreds of tethers on screen (pop open the
devtools timeline as you scroll this page).

Tether is 5kb minified and gzipped, and supports IE9+, and all modern
browsers.

<h2 class="projects-header">Projects Built With Tether</h2>
<p class="projects-paragraph">
<a href="http://github.hubspot.com/select/docs/welcome"><span>Select</span><img src="http://github.hubspot.com/os-icons/select-icon.png" /></a>
<a href="http://github.hubspot.com/drop/docs/welcome"><span>Drop</span><img src="http://github.hubspot.com/os-icons/drop-icon.png" /></a>
<a href="http://github.hubspot.com/tooltip/docs/welcome"><span>Tooltip</span><img src="http://github.hubspot.com/os-icons/tooltip-icon.png" /></a>
<a href="http://github.hubspot.com/shepherd/docs/welcome"><span>Shepherd</span><img src="http://github.hubspot.com/os-icons/shepherd-icon.png" /></a>
</p>

Usage
-----

The element to be moved is called the 'element'.
The element in the page it's to be attached to is called the 'target'.

To use Tether, you define a point on the target and a point on the element.
Tether moves the element to keep those two points on top of each other.

That point is called the attachment (we've marked it in the examples with
a red <span class="attachment-mark"></span>).  For example, if you'd like
the element to sit on the left of the target:

<pre class="pre-with-output"><code class="lang-javascript" data-example='usage'>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top right',
  targetAttachment: 'top left'
});
</code></pre><output data-example='usage'></output>

Attachment
----------

You can move the attachment points of both the element and the target.

For example, lets move the element's attachment:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: <mark>'bottom left'</mark>,
  targetAttachment: 'top left'
});
</code></pre><output></output>

We can also change the target's attachment point:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'bottom left',
  targetAttachment: <mark>'bottom right'</mark>
});
</code></pre><output></output>

There are two more attachment points we haven't seen yet, center and middle:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: <mark>'middle center'</mark>,
  targetAttachment: <mark>'middle center'</mark>
});
</code></pre><output></output>

All told, Tether provides six built in attachment positions:

- left
- center
- right
- top
- middle
- bottom

The syntax of the attachment properties is: `"vertical-attachment horizontal-attachment"`

You must always supply an `attachment`.  If you don't supply a `target-attachment`, it is
assumed to be the mirror image of `attachment`.

### Offset

The six attachment points we provide are not always enough to place the element
exactly where you want it.  To correct this, we provide two more properties,
`offset` and `targetOffset`.

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top right',
  targetAttachment: 'top left',
  <mark>offset: '0 10px'</mark>
});
</code></pre><output></output>

As you can see, we've moved the attachment point of the element 10px to the right.
We can also move the attachment point of the target:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top right',
  targetAttachment: 'top left',
  offset: '0 10px',
  <mark>targetOffset: '20px 0'</mark>
});
</code></pre><output></output>

The offset properties also accept percentages. Percentages in `offset` refer to
the height and width of the element, `targetOffset` the height and width of
the target.

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top right',
  targetAttachment: 'top left',
  targetOffset: <mark>'0 75%'</mark>
});
</code></pre><output></output>

The syntax of the offset properties is `"vertical-offset horizontal-offset"`

Tether offers a couple of special attachments, using the `targetModifier`
option:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: scrollBox,
  attachment: 'middle right',
  targetAttachment: 'middle left',
  targetModifier: 'scroll-handle'
});
</code></pre><output></output>

Set the target to `document.body` to have the element follow the page's scroll bar.

The `targetModifier` `visible` can be used to attach an element to the visible part
of an element:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: document.body,
  attachment: 'middle center',
  targetAttachment: 'middle center',
  <mark>targetModifier: 'visible'</mark>
});
</code></pre><output deactivated></output>

<pre class="pre-with-output"><code class="lang-javascript" data-example="scroll-visible">new Tether({
  element: yellowBox,
  <mark>target: scrollBox</mark>,
  attachment: 'middle center',
  targetAttachment: 'middle center',
  targetModifier: 'visible'
});
</code></pre><output class="no-green scroll-page" data-example="scroll-visible"></output>

Constraints
-----------

If you have tried any of the previous examples, you'll notice that it's pretty
easy to scroll the regions in such a way that the element is hanging out on
its own, with no target in sight.

Constraints allow you to control what happens when the tethered element would
have to fall outside of a defined region to maintain the attachment.

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'middle left',
  targetAttachment: 'middle left',
  <mark>constraints</mark>: [
    {
      to: 'scrollParent',
      pin: true
    }
  ]
});
</code></pre><output></output>

We've created a constraint which will keep the element within its scroll
parent by 'pinning' it to the edges if it tries to escape.  For the sake
of the example, we're also highlighting the pinned edge in red.

Specify an array of sides if you'd only like to pin those edges:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'middle left',
  targetAttachment: 'middle left',
  constraints: [
    {
      to: 'scrollParent',
      pin: <mark>['top']</mark>
    }
  ]
});
</code></pre><output></output>

You might want to allow the element to change its attachment, if doing so
would keep more of it within its assigned region:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  targetAttachment: 'bottom left',
  constraints: [
    {
      to: 'scrollParent',
      <mark>attachment: 'together'</mark>
    }
  ]
});
</code></pre><output></output>

If you scroll the example a bit, you'll see it flip the attachment when necessary.
You can combine `pin` and `attachment` as well:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  targetAttachment: 'bottom left',
  constraints: [
    {
      to: 'scrollParent',
      attachment: 'together',
      <mark>pin: true</mark>
    }
  ]
});
</code></pre><output></output>

Attachment will accept any of these values:

- `element`: Only change the element's attachment
- `target`: Only change the target's attachment
- `both`: Change either's attachment (or both), as needed
- `together`: Change both the element's and target's attachment at the same time (to
'flip' the element to the other side of the attachment)
- `none`: Don't allow changes to attachment (the default)

Together is the option you will use most commonly:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top right',
  targetAttachment: 'bottom left',
  constraints: [
    {
      to: 'scrollParent',
      attachment: <mark>'together'</mark>
    }
  ]
});
</code></pre><output></output>

You can also provide different settings for the horizontal
and vertical attachments:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  targetAttachment: 'bottom left',
  constraints: [
    {
      to: 'scrollParent',
      attachment: <mark>'together none'</mark>
    }
  ]
});
</code></pre><output></output>

Whenever the element is out of the constrained area, we add the `tether-out-of-bounds`
class to it.  If you add some CSS to make items with that class `display: none`, the
tether will hide.

<pre class="pre-with-output"><code class="lang-javascript" data-example="hide">new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'middle center',
  targetAttachment: 'middle center',
  constraints: [
    {
      to: 'scrollParent'
    }
  ]
});
</code></pre><output data-example="hide"></output>

You can also constrain the element to the viewport, you'll have to scroll the
page to see this one.

<pre class="pre-with-output"><code class="lang-javascript" data-example="window">new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  targetAttachment: 'bottom left',
  constraints: [
    {
      to: <mark>'window'</mark>,
      attachment: 'together'
    }
  ]
});
</code></pre><output data-example="window" class="scroll-page"></output>

You can, of course, use pin with the window as well to
make it always visible no matter where the user scrolls:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  targetAttachment: 'bottom left',
  constraints: [
    {
      to: 'window',
      attachment: 'together',
      <mark>pin: true</mark>
    }
  ]
});
</code></pre><output deactivated class="scroll-page visible-enabled"></output>

`to` can be any of:

- `'scrollParent'`
- `'window'`
- any DOM element
- an array of bound points relative to the body `[X1, Y1, X2, Y2]`

You can also provide multiple constraints, keeping in mind that they are
processed in the order supplied (the last one always has the final word).

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  targetAttachment: 'bottom left',
  constraints: [
    {
      to: <mark>'scrollParent'</mark>,
      pin: true
    },
    {
      to: <mark>'window'</mark>,
      attachment: 'together'
    }
  ]
});
</code></pre><output></output>

Optimization
------------

### Element Moving

The goal of Tether's optimizer is to not have to change the positioning
CSS as the page is scrolled or resized.  To accomplish this it looks at the
last few positions, finds commonalities, and uses them to decide whether to
position the element absolutely or with fixed positioning.

If the element is fully contained within its scroll parent, its DOM node
can also be moved inside the scroll parent, to avoid repaints as the
container is scrolled.

<pre class="pre-with-output"><code class="lang-javascript" data-example="optimizer">new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  targetAttachment: 'bottom left'
});
</code></pre><output data-example="optimizer"></output>

We are moving where the DOM node is, so if you have CSS which styles elements
within the offset parent, you may see some rendering changes.  Also note
that this optimization works best if the scroll parent is the offset parent.
In other words, **the scroll parent should be made position relative, fixed or
absolute to enable this optimization.**

If you do see stylistic changes occur when the element is moved,
you might want to disable this optimization.  You can do that by
setting `optimizations.moveElement` to false.

<pre class="pre-with-output"><code class="lang-javascript" data-example="optimizer2">new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  targetAttachment: 'bottom left',
  optimizations: {
    <mark>moveElement: false</mark>
  }
});
</code></pre><output data-example="optimizer2"></output>

### GPU

By default tether positions elements using CSS transforms.  These transforms allow the
tethered element to be moved as its own layer to not force a repaint of the underlying
page.

This method of positioning can cause some issues however, including color shifts and artifacts.

If you experience these issues, you can disable this optimization by setting `optimizations.gpu`
to false:

<pre class="pre-with-output"><code class="lang-javascript" data-example>new Tether({
  element: yellowBox,
  target: greenBox,
  attachment: 'top left',
  optimizations: {
    <mark>gpu: false</mark>
  }
});
</code></pre><output></output>

Methods
-------

The `Tether` constructor we've been using in these examples returns us a
`Tether` object.

The `Tether` object has these methods:

- `setOptions({ options })` - Update any of the options (such as attachment)
- `disable()` - Disable the tethering
- `enable()` - Enable the tethering
- `destroy()` - Disable and remove all references
- `position()` - Manually trigger a repositioning

Options
-------

The full list of options which can be passed to the `Tether` constructor and
`setOptions`:

- `element`: The DOM element, jQuery element, or a selector string of an element which will be moved
- `target`: The DOM element, jQuery element, or a selector string of an element which the `element` will be attached to
- `attachment`: A string of the form `'vert-attachment horiz-attachment'`
  - `vert-attachment` can be any of `'top'`, `'middle'`, `'bottom'`
  - `horiz-attachment` can be any of `'left'`, `'center'`, `'right'`
- `targetAttachment`: A string similar to `attachment`.
  The one difference is that, if it's not provided, targetAttachment will assume the mirror
  image of `attachment`.
- `offset`: A string of the form `'vert-offset horiz-offset'`
  - `vert-offset` and `horiz-offset` can be of the form `"20px"` or `"55%"`
- `targetOffset`: A string similar to `offset`, but refering to the offset of the target
- `targetModifier`: Can be set to `'visible'` or `'scroll-handle'`
- `enabled`: Should the tether be enabled initially? Defaults to `true`.
- `classes`: A hash of classes which should be changed or disabled
- `classPrefix`: The prefix placed at the beginning of the default classes, defaults to `'tether'`
- `optimizations`: A hash of optimizations, used to disable them
- `constraints`: An array of constraint definition objects.  Each definition is of the form:
  - `to`: A DOM element, bounding box, the string `'window'`, or the string `'scrollParent'`
  - `pin`: `true` or an array of strings representing the sides of the constraint
  - `attachment`: A string of the form `"vert-modifier horiz-modifier"`, or a single value
  representing both
    - Each modifier should be one of `"none"`, `"together"`, `"element"`, `"target"`, or `"both"`.
  - `outOfBoundsClass`: An alternative to `"tether-out-of-bounds"`, useful if the class
  needs to be differentiated from that of another constraint.
  - `pinnedClass`: An alternative to `"tether-pinned"`, similar to `outOfBoundsClass`.

Classes
-------

Tether adds a variety of classes to the element and target to allow you to style
them based on their tethering.

You can change the prefix of the classes with the `classPrefix` option.  It is `'tether'` by
default, but you could, for example, change it to be `'bill'` if you were building the bill
library and all the classes would be `'bill-*'`.

```javascript
new Tether({
  classPrefix: 'bill'
});
```

The sass/css is similarily configurable, see
[tooltip](https://github.com/HubSpot/tooltip/blob/master/sass/tooltip-theme-arrows.sass#L14) for
an example of how to make your own prefixed css file.

All classes can be changed or disabled with the `classes` option.  For example, to change the
`tether-element` class to be `my-box`:

```javascript
new Tether({
  classes: {
    element: 'my-box'
  }
});
```

You can also disable classes you're not going to use:

```javascript
new Tether({
  classes: {
    out-of-bounds: false
  }
});
```

- `tether-element` is added to the element
- `tether-target` is added to the target
- `tether-enabled` is added to both elements when tether is not disabled
- `tether-element-attached-[left,right,top,bottom,middle,center]` is added to both
elements based on the elements attachment, if the element becomes detached (for
example, if it's pinned), that class is removed.  The class reflects how the
element is actually attached, so if a constraint changes the attachment, that
change will be reflected in the class.
- `tether-target-attached-[left,right,top,bottom,middle,center]` is added to both
elements based on the target's attachment.  All of the characteristics are the
same as for element-attached.

### Constraint-related Classes

- `tether-out-of-bounds`, `tether-out-of-bounds-[side]` are added to both the element and the target
when the element is placed outside of its constraint.
- `tether-pinned`, `tether-pinned-[side]` are added to both the element and target when a constraint
has pinned the element to the [side] of the container.

Browser Support
---------------

Tether supports IE9+, and all modern browsers.

Google doesn't support IE8, Microsoft is dropping support in a few months, and not supporting it saves
us a whole lot of trouble.  If you are interested in adding support, get in touch, we're happy to accept
a PR.

Contributing
------------

Please contribute! Tether is developed in Coffeescript, but if that's problematic for you, feel free
to submit pull requests which just change the JavaScript files, we can adapt them as needed.

To build Tether, you need:

- Node.js
- Ruby (for compass)

#### Instructions

- Install compass (if you don't have it already)

```bash
gem update --system
gem install compass
```

- Install the build tool

```bash
npm install -g gulp
```

- Install the project

```bash
# In the project directory
npm install
```

- Build / Watch

```bash
gulp
```