@vettvangur/design-system 2.0.62 → 2.0.64
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/dist/index.js +74 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2154,6 +2154,57 @@ function normalizeValuesKeys(values) {
|
|
|
2154
2154
|
function escapeCssSingleQuotedString(value) {
|
|
2155
2155
|
return String(value).replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
2156
2156
|
}
|
|
2157
|
+
function splitFontStack(raw) {
|
|
2158
|
+
return String(raw ?? '').split(',').map(p => p.trim()).filter(Boolean);
|
|
2159
|
+
}
|
|
2160
|
+
function maybeCollapseToBaseFont(raw, {
|
|
2161
|
+
baseTokenKey,
|
|
2162
|
+
baseFamily
|
|
2163
|
+
}) {
|
|
2164
|
+
// Collapse "Sharp Grotesk Medium 19" -> var(--font-body) when baseFamily is "Sharp Grotesk".
|
|
2165
|
+
// This keeps projects with a single font family from generating multiple (near-identical) families.
|
|
2166
|
+
// Does not hardcode any specific family name.
|
|
2167
|
+
const v = String(raw ?? '').trim();
|
|
2168
|
+
if (!v || !baseTokenKey || !baseFamily) {
|
|
2169
|
+
return raw;
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
// Avoid changing var() or multi-stack definitions.
|
|
2173
|
+
if (/^var\(/.test(v) || v.includes(',')) {
|
|
2174
|
+
return raw;
|
|
2175
|
+
}
|
|
2176
|
+
const base = String(baseFamily).trim();
|
|
2177
|
+
if (!base) {
|
|
2178
|
+
return raw;
|
|
2179
|
+
}
|
|
2180
|
+
if (v === base || v.startsWith(`${base} `)) {
|
|
2181
|
+
return `var(--${baseTokenKey})`;
|
|
2182
|
+
}
|
|
2183
|
+
return raw;
|
|
2184
|
+
}
|
|
2185
|
+
function normalizeBaseFontValue(raw, {
|
|
2186
|
+
baseFamily
|
|
2187
|
+
}) {
|
|
2188
|
+
// For the base token itself, prefer emitting the canonical family name.
|
|
2189
|
+
// Example: "Sharp Grotesk Book 19" -> "Sharp Grotesk".
|
|
2190
|
+
const v = String(raw ?? '').trim();
|
|
2191
|
+
if (!v || !baseFamily) {
|
|
2192
|
+
return raw;
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
// Avoid changing var() or multi-stack definitions.
|
|
2196
|
+
if (/^var\(/.test(v) || v.includes(',')) {
|
|
2197
|
+
return raw;
|
|
2198
|
+
}
|
|
2199
|
+
const base = String(baseFamily).trim();
|
|
2200
|
+
if (!base) {
|
|
2201
|
+
return raw;
|
|
2202
|
+
}
|
|
2203
|
+
if (v === base || v.startsWith(`${base} `)) {
|
|
2204
|
+
return base;
|
|
2205
|
+
}
|
|
2206
|
+
return raw;
|
|
2207
|
+
}
|
|
2157
2208
|
function formatFontFamilyValue(raw) {
|
|
2158
2209
|
if (raw == null) {
|
|
2159
2210
|
return raw;
|
|
@@ -2164,7 +2215,7 @@ function formatFontFamilyValue(raw) {
|
|
|
2164
2215
|
}
|
|
2165
2216
|
|
|
2166
2217
|
// Allow comma-separated stacks and quote parts that would otherwise be parsed as separate identifiers.
|
|
2167
|
-
const parts = s
|
|
2218
|
+
const parts = splitFontStack(s);
|
|
2168
2219
|
const formatted = parts.map(p => {
|
|
2169
2220
|
// Keep CSS var() and already-quoted values.
|
|
2170
2221
|
if (/^var\(/.test(p)) {
|
|
@@ -2226,9 +2277,29 @@ async function generateFontFamily(variableSet, config) {
|
|
|
2226
2277
|
*/
|
|
2227
2278
|
async function generateFile(variables, config) {
|
|
2228
2279
|
let css = `/* AUTO-GENERATED - DO NOT EDIT BY HAND */\n\n` + `@theme {\n`;
|
|
2280
|
+
|
|
2281
|
+
// If a project uses a single font family, designers sometimes encode style/optical size into the
|
|
2282
|
+
// "family" token (e.g. "Sharp Grotesk Medium 19"). For CSS, we usually want a stable family name.
|
|
2283
|
+
// We use the first part of the base token's mobile stack as the canonical family and collapse
|
|
2284
|
+
// other families that match (or start with) it.
|
|
2285
|
+
const baseTokenKey = Object.hasOwn(variables || {}, 'font-body') ? 'font-body' : null;
|
|
2286
|
+
const baseMobileRaw = baseTokenKey ? variables?.[baseTokenKey]?.values?.mobile : null;
|
|
2287
|
+
const baseFamily = baseMobileRaw ? splitFontStack(baseMobileRaw)[0] : null;
|
|
2229
2288
|
for (const [key, token] of Object.entries(variables || {})) {
|
|
2230
|
-
const
|
|
2231
|
-
|
|
2289
|
+
const mobileRaw = key === baseTokenKey ? normalizeBaseFontValue(token?.values?.mobile, {
|
|
2290
|
+
baseFamily
|
|
2291
|
+
}) : maybeCollapseToBaseFont(token?.values?.mobile, {
|
|
2292
|
+
baseTokenKey,
|
|
2293
|
+
baseFamily
|
|
2294
|
+
});
|
|
2295
|
+
const desktopRaw = key === baseTokenKey ? normalizeBaseFontValue(token?.values?.desktop, {
|
|
2296
|
+
baseFamily
|
|
2297
|
+
}) : maybeCollapseToBaseFont(token?.values?.desktop, {
|
|
2298
|
+
baseTokenKey,
|
|
2299
|
+
baseFamily
|
|
2300
|
+
});
|
|
2301
|
+
const mobile = formatFontFamilyValue(mobileRaw);
|
|
2302
|
+
const desktop = formatFontFamilyValue(desktopRaw);
|
|
2232
2303
|
if (mobile != null) {
|
|
2233
2304
|
css += ` --${key}: ${mobile};\n`;
|
|
2234
2305
|
}
|