-
Notifications
You must be signed in to change notification settings - Fork 15
refactor(wizard): use relative units for wizard sizing #1848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,13 +10,15 @@ import { | |
| computed, | ||
| contentChildren, | ||
| ElementRef, | ||
| inject, | ||
| input, | ||
| linkedSignal, | ||
| output, | ||
| signal, | ||
| untracked, | ||
| viewChild | ||
| } from '@angular/core'; | ||
| import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop'; | ||
| import { | ||
| elementCancel, | ||
| elementChecked, | ||
|
|
@@ -27,10 +29,15 @@ import { | |
| elementRight4, | ||
| elementWarningFilled | ||
| } from '@siemens/element-icons'; | ||
| import { WebComponentContentChildren } from '@siemens/element-ng/common'; | ||
| import { TextMeasureService, WebComponentContentChildren } from '@siemens/element-ng/common'; | ||
| import { addIcons, SiIconComponent } from '@siemens/element-ng/icon'; | ||
| import { SiResizeObserverDirective } from '@siemens/element-ng/resize-observer'; | ||
| import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate'; | ||
| import { | ||
| injectSiTranslateService, | ||
| SiTranslatePipe, | ||
| t | ||
| } from '@siemens/element-translate-ng/translate'; | ||
| import { switchMap } from 'rxjs'; | ||
|
|
||
| import { SiWizardStepComponent } from './si-wizard-step.component'; | ||
|
|
||
|
|
@@ -61,6 +68,16 @@ interface StepMetadata { | |
| } | ||
| }) | ||
| export class SiWizardComponent { | ||
| /** em-based multipliers relative to the step title font-size. */ | ||
| private static readonly minStepWidthEm = 6; | ||
| private static readonly maxStepWidthEm = 14; | ||
| private static readonly defaultStepWidthEm = 11; | ||
| private static readonly fallbackFontSize = 14; | ||
| /** Fixed horizontal padding of the step title (px-6 = 2 × 16px). */ | ||
| private static readonly stepPadding = 32; | ||
| private readonly translateService = injectSiTranslateService(); | ||
| private readonly textMeasureService = inject(TextMeasureService); | ||
|
|
||
| protected readonly containerSteps = viewChild<ElementRef<HTMLDivElement>>('containerSteps'); | ||
|
|
||
| /** | ||
|
|
@@ -219,7 +236,10 @@ export class SiWizardComponent { | |
| protected readonly showCompletionPage = signal(false); | ||
| /** The list of visible steps. */ | ||
| protected readonly activeSteps = computed(() => this.computeVisibleSteps()); | ||
|
|
||
| private readonly headingKeys = computed(() => this.steps().map(s => s.heading())); | ||
| private readonly maxStepWidth = signal( | ||
| SiWizardComponent.defaultStepWidthEm * SiWizardComponent.fallbackFontSize | ||
| ); | ||
| private readonly _index = linkedSignal(() => { | ||
| const currentStep = this._currentStep(); | ||
| const currentStepIndex = currentStep ? this.steps().indexOf(currentStep) : 0; | ||
|
|
@@ -288,6 +308,17 @@ export class SiWizardComponent { | |
| }); | ||
| }); | ||
|
|
||
| constructor() { | ||
| toObservable(this.headingKeys) | ||
| .pipe( | ||
| switchMap(keys => this.translateService.translateAsync(keys)), | ||
| takeUntilDestroyed() | ||
| ) | ||
| .subscribe(translations => | ||
| this.maxStepWidth.set(this.measureMaxTextWidth(Object.values(translations))) | ||
| ); | ||
| } | ||
|
|
||
| protected activateStep(event: Event, stepIndex: number): void { | ||
| event.preventDefault(); | ||
| if (this.stepsMetadata()[stepIndex].canActivate) { | ||
|
|
@@ -374,6 +405,30 @@ export class SiWizardComponent { | |
| this._index.set(this.steps().indexOf(step)); | ||
| } | ||
|
|
||
| private measureMaxTextWidth(texts: string[]): number { | ||
| const titleEl = | ||
| this.containerSteps()?.nativeElement.querySelector<HTMLElement>('.title') ?? undefined; | ||
| const fontSize = titleEl | ||
| ? parseFloat(getComputedStyle(titleEl).fontSize) | ||
| : SiWizardComponent.fallbackFontSize; | ||
| const defaultWidth = SiWizardComponent.defaultStepWidthEm * fontSize; | ||
|
|
||
| if (texts.length === 0) { | ||
| return defaultWidth; | ||
| } | ||
|
|
||
| const minWidth = SiWizardComponent.minStepWidthEm * fontSize; | ||
| const maxWidth = SiWizardComponent.maxStepWidthEm * fontSize; | ||
|
|
||
| // Only take texts into account which aren't much shorter than the longest text. | ||
| const maxCharLength = Math.max(...texts.map(text => text.length)); | ||
| const candidates = texts.filter(text => text.length >= maxCharLength * 0.8); | ||
| const maxTextWidth = Math.max( | ||
| ...candidates.map(text => this.textMeasureService.measureText(text, titleEl)) | ||
| ); | ||
| return Math.min(Math.max(maxTextWidth + SiWizardComponent.stepPadding, minWidth), maxWidth); | ||
| } | ||
|
|
||
|
Comment on lines
+408
to
+431
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it worth the complexity? I mean we could just assume a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we directly replace the pixel values with rem or ch, small container sizes become problematic — a larger font size can cause only a single step to be visible at a time. To address this, we introduce the additional complexity of a solution that also handles smaller containers. See also https://code.siemens.com/simpl/simpl-element/-/work_items/2464 @dr-itz but if you believe it it isn't worth it - I adjust the code
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| protected updateVisibleSteps(): void { | ||
| const newVisibleSteps = this.calculateVisibleStepCount(); | ||
| if (newVisibleSteps !== this.visibleSteps()) { | ||
|
|
@@ -392,10 +447,12 @@ export class SiWizardComponent { | |
| containerSteps.nativeElement.clientHeight - | ||
| parseInt(computedStyle.paddingBlockStart) - | ||
| parseInt(computedStyle.paddingBlockEnd); | ||
| return Math.max(Math.floor(clientHeight / 48), 1); | ||
| const stepEl = containerSteps.nativeElement.querySelector('.step'); | ||
| const stepHeight = stepEl?.getBoundingClientRect().height ?? 48; | ||
| return Math.max(Math.floor(clientHeight / stepHeight), 1); | ||
| } else { | ||
| const clientWidth = containerSteps.nativeElement.clientWidth; | ||
| return Math.max(Math.floor(clientWidth / 150), 1); | ||
| return Math.max(Math.floor(clientWidth / this.maxStepWidth()), 1); | ||
| } | ||
| } | ||
|
|
||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.