Check out the code I've put together on Stackblitz. Here, you'll find my custom component that allows for toggling between two ng-content elements based on click events.
import {Component, ElementRef, EventEmitter, OnDestroy, OnInit, Output} from '@angular/core';
import {fromEvent, Subject} from "rxjs";
import {untilDestroyed} from "ngx-take-until-destroy";
import {filter, switchMapTo, take} from "rxjs/operators";
@Component({
selector: 'app-editable-inplace',
templateUrl: './editable-inplace.component.html',
styleUrls: ['./editable-inplace.component.css']
})
export class EditableInplaceComponent implements OnInit, OnDestroy {
@Output() update = new EventEmitter();
mode: 'view' | 'edit' = 'view';
editMode = new Subject();
editMode$ = this.editMode.asObservable();
// Remaining code goes here...
Template:
<div *ngIf="mode==='view'" >
<ng-content select="[view]"></ng-content>
</div>
<div *ngIf="mode==='edit'" >
<ng-content select="[edit]"></ng-content>
</div>
Usage:
<app-editable-inplace >
<div view>
<h3>Click to edit [not working :-( ]</h3>
</div>
<div edit>
<input placeholder="Click to edit" >
</div>
</app-editable-inplace>
However, I'm facing some issues such as the 'clickOutside$' triggering instantly when clicking on the view element. Additionally, the line
this.element.contains(target) === false
doesn't seem to work as expected. Even though the console shows that the host contains the clicked item, it always registers as a click outside (a bit confusing).