Skip to content

Commit

Permalink
Add pixel-art directive
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcere committed Apr 16, 2022
1 parent 1ea3331 commit 705846d
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 2 deletions.
15 changes: 14 additions & 1 deletion demo/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,20 @@ <h1 class="demo-title">

<section>
<nes-container title="Pixel-arts">
Soon to come...
<div class="flex-row flex-wrap flex-align-end">

<i nes-pixel-art pixel-art="nes-logo"></i>
<i nes-pixel-art pixel-art="nes-jp-logo"></i>
<i nes-pixel-art pixel-art="snes-logo"></i>
<i nes-pixel-art pixel-art="snes-jp-logo"></i>

<i nes-pixel-art pixel-art="nes-octocat"></i>
<i nes-pixel-art pixel-art="nes-octocat-animate"></i>

<i nes-pixel-art pixel-art="nes-smartphone"></i>
<i nes-pixel-art pixel-art="nes-phone"></i>

</div>
</nes-container>
</section>

Expand Down
4 changes: 4 additions & 0 deletions demo/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ code {
align-items: center;
}

.flex-align-end {
align-items: flex-end;
}

.flex-justify-center {
justify-content: center;
}
5 changes: 4 additions & 1 deletion lib/src/icon/icon.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { ElementRef, Renderer2 } from '@angular/core';
import { NesIconDirective } from './icon.directive';

describe('IconDirective', () => {
it('should create an instance', () => {
const directive = new NesIconDirective();
const mockElementRef = {} as ElementRef<HTMLElement>;
const mockRenderer2 = {} as Renderer2;
const directive = new NesIconDirective(mockElementRef, mockRenderer2);
expect(directive).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './dialog';
export * from './icon';
export * from './input';
export * from './list';
export * from './pixel-art';
export * from './pointer';
export * from './progress';
export * from './radio';
Expand Down
2 changes: 2 additions & 0 deletions lib/src/nes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { NesDialogModule } from './dialog';
import { NesIconModule } from './icon';
import { NesInputModule } from './input';
import { NesListModule } from './list';
import { NesPixelArtModule } from './pixel-art';
import { NesPointerModule } from './pointer';
import { NesProgressModule } from './progress';
import { NesRadioModule } from './radio';
Expand All @@ -28,6 +29,7 @@ const SHARED_IMPORTS = [
NesIconModule,
NesInputModule,
NesListModule,
NesPixelArtModule,
NesPointerModule,
NesProgressModule,
NesRadioModule,
Expand Down
2 changes: 2 additions & 0 deletions lib/src/pixel-art/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './pixel-art.directive';
export * from './pixel-art.module';
11 changes: 11 additions & 0 deletions lib/src/pixel-art/pixel-art.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ElementRef, Renderer2 } from '@angular/core';
import { NesPixelArtDirective } from './pixel-art.directive';

describe('PixelArtDirective', () => {
it('should create an instance', () => {
const mockElementRef = {} as ElementRef<HTMLElement>;
const mockRenderer2 = {} as Renderer2;
const directive = new NesPixelArtDirective(mockElementRef, mockRenderer2);
expect(directive).toBeTruthy();
});
});
48 changes: 48 additions & 0 deletions lib/src/pixel-art/pixel-art.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Directive, ElementRef, Input, OnChanges, Renderer2, SimpleChanges } from '@angular/core';

export type NesPixelArt
= 'nes-logo'
| 'nes-jp-logo'
| 'snes-logo'
| 'snes-jp-logo'
| 'nes-octocat'
| 'nes-octocat-animate'
| 'nes-smartphone'
| 'nes-phone';

const PIXELART_CLASSLIST_MAP = new Map<NesPixelArt, string[]>([
['nes-octocat-animate', ['nes-octocat', 'animate']],
]);

@Directive({
selector: '[nes-pixel-art]',
})
export class NesPixelArtDirective implements OnChanges {
@Input('pixel-art') pixelArt?: NesPixelArt;

constructor(
private readonly _elementRef: ElementRef<HTMLElement>,
private readonly _renderer: Renderer2,
) { }

ngOnChanges(change: SimpleChanges): void {
this._handlePixelArtChange(change);
}

private _handlePixelArtChange({ pixelArt }: SimpleChanges): void {
if (pixelArt == null || pixelArt.currentValue === pixelArt.previousValue) {
return;
}

const previousClassList = PIXELART_CLASSLIST_MAP.get(pixelArt.previousValue) || [pixelArt.previousValue];
const currentClassList = PIXELART_CLASSLIST_MAP.get(pixelArt.currentValue) || [pixelArt.currentValue];

previousClassList.forEach(
(cssClass) => this._renderer.removeClass(this._elementRef.nativeElement, cssClass),
);

currentClassList.forEach(
(cssClass) => this._renderer.addClass(this._elementRef.nativeElement, cssClass),
);
}
}
20 changes: 20 additions & 0 deletions lib/src/pixel-art/pixel-art.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { NesPixelArtDirective } from './pixel-art.directive';

const SHARED_DECLARATIONS = [
NesPixelArtDirective,
];

@NgModule({
declarations: [
...SHARED_DECLARATIONS,
],
imports: [
CommonModule,
],
exports: [
...SHARED_DECLARATIONS,
],
})
export class NesPixelArtModule { }

0 comments on commit 705846d

Please sign in to comment.