Skip to content

Commit

Permalink
feat(demo): add "old" example to the demo
Browse files Browse the repository at this point in the history
  • Loading branch information
homj committed Nov 18, 2023
1 parent cb37eb3 commit e519eba
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 57 deletions.
42 changes: 35 additions & 7 deletions apps/demo/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,74 @@ <h1>{{ title() }}</h1>
<fieldset class="control-group">
<legend>State</legend>
<label>
<input #disabled type="checkbox" (change)="button.disabled.set(disabled.checked)" />
<input #disabled type="checkbox" (change)="button.isDisabled.set(disabled.checked); oldButton.isDisabled = disabled.checked" />
Disabled
</label>
<label>
<input #loading type="checkbox" (change)="button.loading.set(loading.checked)" />
<input #loading type="checkbox" (change)="button.isLoading.set(loading.checked); oldButton.isLoading = loading.checked" />
Loading
</label>
</fieldset>

<fieldset>
<legend>Type</legend>
<label>
<input type="radio" name="type" [checked]="button.type() === 'button'" (change)="button.type.set('button')" />
<input type="radio" name="type" [checked]="button.type() === 'button'" (change)="button.type.set('button'); oldButton.type = 'button'" />
Button
</label>
<label>
<input type="radio" name="type" [checked]="button.type() === 'submit'" (change)="button.type.set('submit')" />
<input type="radio" name="type" [checked]="button.type() === 'submit'" (change)="button.type.set('submit'); oldButton.type = 'submit'" />
Submit
</label>
</fieldset>

<fieldset>
<legend>Appearance</legend>
<label>
<input type="radio" name="appearance" [checked]="!button.appearance()" (change)="button.appearance.set(undefined)" />
<input type="radio" name="appearance" [checked]="!button.appearance()" (change)="button.appearance.set(undefined); oldButton.appearance = undefined" />
Default
</label>
<label>
<input type="radio" name="appearance" [checked]="button.appearance() === 'outline'" (change)="button.appearance.set('outline')" />
<input type="radio" name="appearance" [checked]="button.appearance() === 'outline'" (change)="button.appearance.set('outline'); oldButton.appearance = 'outline'" />
Outline
</label>
<label>
<input type="radio" name="appearance" [checked]="button.appearance() === 'solid'" (change)="button.appearance.set('solid')" />
<input type="radio" name="appearance" [checked]="button.appearance() === 'solid'" (change)="button.appearance.set('solid'); oldButton.appearance = 'solid'" />
Solid
</label>
</fieldset>

<fieldset>
<legend>Color</legend>
<label>
<input type="radio" name="appearance" [checked]="!button.color()" (change)="button.color.set(undefined); oldButton.color = undefined" />
Default
</label>
<label>
<input type="radio" name="appearance" [checked]="button.color() === 'red'" (change)="button.color.set('red'); oldButton.color = 'red'" />
Red
</label>
<label>
<input type="radio" name="appearance" [checked]="button.color() === 'green'" (change)="button.color.set('green'); oldButton.color = 'green'" />
Green
</label>
</fieldset>
</form>
</aside>

<div class="card output">
<header>
<h2>Old</h2>
<p>Using plain Angular APIs (<a href="https://github.com/bynaryDE/angular-extensions/blob/main/apps/demo/src/app/components/old-button/old-button.component.ts" target="_blank">code</a>)</p>
</header>
<demo-old-button #oldButton type="submit" (click)="onClick()">Button</demo-old-button>
</div>

<div class="card output">
<header>
<h2>New</h2>
<p>Using <strong>@bynary/composables</strong> (<a href="https://github.com/bynaryDE/angular-extensions/blob/main/apps/demo/src/app/components/button/button.component.ts" target="_blank">code</a>)</p>
</header>
<demo-button #button type="submit" (click)="onClick()">Button</demo-button>
</div>
</main>
16 changes: 16 additions & 0 deletions apps/demo/src/app/app.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@
min-height: 15rem;
width: 25rem;
max-width: 100%;
position: relative;

header {
display: flex;
flex-direction: column;
inset: 1rem 0.5rem auto 1rem;
position: absolute;

h2 {
font-size: var(--font-size-fluid-1);
}

p {
color: var(--text-2);
}
}
}

