Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide mat-tab-header with nested tab groups, hide for outer mat-tab-group without hiding for inner

I have html essentially

<mat-tab-group>
    <mat-tab>
        <mat-tab-group>
            <mat-tab>
            </mat-tab>
        </mat-tab-group>
    </mat-tab>
</mat-tab-group>

How can I use css (or any method really) to hide the outer tabs groups mat-tab-header element, but not affect the inner tab header?

like image 807
Josh Avatar asked Aug 17 '18 21:08

Josh


People also ask

How do you hide a mat tab?

You have to hide a specific div that gets an id depending on the quantity of tabs. That is why your hide-directive on the <mat-tab> directive does not work. You have to write a directive that targets these elements using the classes as selector.

How do you conditionally prevent users from navigating to other tab in Mat-tab-group?

The solution 🕶 First, we need to add a click listener on the mat-tab-group and disable all tabs because if tabs are not disabled then the user will be able to navigate on them and we want to navigate on tabs conditionally not manually.

How do you change the height of a mat tab?

By default, the tab group will not change its height to the height of the currently active tab. To change this, set the dynamicHeight input to true. The tab body will animate its height according to the height of the active tab.

What is Mat tab?

mat-tab-group is used mainly for navbar requirements when we have multiple tabs to display. Approach: First, install the angular material using the above-mentioned command. After completing the installation, Import 'MatTabsModule' from '@angular/material/tabs' in the app.


1 Answers

I highly recommend you to use this custom directive instead of using css.

import { Directive, ElementRef, OnInit } from "@angular/core";

@Directive({
  selector: "[header-less-tabs]",
})
export class HeaderLessTabsDirective implements OnInit {
  constructor(private eleRef: ElementRef) {}

  ngOnInit(): void {
    this.eleRef.nativeElement.children[0].style.display = "none";
  }
}

It's very simple and reusable. usage example:

<mat-tab-group header-less-tabs>
    <mat-tab>
        <mat-tab-group>
            <mat-tab>
            </mat-tab>
        </mat-tab-group>
    </mat-tab>
</mat-tab-group>
like image 77
Shadowalker Avatar answered Oct 29 '22 05:10

Shadowalker