Showing posts with label Material Design. Show all posts
Showing posts with label Material Design. Show all posts

How to Create a Bullet List with Material UI in React

Material UI is a popular library for building user interfaces in React. One of the common UI elements is a bullet list, which allows you to display a list of items with bullets. In this blog, we will explore how to create a bullet list using Material UI with sample code and examples.

Creating a Material UI Bullet List

To create a bullet list using Material UI, we can use the List and ListItem components. Here's an example:

import React from 'react'; import { List, ListItem, ListItemText } from '@material-ui/core'; function MyBulletList() { return ( <List> <ListItem> <ListItemText primary="Item 1" /> </ListItem> <ListItem> <ListItemText primary="Item 2" /> </ListItem> <ListItem> <ListItemText primary="Item 3" /> </ListItem> </List> ); }

In this example, we import the necessary components from Material UI (List, ListItem, and ListItemText) and use them to create a list with three items. The primary prop on ListItemText is used to display the text for each item.

Customizing a Material UI Bullet List

We can customize the appearance of the bullet list by using different Material UI props. Here are some examples:

  • dense: reduces the vertical padding of the list items.
  • disablePadding: removes the padding around the entire list.
  • divider: adds a divider between each list item.
  • secondary: displays secondary text for each list item.

Here's an example of a bullet list with these customizations:

import React from 'react'; import { List, ListItem, ListItemText } from '@material-ui/core'; function MyCustomBulletList() { return ( <List dense disablePadding> <ListItem divider> <ListItemText primary="Item 1" secondary="Secondary text" /> </ListItem> <ListItem divider> <ListItemText primary="Item 2" /> </ListItem> <ListItem divider> <ListItemText primary="Item 3" /> </ListItem> </List> ); }

In this example, we use the dense and disablePadding props to reduce the vertical padding and remove the padding around the entire list, respectively. We also use the divider prop on each ListItem to add a divider between each list item. Finally, we use the secondary prop on the first ListItemText to display secondary text for that item.

Conclusion

In this blog, we explored how to create a bullet list using Material UI in React, and how to customize the appearance of the list using different Material UI props. With these examples and the Material UI documentation, you can create bullet lists that fit your UI design and functionality needs.

Simplifying User Experience: How to Add a Logout Icon using Angular Material Icons

Angular Material Icons is a popular icon set for use in Angular applications. In this blog, we will discuss how to add a logout icon using Angular Material Icons in an Angular application.

To get started, we need to add the Angular Material Icons package to our project. We can do this by running the following command in the terminal:

npm install @angular/material @angular/cdk @angular/animations @angular/material-icons

Once we have installed the package, we can add the logout icon to our application. Here's an example of how to do this:

  1. Import the required modules in the app.module.ts file:

import { MatIconModule } from '@angular/material/icon'
import { MatButtonModule } from '@angular/material/button'
import { MatMenuModule } from '@angular/material/menu';

  1. Add the imported modules to the imports array:

@NgModule({ 
    declarations: [ 
        AppComponent 
    ], 
    imports: [ 
        BrowserModule
        BrowserAnimationsModule
        MatIconModule
        MatButtonModule
        MatMenuModule 
    ], 
    providers: [], 
    bootstrap: [AppComponent
}) 
export class AppModule { }

  1. Use the mat-icon-button element to create a button with an icon:

<button mat-icon-button [matMenuTriggerFor]="menu"> 
    <mat-icon>logout</mat-icon> 
</button>

In this code, the mat-icon-button element creates a button with an icon using Angular Material Icons. The matMenuTriggerFor attribute specifies the dropdown menu to open when the button is clicked.

The mat-icon element inside the button specifies the icon to use. In this case, it is the "logout" icon.

  1. Use the mat-menu element to create a dropdown menu:

<mat-menu #menu="matMenu"> 
    <button mat-menu-item (click)="logout()">Logout</button> 
</mat-menu>

The mat-menu element creates the dropdown menu. The #menu attribute creates a reference to the menu that can be used later in the code.

The mat-menu-item element creates a button inside the dropdown menu with the text "Logout". The (click) event listener calls the logout() function in the component when the button is clicked.

  1. Define the logout() function in the component:
logout() { // Perform logout action here }

6. Full sample code:

app.module.ts:

import { BrowserModule } from '@angular/platform-browser'
import { NgModule } from '@angular/core'
import { AppComponent } from './app.component'
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
import { MatIconModule } from '@angular/material/icon'
import { MatButtonModule } from '@angular/material/button'
@NgModule({ 
    declarations: [ 
        AppComponent 
    ], 
    imports: [ 
        BrowserModule
        BrowserAnimationsModule
        MatIconModule
        MatButtonModule 
    ], 
    providers: [], 
    bootstrap: [AppComponent
}) 
export class AppModule { }

app.component.html:

<button mat-icon-button color="primary" (click)="logout()"> 
    <mat-icon>logout</mat-icon> 
</button>

app.component.ts:

import { Component } from '@angular/core'
@Component({ 
    selector: 'app-root'
    templateUrl: './app.component.html'
    styleUrls: ['./app.component.css'
}) 
export class AppComponent
    logout() { 
        // code to logout user 
    
}

In this example, we import the MatIconModule and MatButtonModule modules from Angular Material in our app.module.ts file. We then use the mat-icon-button and mat-icon components in our app.component.html file to display the logout icon. Finally, we define the logout function in our app.component.ts file, which will contain the code to log the user out of our application.

Note that the actual code to log the user out will depend on the implementation of your authentication system. The logout function in this example is just a placeholder.

That's it! You have now added a logout icon using Angular Material Icons in your Angular application.

References: