Skip to content

Commit 91307ca

Browse files
authored
Merge pull request #64 from jon-coffey/master
2 parents 7e7904b + e9f0e89 commit 91307ca

15 files changed

+675
-391
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ Now you can use it in your template:
210210
<button (click)="tooltip.hide()">Close</button>
211211
```
212212

213+
### Show/hide declarativly
214+
215+
Use isVisible to trigger show and hide. Set trigger to manual.
216+
217+
```html
218+
<div tippy="Helpful Message" trigger="manual" [isVisible]="visibility">
219+
Click Open to see me
220+
</div>
221+
222+
<button (click)="visibility = true">Open</button>
223+
<button (click)="visibility = false">Close</button>
224+
```
225+
213226
You can see more examples in
214227
our [playground](https://github.com/ngneat/helipopper/blob/master/src/app/app.component.html), or
215228
live [here](https://ngneat.github.io/helipopper/).
@@ -236,6 +249,7 @@ tippyHost: HTMLElement;
236249
lazy: boolean;
237250
variation: string;
238251
isEnabled: boolean;
252+
isVisible: boolean;
239253
className: string;
240254
onlyTextOverflow: boolean;
241255
useHostWidth: boolean;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Cypress.on('scrolled', element => {
2+
// When we do `cy.get()` the Cypress finds the element
3+
// and scrolls down, thus this element becomes at the very top
4+
// of the page.
5+
// `tippy.js` will not show tooltips if they appeared in an invisible
6+
// part of the window.
7+
element.get(0).scrollIntoView({
8+
block: 'center',
9+
inline: 'center'
10+
});
11+
});
12+
13+
describe('@ngneat/helipopper/is_visible', () => {
14+
const popperSelector = '.tippy-box .tippy-content';
15+
16+
describe('isVisible attribute', () => {
17+
beforeEach(() => {
18+
cy.visit('/').wait(200);
19+
});
20+
21+
it('should not be visible while flag is false', () => {
22+
cy.get('.declarativeTooltip').should('not.exist');
23+
});
24+
it('should be visible while flag is true', () => {
25+
cy.get('[data-cy~="trigger-declarative"]').click({ force: true });
26+
cy.get(popperSelector).contains("I'm a declarative tooltip");
27+
cy.get('[data-cy~="trigger-declarative"]').click({ force: true });
28+
cy.get('.declarativeTooltip').should('not.exist');
29+
});
30+
});
31+
32+
describe('isVisible attribute with initial value true', () => {
33+
beforeEach(() => {
34+
cy.visit('/is-visible').wait(200);
35+
});
36+
37+
it('should be visible while flag is already set true before view renders', () => {
38+
cy.get(popperSelector).contains("I'm a declarative tooltip");
39+
});
40+
});
41+
});

package-lock.json

Lines changed: 148 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"build:playground": "ng build --project helipopper-playground --configuration production",
2424
"test:playground": "ng test --project helipopper-playground",
2525
"serve:playground": "ng s --project helipopper-playground --live-reload false",
26-
"serve:playground:static": "serve dist/helipopper-playground -l 4200",
26+
"serve:playground:static": "angular-http-server --path dist/helipopper-playground -p 4200",
2727
"// - E2E": "E2E testing",
2828
"cy:open": "cypress open --config integrationFolder=cypress/integration",
2929
"cy:run": "cypress run --config integrationFolder=cypress/integration",
@@ -65,6 +65,7 @@
6565
"@types/node": "^15.3.0",
6666
"all-contributors-cli": "^6.8.1",
6767
"angular-cli-ghpages": "^0.6.2",
68+
"angular-http-server": "^1.10.0",
6869
"codelyzer": "^6.0.0",
6970
"cross-env": "^5.2.0",
7071
"cypress": "^7.4.0",

projects/ngneat/helipopper/src/lib/tippy.directive.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
5656
@Input('tippyHost') customHost: HTMLElement;
5757

5858
@Output() visible = new EventEmitter<boolean>();
59-
public isVisible = false;
59+
@Input() public isVisible = false;
6060

6161
private instance: TippyInstance;
6262
private viewRef: ViewRef;
@@ -79,6 +79,8 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
7979
if (isPlatformServer(this.platformId)) return;
8080

8181
let props: Partial<TippyConfig> = Object.keys(changes).reduce((acc, change) => {
82+
if (change === 'isVisible') return acc;
83+
8284
acc[change] = changes[change].currentValue;
8385

8486
return acc;
@@ -106,6 +108,10 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
106108
this.setStatus();
107109
}
108110

111+
if (isChanged<NgChanges<TippyDirective>>('isVisible', changes)) {
112+
this.isVisible ? this.show() : this.hide();
113+
}
114+
109115
this.setProps(props);
110116
}
111117

@@ -214,6 +220,9 @@ export class TippyDirective implements OnChanges, AfterViewInit, OnDestroy, OnIn
214220
}
215221
}
216222
this.globalConfig.onCreate?.(instance);
223+
if (this.isVisible === true) {
224+
instance.show();
225+
}
217226
},
218227
onShow: instance => {
219228
this.zone.run(() => {

src/app/app-routing.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
3+
import { IsVisibleComponent } from './is-visible/isVisible.component';
4+
import { PlaygroundComponent } from './playground/playground.component';
35

4-
const routes: Routes = [];
6+
const routes: Routes = [
7+
{ path: 'is-visible', component: IsVisibleComponent },
8+
{ path: '**', component: PlaygroundComponent }
9+
];
510

611
@NgModule({
712
imports: [RouterModule.forRoot(routes, { relativeLinkResolution: 'legacy' })],

0 commit comments

Comments
 (0)