.card {
Expand Down
3 changes: 2 additions & 1 deletion apps/demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { bindTitle } from '@bynary/composables/title';

import { ButtonComponent } from './components/button/button.component';
import { ColorSchemeSwitchComponent } from './components/color-scheme-switch/color-scheme-switch.component';
import { OldButtonComponent } from './components/old-button/old-button.component';

@Component({
standalone: true,
imports: [ RouterModule, ButtonComponent, ColorSchemeSwitchComponent ],
imports: [ RouterModule, ButtonComponent, ColorSchemeSwitchComponent, OldButtonComponent ],
selector: 'demo-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.scss' ]
Expand Down
2 changes: 1 addition & 1 deletion apps/demo/src/app/components/button/button.component.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div class="c-button__loading-indicator" *ngIf="loading()"></div>
<div class="c-button__loading-indicator" *ngIf="isLoading()"></div>
<ng-content></ng-content>
10 changes: 9 additions & 1 deletion apps/demo/src/app/components/button/button.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@
border: var(--border-size-1) solid var(--surface-4);
}

&.c-button--loading {
&.c-button--color-red {
color: var(--red-5);
}

&.c-button--color-green {
color: var(--green-5);
}

&.c-button--is-loading {
cursor: wait;
}

Expand Down
15 changes: 5 additions & 10 deletions apps/demo/src/app/components/button/button.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, computed, Output, ViewEncapsulation } from '@angular/core';
import { ChangeDetectionStrategy, Component, computed, ViewEncapsulation } from '@angular/core';
import { bindAttribute, useAttribute, useBooleanAttribute } from '@bynary/composables/attribute';
import { provideBaseClass, useModifier, useModifierGroup } from '@bynary/composables/class';
import { θuseActivate } from '@bynary/composables/observer';

/**
* A demo button
Expand All @@ -22,17 +21,13 @@ import { θuseActivate } from '@bynary/composables/observer';
export class ButtonComponent {

readonly type = useAttribute('type', { defaultValue: 'button' });
readonly disabled = useBooleanAttribute('disabled');

readonly loading = useModifier('loading', { initialValue: false });

readonly isDisabled = useBooleanAttribute('disabled');
readonly isLoading = useModifier('is-loading', { initialValue: false });
readonly appearance = useModifierGroup('solid');

@Output()
readonly active = θuseActivate({ click: true, keydown: [ 'Enter' ] });
readonly color = useModifierGroup(undefined, { prefix: 'color' });

constructor() {
bindAttribute('tabindex', computed(() => this.disabled() ? '-1' : '0'));
bindAttribute('tabindex', computed(() => this.isDisabled() ? '-1' : '0'));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component, HostListener } from '@angular/core';
import { bindAttribute } from '@bynary/composables/attribute';
import { useColorScheme } from '@bynary/composables/observer';
import { ColorScheme, useColorScheme } from '@bynary/composables/observer';
import { useStorage } from '@bynary/composables/storage';

@Component({
Expand All @@ -14,7 +14,7 @@ import { useStorage } from '@bynary/composables/storage';
})
export class ColorSchemeSwitchComponent {

colorScheme = useColorScheme({ store: useStorage('color-scheme') });
colorScheme = useColorScheme({ store: useStorage<ColorScheme>('color-scheme') });

constructor() {
const root = document.firstElementChild;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div class="c-button__loading-indicator" *ngIf="isLoading"></div>
<ng-content></ng-content>
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { OldButtonComponent } from './old-button.component';

describe('OldButtonComponent', () => {
let component: OldButtonComponent;
let fixture: ComponentFixture<OldButtonComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [OldButtonComponent],
}).compileComponents();

fixture = TestBed.createComponent(OldButtonComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
97 changes: 97 additions & 0 deletions apps/demo/src/app/components/old-button/old-button.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { CommonModule } from '@angular/common';
import { Attribute, Component, ElementRef, HostBinding, inject, OnInit, Renderer2 } from '@angular/core';

/**
* A demo button without using @bynary/composables
*
* Has the same functionality as the {@link ButtonComponent} component.
*/
@Component({
selector: 'demo-old-button',
standalone: true,
imports: [ CommonModule ],
templateUrl: './old-button.component.html',
styleUrls: ['../button/button.component.scss'],
host: {
class: 'c-button'
}
})
export class OldButtonComponent implements OnInit {
@HostBinding('attr.type')
type: string;

isDisabled: boolean;

@HostBinding('class.c-button--is-loading')
isLoading = false;

private _appearance?: 'solid' | 'outline';
private _color?: 'red' | 'green';
private readonly _renderer = inject(Renderer2);
private readonly _elementRef = inject(ElementRef);

constructor(@Attribute('type') type: string, @Attribute('disabled') disabled: string) {
this.type = type ?? 'button';
this.isDisabled = disabled != null;
}

@HostBinding('attr.disabled')
get disabledAttr() {
return this.isDisabled ? '' : null;
}

@HostBinding('attr.tabindex')
get tabIndex() {
return this.isDisabled ? '-1' : '0';
}

get appearance(): 'solid' | 'outline' | undefined {
return this._appearance;
}

set appearance(value: 'solid' | 'outline' | undefined) {
if (this._appearance === value) {
return;
}

this._renderer.removeClass(
this._elementRef.nativeElement,
`c-button--${this._appearance}`
);
this._appearance = value;
this._renderer.addClass(
this._elementRef.nativeElement,
`c-button--${this._appearance}`
);
}

get color(): 'red' | 'green' | undefined {
return this._color;
}

set color(value: 'red' | 'green' | undefined) {
if (this._color === value) {
return;
}

if (this._color) {
this._renderer.removeClass(
this._elementRef.nativeElement,
`c-button--color-${this._color}`
);
}

this._color = value;

if (this._color) {
this._renderer.addClass(
this._elementRef.nativeElement,
`c-button--color-${this._color}`
);
}
}

ngOnInit() {
this.appearance = 'solid';
}
}
12 changes: 4 additions & 8 deletions apps/demo/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@

html {
--brand-light: var(--indigo-6);
--brand-dark: var(--indigo-4);

--text-1-light: var(--gray-8);
--text-2-light: var(--gray-7);
--text-2-light: var(--gray-6);
--text-1-dark: var(--gray-1);
--text-2-dark: var(--gray-5);

--surface-1-light: var(--gray-0);
--surface-2-light: var(--gray-1);
--surface-3-light: var(--gray-2);
--surface-4-light: var(--gray-3);
--surface-5-light: var(--gray-4);
--surface-6-light: var(--gray-5);
}

html {
--brand-dark: var(--indigo-4);

--text-1-dark: var(--gray-1);
--text-2-dark: var(--gray-3);

--surface-1-dark: var(--gray-12);
--surface-2-dark: var(--gray-11);
Expand Down
Loading

0 comments on commit e519eba

Please sign in to comment.