-
Notifications
You must be signed in to change notification settings - Fork 1
Docs: Sheath
- Introduction
- Installation
- Overview
- Core Module
- Key Services
- Models
- Helpers
- Usage Examples
- Testing
The Angular Sheath Library provides advanced utilities for monitoring, transforming, and dynamically manipulating DOM elements, CSS classes, and inline styles. It includes services and helpers for tracking DOM mutations, performing dynamic style overlay modifications, and classifying CSS classes.
Install the library using npm:
npm install @your-org/sheathThe Sheath Library empowers Angular developers to interact with the DOM dynamically. It includes two core services:
-
ClassifyService: Tracks mutations in CSS classes and applies classifications (
Keep,Add, orRemove). - StylizerService: Tracks mutations in inline styles and generates dynamic overlay stylesheets.
The library also provides utility helpers for selector validation and managing complex CSS structures.
The SheathModule is the central Angular module exported by the library. While it does not contain any standalone components, you should import this module to access the library's services.
import { SheathModule } from '@your-org/sheath';
@NgModule({
imports: [SheathModule],
})
export class AppModule {}The ClassifyService provides functionality to track changes in an element's CSS classes. It utilizes a MutationObserver to monitor attributes and class mutations.
- Tracks
classmutations on DOM elements. - Categorizes classes into
Keep,Add, orRemoveclassifications usingClassClassification. - Generates a class overlay map (
ClassMap) for dynamic manipulation.
import { ClassifyService } from '@your-org/sheath';
constructor(private classifyService: ClassifyService) {}
ngOnInit() {
const targetNode = document.querySelector('#target-element');
// Initializes class mutation tracking
this.classifyService.classify({ targetNode });
// Subscribe to mutated class overlays
this.classifyService.mutated$.subscribe(({ classes }) => {
console.log(classes); // Output: Map of classified CSS classes
});
}The StylizerService dynamically tracks and generates stylesheets based on mutations to inline styles. It monitors the style attribute using MutationObserver and creates an overlay stylesheet for runtime manipulation.
- Tracks inline
stylemutations on DOM elements. - Dynamically merges old and new inline style changes.
- Generates optimized CSS selectors and builds a dynamic stylesheet.
import { StylizerService } from '@your-org/sheath';
constructor(private stylizerService: StylizerService) {}
ngOnInit() {
const targetNode = document.querySelector('#target-element');
// Initializes style mutation tracking
this.stylizerService.stylize({ targetNode });
// Subscribe to mutated stylesheets
this.stylizerService.mutated$.subscribe(({ stylesheet }) => {
console.log(stylesheet); // Output: Generated dynamic CSS
});
}An enumeration for CSS class tracking classifications:
- KEEP: Class is retained.
- ADD: Class is newly added.
- REMOVE: Class is removed.
export enum ClassClassification {
KEEP,
ADD,
REMOVE
}A ClassMap is a mapping of selectors to a secondary mapping of CSS classes and their classifications.
export type ClassMap = Map<string, Map<string, ClassClassification>>;The isSelectorValid helper checks the validity of a CSS selector.
isSelectorValid({ selector, document }: { selector: string, document: Document }): booleanimport { isSelectorValid } from '@your-org/sheath';
const valid = isSelectorValid({
selector: '.container div',
document: document,
});
console.log(valid); // true or falseTrack class mutations and dynamically classify CSS classes.
constructor(private classifyService: ClassifyService) {}
ngOnInit() {
const targetNode = document.querySelector('#element-to-observe');
this.classifyService.classify({ targetNode });
this.classifyService.mutated$.subscribe(({ classes }) => {
console.log(classes); // Example output: Map { '.container': Map { 'example-class' => KEEP } }
});
}Track style mutations and dynamically generate overlay stylesheets.
constructor(private stylizerService: StylizerService) {}
ngOnInit() {
const targetNode = document.querySelector('#element-with-inline-styles');
this.stylizerService.stylize({ targetNode });
this.stylizerService.mutated$.subscribe(({ stylesheet }) => {
console.log(stylesheet); // Example output: Optimized dynamic CSS rules.
});
}Use the isSelectorValid helper to validate a selector.
import { isSelectorValid } from '@your-org/sheath';
const selector = '.invalid-selector::after';
if (isSelectorValid({ selector, document })) {
console.log('Valid CSS Selector');
} else {
console.log('Invalid CSS Selector');
}The Sheath Library includes unit tests that validate its core functionality. Use Jasmine and Karma for testing components and services.
import { TestBed } from '@angular/core/testing';
import { ClassifyService } from '@your-org/sheath';
describe('ClassifyService', () => {
let service: ClassifyService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ClassifyService);
});
it('should classify elements', () => {
const dummyNode = document.createElement('div');
service.classify({ targetNode: dummyNode });
expect(service).toBeTruthy();
});
});import { TestBed } from '@angular/core/testing';
import { StylizerService } from '@your-org/sheath';
describe('StylizerService', () => {
let service: StylizerService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(StylizerService);
});
it('should stylize elements', () => {
const dummyNode = document.createElement('div');
service.stylize({ targetNode: dummyNode });
expect(service).toBeTruthy();
});
});Execute tests using:
npm run testThe Angular Sheath Library is a powerful tool for DOM manipulation and real-time modifications to CSS classes and inline styles within Angular applications. It can be integrated seamlessly for advanced CSS tracking and overlay creation.
For feature requests or contributions, feel free to contact the development team!