Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add <tour-proxy-anchor> component #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

phrei
Copy link

@phrei phrei commented Nov 16, 2023

In discussion #159 I had the following problem:

I'm using an external component (in this case a calendar component) that contains a button group to switch views. I'm not able to set the tourAnchor-Directive on this button group (as the code is not in my domain).

My first solution was to create a div and position it absolutely over this button group and use the tourAnchor-Directive on this new div. If the layout changed somehow and these buttons were not in the same place anymore or changed in dimension, the absolutely positioned div was wrong (especially visible, if backdrop is enabled).

So instead of positioning this "artificial" tourAnchor-DIV I've created a directive which receives a CSS selector. This directive finds the element I want to overlay, gets it's bounding rect and sets the div's position and dimensions accordingly.

In this branch you can see a demo of the directive. As a test I try to highlight the label of the button instead of the whole button (to showcase an element, that is not in my domain but in angular material's).

Additionally theres an Input for padding (which uses the technique from your docs to set padding and remove margin).

@phrei phrei changed the title New Directive "tourAnchorOverlay" Add New Directive "tourAnchorOverlay" Nov 17, 2023
@hakimio
Copy link
Owner

hakimio commented Dec 5, 2023

@phrei I thought a little bit about this and I think I came up with a better/simpler way to implement this feature.

In the core library I would define a simple abstract base class like the following:

import {AfterViewInit, Directive, ElementRef, inject, Input} from '@angular/core';
import {TourAnchorDirective} from './tour-anchor.directive';
import {DOCUMENT} from '@angular/common';

@Directive()
export abstract class BaseProxyAnchor implements AfterViewInit {

    protected abstract readonly anchorDirective: TourAnchorDirective;
    private readonly document = inject(DOCUMENT);

    public abstract anchorEl: string | HTMLElement;

    ngAfterViewInit(): void {
        if (this.anchorEl instanceof HTMLElement) {
            this.anchorDirective.element = new ElementRef<HTMLElement>(this.anchorEl);
            return;
        }
        const htmlElement = this.document.querySelector<HTMLElement>(this.anchorEl);

        if (!htmlElement) {
            throw new Error(`Element with "${this.anchorEl}" CSS selector could not be found!`);
        }

        this.anchorDirective.element = new ElementRef<HTMLElement>(htmlElement);
    }

}

Then I would introduce a tour-proxy-anchor component in each of the tour UIs like this:

import {BaseProxyAnchor} from 'ngx-ui-tour-core';
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import {TourAnchorMatMenuDirective} from './tour-anchor.directive';

@Component({
    selector: 'tour-proxy-anchor',
    template: ``,
    standalone: true,
    changeDetection: ChangeDetectionStrategy.OnPush,
    imports: [
        TourAnchorMatMenuDirective
    ],
    hostDirectives: [{
        directive: TourAnchorMatMenuDirective,
        inputs: ['tourAnchor: anchorId']
    }]
})
export class TourProxyAnchorComponent extends BaseProxyAnchor {

    protected override readonly anchorDirective = inject(TourAnchorMatMenuDirective);

    @Input({required: true})
    public override anchorEl: string | HTMLElement;

}

Finally, you can just use the component:

<tour-proxy-anchor
    anchorId="start.tour"
    anchorEl="#TESTButton span.t-content"
></tour-proxy-anchor>

Some of the advantages of this approach:

  • Doesn't need an extra element with position absolute.
  • Simple code.
  • User of tour-proxy-anchor component doesn't have to remember to import TourAnchorDirective since it's already used as a host directive.

@phrei
Copy link
Author

phrei commented Dec 5, 2023

@hakimio Your solution definitely looks a lot neater! I'm totally fine with closing this pull request.

@hakimio
Copy link
Owner

hakimio commented Dec 5, 2023

@phrei It's still your PR. I only made a suggestion.
If you want to have this feature, you still have to:

  • update all tour UIs
  • update all changelogs
  • update documentation

Whether you update this PR or create a new one is your choice.

@phrei
Copy link
Author

phrei commented Dec 6, 2023

@hakimio Oh, okay. Sure, I'll take care of that as soon as possible.

@hakimio hakimio changed the title Add New Directive "tourAnchorOverlay" Add "tour-proxy-anchor" component Dec 18, 2023
@hakimio hakimio changed the title Add "tour-proxy-anchor" component Add <tour-proxy-anchor> component Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants