Lightweight Angular tooltip component











up vote
6
down vote

favorite
2












I made a small Popup component to show different error/success...etc. messages.



Template:



<div [@visibilityChanged]="isVisible" *ngIf="shouldShow" [ngClass]="messageType">
<ng-content></ng-content>
</div>


Component:



import {
Component, OnChanges, Input,
Renderer2, AfterViewInit, ElementRef, AfterViewChecked,
HostListener
} from '@angular/core';

import { trigger, state, animate, transition, style } from '@angular/animations';

/**
* This class represents the popup component.
*/
@Component({
moduleId: module.id,
selector: 'sd-popup',
templateUrl: 'popup.component.html',
styleUrls: ['popup.component.css'],
animations: [trigger('visibilityChanged', [
transition(':enter', [
style({ opacity: 0 }),
animate(100, style({ opacity: 1 }))
]),
transition(':leave', [
style({ opacity: 1 }),
animate(100, style({ opacity: 0 }))
])
])
]
})
export class PopUpComponent implements AfterViewInit, AfterViewChecked {
@Input() messageType: string | string;
@Input() errorState: boolean;
@Input() parentEl: Element;

public shouldShow: boolean;

constructor(private popupEl: ElementRef,
private renderer: Renderer2) {
}

ngAfterViewInit(): void {
// Append to body for better calculation
document.body.appendChild(this.popupEl.nativeElement);

//use random delay for load balancing
this.renderer.listen(this.parentEl, 'keydown', () => {
setTimeout(() => {
this.shouldShow = this.errorState;
}, this.getRandomInt(1, 50));
});
}

@HostListener('window:resize')
@HostListener('window:scroll')
ngAfterViewChecked(): void {
// calculate position when view checked,window is resized or scrolling
// consider to use load balancing here as well (random delay might be visible)
this.calculatePosition(this.popupEl.nativeElement, this.parentEl);
}

public calculatePosition(element: any, target: any): void {
const targetOffset = target.getBoundingClientRect();

this.renderer.setStyle(element, 'top', `${(targetOffset.top + document.documentElement.scrollTop) - element.offsetHeight - 5}px`);
this.renderer.setStyle(element, 'left', `${targetOffset.left + (target.offsetWidth / 2 - element.offsetWidth / 2)}px`);
this.renderer.setStyle(element, 'zindex', `${target.zindex + 100}px`);
}

getRandomInt(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

}


SCSS:



:host {
position:absolute;
opacity: 0.9;
}

.errorMessage {
padding: 0.375rem 0.75rem;
font-size: 1rem;
line-height: 1.5;
color: #fff;
background-image: none;
background-clip: padding-box;
border-radius: 0.25rem;
}

.warning {
@extend .errorMessage;
background-color: #CD464D;
border: 1px solid #CD464D;
}

.success {
@extend .errorMessage;
background-color: rgb(15, 230, 147);
border: 1px solid rgb(15, 230, 147);
}

div:after, div:before {
top: 100%;
left: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}

div:after {
border-color: rgba(136, 183, 213, 0);
border-top-color: inherit;
border-width: 5px;
margin-left: -5px;
}
div:before {
border-color: rgba(194, 225, 245, 0);
border-top-color: inherit;
border-width: 6px;
margin-left: -6px;
}


And finally usage:



<input #passInput type="password" name="password" id="password" class="form-control" formControlName="password"/>
<sd-popup [parentEl]="passInput" [errorState]="password.errors?.required && (password.dirty || password.touched)"
messageType="success">Password is required</sd-popup>


Tell me what do you think about it!










share|improve this question
















bumped to the homepage by Community 11 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    6
    down vote

    favorite
    2












    I made a small Popup component to show different error/success...etc. messages.



    Template:



    <div [@visibilityChanged]="isVisible" *ngIf="shouldShow" [ngClass]="messageType">
    <ng-content></ng-content>
    </div>


    Component:



    import {
    Component, OnChanges, Input,
    Renderer2, AfterViewInit, ElementRef, AfterViewChecked,
    HostListener
    } from '@angular/core';

    import { trigger, state, animate, transition, style } from '@angular/animations';

