Skip to content

Commit 6fa37e7

Browse files
authored
feat(relative position): added support for content sticking with target element when autoUpdate is set to true
- Added drag example in documentation - Refactored `RelativePosition` to reduce bundle size - Added test cases for utils, and relative position - Fixed the issue where the HTML string is given with `{hasHTML:true}` but still renders as string
1 parent 563da25 commit 6fa37e7

32 files changed

+664
-222
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/.sass-cache
3030
/connect.lock
3131
/coverage
32+
/documentation
3233
/libpeerconnection.log
3334
npm-debug.log
3435
yarn-error.log

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ new RelativePosition({
154154
placement: OutsidePlacement, // location of the content
155155
hostWidth: string | number, // content width eg, `auto`, 150, `30%`
156156
hostHeight: string | number, // content height eg, `auto`, 150, `30%`
157-
autoUpdate: boolean // update position when window scoll/resize
157+
autoUpdate: boolean // update position when window scroll/resize/drag
158158
});
159159
```
160160

dist/toppy-app/assets/archived-versions.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/app/app.component.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
>
1212
</select>
1313
</p>
14+
<div class="badges">
15+
<div class="badge">
16+
<a
17+
class="github-button"
18+
href="https://github.com/lokesh-coder/toppy"
19+
data-show-count="true"
20+
aria-label="Star lokesh-coder/toppy on GitHub"
21+
>Star</a
22+
>
23+
</div>
24+
<div class="badge">
25+
<a
26+
href="https://twitter.com/share?ref_src=twsrc%5Etfw"
27+
class="twitter-share-button"
28+
data-text="Cute overlay library for Angular - tooltips, modals, toastr, menu, dropdowns, alerts, popovers, sidebar and more... "
29+
data-url="https://lokesh-coder.github.io/toppy/"
30+
data-via="lokesh-coder"
31+
data-hashtags="angular"
32+
data-show-count="false"
33+
>Tweet</a
34+
>
35+
</div>
36+
</div>
1437
</div>
1538
<app-section heading="" class="d-block text-center mb-5">
1639
<markdown ngPreserveWhitespaces [src]="'./assets/md/introduction.md'"></markdown>
@@ -47,6 +70,9 @@
4770
<app-section heading="Configuration" class="table" icon="settings">
4871
<markdown ngPreserveWhitespaces [src]="'./assets/md/configuration.md'"></markdown>
4972
</app-section>
73+
<app-section heading="Examples" icon="zap">
74+
<markdown ngPreserveWhitespaces [src]="'./assets/md/drag.md'"></markdown> <app-drag-example></app-drag-example>
75+
</app-section>
5076
<app-section heading="API" icon="file-text">
5177
<markdown ngPreserveWhitespaces [src]="'./assets/md/api.md'"></markdown>
5278
</app-section>

docs/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BrowserModule } from '@angular/platform-browser';
55
import { MarkdownModule } from 'ngx-markdown';
66
import { ToppyModule } from 'toppy';
77
import { AppComponent } from './app.component';
8+
import { DragExampleComponent } from './examples/drag-example/drag-example.component';
89
import { FullscreenPositionExampleComponent } from './examples/fullscreen-position-example/fullscreen-position-example.component';
910
import { GlobalPositionExampleComponent } from './examples/global-position-example/global-position-example.component';
1011
import { RelativePositionExampleComponent } from './examples/relative-position-example/relative-position-example.component';
@@ -32,7 +33,8 @@ import { SubSectionComponent } from './utils/sub-section/sub-section.component';
3233
SlidePositionExampleComponent,
3334
FullscreenPositionExampleComponent,
3435
HeroScreenComponent,
35-
ScollSpyDirective
36+
ScollSpyDirective,
37+
DragExampleComponent
3638
],
3739
imports: [BrowserModule, FormsModule, HttpClientModule, MarkdownModule.forRoot({ loader: HttpClient }), ToppyModule],
3840
providers: [],
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="drag-element-holder">
2+
<span draggable="true" #el class="drag-element" (mouseenter)="onMouseOver()" (mouseleave)="onMouseLeave()"
3+
>Drag me</span
4+
>
5+
</div>
6+
7+
<div class="reset-link" (click)="reset()">reset</div>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
2+
import { OutsidePlacement } from '../../../../projects/toppy/src/lib/models';
3+
import { RelativePosition, Toppy, ToppyRef } from '../../../../projects/toppy/src/public_api';
4+
5+
@Component({
6+
selector: 'app-drag-example',
7+
templateUrl: './drag-example.component.html'
8+
})
9+
export class DragExampleComponent implements OnInit {
10+
pos1 = 0;
11+
pos2 = 0;
12+
pos3 = 0;
13+
pos4 = 0;
14+
@ViewChild('el') el: ElementRef;
15+
private _toppyRef: ToppyRef;
16+
constructor(private toppy: Toppy) {}
17+
18+
ngOnInit() {
19+
this._toppyRef = this.toppy
20+
.overlay(
21+
new RelativePosition({
22+
placement: OutsidePlacement.TOP,
23+
src: this.el.nativeElement,
24+
hostWidth: 'auto',
25+
autoUpdate: true
26+
}),
27+
{
28+
backdrop: false,
29+
dismissOnDocumentClick: false
30+
}
31+
)
32+
.host('<div class="tooltip">tooltip</div>', { hasHTML: true })
33+
.create();
34+
}
35+
36+
ngAfterViewInit() {
37+
this.dragElement();
38+
}
39+
reset() {
40+
this.el.nativeElement.style.left = '0px';
41+
this.el.nativeElement.style.top = '0px';
42+
}
43+
onMouseOver() {
44+
this._toppyRef.open();
45+
}
46+
onMouseLeave() {
47+
this._toppyRef.close();
48+
}
49+
dragMouseDown(e) {
50+
e = e || window.event;
51+
e.preventDefault();
52+
// get the mouse cursor position at startup:
53+
this.pos3 = e.clientX;
54+
this.pos4 = e.clientY;
55+
document.onmouseup = this.closeDragElement.bind(this);
56+
// call a function whenever the cursor moves:
57+
document.onmousemove = this.elementDrag.bind(this);
58+
}
59+
60+
elementDrag(e) {
61+
e = e || window.event;
62+
e.preventDefault();
63+
// calculate the new cursor position:
64+
this.pos1 = this.pos3 - e.clientX;
65+
this.pos2 = this.pos4 - e.clientY;
66+
this.pos3 = e.clientX;
67+
this.pos4 = e.clientY;
68+
this.el.nativeElement.style.top = this.el.nativeElement.offsetTop - this.pos2 + 'px';
69+
this.el.nativeElement.style.left = this.el.nativeElement.offsetLeft - this.pos1 + 'px';
70+
}
71+
72+
closeDragElement() {
73+
document.onmouseup = null;
74+
document.onmousemove = null;
75+
}
76+
77+
dragElement() {
78+
console.log('fofo');
79+
this.el.nativeElement.onmousedown = this.dragMouseDown.bind(this);
80+
}
81+
}

docs/assets/md/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ InsidePlacement.TOP_RIGHT;
151151
```
152152

153153
```typescript
154-
InsidePlacement.CENTER**
154+
InsidePlacement.CENTER;
155155
```
156156

157157
</div>

docs/assets/md/drag.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
##### 1. Stick content on dragging
2+
3+
In below example you can see that the tooltip content is sticked with the `src` element in `RelativePosition`

docs/assets/md/relative-position-api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ new RelativePosition({
44
placement: OutsidePlacement, // location of the content
55
hostWidth: string | number, // content width eg, `auto`, 150, `30%`
66
hostHeight: string | number, // content height eg, `auto`, 150, `30%`
7-
autoUpdate: boolean // update position when window scoll/resize
7+
autoUpdate: boolean // update position when window scroll/resize/drag
88
});
99
```

0 commit comments

Comments
 (0)