Skip to content

Commit eca914e

Browse files
committed
Dynamic to input and renderInPlace switch
1 parent bd0874d commit eca914e

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

Diff for: README.md

+31
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ The `<my-popover>` component will then be rendered in the `#wormhole-target`
5050
element, it will also automatically cleaned up once your component will be
5151
destroyed.
5252

53+
### Attributes
54+
55+
*to: selector*
56+
Which element to append to.
57+
58+
```html
59+
<ng-wormhole to="#wormhole-target">
60+
<my-popover>...</my-popover>
61+
</ng-wormhole>
62+
```
63+
64+
### Inputs
65+
66+
*to: selector*
67+
Which element to append to.
68+
69+
```html
70+
<ng-wormhole [to]="'#wormhole-target'">
71+
<my-popover>...</my-popover>
72+
</ng-wormhole>
73+
```
74+
75+
*renderInPlace: boolean = false*
76+
Should the component render its children in place?
77+
78+
```html
79+
<ng-wormhole to="#wormhole-target" [renderInPlace]="true">
80+
<my-popover>...</my-popover>
81+
</ng-wormhole>
82+
```
83+
5384
# Credits
5485

5586
This component is heavily inspired by

Diff for: angular-wormhole.component.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
11
import {
22
Component,
33
Attribute,
4+
Input,
45
ElementRef,
56
AfterViewInit,
6-
OnDestroy
7+
OnDestroy,
8+
OnChanges,
9+
SimpleChanges
710
} from '@angular/core';
811

912
@Component({
1013
selector: 'ng-wormhole,angular-wormhole',
1114
template: '<ng-content></ng-content>',
12-
styles: [`:host { display: none; }`]
15+
styles: [`
16+
:host { display: none; }
17+
:host.render-in-place { display: block }
18+
`],
19+
host: {
20+
'[class.render-in-place]': 'renderInPlace'
21+
}
1322
})
14-
export class AngularWormholeComponent implements AfterViewInit, OnDestroy {
23+
export class AngularWormholeComponent
24+
implements AfterViewInit, OnDestroy, OnChanges {
25+
@Input()
26+
renderInPlace: boolean = false;
27+
28+
@Input('to')
29+
toInput: string;
30+
1531
private wormholeHeadNode: Node;
1632
private wormholeFootNode: Node;
33+
private initialized: boolean = false;
1734

1835
constructor(
19-
@Attribute('to') private to: string,
36+
@Attribute('to') private toAttr: string,
2037
private element: ElementRef
2138
) {
2239
this.wormholeHeadNode = this.createTextNode('');
2340
this.wormholeFootNode = this.createTextNode('');
2441
}
2542

2643
get destinationElement(): Element {
27-
return document.querySelector(this.to);
44+
if (this.renderInPlace) {
45+
return this.element.nativeElement;
46+
}
47+
48+
return document.querySelector(this.toInput || this.toAttr);
2849
}
2950

3051
ngAfterViewInit(): void {
@@ -34,10 +55,18 @@ export class AngularWormholeComponent implements AfterViewInit, OnDestroy {
3455
);
3556
this.element.nativeElement.appendChild(this.wormholeFootNode);
3657
this.appendToDestination();
58+
this.initialized = true;
3759
}
3860

3961
ngOnDestroy(): void {
4062
this.removeRange(this.wormholeHeadNode, this.wormholeFootNode);
63+
this.initialized = false;
64+
}
65+
66+
ngOnChanges(changes: SimpleChanges): void {
67+
if (this.initialized) {
68+
this.appendToDestination();
69+
}
4170
}
4271

4372
private appendToDestination() {

0 commit comments

Comments
 (0)