@bytebrand/fe-ui-core-autobahn 1.0.203 → 1.0.205

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.
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { useEffect, useLayoutEffect, useRef } from 'react';
2
2
 
3
3
  import { TranslationFunction } from '../types';
4
4
  import { formatEur, formatEurCents, formatPct } from '../utils/financeFormat';
@@ -29,6 +29,9 @@ export interface CreditKeyFiguresProps {
29
29
  details?: React.ReactNode;
30
30
  /** host class(es) for size/colour/spacing (follow the host block's conditions line). */
31
31
  cls?: string;
32
+ /** false: no two-line auto-fit — the host keeps its own font size and lets the figures flow as
33
+ * one wrapping paragraph (SRL desktop price column, PM 19.11: "own size, three lines are fine"). */
34
+ fit?: boolean;
32
35
  }
33
36
 
34
37
  const real = (n: number | null | undefined): n is number => typeof n === 'number' && isFinite(n) && n > 0;
@@ -68,13 +71,53 @@ export function hasCreditKeyFigures(f: CreditKeyFiguresData): boolean {
68
71
  return [f.months, f.netLoan, f.total, f.nominal, f.apr].some(real);
69
72
  }
70
73
 
74
+ /** smallest font the auto-fit goes down to (px); the PM wants both lines to stay one line each. */
75
+ const MIN_FIT_PX = 6;
76
+ const useIsoLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
77
+
71
78
  /**
72
- * Two short lines with the financing key figures (Kerndaten). Plain text in the host's
73
- * conditions-line style; the " · " separators reuse the quiet .cc-terms-dot. The optional
74
- * `details` element (the host's existing details link) ends line 2.
79
+ * Exactly two lines with the financing key figures (Kerndaten) — PM 19.11: "from the months to
80
+ * Details it has to fit in 2 lines everywhere". Each line is one unbroken line (nowrap); when the
81
+ * host is too narrow the block's font size steps down (from the host's conditions-line size) until
82
+ * both lines fit, re-measured on resize. The " · " separators reuse the quiet .cc-terms-dot. The
83
+ * optional `details` element (the host's details link/button) ends line 2, always labelled
84
+ * "Details" (card.price.detailsLink), as in the PM's mock.
75
85
  */
76
- const CreditKeyFigures: React.FunctionComponent<CreditKeyFiguresProps> = ({ t, figures, details, cls }) => {
86
+ const CreditKeyFigures: React.FunctionComponent<CreditKeyFiguresProps> = ({ t, figures, details, cls, fit: autoFit = true }) => {
77
87
  const [line1, line2] = creditKeyFigureLines(t, figures);
88
+ const ref = useRef<HTMLDivElement>(null);
89
+ const detailsLabel = t('autobahn:card.price.detailsLink', { defaultValue: 'Details' });
90
+ const detailsEl = React.isValidElement(details) ? React.cloneElement(details as React.ReactElement<any>, {}, detailsLabel) : details;
91
+ const key = `${line1.join('|')}#${line2.join('|')}#${detailsEl ? detailsLabel : ''}`;
92
+
93
+ useIsoLayoutEffect(() => {
94
+ const el = ref.current;
95
+ if (!autoFit || !el || typeof window === 'undefined') return undefined;
96
+ const fit = (): void => {
97
+ el.style.fontSize = '';
98
+ const lines = Array.prototype.slice.call(el.querySelectorAll('.ckf-line')) as HTMLElement[];
99
+ const tooWide = (): boolean => lines.some((l) => l.scrollWidth > l.clientWidth + 0.5);
100
+ if (!el.clientWidth || !tooWide()) return;
101
+ let size = parseFloat(window.getComputedStyle(el).fontSize) || 12;
102
+ for (let i = 0; i < 40 && size > MIN_FIT_PX && tooWide(); i++) {
103
+ size = Math.max(MIN_FIT_PX, size - 0.25);
104
+ el.style.fontSize = `${size}px`;
105
+ }
106
+ };
107
+ fit();
108
+ const RO = (window as any).ResizeObserver;
109
+ if (!RO) return undefined;
110
+ let width = el.clientWidth;
111
+ const ro = new RO(() => {
112
+ if (el.clientWidth !== width) {
113
+ width = el.clientWidth;
114
+ fit();
115
+ }
116
+ });
117
+ ro.observe(el);
118
+ return () => ro.disconnect();
119
+ }, [key, autoFit]);
120
+
78
121
  const renderParts = (parts: string[]): React.ReactNode =>
79
122
  parts.map((part, i) => (
80
123
  <React.Fragment key={i}>
@@ -83,15 +126,17 @@ const CreditKeyFigures: React.FunctionComponent<CreditKeyFiguresProps> = ({ t, f
83
126
  </React.Fragment>
84
127
  ));
85
128
  return (
86
- <div className={'ckf' + (cls ? ` ${cls}` : '')}>
129
+ <div ref={ref} className={'ckf' + (autoFit ? '' : ' ckf--flow') + (cls ? ` ${cls}` : '')}>
87
130
  {line1.length > 0 && <span className="ckf-line">{renderParts(line1)}</span>}
88
- {(line2.length > 0 || details) && (
131
+ {/* separator between the two lines — only shown when they flow as one paragraph (ckf--flow) */}
132
+ {line1.length > 0 && (line2.length > 0 || detailsEl) && <span className="ckf-sep cc-terms-dot"> · </span>}
133
+ {(line2.length > 0 || detailsEl) && (
89
134
  <span className="ckf-line">
90
135
  {renderParts(line2)}
91
- {details && (
136
+ {detailsEl && (
92
137
  <>
93
138
  {line2.length > 0 && <span className="cc-terms-dot"> · </span>}
94
- {details}
139
+ <span className="ckf-details">{detailsEl}</span>
95
140
  </>
96
141
  )}
97
142
  </span>