Coder Social home page Coder Social logo

angular-dynamic-height's Introduction

AngularDynamicHeight

In this tutorial, i have explained how you an dynamically set height of a div of component depending on other div of component. Here, i have two sections : 1. Top 2. Bottom After loading, Bottom section is hidden with only Arrow icon. This arrow icon is used to show and hide the content of bottom section.

Dynamic Height

resize-comonent-facotry

this factory is being used to define export function "resizeComponent". This "resizeComponent" function can be exported using export keyword. The component where you want to use this function, you have to import it.

export function resizeComponent(topSectionclassName, bottomSectionclassName) {
    const height = window.innerHeight;
    let bottomSectionHeight = 0;
    if (topSectionclassName && bottomSectionclassName) {
        bottomSectionHeight = document.querySelector<HTMLElement>('.' + bottomSectionclassName).offsetHeight;
        document.querySelector<HTMLElement>('.' + topSectionclassName).style.height = (height - bottomSectionHeight) + 'px';
    }
}

app.component

APPComponent is the parent component of both top and bottom components. So, i have included "resizeComponent" function in app component. I am passing the top and bottom panel class names using "resizeComponent" function parameters to calculate the top and bottom component heights when the content of bottom component is showed or hidden.

ngOnInit(): void {
    resizeComponent('top-panel', 'bottom-panel');
  }

  eventNotifyPanelOpen(event) {
    resizeComponent('top-panel', 'bottom-panel');
  }

If you resize your window then again "resizeComponent" function should be called.

@HostListener('window:resize', ['$event'])
  onResize(event) {
    resizeComponent('top-panel', 'bottom-panel');
  }

bottom-panel.component

After component initialization, bottom panel should be hidden. I have used ElementRef with querySelector to get the DOM element and add / remove class. For better coding, you should keep checking for elementRef and other elements getting undefined or not. Sometimes UI may take time to rendering then you will get console error. This error will break your functionlities.

this.btnArrow = this.elementRef.nativeElement.querySelector('i.fas');
    this.bottomPanel = this.elementRef.nativeElement.querySelector('.bottom-panel');
    this.boxes = this.elementRef.nativeElement.querySelectorAll('.box');
    this.latestPostElem = this.elementRef.nativeElement.querySelector('.latest-posts');
    if (!this.isBottomPanelOpen && this.elementRef && this.btnArrow && this.bottomPanel && this.boxes.length > 0) {
      this.btnArrow.classList.add('fa-caret-up');
      this.setBoxHeight(0, this.boxes);
      this.latestPostElem.style.display = 'none';
    }

angular-dynamic-height's People

Contributors

piyalidas10 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.