@bytebrand/fe-ui-core-autobahn 1.0.217 → 1.0.219

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ import React from 'react';
2
+
3
+ /** id of the VDP's "Preis & Marktwert" section (fe-main PriceMarket), where a price-rating
4
+ * badge leads; `PRICE_SECTION_HASH` is the URL fragment that opens a VDP scrolled to it. */
5
+ export const PRICE_SECTION_ID = 'vdp-preis';
6
+ export const PRICE_SECTION_HASH = '#' + PRICE_SECTION_ID;
7
+
8
+ /**
9
+ * Props that make a price-rating badge (and the bar next to it) a link to the VDP's
10
+ * "Preis & Marktwert" section: a click, Enter or Space calls `onActivate`. The event is stopped,
11
+ * so the card the badge sits in does not also run its own "open the car" click. The element
12
+ * gets the class `rating-link` (md3.css: pointer cursor, focus ring) — spread these onto it and
13
+ * add that class.
14
+ */
15
+ export function ratingLinkProps(onActivate: () => void, ariaLabel: string): {
16
+ role: 'link';
17
+ tabIndex: number;
18
+ 'aria-label': string;
19
+ onClick: (e: React.MouseEvent) => void;
20
+ onKeyDown: (e: React.KeyboardEvent) => void;
21
+ } {
22
+ const activate = (e: React.SyntheticEvent): void => {
23
+ e.preventDefault();
24
+ e.stopPropagation();
25
+ onActivate();
26
+ };
27
+ return {
28
+ role: 'link',
29
+ tabIndex: 0,
30
+ 'aria-label': ariaLabel,
31
+ onClick: activate,
32
+ onKeyDown: (e: React.KeyboardEvent) => {
33
+ if (e.key === 'Enter' || e.key === ' ') activate(e);
34
+ },
35
+ };
36
+ }
@@ -13,12 +13,13 @@
13
13
  //
14
14
  // Renders correctly only inside a `<div className="md3">` subtree.
15
15
 
16
- import React, { useState, useEffect } from 'react';
16
+ import React, { useState, useEffect, useLayoutEffect, useRef } from 'react';
17
17
  import { TranslationFunction } from '../types';
18
18
 
19
19
  import { Button, Icon, Switch, CreditCostNotice, CreditKeyFigures, hasCreditKeyFigures } from '../ui';
20
20
  import type { CreditKeyFiguresData } from '../ui';
21
- import { TIER_LABELS, TIER_KEYS, TIER_COLORS } from '../constants';
21
+ import { ratingLinkProps } from '../ui/ratingLink';
22
+ import { TIER_KEYS, TIER_COLORS, TIER_TEXT_COLORS, TIER_BACKGROUNDS, NO_RATING_KEY, NO_RATING_COLOR, NO_RATING_BACKGROUND, evalTierOf } from '../constants';
22
23
 
23
24
  /** Payment mode segment keys. Mirrors the design's ['fin','leasing','cash']. */
24
25
  export type FinanceMode = 'fin' | 'leasing' | 'cash';