    /**
    * This class represents the popup component.
    */
    @Component({
    moduleId: module.id,
    selector: 'sd-popup',
    templateUrl: 'popup.component.html',
    styleUrls: ['popup.component.css'],
    animations: [trigger('visibilityChanged', [
    transition(':enter', [
    style({ opacity: 0 }),
    animate(100, style({ opacity: 1 }))
    ]),
    transition(':leave', [
    style({ opacity: 1 }),
    animate(100, style({ opacity: 0 }))
    ])
    ])
    ]
    })
    export class PopUpComponent implements AfterViewInit, AfterViewChecked {
    @Input() messageType: string | string;
    @Input() errorState: boolean;
    @Input() parentEl: Element;

    public shouldShow: boolean;

    constructor(private popupEl: ElementRef,
    private renderer: Renderer2) {
    }

    ngAfterViewInit(): void {
    // Append to body for better calculation
    document.body.appendChild(this.popupEl.nativeElement);

    //use random delay for load balancing
    this.renderer.listen(this.parentEl, 'keydown', () => {
    setTimeout(() => {
    this.shouldShow = this.errorState;
    }, this.getRandomInt(1, 50));
    });
    }

    @HostListener('window:resize')
    @HostListener('window:scroll')
    ngAfterViewChecked(): void {
    // calculate position when view checked,window is resized or scrolling
    // consider to use load balancing here as well (random delay might be visible)
    this.calculatePosition(this.popupEl.nativeElement, this.parentEl);
    }

    public calculatePosition(element: any, target: any): void {
    const targetOffset = target.getBoundingClientRect();

    this.renderer.setStyle(element, 'top', `${(targetOffset.top + document.documentElement.scrollTop) - element.offsetHeight - 5}px`);
    this.renderer.setStyle(element, 'left', `${targetOffset.left + (target.offsetWidth / 2 - element.offsetWidth / 2)}px`);
    this.renderer.setStyle(element, 'zindex', `${target.zindex + 100}px`);
    }

    getRandomInt(min: number, max: number) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    }


    SCSS:



    :host {
    position:absolute;
    opacity: 0.9;
    }

    .errorMessage {
    padding: 0.375rem 0.75rem;
    font-size: 1rem;
    line-height: 1.5;
    color: #fff;
    background-image: none;
    background-clip: padding-box;
    border-radius: 0.25rem;
    }

    .warning {
    @extend .errorMessage;
    background-color: #CD464D;
    border: 1px solid #CD464D;
    }

    .success {
    @extend .errorMessage;
    background-color: rgb(15, 230, 147);
    border: 1px solid rgb(15, 230, 147);
    }

    div:after, div:before {
    top: 100%;
    left: 50%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
    }

    div:after {
    border-color: rgba(136, 183, 213, 0);
    border-top-color: inherit;
    border-width: 5px;
    margin-left: -5px;
    }
    div:before {
    border-color: rgba(194, 225, 245, 0);
    border-top-color: inherit;
    border-width: 6px;
    margin-left: -6px;
    }


    And finally usage:



    <input #passInput type="password" name="password" id="password" class="form-control" formControlName="password"/>
    <sd-popup [parentEl]="passInput" [errorState]="password.errors?.required && (password.dirty || password.touched)"
    messageType="success">Password is required</sd-popup>


    Tell me what do you think about it!










    share|improve this question
















    bumped to the homepage by Community 11 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      6
      down vote

      favorite
      2









      up vote
      6
      down vote

      favorite
      2






      2





      I made a small Popup component to show different error/success...etc. messages.



      Template:



      <div [@visibilityChanged]="isVisible" *ngIf="shouldShow" [ngClass]="messageType">
      <ng-content></ng-content>
      </div>


      Component:



      import {
      Component, OnChanges, Input,
      Renderer2, AfterViewInit, ElementRef, AfterViewChecked,
      HostListener
      } from '@angular/core';

      import { trigger, state, animate, transition, style } from '@angular/animations';

      /**
      * This class represents the popup component.
      */
      @Component({
      moduleId: module.id,
      selector: 'sd-popup',
      templateUrl: 'popup.component.html',
      styleUrls: ['popup.component.css'],
      animations: [trigger('visibilityChanged', [
      transition(':enter', [
      style({ opacity: 0 }),
      animate(100, style({ opacity: 1 }))
      ]),
      transition(':leave', [
      style({ opacity: 1 }),
      animate(100, style({ opacity: 0 }))
      ])
      ])
      ]
      })
      export class PopUpComponent implements AfterViewInit, AfterViewChecked {
      @Input() messageType: string | string;
      @Input() errorState: boolean;
      @Input() parentEl: Element;

      public shouldShow: boolean;

      constructor(private popupEl: ElementRef,
      private renderer: Renderer2) {
      }

