Utilize selector properties from a different UI library when defining attributes for a component selector

I have a custom Angular library that I want to enhance by encapsulating the ng-zorro button (along with other components) and adding my own attribute selector instead of the ng-zorro selector, as well as incorporating additional properties to the original component.

Looking at the source code of ng-zorro, you'll find the implementation of the nz-button attribute:


@Component({
  selector: 'button[nz-button], a[nz-button]',
  exportAs: 'nzButton',
  preserveWhitespaces: false,
  changeDetection: ChangeDetectionStrategy.OnPush,
  encapsulation: ViewEncapsulation.None,
  template: `
    <span nz-icon nzType="loading" *ngIf="nzLoading"></span>
    <ng-content></ng-content>
  `,
  host: {
    class: 'ant-btn',
    '[class.ant-btn-primary]': `nzType === 'primary'`,
    '[class.ant-btn-dashed]': `nzType === 'dashed'`,
    // ...
  }
})

Unlike a @Directive, this is a @Component that selects elements based on the condition button[nz-button].

Below is the component I developed to wrap the aforementioned component:

@Component({
  selector: 'button, a',
  preserveWhitespaces: false,
  imports: [
    NzButtonModule
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  encapsulation: ViewEncapsulation.None,
  template: `<ng-content></ng-content>`,
  styleUrls: ['./button.component.scss'],
  standalone: true,
  host: {
    'nz-button': '',
    class: 'btn',
    '[class.btn-primary]': `color === 'primary'`,
    '[class.btn-secondary]': `color === 'secondary'`,
    // ...
  }
})

export class ButtonComponent {
  @Input() size: ButtonSize = 'md';
  @Input() shape: ButtonShape = 'default';
  @Input() color: ButtonColor = 'primary';
  @Input() fill: ButtonFill = 'solid';
  @Input() expand: ButtonExpand = 'default';
}

My goal is to have all ng-zorro attributes and my own attributes on the <button> tag within consumer components. This will allow me to introduce new attributes to the native tag and customize the ant styles in specific scenarios.

In simple terms, I aim to utilize both [nzType] (from ng-zorro) and shape (from my ButtonComponent) on a <button> element.

Answer №1

After some tinkering, I decided to extend the ng-zorro component in order to inherit its Inputs onto my custom component. While this approach did partially achieve what I wanted, it meant that I now had to use the ng-zorro inputs by their original names rather than my preferred names. For instance, I found myself using [nzSize] instead of [size].

This workaround manages to get the job done, but it falls short of being a robust and complete solution.

I'm content with my current solution for now, but I remain open to better alternatives in the future.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The State of NgRX Entity is encountering undefined IDs

I decided to experiment with @ngrx/entity in a simple "Todo" project, where I had only one AppModule, one reducer, and a single component. However, as I delved into it, I encountered some challenges. The actions I defined were quite basic, focusing on CRU ...

When trying to import axios from the 'axios.js' file in the 'lib' directory, a SyntaxError was encountered with the message: Unexpected identifier

My server.ts is causing issues. What am I doing wrong? const express = require('express'); const bodyParser = require('body-parser'); const cors = require('cors'); const morgan = require('morgan'); const axios = requ ...

Using NavParams within a service component,

I'm facing a challenge in accessing NavParams within a provider, and simply importing NavParams is not solving the issue. Here's a brief overview of my application: users input their name and address, a pin is dropped on the map based on the add ...

Error SCRIPT1002 was encountered in the vendor.js file while using Angular 8 on Internet Explorer 11

Having trouble getting Angular to function properly in IE 11. I've tried all the solutions I could find online. The errors I'm encountering are as follows: SCRIPT1002: Syntax error File: vendor.js, Line: 110874, Column: 40 At line 110874 args[ ...

The combination of Autodesk Forge Viewer and React with TypeScript provides a powerful platform for developing

I'm brand new to React and Typescript, and I have a very basic question. In the viewer documentation, extensions are defined as classes. Is it possible to transform that class into a typescript function? Does that even make sense? For example, take th ...

Creating React Components with TypeScript: Ensuring Typechecking in Class and Function Components

Want to ensure typechecking works when defining React Class/Function components in TypeScript? Struggling to find a comprehensive guide on how to do it correctly. I've managed to define the Props and State interfaces, but I'm unsure about how to ...

Explore how Angular can display the highlights of deepDiff results in JSON format!

I am utilizing deepDiff to display the variances between two JSON objects in a structured manner, which is working well so far. However, I am aiming to highlight these disparities within the parsed JSON contents, but it's not quite effective. How can ...

Absent observable functions in the RxJS 5.0.0-beta.0 release

I am encountering an issue when trying to use RxJS with Angular 2. The methods recommended from the Typescript definition file are not present on my Observable object... https://i.stack.imgur.com/6qhS4.png https://i.stack.imgur.com/m7OBk.png After inves ...

Investigating SCSS Issues: The Problem with Absolute Imports

I am working on a project with the following structure: - my/project - assets - images - image-name.svg - source - components - MyComponent - MyComponent.module.scss - ... - ... ...

Building a dynamic form in Angular with nested JSON data for enhanced reactivity

Although similar questions have been asked before, I am struggling with a large nested JSON value like the following: "Q-1": { "question": { "text": "What are your hopes and goals for this yea ...

Is there a way to deactivate the Interceptor in a exported function?

Utilizing ngx-translator for localization has been a great help, but I've encountered an issue while attempting to access json-files for localization that are stored in a local folder. The error message I receive is HttpErrorResponse {headers: HttpHea ...

How to format dates with month names in various languages using the date pipe

In my HTML code, I have set up the date display like this: <span >{{ item.lastModified | date : 'MMM d, y' }}</span> As a result, the displayed date looks something like Jul 20, 2021. However, when I switch my browser's language ...

The takeUntil function will cancel an effect request if a relevant action has been dispatched before

When a user chooses an order in my scenario: selectOrder(orderId): void { this.store$.dispatch( selectOrder({orderId}) ); } The order UI component triggers an action to load all associated data: private fetchOrderOnSelectOrder(): void { this.sto ...

What causes the failure of $event binding when using RowGroup tables with PrimeNG?

I have successfully implemented RowGroup to store 3 different tables, which is working great. However, I am facing an issue with the onRowSelect function not functioning properly. When I click on any row of the RowGroup tables, nothing happens. On another ...

Trouble with modifying a cell in a TypeScript office script

Hello everyone. I am in need of a code that can track which cells are active or selected, and then block them once a user is no longer interacting with them. I understand that there may be some issues, especially if the user selects a cell but does not ma ...

How can one interpret the act of "passing" an interface to an RxJS Store using angle brackets?

When working with NgRx and typescript, I often come across this syntax within class constructors: import { Store, select } from '@ngrx/store' class MyClass { constructor(private store: Store<AppState>) { this.count$ = store.pipe(sele ...

What steps should I take to turn off ES Module Error notifications in VSCode?

After switching to the Bun JS Runtime, the distinction between ES Modules and CommonJS became irrelevant as Bun seamlessly handles both. However, VSCode seems to not be on the same page, throwing errors whenever actions that would work in Bun but not in No ...

Encountering an Error When Posting with Angular 2 HTTP

I am encountering an issue with my Ionic 2 app that is trying to retrieve events from Facebook. I am attempting to send a post request to the Graph Api using a batch containing multiple requests, but every time I subscribe, I receive the following error: ...

You cannot use Angular 5 to send a post request with a token that has been retrieved

Hello, I'm facing an issue with making a post request in Angular 5. The token I retrieve seems correct as it works fine when tested with Postman. Can someone provide me with a hint or suggestion on what could be going wrong? AuthService.ts getProfi ...

Searching for NavigationEnd events specifically in Angular 12?

When using Angular 11, the following code snippet functions correctly: this.r.events.pipe( filter(event => event instanceof NavigationEnd), map((event: NavigationEnd) => event.url == ROUTES.APPLICATION)) However, when migrating to Angular 12 ...