@@ -103,20 +104,119 @@ export const Slider: React.FunctionComponent<SliderProps> = ({
103
104
  /**
104
105
  * Segmented price-evaluation bar (Figma: 5 discrete tier segments, NOT a gradient) with a
105
106
  * white ring marker. `leftPct` overrides the marker position (the Marktwert gauge positions
106
- * it continuously); default = centered on the tier segment.
107
+ * it continuously); default = centered on the tier segment. `tier` null = "Keine Bewertung":
108
+ * the usual colours but no marker, since the price has no place on the scale.
107
109
  */
108
- export const EvalBar: React.FunctionComponent<{ tier: number; leftPct?: number }> = ({ tier, leftPct }) => (
109
- <div className="fin-eval-bar">
110
+ export const EvalBar: React.FunctionComponent<{ tier: number | null; leftPct?: number }> = ({ tier, leftPct }) => (
111
+ <div className={`fin-eval-bar${tier == null ? ' fin-eval-bar--none' : ''}`}>
110
112
  {TIER_COLORS.map((c, i) => (
111
113
  <span key={i} className="fin-eval-seg" style={{ background: c }} />
112
114
  ))}
113
- <div
114
- className={`fin-eval-thumb${TIER_KEYS[Math.max(0, Math.min(4, tier))] === 'good' ? ' fin-eval-thumb--good' : ''}`}
115
- style={{ left: `${leftPct != null ? leftPct : (tier + 0.5) * 20}%`, borderColor: TIER_COLORS[Math.max(0, Math.min(4, tier))] }}
116
- />
115
+ {tier != null && (
116
+ <div
117
+ className="fin-eval-thumb"
118
+ style={{ left: `${leftPct != null ? leftPct : (tier + 0.5) * 20}%`, borderColor: TIER_COLORS[Math.max(0, Math.min(4, tier))] }}
119
+ />
120
+ )}
117
121
  </div>
118
122
  );
119
123
 
124
+ /** The price-rating pill's state for a mapRating() `ratingLabel`, shared by every pill + EvalBar
125
+ * pair: the tier (null = "Keine Bewertung"), its translated label and its badge colours (text
126
+ * `color` on `background`, TIER_TEXT_COLORS / TIER_BACKGROUNDS: each tier its own, the same on
127
+ * every page). Every pill carries the "4" footnote (the footer's Preisbewertung note), whatever
128
+ * the rating, as on fe-landing's cards. */
129
+ export function evalPillOf(ratingLabel: string | undefined, t: (key: string) => string): {
130
+ tier: number | null;
131
+ isGood: boolean;
132
+ label: string;
133
+ color: string;
134
+ background: string;
135
+ } {
136
+ const tier = evalTierOf(ratingLabel);
137
+ if (tier == null) {
138
+ return { tier, isGood: false, label: t('autobahn:tier.' + NO_RATING_KEY), color: NO_RATING_COLOR, background: NO_RATING_BACKGROUND };
139
+ }
140
+ return {
141
+ tier,
142
+ isGood: TIER_KEYS[tier] === 'good',
143
+ label: t('autobahn:tier.' + TIER_KEYS[tier]),
144
+ color: TIER_TEXT_COLORS[tier],
145
+ background: TIER_BACKGROUNDS[tier],
146
+ };
147
+ }
148
+
149
+ /** the smallest font size (px) the pill's label is fitted down to */
150
+ const EVAL_PILL_MIN_FONT = 7;
151
+ const useIsoLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
152
+
153
+ /**
154
+ * The price-rating pill: always 108×28 whatever the rating (Figma "Guter-Preis⁴", 108 Fill × 28 Hug;
155
+ * md3.css .fin-eval-pill--fit), with the label and its "4" on one line between ~20px side paddings.
156
+ * A long label ("Keine Bewertung", "Erhöhter Preis", the English ones) steps the font size down
157
+ * from the CSS 12px until it fits, the way CreditKeyFigures fits its lines. It is fitted again when
158
+ * the label changes (language), when the pill first gets a width (a hidden tab or sheet), and once
159
+ * the fonts are ready (Arimo loads late where Arial is missing).
160
+ */
161
+ export const EvalPill: React.FunctionComponent<{
162
+ label: string;
163
+ /** text colour and background: evalPillOf's (the tier's badge colours) */
164
+ color: string;
165
+ background?: string;
166
+ good?: boolean;
167
+ ariaLabel?: string;
168
+ }> = ({ label, color, background, good, ariaLabel }) => {
169
+ const ref = useRef<HTMLSpanElement>(null);
170
+ useIsoLayoutEffect(() => {
171
+ const el = ref.current;
172
+ if (!el || typeof window === 'undefined') return undefined;
173
+ const fit = (): void => {
174
+ el.style.fontSize = '';
175
+ const text = el.firstElementChild as HTMLElement | null;
176
+ if (!text || !el.clientWidth) return;
177
+ const style = window.getComputedStyle(el);
178
+ const room = el.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight);
179
+ let size = parseFloat(style.fontSize) || 12;
180
+ for (let i = 0; i < 40 && size > EVAL_PILL_MIN_FONT && text.getBoundingClientRect().width > room + 0.5; i += 1) {
181
+ size = Math.max(EVAL_PILL_MIN_FONT, size - 0.25);
182
+ el.style.fontSize = `${size}px`;
183
+ }
184
+ };
185
+ fit();
186
+ let alive = true;
187
+ const fonts = (document as any).fonts;
188
+ if (fonts && fonts.ready) fonts.ready.then(() => alive && fit());
189
+ const RO = (window as any).ResizeObserver;
190
+ let width = el.clientWidth;
191
+ const ro = RO
192
+ ? new RO(() => {
193
+ if (el.clientWidth !== width) {
194
+ width = el.clientWidth;
195
+ fit();
196
+ }
197
+ })
198
+ : null;
199
+ if (ro) ro.observe(el);
200
+ return () => {
201
+ alive = false;
202
+ if (ro) ro.disconnect();
203
+ };
204
+ }, [label]);
205
+ return (
206
+ <span
207
+ ref={ref}
208
+ className={`fin-eval-pill fin-eval-pill--fit${good ? ' fin-eval-pill--good' : ''}`}
209
+ style={{ color, background: background || `color-mix(in srgb, ${color} 16%, transparent)` }}
210
+ aria-label={ariaLabel}
211
+ >
212
+ <span className="fin-eval-pill-text">
213
+ {label}
214
+ <sup>4</sup>
215
+ </span>
216
+ </span>
217
+ );
218
+ };
219
+
120
220
  /** Figma CTA icon — document with a check-in-circle badge over its bottom-right corner
121
221
  * (no single lucide glyph matches; composed from file-text + a knocked-out check-circle).
122
222
  * The badge backdrop assumes a filled (primary) button. */
@@ -425,6 +525,9 @@ export interface FinanceCalculatorProps {
425
525
  onCall?: () => void;
426
526
  /** open the Finanzierungs-/Leasingdetails popup (full breakdown + legal, expanded). Omit to hide the link. */
427
527
  onShowDetails?: () => void;
528
+ /** the price-rating badge + bar were clicked: the page scrolls to its "Preis & Marktwert"
529
+ * section (#vdp-preis). Omit and they are not a link. */
530
+ onShowPriceRating?: () => void;
428
531
  /** mobile only: open the "Rate anpassen" / "Details anzeigen" sheet (collapsed/default view).
429
532
  * Omit to hide the trigger row (falls back to the desktop inline expander). */
430
533
  onOpenAdjust?: () => void;
@@ -530,6 +633,7 @@ const FinanceCalculator: React.FunctionComponent<FinanceCalculatorProps> = ({
530
633
  secureLabel,
531
634
  onCall,
532
635
  onShowDetails,
636
+ onShowPriceRating,
533
637
  onOpenAdjust,
534
638
  onModeChange,
535
639
  onDownChange,
@@ -545,14 +649,9 @@ const FinanceCalculator: React.FunctionComponent<FinanceCalculatorProps> = ({
545
649
  const isLease = mode === 'leasing';
546
650
  const terms = termOptions && termOptions.length ? termOptions : range(termMin, termMax, termStep);
547
651
  const showStrike = listPrice != null && listPrice > price;
548
- // price-evaluation tier from the rating label (defaults to 'Guter Preis' tier when unmapped).
549
- const tierIdx = TIER_LABELS.indexOf(ratingLabel as (typeof TIER_LABELS)[number]);
550
- const evalTier = tierIdx >= 0 ? tierIdx : 1;
551
- const isGoodTier = TIER_KEYS[evalTier] === 'good';
552
- // each tier shows its own real label now (Top/Guter/Fairer/Erhöht/Hoher Preis) — no more
553
- // collapsing 'good' onto the "Top-Preis" claim. Pill styling (colour/class/footnote) unchanged.
554
- const evalTierLabel = t('autobahn:tier.' + TIER_KEYS[evalTier]);
555
- const evalPillColor = isGoodTier ? TIER_COLORS[0] : TIER_COLORS[evalTier];
652
+ // price-evaluation pill + bar from the rating label: each tier its own label and colour,
653
+ // "Keine Bewertung" grey without a marker (an unknown label defaults to 'Guter Preis').
654
+ const { tier: evalTier, isGood: isGoodTier, label: evalTierLabel, color: evalPillColor, background: evalPillBackground } = evalPillOf(ratingLabel, t);
556
655
  // condition summary (current selection) + its own details link — financing: "0 € Anzahlung ·
557
656
  // 60 Monate · Finanzierungsdetails"; leasing: "25.000 € Sonderzahlung · 60 Mon. · 10.000
558
657
  // km/Jahr · Details" (same wording/order as CarCard's own PriceTerms leaseTerms).
@@ -880,19 +979,23 @@ const FinanceCalculator: React.FunctionComponent<FinanceCalculatorProps> = ({
880
979
  </div>
881
980
  {/* col 2: pill + eval bar grouped together (display:contents on desktop — no-op,
882
981
  preserves the original rate|pill / cond|bar auto-placement; mobile turns this into
883
- a real flex column so the two stack tight with a fixed 16px gap). */}
884
- <div className="fin-eval-col">
982
+ a real flex column so the two stack tight with a fixed 13px gap). */}
983
+ <div
984
+ className={ratingLabel && onShowPriceRating ? 'fin-eval-col rating-link' : 'fin-eval-col'}
985
+ {...(ratingLabel && onShowPriceRating
986
+ ? ratingLinkProps(onShowPriceRating, t('autobahn:finance.priceRatingLink', { rating: evalTierLabel }))
987
+ : {})}
988
+ >
885
989
  {/* col 2 row 1: price-rating pill (Figma: bold colored label, NO icon) */}
886
990
  <div className="fin-eval-pill-cell">
887
991
  {ratingLabel && (
888
- <span
889
- className={`fin-eval-pill${isGoodTier ? ' fin-eval-pill--good' : ''}`}
890
- style={{ color: evalPillColor, background: `color-mix(in srgb, ${evalPillColor} 16%, transparent)` }}
891
- aria-label={t('autobahn:finance.priceRatingAria', { rating: evalTierLabel })}
892
- >
893
- {evalTierLabel}
894
- {isGoodTier && <sup>4</sup>}
895
- </span>
992
+ <EvalPill
993
+ label={evalTierLabel}
994
+ color={evalPillColor}
995
+ background={evalPillBackground}
996
+ good={isGoodTier}
997
+ ariaLabel={t('autobahn:finance.priceRatingAria', { rating: evalTierLabel })}
998
+ />
896
999
  )}
897
1000
  </div>
898
1001
  {/* segmented evaluation bar (Figma: discrete tiers, not a gradient) */}