@buffcore/tokens 0.8.41 → 0.8.42
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/font-files.d.ts +29 -0
- package/dist/font-files.js +80 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/outputs/css.js +38 -2
- package/package.json +1 -1
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Theme } from './theme.ts';
|
|
2
|
+
/** One `@font-face`: a theme family, the file that holds it, its weights and its style. */
|
|
3
|
+
export interface FontFile {
|
|
4
|
+
family: string;
|
|
5
|
+
url: string;
|
|
6
|
+
/** One weight (`700`) or a range (`100 900`, a variable file). */
|
|
7
|
+
weight: string;
|
|
8
|
+
style: 'normal' | 'italic';
|
|
9
|
+
format: 'woff2' | 'woff' | 'truetype' | 'opentype';
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* What follows a family in a font file's name (`-bold`, `_600`, `variable`), or undefined when
|
|
13
|
+
* the file is not that family's. The stem is the family with its spaces removed, case aside, then
|
|
14
|
+
* nothing, a suffix after `-` or `_`, or `Variable` (`InterVariable`): `RobotoMono-Bold` is not
|
|
15
|
+
* "Roboto".
|
|
16
|
+
*/
|
|
17
|
+
export declare function fontFileSuffix(stem: string, family: string): string | undefined;
|
|
18
|
+
/** The families a theme names that the platform does not supply. */
|
|
19
|
+
export declare function themeFamilies(theme: Theme): string[];
|
|
20
|
+
/**
|
|
21
|
+
* Each file named from a family the theme names (`fontFileSuffix`: `JetBrainsMono-Bold.woff2` is
|
|
22
|
+
* "JetBrains Mono" at 700, `RobotoMono.woff2` is not "Roboto"). The rest of the name gives the weight
|
|
23
|
+
* (`Bold`, `600`) and the style (`Italic`). A file named only by its family, or with `Variable` in
|
|
24
|
+
* its name, covers every weight, unless the family also has files for single weights. Other files,
|
|
25
|
+
* and families with no file, are left out: the platform's fonts stand in.
|
|
26
|
+
*/
|
|
27
|
+
export declare function themeFontFiles(theme: Theme, urls: readonly string[]): FontFile[];
|
|
28
|
+
/** The `@font-face` rules for the files, shown with `font-display: swap`. */
|
|
29
|
+
export declare function fontFaceCss(files: readonly FontFile[]): string;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// The design system's own font files (`design/fonts/`), matched to the families a theme names, so
|
|
2
|
+
// the console, the prototype and the app load them without a list kept by hand.
|
|
3
|
+
import { cssFontFamily, isSystemFont } from './fonts.js';
|
|
4
|
+
const FORMATS = {
|
|
5
|
+
woff2: 'woff2',
|
|
6
|
+
woff: 'woff',
|
|
7
|
+
ttf: 'truetype',
|
|
8
|
+
otf: 'opentype',
|
|
9
|
+
};
|
|
10
|
+
// Longer names first: "semibold" before "bold", "extralight" before "light".
|
|
11
|
+
const WEIGHTS = [
|
|
12
|
+
[/thin|hairline/, 100],
|
|
13
|
+
[/extralight|ultralight/, 200],
|
|
14
|
+
[/semibold|demibold/, 600],
|
|
15
|
+
[/extrabold|ultrabold/, 800],
|
|
16
|
+
[/light/, 300],
|
|
17
|
+
[/medium/, 500],
|
|
18
|
+
[/bold/, 700],
|
|
19
|
+
[/black|heavy/, 900],
|
|
20
|
+
];
|
|
21
|
+
const squash = (text) => text.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
22
|
+
/**
|
|
23
|
+
* What follows a family in a font file's name (`-bold`, `_600`, `variable`), or undefined when
|
|
24
|
+
* the file is not that family's. The stem is the family with its spaces removed, case aside, then
|
|
25
|
+
* nothing, a suffix after `-` or `_`, or `Variable` (`InterVariable`): `RobotoMono-Bold` is not
|
|
26
|
+
* "Roboto".
|
|
27
|
+
*/
|
|
28
|
+
export function fontFileSuffix(stem, family) {
|
|
29
|
+
const key = family.replace(/\s+/g, '').toLowerCase();
|
|
30
|
+
const name = stem.replace(/\s+/g, '').toLowerCase();
|
|
31
|
+
if (!key || !name.startsWith(key))
|
|
32
|
+
return;
|
|
33
|
+
const rest = name.slice(key.length);
|
|
34
|
+
return /^(?:$|[-_]|variable|vf(?![a-z]))/.test(rest) ? rest : undefined;
|
|
35
|
+
}
|
|
36
|
+
/** The families a theme names that the platform does not supply. */
|
|
37
|
+
export function themeFamilies(theme) {
|
|
38
|
+
const { body, display, mono, fallbacks } = theme.font;
|
|
39
|
+
const named = [...display, ...body, ...mono, ...Object.values(fallbacks)];
|
|
40
|
+
return [...new Set(named.filter((family) => family && !isSystemFont(family)))];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Each file named from a family the theme names (`fontFileSuffix`: `JetBrainsMono-Bold.woff2` is
|
|
44
|
+
* "JetBrains Mono" at 700, `RobotoMono.woff2` is not "Roboto"). The rest of the name gives the weight
|
|
45
|
+
* (`Bold`, `600`) and the style (`Italic`). A file named only by its family, or with `Variable` in
|
|
46
|
+
* its name, covers every weight, unless the family also has files for single weights. Other files,
|
|
47
|
+
* and families with no file, are left out: the platform's fonts stand in.
|
|
48
|
+
*/
|
|
49
|
+
export function themeFontFiles(theme, urls) {
|
|
50
|
+
const families = themeFamilies(theme).sort((a, b) => b.length - a.length);
|
|
51
|
+
const found = [];
|
|
52
|
+
for (const url of urls) {
|
|
53
|
+
const name = decodeURIComponent(url.split(/[?#]/)[0]?.split('/').pop() ?? '');
|
|
54
|
+
const dot = name.lastIndexOf('.');
|
|
55
|
+
const format = FORMATS[name.slice(dot + 1).toLowerCase()];
|
|
56
|
+
if (dot < 1 || !format)
|
|
57
|
+
continue;
|
|
58
|
+
const stem = name.slice(0, dot);
|
|
59
|
+
const family = families.find((family) => fontFileSuffix(stem, family) !== undefined);
|
|
60
|
+
if (!family)
|
|
61
|
+
continue;
|
|
62
|
+
const rest = squash(fontFileSuffix(stem, family) ?? '');
|
|
63
|
+
const style = /italic|oblique/.test(rest) ? 'italic' : 'normal';
|
|
64
|
+
const number = /(?:^|\D)([1-9]00)(?:\D|$)/.exec(rest)?.[1];
|
|
65
|
+
const named = WEIGHTS.find(([pattern]) => pattern.test(rest))?.[1];
|
|
66
|
+
const weight = number ?? (named ? String(named) : '');
|
|
67
|
+
found.push({ family, url, weight, style, format, rest });
|
|
68
|
+
}
|
|
69
|
+
return found.map(({ rest, weight, ...file }) => {
|
|
70
|
+
const variable = /variable|vf/.test(rest);
|
|
71
|
+
const single = found.some((other) => other.family === file.family && other.weight);
|
|
72
|
+
return { ...file, weight: weight || (variable || !single ? '100 900' : '400') };
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/** The `@font-face` rules for the files, shown with `font-display: swap`. */
|
|
76
|
+
export function fontFaceCss(files) {
|
|
77
|
+
return files
|
|
78
|
+
.map(({ family, url, weight, style, format }) => `@font-face{font-family:${cssFontFamily(family)};src:url("${url.replace(/["\\\n]/g, encodeURIComponent)}") format("${format}");font-weight:${weight};font-style:${style};font-display:swap}`)
|
|
79
|
+
.join('');
|
|
80
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { checkTheme, contrastRatio, setAsideWords } from './checks.ts';
|
|
2
2
|
export { extendTheme, type ThemeOverrides } from './extend.ts';
|
|
3
|
+
export { type FontFile, fontFaceCss, fontFileSuffix, themeFamilies, themeFontFiles, } from './font-files.ts';
|
|
3
4
|
export { cssFontFamily, isSystemFont } from './fonts.ts';
|
|
4
5
|
export { toCss } from './outputs/css.ts';
|
|
5
6
|
export { type NativeTheme, scaleNative, toNative } from './outputs/native.ts';
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { soft } from './themes/soft.js';
|
|
|
5
5
|
import { warm } from './themes/warm.js';
|
|
6
6
|
export { checkTheme, contrastRatio, setAsideWords } from './checks.js';
|
|
7
7
|
export { extendTheme } from './extend.js';
|
|
8
|
+
export { fontFaceCss, fontFileSuffix, themeFamilies, themeFontFiles, } from './font-files.js';
|
|
8
9
|
export { cssFontFamily, isSystemFont } from './fonts.js';
|
|
9
10
|
export { toCss } from './outputs/css.js';
|
|
10
11
|
export { scaleNative, toNative } from './outputs/native.js';
|
package/dist/outputs/css.js
CHANGED
|
@@ -17,7 +17,7 @@ export function toCss(theme, options = {}) {
|
|
|
17
17
|
...theme.tokens?.[mode],
|
|
18
18
|
});
|
|
19
19
|
const rules = [rule(root, fixedVars(theme))];
|
|
20
|
-
for (const mode of Object.keys(theme.color)) {
|
|
20
|
+
for (const mode of Object.keys(theme.color).filter((name) => CSS_NAME.test(name))) {
|
|
21
21
|
const selector = mode === 'light'
|
|
22
22
|
? `${root}, ${root} [data-mode="light"]`
|
|
23
23
|
: `${root}[data-mode="${mode}"], ${root} [data-mode="${mode}"]`;
|
|
@@ -45,7 +45,43 @@ function fixedVars(theme) {
|
|
|
45
45
|
}
|
|
46
46
|
return vars;
|
|
47
47
|
}
|
|
48
|
+
/** A name a custom property or a `data-mode` value may have. */
|
|
49
|
+
const CSS_NAME = /^[A-Za-z0-9_-]+$/;
|
|
50
|
+
/**
|
|
51
|
+
* Whether a value stays inside its declaration: no line break or `</`, quotes and brackets closed,
|
|
52
|
+
* and no `{`, `}`, `;`, `<`, `>`, a comment or a trailing escape outside a quoted string.
|
|
53
|
+
*/
|
|
54
|
+
function staysInside(value) {
|
|
55
|
+
if (/[\n\r\f]|<\//.test(value))
|
|
56
|
+
return false;
|
|
57
|
+
let quote = '';
|
|
58
|
+
let depth = 0;
|
|
59
|
+
for (let i = 0; i < value.length; i++) {
|
|
60
|
+
const c = value[i];
|
|
61
|
+
if (c === '\\') {
|
|
62
|
+
if (i === value.length - 1)
|
|
63
|
+
return false;
|
|
64
|
+
i++;
|
|
65
|
+
}
|
|
66
|
+
else if (quote) {
|
|
67
|
+
if (c === quote)
|
|
68
|
+
quote = '';
|
|
69
|
+
}
|
|
70
|
+
else if (c === '"' || c === "'")
|
|
71
|
+
quote = c;
|
|
72
|
+
else if ('{};<>'.includes(c) || (c === '/' && value[i + 1] === '*'))
|
|
73
|
+
return false;
|
|
74
|
+
else if (c === '(')
|
|
75
|
+
depth++;
|
|
76
|
+
else if (c === ')' && --depth < 0)
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return !quote && depth === 0;
|
|
80
|
+
}
|
|
81
|
+
/** A theme's own values never leave their rule: a declaration that would is left out. */
|
|
48
82
|
function rule(selector, vars, indent = '') {
|
|
49
|
-
const lines = Object.entries(vars)
|
|
83
|
+
const lines = Object.entries(vars)
|
|
84
|
+
.filter(([name, value]) => CSS_NAME.test(name) && staysInside(String(value)))
|
|
85
|
+
.map(([name, value]) => `${indent} --${name}: ${value};`);
|
|
50
86
|
return [`${indent}${selector} {`, ...lines, `${indent}}`].join('\n');
|
|
51
87
|
}
|