Skip to content

Commit

Permalink
refactor(composables): move addClass to class utils
Browse files Browse the repository at this point in the history
  • Loading branch information
homj committed Nov 7, 2023
1 parent d986c8b commit 1f31fa9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 53 deletions.
35 changes: 0 additions & 35 deletions libs/composables/class/src/add-class.spec.ts

This file was deleted.

14 changes: 0 additions & 14 deletions libs/composables/class/src/add-class.ts

This file was deleted.

2 changes: 1 addition & 1 deletion libs/composables/class/src/modifier-group.composable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { computed, inject, Signal, signal } from '@angular/core';

import { addClass } from './add-class';
import { addClass } from './utils/class.utils';
import { bindClasses } from './classes.composable';
import { BASE_CLASS } from './provide-base-class';

Expand Down
2 changes: 1 addition & 1 deletion libs/composables/class/src/modifier.composable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inject, Signal, signal, WritableSignal } from '@angular/core';

import { addClass } from './add-class';
import { addClass } from './utils/class.utils';
import { bindClass } from './class.composable';
import { BASE_CLASS } from './provide-base-class';

Expand Down
1 change: 0 additions & 1 deletion libs/composables/class/src/public-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './add-class';
export * from './class.composable';
export * from './classes.composable';
export * from './modifier.composable';
Expand Down
37 changes: 36 additions & 1 deletion libs/composables/class/src/utils/class.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { splitClasses } from './class.utils';
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { addClass, splitClasses } from './class.utils';

describe('class utils', () => {

Expand Down Expand Up @@ -29,4 +31,37 @@ describe('class utils', () => {
});
});
});

describe('addClass', () => {

@Component({
template: ''
})
class TestComponent {
constructor() {
addClass('test');
}
}

let fixture: ComponentFixture<TestComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ TestComponent ]
}).compileComponents();

fixture = TestBed.createComponent(TestComponent);

fixture.detectChanges();
});

it('should be a function', () => {
expect(typeof addClass).toEqual('function');
});

it('should add the class', () => {
expect(fixture.debugElement.classes['test']).toBeTruthy();
});
});

});
16 changes: 16 additions & 0 deletions libs/composables/class/src/utils/class.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ElementRef, inject, Renderer2 } from '@angular/core';

const CLASS_SEPARATOR_REGEX = /\s+/;


/**
* Splits the given classes into an array of classes.
*
Expand All @@ -12,3 +15,16 @@ export const splitClasses = (classes: string | string[] | null | undefined) => {

return classes ? classes.split(CLASS_SEPARATOR_REGEX) : [];
};

/**
* Adds the class to the host element.
* This function uses {@link inject} and has to be called in the injection context of a component or directive.
*
* @param clazz - The class to add.
*/
export const addClass = (clazz: string) => {
const element = inject(ElementRef).nativeElement;
const renderer = inject(Renderer2);

renderer.addClass(element, clazz);
};

0 comments on commit 1f31fa9

Please sign in to comment.