admin管理员组文章数量:1316974
I would like to have a responsive design for my angular application using angular material.
My page should have 3 columns on desktop that should be grouped into 3 tabs when on mobile.
I was able to do this but had to duplicate the components and @if
them out depending on the available width. This leads to extra maintenance and possible issues, like updating the desktop instance and fetting to do the same for the mobile.
Is there a way to achieve this without too much code duplication?
Here is a sketch if I wasn't clear on the layout
I would like to have a responsive design for my angular application using angular material.
My page should have 3 columns on desktop that should be grouped into 3 tabs when on mobile.
I was able to do this but had to duplicate the components and @if
them out depending on the available width. This leads to extra maintenance and possible issues, like updating the desktop instance and fetting to do the same for the mobile.
Is there a way to achieve this without too much code duplication?
Here is a sketch if I wasn't clear on the layout
Share Improve this question edited Jan 29 at 20:15 Ivo Udelsmann asked Jan 29 at 19:59 Ivo UdelsmannIvo Udelsmann 3862 silver badges19 bronze badges 1- Can you provide us with the source code in order to have a better understanding of your task? – Vasilis G. Commented Jan 29 at 20:06
1 Answer
Reset to default 1Separate the layout concern into its own component:
import { Component, signal } from '@angular/core';
import { MatTabsModule } from '@angular/material/tabs';
@Component({
selector: 'app-responsive-layout',
imports: [MatTabsModule],
template: `
@if (isMobile()) {
<mat-tab-group>
<mat-tab label="Tab 1">
<ng-container *ngTemplateOutlet="section1" />
</mat-tab>
<mat-tab label="Tab 2">
<ng-container *ngTemplateOutlet="section2" />
</mat-tab>
<mat-tab label="Tab 3">
<ng-container *ngTemplateOutlet="section3" />
</mat-tab>
</mat-tab-group>
} @else {
<div class="columns-collection">
<div class="column">
<ng-container *ngTemplateOutlet="section1" />
</div>
<div class="column">
<ng-container *ngTemplateOutlet="section2" />
</div>
<div class="column">
<ng-container *ngTemplateOutlet="section3" />
</div>
</div>
}
<ng-template #section1>
<ng-content select="[data-section-1]" />
</ng-template>
<ng-template #section2>
<ng-content select="[data-section-2]" />
</ng-template>
<ng-template #section3>
<ng-content select="[data-section-3]" />
</ng-template>
`,
styles: `
.column-collection {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.column {
display: flex;
flex-direction: column;
}
`,
})
export class ResponsiveLayoutComponent {
// TODO: update when on a mobile device
protected readonly isMobile = signal(false);
}
Then content project into the layout component:
<app-responsive-layout>
<app-component-1 data-section-1=""></app-component-1>
<app-component-2 data-section-2=""></app-component-2>
<app-component-3 data-section-3=""></app-component-3>
</app-responsive-layout>
I'm using the data-*
attribute for selecting which component goes into which column/tab, but you can use any valid CSS selector (including the selector
of your component).
If you don't already have a way to determine if you're on a mobile device, you can use something like the Layout package from the Angular Material CDK.
本文标签: Angular material responsive layout from columns into tabsStack Overflow
版权声明:本文标题:Angular material responsive layout from columns into tabs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741999738a2410764.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论