@bytebrand/fe-ui-core-autobahn 1.0.203 → 1.0.204
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.
- package/package.json +1 -1
- package/theme/md3.css +15607 -15608
- package/ui/CreditKeyFigures.tsx +48 -8
package/ui/CreditKeyFigures.tsx
CHANGED
|
@@ -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';
|
|
@@ -68,13 +68,53 @@ export function hasCreditKeyFigures(f: CreditKeyFiguresData): boolean {
|
|
|
68
68
|
return [f.months, f.netLoan, f.total, f.nominal, f.apr].some(real);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/** smallest font the auto-fit goes down to (px); the PM wants both lines to stay one line each. */
|
|
72
|
+
const MIN_FIT_PX = 6;
|
|
73
|
+
const useIsoLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
74
|
+
|
|
71
75
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
76
|
+
* Exactly two lines with the financing key figures (Kerndaten) — PM 19.11: "from the months to
|
|
77
|
+
* Details it has to fit in 2 lines everywhere". Each line is one unbroken line (nowrap); when the
|
|
78
|
+
* host is too narrow the block's font size steps down (from the host's conditions-line size) until
|
|
79
|
+
* both lines fit, re-measured on resize. The " · " separators reuse the quiet .cc-terms-dot. The
|
|
80
|
+
* optional `details` element (the host's details link/button) ends line 2, always labelled
|
|
81
|
+
* "Details" (card.price.detailsLink), as in the PM's mock.
|
|
75
82
|
*/
|
|
76
83
|
const CreditKeyFigures: React.FunctionComponent<CreditKeyFiguresProps> = ({ t, figures, details, cls }) => {
|
|
77
84
|
const [line1, line2] = creditKeyFigureLines(t, figures);
|
|
85
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
86
|
+
const detailsLabel = t('autobahn:card.price.detailsLink', { defaultValue: 'Details' });
|
|
87
|
+
const detailsEl = React.isValidElement(details) ? React.cloneElement(details as React.ReactElement<any>, {}, detailsLabel) : details;
|
|
88
|
+
const key = `${line1.join('|')}#${line2.join('|')}#${detailsEl ? detailsLabel : ''}`;
|
|
89
|
+
|
|
90
|
+
useIsoLayoutEffect(() => {
|
|
91
|
+
const el = ref.current;
|
|
92
|
+
if (!el || typeof window === 'undefined') return undefined;
|
|
93
|
+
const fit = (): void => {
|
|
94
|
+
el.style.fontSize = '';
|
|
95
|
+
const lines = Array.prototype.slice.call(el.querySelectorAll('.ckf-line')) as HTMLElement[];
|
|
96
|
+
const tooWide = (): boolean => lines.some((l) => l.scrollWidth > l.clientWidth + 0.5);
|
|
97
|
+
if (!el.clientWidth || !tooWide()) return;
|
|
98
|
+
let size = parseFloat(window.getComputedStyle(el).fontSize) || 12;
|
|
99
|
+
for (let i = 0; i < 40 && size > MIN_FIT_PX && tooWide(); i++) {
|
|
100
|
+
size = Math.max(MIN_FIT_PX, size - 0.25);
|
|
101
|
+
el.style.fontSize = `${size}px`;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
fit();
|
|
105
|
+
const RO = (window as any).ResizeObserver;
|
|
106
|
+
if (!RO) return undefined;
|
|
107
|
+
let width = el.clientWidth;
|
|
108
|
+
const ro = new RO(() => {
|
|
109
|
+
if (el.clientWidth !== width) {
|
|
110
|
+
width = el.clientWidth;
|
|
111
|
+
fit();
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
ro.observe(el);
|
|
115
|
+
return () => ro.disconnect();
|
|
116
|
+
}, [key]);
|
|
117
|
+
|
|
78
118
|
const renderParts = (parts: string[]): React.ReactNode =>
|
|
79
119
|
parts.map((part, i) => (
|
|
80
120
|
<React.Fragment key={i}>
|
|
@@ -83,15 +123,15 @@ const CreditKeyFigures: React.FunctionComponent<CreditKeyFiguresProps> = ({ t, f
|
|
|
83
123
|
</React.Fragment>
|
|
84
124
|
));
|
|
85
125
|
return (
|
|
86
|
-
<div className={'ckf' + (cls ? ` ${cls}` : '')}>
|
|
126
|
+
<div ref={ref} className={'ckf' + (cls ? ` ${cls}` : '')}>
|
|
87
127
|
{line1.length > 0 && <span className="ckf-line">{renderParts(line1)}</span>}
|
|
88
|
-
{(line2.length > 0 ||
|
|
128
|
+
{(line2.length > 0 || detailsEl) && (
|
|
89
129
|
<span className="ckf-line">
|
|
90
130
|
{renderParts(line2)}
|
|
91
|
-
{
|
|
131
|
+
{detailsEl && (
|
|
92
132
|
<>
|
|
93
133
|
{line2.length > 0 && <span className="cc-terms-dot"> · </span>}
|
|
94
|
-
{
|
|
134
|
+
<span className="ckf-details">{detailsEl}</span>
|
|
95
135
|
</>
|
|
96
136
|
)}
|
|
97
137
|
</span>
|