Hone logo
Hone
Problems

Angular Attribute Directive: Dynamic Styling

This challenge requires you to create an Angular attribute directive that dynamically applies styling to an HTML element based on a condition. This is a common pattern for enhancing user interfaces by conditionally highlighting or styling elements, improving readability and user experience.

Problem Description

You need to implement an Angular attribute directive named HighlightIf. This directive will take a boolean value as input. If the input value is true, the directive should apply a specific background color (e.g., yellow) to the host element. If the input value is false, the directive should remove this background color.

Key Requirements:

  1. Directive Creation: Create a new Angular attribute directive.
  2. Input Property: The directive must accept an input property that determines whether to apply the styling. Let's name this input highlightIf.
  3. Conditional Styling:
    • When highlightIf is true, set the backgroundColor style of the host element to 'yellow'.
    • When highlightIf is false, remove the backgroundColor style from the host element.
  4. Dynamic Updates: The styling should update automatically if the input highlightIf property changes after the component has been initialized.

Expected Behavior:

When the HighlightIf directive is applied to an element and its input property is true, the element's background should turn yellow. When the input property becomes false, the yellow background should disappear.

Edge Cases:

  • What happens if the directive is applied to an element that already has a backgroundColor style defined through CSS? The directive should override or manage this, preferably by directly manipulating the style attribute.
  • Consider the initial state: if the input is false on initialization, no style should be applied.

Examples

Example 1:

<p [highlightIf]="true">This paragraph should be highlighted.</p>

Output: The <p> element will have a yellow background.

Explanation: The highlightIf input is true, so the directive applies the yellow background.

Example 2:

<div [highlightIf]="false">This div should not be highlighted.</div>

Output: The <div> element will have its default background.

Explanation: The highlightIf input is false, so the directive does not apply any special background styling.

Example 3:

Consider a component that toggles a boolean property:

Component (TS):

import { Component } from '@angular/core';

@Component({
  selector: 'app-my-component',
  template: `
    <button (click)="toggleHighlight()">Toggle Highlight</button>
    <div [highlightIf]="isHighlighted">This text will be highlighted when the button is clicked.</div>
  `
})
export class MyComponent {
  isHighlighted = false;

  toggleHighlight() {
    this.isHighlighted = !this.isHighlighted;
  }
}

Output: Initially, the <div> has no yellow background. When the "Toggle Highlight" button is clicked, isHighlighted becomes true, and the <div> gets a yellow background. Clicking the button again will turn the background back to normal.

Explanation: The directive's input property highlightIf is bound to the component's isHighlighted property. As isHighlighted changes, the directive reacts and updates the styling accordingly.

Constraints

  • The directive must be implemented in TypeScript using Angular.
  • You should use ElementRef and Renderer2 for DOM manipulation to ensure best practices for Angular directives.
  • The directive should be designed to be reusable across different components.
  • No external libraries beyond Angular itself should be used.

Notes

  • Remember to import Directive, Input, ElementRef, and Renderer2 from @angular/core.
  • The @Directive decorator is crucial for defining the directive and its selector.
  • The selector property in the @Directive decorator defines how you will use the directive in your templates (e.g., [highlightIf]).
  • Consider using the ngOnChanges lifecycle hook if you need to perform actions specifically when the input properties change. However, for simple style updates, using the setter of the input property might be more direct.
  • Think about how Renderer2 can safely modify the element's style without directly accessing nativeElement for every operation.
Loading editor...
typescript