      ngAfterViewInit(): void {
      // Append to body for better calculation
      document.body.appendChild(this.popupEl.nativeElement);

      //use random delay for load balancing
      this.renderer.listen(this.parentEl, 'keydown', () => {
      setTimeout(() => {
      this.shouldShow = this.errorState;
      }, this.getRandomInt(1, 50));
      });
      }

      @HostListener('window:resize')
      @HostListener('window:scroll')
      ngAfterViewChecked(): void {
      // calculate position when view checked,window is resized or scrolling
      // consider to use load balancing here as well (random delay might be visible)
      this.calculatePosition(this.popupEl.nativeElement, this.parentEl);
      }

      public calculatePosition(element: any, target: any): void {
      const targetOffset = target.getBoundingClientRect();

      this.renderer.setStyle(element, 'top', `${(targetOffset.top + document.documentElement.scrollTop) - element.offsetHeight - 5}px`);
      this.renderer.setStyle(element, 'left', `${targetOffset.left + (target.offsetWidth / 2 - element.offsetWidth / 2)}px`);
      this.renderer.setStyle(element, 'zindex', `${target.zindex + 100}px`);
      }

      getRandomInt(min: number, max: number) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
      }

      }


      SCSS:



      :host {
      position:absolute;
      opacity: 0.9;
      }

      .errorMessage {
      padding: 0.375rem 0.75rem;
      font-size: 1rem;
      line-height: 1.5;
      color: #fff;
      background-image: none;
      background-clip: padding-box;
      border-radius: 0.25rem;
      }

      .warning {
      @extend .errorMessage;
      background-color: #CD464D;
      border: 1px solid #CD464D;
      }

      .success {
      @extend .errorMessage;
      background-color: rgb(15, 230, 147);
      border: 1px solid rgb(15, 230, 147);
      }

      div:after, div:before {
      top: 100%;
      left: 50%;
      border: solid transparent;
      content: " ";
      height: 0;
      width: 0;
      position: absolute;
      pointer-events: none;
      }

      div:after {
      border-color: rgba(136, 183, 213, 0);
      border-top-color: inherit;
      border-width: 5px;
      margin-left: -5px;
      }
      div:before {
      border-color: rgba(194, 225, 245, 0);
      border-top-color: inherit;
      border-width: 6px;
      margin-left: -6px;
      }


      And finally usage:



      <input #passInput type="password" name="password" id="password" class="form-control" formControlName="password"/>
      <sd-popup [parentEl]="passInput" [errorState]="password.errors?.required && (password.dirty || password.touched)"
      messageType="success">Password is required</sd-popup>


      Tell me what do you think about it!










      share|improve this question















      I made a small Popup component to show different error/success...etc. messages.



      Template:



      <div [@visibilityChanged]="isVisible" *ngIf="shouldShow" [ngClass]="messageType">
      <ng-content></ng-content>
      </div>


      Component:



      import {
      Component, OnChanges, Input,
      Renderer2, AfterViewInit, ElementRef, AfterViewChecked,
      HostListener
      } from '@angular/core';

      import { trigger, state, animate, transition, style } from '@angular/animations';

      /**
      * This class represents the popup component.
      */
      @Component({
      moduleId: module.id,
      selector: 'sd-popup',
      templateUrl: 'popup.component.html',
      styleUrls: ['popup.component.css'],
      animations: [trigger('visibilityChanged', [
      transition(':enter', [
      style({ opacity: 0 }),
      animate(100, style({ opacity: 1 }))
      ]),
      transition(':leave', [
      style({ opacity: 1 }),
      animate(100, style({ opacity: 0 }))
      ])
      ])
      ]
      })
      export class PopUpComponent implements AfterViewInit, AfterViewChecked {
      @Input() messageType: string | string;
      @Input() errorState: boolean;
      @Input() parentEl: Element;

      public shouldShow: boolean;

      constructor(private popupEl: ElementRef,
      private renderer: Renderer2) {
      }

      ngAfterViewInit(): void {
      // Append to body for better calculation
      document.body.appendChild(this.popupEl.nativeElement);

      //use random delay for load balancing
      this.renderer.listen(this.parentEl, 'keydown', () => {
      setTimeout(() => {
      this.shouldShow = this.errorState;
      }, this.getRandomInt(1, 50));
      });
      }

      @HostListener('window:resize')
      @HostListener('window:scroll')
      ngAfterViewChecked(): void {
      // calculate position when view checked,window is resized or scrolling
      // consider to use load balancing here as well (random delay might be visible)
      this.calculatePosition(this.popupEl.nativeElement, this.parentEl);
      }

      public calculatePosition(element: any, target: any): void {
      const targetOffset = target.getBoundingClientRect();

      this.renderer.setStyle(element, 'top', `${(targetOffset.top + document.documentElement.scrollTop) - element.offsetHeight - 5}px`);
      this.renderer.setStyle(element, 'left', `${targetOffset.left + (target.offsetWidth / 2 - element.offsetWidth / 2)}px`);
      this.renderer.setStyle(element, 'zindex', `${target.zindex + 100}px`);
      }

      getRandomInt(min: number, max: number) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
      }

      }


      SCSS:



      :host {
      position:absolute;
      opacity: 0.9;
      }

      .errorMessage {
      padding: 0.375rem 0.75rem;
      font-size: 1rem;
      line-height: 1.5;
      color: #fff;
      background-image: none;
      background-clip: padding-box;
      border-radius: 0.25rem;
      }

      .warning {
      @extend .errorMessage;
      background-color: #CD464D;
      border: 1px solid #CD464D;
      }

      .success {
      @extend .errorMessage;
      background-color: rgb(15, 230, 147);
      border: 1px solid rgb(15, 230, 147);
      }

      div:after, div:before {
      top: 100%;
      left: 50%;
      border: solid transparent;
      content: " ";
      height: 0;
      width: 0;
      position: absolute;
      pointer-events: none;
      }

      div:after {
      border-color: rgba(136, 183, 213, 0);
      border-top-color: inherit;
      border-width: 5px;
      margin-left: -5px;
      }
      div:before {
      border-color: rgba(194, 225, 245, 0);
      border-top-color: inherit;
      border-width: 6px;
      margin-left: -6px;
      }


      And finally usage:



      <input #passInput type="password" name="password" id="password" class="form-control" formControlName="password"/>
      <sd-popup [parentEl]="passInput" [errorState]="password.errors?.required && (password.dirty || password.touched)"
      messageType="success">Password is required</sd-popup>


      Tell me what do you think about it!







      html5 typescript sass angular-2+






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 3 at 5:10









      200_success

      127k15148412




      127k15148412










      asked Dec 3 '17 at 0:13









      Embrioka

      635




      635





      bumped to the homepage by Community 11 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 11 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          It just looks really good.



          I think it stayed this long unanswered because there's not much feedback to give.



          I reviewed and liked




          • The commenting

          • The naming

          • The sizes of your functions

          • Reading flow


          I am not convinced that choosing a random int actually helps the load balancing much, but don't have a better alternative. I would also probably put the random number generator in a helper class, it does not really belong in a tooltip component.






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f181880%2flightweight-angular-tooltip-component%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            It just looks really good.



            I think it stayed this long unanswered because there's not much feedback to give.



            I reviewed and liked




            • The commenting

            • The naming

            • The sizes of your functions

            • Reading flow


            I am not convinced that choosing a random int actually helps the load balancing much, but don't have a better alternative. I would also probably put the random number generator in a helper class, it does not really belong in a tooltip component.






            share|improve this answer

























              up vote
              0
              down vote













              It just looks really good.



              I think it stayed this long unanswered because there's not much feedback to give.



              I reviewed and liked




              • The commenting

              • The naming

              • The sizes of your functions

              • Reading flow


              I am not convinced that choosing a random int actually helps the load balancing much, but don't have a better alternative. I would also probably put the random number generator in a helper class, it does not really belong in a tooltip component.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                It just looks really good.



                I think it stayed this long unanswered because there's not much feedback to give.



                I reviewed and liked




                • The commenting

                • The naming

                • The sizes of your functions

                • Reading flow


                I am not convinced that choosing a random int actually helps the load balancing much, but don't have a better alternative. I would also probably put the random number generator in a helper class, it does not really belong in a tooltip component.






                share|improve this answer












                It just looks really good.



                I think it stayed this long unanswered because there's not much feedback to give.



                I reviewed and liked




                • The commenting

                • The naming

                • The sizes of your functions

                • Reading flow


                I am not convinced that choosing a random int actually helps the load balancing much, but don't have a better alternative. I would also probably put the random number generator in a helper class, it does not really belong in a tooltip component.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 2 at 13:33









                konijn

                26.9k453235




                26.9k453235






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f181880%2flightweight-angular-tooltip-component%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'