Skip to content

Commit

Permalink
Clean up button toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
Nateowami committed Aug 23, 2024
1 parent 216d4f3 commit 1a80a2f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@
<div class="flex-row">
@for (book of bookOptions; track book) {
<app-toggle-book
[book]="book.bookNum"
[selected]="book.selected"
(selected)="onChipListChange(book)"
(selectedChanged)="onChipListChange(book)"
[disabled]="readonly"
[progress]="book.progressPercentage / 100"
(change)="onChipListChange(book)"
></app-toggle-book>
>{{ "canon.book_names." + book.bookId | transloco }}</app-toggle-book
>
}
</div>
</ng-container>
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<ng-container transloco>
<span
class="book"
[style.--progress]="progressCssValue"
[style.--progress-selected-color]="progressColorCssValue"
[style.--progress-selected-hover-color]="progressColorHoverCssValue"
[style.--progress-selected-bg-color]="progressBgColorCssValue"
[style.--progress-selected-hover-bg-color]="progressHoverBgColorCssValue"
[class.selected]="selected"
[class.disabled]="disabled"
(click)="toggleSelected()"
(keypress)="onKeyPress($event)"
[matTooltip]="progressDescription"
matRipple
[matRippleDisabled]="disabled"
[tabindex]="disabled ? -1 : 0"
>
{{ bookName(book) }}
</span>
</ng-container>
<span
class="book"
[style.--progress]="progressCssValue"
[style.--progress-color]="progressColorCssValue"
[style.--progress-hover-color]="progressColorHoverCssValue"
[style.--progress-bg-color]="progressBgColorCssValue"
[style.--progress-hover-bg-color]="progressHoverBgColorCssValue"
[style.--progress-direction-degrees]="progressDirectionCssValue"
(click)="toggleSelected()"
(keypress)="onKeyPress($event)"
[matTooltip]="progressDescription"
matRipple
[matRippleDisabled]="disabled"
[tabindex]="disabled ? -1 : 0"
>
<ng-content></ng-content>
</span>
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
.book.selected {
:host.selected .book {
color: white;
--progress-color: var(--progress-selected-color);
--progress-hover-color: var(--progress-selected-hover-color);
--progress-bg-color: var(--progress-selected-bg-color);
--progress-hover-bg-color: var(--progress-selected-hover-bg-color);
}

.book:not(.selected) {
--progress-color: hsl(0, 0%, 80%);
--progress-hover-color: hsl(0, 0%, 70%);
--progress-bg-color: hsl(0, 0%, 90%);
--progress-hover-bg-color: hsl(0, 0%, 80%);
}

.book {
Expand All @@ -19,18 +8,28 @@
padding: 0 16px;
line-height: 32px;
border-radius: 1000px;
min-width: 5em;
text-align: center;

background-image: linear-gradient(
90deg,
var(--progress-direction-degrees),
var(--progress-color) var(--progress),
var(--progress-bg-color) var(--progress)
);
}

:host:not(.disabled) .book:hover {
background-image: linear-gradient(
var(--progress-direction-degrees),
var(--progress-hover-color) var(--progress),
var(--progress-hover-bg-color) var(--progress)
);
}

&.disabled:not(.selected) {
opacity: 0.7;
}
:host.disabled:not(.selected) {
opacity: 0.7;
}

&:not(.disabled) {
cursor: pointer;
}
:host:not(.disabled) {
cursor: pointer;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Component, EventEmitter, HostBinding, Input, Output } from '@angular/core';
import { MatRippleModule } from '@angular/material/core';
import { MatTooltipModule } from '@angular/material/tooltip';
import { TranslocoModule } from '@ngneat/transloco';
import { I18nService } from '../../../../xforge-common/i18n.service';

interface ButtonColorSpec {
Expand Down Expand Up @@ -43,31 +42,39 @@ const availableColors: ButtonColorSpec[] = [
@Component({
selector: 'app-toggle-book',
standalone: true,
imports: [TranslocoModule, MatTooltipModule, MatRippleModule],
imports: [MatTooltipModule, MatRippleModule],
templateUrl: './toggle-book.component.html',
styleUrl: './toggle-book.component.scss'
})
export class ToggleBookComponent {
@Output() selectedChanged = new EventEmitter<number>();
@Input() selected = false;
@Input() disabled = false;
@Output() selectedChanged = new EventEmitter<boolean>();

Check warning on line 50 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L50

Added line #L50 was not covered by tests

@HostBinding('class.selected')
@Input()
selected = false;

Check warning on line 54 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L54

Added line #L54 was not covered by tests

@HostBinding('class.disabled')
@Input()
disabled = false;

Check warning on line 58 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L58

Added line #L58 was not covered by tests

@Input() borderWidth = 2;

Check warning on line 60 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L60

Added line #L60 was not covered by tests
@Input() book!: number;
@Input() progress?: number;
@Input() hues: number[] = [230];

colorSpec = availableColors[3];
selectedColorSpec = availableColors[3];
unselectedColorSpec: ButtonColorSpec = { hue: 0, saturation: 0, dark: 80, light: 90, hoverLight: 80, hoverDark: 70 };

Check warning on line 64 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L63-L64

Added lines #L63 - L64 were not covered by tests

constructor(private readonly i18n: I18nService) {}
get colorSpec(): ButtonColorSpec {
return this.selected ? this.selectedColorSpec : this.unselectedColorSpec;
}

bookName(book: number): string {
return this.i18n.localizeBook(book);
constructor(readonly i18n: I18nService) {
console.log(i18n.direction);

Check warning on line 71 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L70-L71

Added lines #L70 - L71 were not covered by tests
}

toggleSelected(): void {
if (!this.disabled) {
this.selected = !this.selected;
this.selectedChanged.emit(this.book);
this.selectedChanged.emit(this.selected);

Check warning on line 77 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L76-L77

Added lines #L76 - L77 were not covered by tests
}
}

Expand All @@ -78,33 +85,29 @@ export class ToggleBookComponent {
}
}

get backgroundCssGradientStripes(): string {
const percentPerStripe = 12.5;
const colors = this.hues.map(hue => `hsl(${hue}, 80%, 60%)`);
let gradient = [];
for (const [index, color] of colors.entries()) {
const from = index * percentPerStripe;
const to = (index + 1) * percentPerStripe;
gradient.push(`${color} ${from}%, ${color} ${to}%`);
}
return `repeating-linear-gradient(135deg, ${gradient.join(', ')})`;
}

get progressCssValue(): string {
return `${(this.progress ?? 0) * 100}%`;
}

get progressColorCssValue(): string {
return `hsl(${this.colorSpec.hue}, ${this.colorSpec.saturation}%, ${this.colorSpec.dark}%)`;
return this.hsl(this.colorSpec.hue, this.colorSpec.saturation, this.colorSpec.dark);

Check warning on line 93 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L93

Added line #L93 was not covered by tests
}
get progressColorHoverCssValue(): string {
return `hsl(${this.colorSpec.hue}, ${this.colorSpec.saturation}%, ${this.colorSpec.hoverDark}%)`;
return this.hsl(this.colorSpec.hue, this.colorSpec.saturation, this.colorSpec.hoverDark);

Check warning on line 96 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L96

Added line #L96 was not covered by tests
}
get progressBgColorCssValue(): string {
return `hsl(${this.colorSpec.hue}, ${this.colorSpec.saturation}%, ${this.colorSpec.light}%)`;
return this.hsl(this.colorSpec.hue, this.colorSpec.saturation, this.colorSpec.light);

Check warning on line 99 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L99

Added line #L99 was not covered by tests
}
get progressHoverBgColorCssValue(): string {
return `hsl(${this.colorSpec.hue}, ${this.colorSpec.saturation}%, ${this.colorSpec.hoverLight}%)`;
return this.hsl(this.colorSpec.hue, this.colorSpec.saturation, this.colorSpec.hoverLight);

Check warning on line 102 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L102

Added line #L102 was not covered by tests
}

get progressDirectionCssValue(): string {
return this.i18n.direction === 'rtl' ? '270deg' : '90deg';
}

hsl(hue: number, saturation: number, light: number): string {
return `hsl(${hue}, ${saturation}%, ${light}%)`;

Check warning on line 110 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/toggle-book/toggle-book.component.ts#L110

Added line #L110 was not covered by tests
}

get progressDescription(): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,25 @@ const meta: Meta = {
})
],
argTypes: {
progress: { control: { type: 'range', min: 0, max: 1, step: 0.01 } }
}
progress: { control: { type: 'range', min: 0, max: 1, step: 0.01 } },
selected: { control: 'boolean' },
text: { control: 'text' }
},
render: args => ({
props: args,
template: `<app-toggle-book [selected]="selected" [disabled]="disabled" [progress]="progress">${args.text}</app-toggle-book>`
})
};

export default meta;

interface StoryState {
book: number;
progress?: number;
hues: number[];
selected: boolean;
disabled: boolean;
}

type Story = StoryObj<StoryState>;
type Story = StoryObj<ToggleBookComponent & { text: string }>;

export const Default: Story = {
args: {
book: 1,
progress: 0.37,
hues: [0]
text: 'Genesis',
selected: false,
progress: 0.37
}
};

Expand All @@ -46,23 +44,19 @@ export const Selected: Story = {
}
};

export const TwoColor: Story = {
args: {
...Selected.args,
hues: [0, 240]
}
};

export const ThreeColor: Story = {
export const Disabled: Story = {
args: {
...Selected.args,
hues: [0, 120, 240]
...Default.args,
disabled: true
}
};

export const Disabled: Story = {
export const RTL: Story = {
args: {
book: 8,
disabled: true
...Default.args,
text: 'تكوين'
},
parameters: {
locale: 'ar'
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { MatStepperModule } from '@angular/material/stepper';
import { MatTableModule } from '@angular/material/table';
import { MatTabsModule } from '@angular/material/tabs';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatTooltipModule } from '@angular/material/tooltip';
import { MAT_TOOLTIP_DEFAULT_OPTIONS, MatTooltipModule } from '@angular/material/tooltip';
import { TranslocoService } from '@ngneat/transloco';
import { NgCircleProgressModule } from 'ng-circle-progress';
import { AutofocusDirective } from './autofocus.directive';
Expand Down Expand Up @@ -153,6 +153,10 @@ const appFlexLayoutBreakPoints = [
{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
useValue: { appearance: 'outline', hideRequiredMarker: true }
},
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: { disableTooltipInteractivity: true }
}
]
})
Expand Down

0 comments on commit 1a80a2f

Please sign in to comment.