@open-artifact/design-system-core 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +33 -4
- package/src/check.d.ts +13 -0
- package/src/check.js +122 -0
- package/src/contrast.d.ts +42 -0
- package/src/contrast.js +87 -0
- package/src/edit.d.ts +52 -0
- package/src/edit.js +163 -0
- package/src/generate.d.ts +48 -0
- package/src/generate.js +217 -0
- package/src/index.d.ts +12 -0
- package/src/index.js +12 -0
- package/src/installed.d.ts +23 -0
- package/src/installed.js +72 -0
- package/src/props.d.ts +38 -0
- package/src/props.js +127 -0
- package/src/system.d.ts +202 -0
- package/src/system.js +342 -0
- package/src/tokens.d.ts +220 -0
- package/src/tokens.js +534 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @open-artifact/design-system-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Design System artifact model: tokens.json grammar and tokens.css compiler, the index, components, assets, manifest and checks.
|
|
4
|
+
|
|
5
|
+
Part of [Open Artifact](https://github.com/JimLiu/open-artifact): slide decks, design canvases and whiteboards an agent makes
|
|
6
|
+
and you view, edit and comment on in one local page. Source: [`packages/design-system-core`](https://github.com/JimLiu/open-artifact/tree/main/packages/design-system-core).
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-artifact/design-system-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The Design System artifact model: tokens.json grammar and tokens.css compiler, the index, components, assets, manifest and checks.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"default": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"NOTICE"
|
|
17
|
+
],
|
|
18
|
+
"license": "Apache-2.0",
|
|
19
|
+
"author": {
|
|
20
|
+
"name": "Jim Liu 宝玉",
|
|
21
|
+
"url": "https://github.com/JimLiu"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/JimLiu/open-artifact#readme",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/JimLiu/open-artifact.git",
|
|
27
|
+
"directory": "packages/design-system-core"
|
|
28
|
+
},
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/JimLiu/open-artifact/issues"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
}
|
|
35
|
+
}
|
package/src/check.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whole-project checks of a Design System artifact. Paths are relative to
|
|
3
|
+
* `project/`. Binary files are passed with an empty string as content; only
|
|
4
|
+
* their paths matter here.
|
|
5
|
+
*/
|
|
6
|
+
/** A finding tied to a file. */
|
|
7
|
+
export interface SystemDiagnostic {
|
|
8
|
+
file: string;
|
|
9
|
+
level: 'error' | 'warning' | 'info';
|
|
10
|
+
message: string;
|
|
11
|
+
}
|
|
12
|
+
/** Checks a design system's files. */
|
|
13
|
+
export declare function checkDesignSystem(files: ReadonlyMap<string, string>): SystemDiagnostic[];
|
package/src/check.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whole-project checks of a Design System artifact. Paths are relative to
|
|
3
|
+
* `project/`. Binary files are passed with an empty string as content; only
|
|
4
|
+
* their paths matter here.
|
|
5
|
+
*/
|
|
6
|
+
import { normalizeTokens } from "./tokens.js";
|
|
7
|
+
import { COMPONENT_NAME, MAX_SECTIONS, fileRole, hasMarker, listSections, normalizeSystemIndex, parseBundleHeader, parseCardMarker, pathProblem, } from "./system.js";
|
|
8
|
+
/** Size caps of text files, bytes. */
|
|
9
|
+
const CAPS = {
|
|
10
|
+
readme: 200 * 1024,
|
|
11
|
+
tokens: 512 * 1024,
|
|
12
|
+
bundle: 6 * 1024 * 1024,
|
|
13
|
+
'bundle-css': 2 * 1024 * 1024,
|
|
14
|
+
library: 2 * 1024 * 1024,
|
|
15
|
+
'component-preview': 256 * 1024,
|
|
16
|
+
'component-readme': 64 * 1024,
|
|
17
|
+
'component-source': 512 * 1024,
|
|
18
|
+
};
|
|
19
|
+
/** Embeds a preview may not use. */
|
|
20
|
+
const PREVIEW_EMBEDS = /<(iframe|frame|object|embed|portal|noscript)\b/i;
|
|
21
|
+
/** Checks a design system's files. */
|
|
22
|
+
export function checkDesignSystem(files) {
|
|
23
|
+
const out = [];
|
|
24
|
+
const add = (file, level, message) => {
|
|
25
|
+
out.push({ file, level, message });
|
|
26
|
+
};
|
|
27
|
+
// The index.
|
|
28
|
+
const indexText = files.get('design-system.json');
|
|
29
|
+
let namespace = '';
|
|
30
|
+
if (indexText === undefined) {
|
|
31
|
+
add('design-system.json', 'error', 'project/design-system.json is missing: the page treats the system as empty.');
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
try {
|
|
35
|
+
const raw = JSON.parse(indexText);
|
|
36
|
+
if (!hasMarker(raw))
|
|
37
|
+
add('design-system.json', 'error', 'The index has no createdOnFiles or convertedFrom marker; the page opens read-only.');
|
|
38
|
+
const index = normalizeSystemIndex(raw);
|
|
39
|
+
namespace = index.namespace;
|
|
40
|
+
if (!/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(index.namespace))
|
|
41
|
+
add('design-system.json', 'error', `namespace "${index.namespace}" must be a JS identifier.`);
|
|
42
|
+
if (raw.layout !== 'files')
|
|
43
|
+
add('design-system.json', 'error', '"layout" must be "files".');
|
|
44
|
+
for (const [key, group] of Object.entries(index.assetGroups)) {
|
|
45
|
+
for (const [fileKey, record] of Object.entries(group.files)) {
|
|
46
|
+
if (!record || typeof record.blob !== 'string' || typeof record.name !== 'string') {
|
|
47
|
+
add('design-system.json', 'error', `assetGroups.${key}.files.${fileKey} needs name, blob, size and type.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (index.lastChange && (typeof index.lastChange.by !== 'string' || typeof index.lastChange.at !== 'string')) {
|
|
52
|
+
add('design-system.json', 'warning', 'lastChange needs "by" and "at".');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
add('design-system.json', 'error', `design-system.json is not valid JSON: ${String(error)}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Tokens.
|
|
60
|
+
const tokensText = files.get('tokens.json');
|
|
61
|
+
if (tokensText === undefined) {
|
|
62
|
+
add('tokens.json', 'warning', 'No tokens.json: Colors, Typography and Spacing show empty.');
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
try {
|
|
66
|
+
const { notes } = normalizeTokens(JSON.parse(tokensText));
|
|
67
|
+
for (const note of notes)
|
|
68
|
+
add('tokens.json', note.level, note.message);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
add('tokens.json', 'error', `tokens.json is not valid JSON: ${String(error)}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// Bundle.
|
|
75
|
+
const bundle = files.get('components/bundle.js');
|
|
76
|
+
if (bundle !== undefined) {
|
|
77
|
+
if (/<\/script/i.test(bundle) || /<!--/.test(bundle))
|
|
78
|
+
add('components/bundle.js', 'warning', 'bundle.js contains "</script" or "<!--"; consumers that inline it will break.');
|
|
79
|
+
if (/^\s*(import|export)\s/m.test(bundle))
|
|
80
|
+
add('components/bundle.js', 'error', 'bundle.js must be one classic script: no import or export.');
|
|
81
|
+
const header = parseBundleHeader(bundle);
|
|
82
|
+
if (header?.namespace && namespace && header.namespace !== namespace) {
|
|
83
|
+
add('components/bundle.js', 'warning', `Bundle header namespace "${header.namespace}" differs from the index namespace "${namespace}".`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Every file.
|
|
87
|
+
for (const [path, content] of files) {
|
|
88
|
+
const problem = pathProblem(path);
|
|
89
|
+
if (problem)
|
|
90
|
+
add(path, 'error', `Path ${problem}.`);
|
|
91
|
+
const role = fileRole(path);
|
|
92
|
+
const cap = CAPS[role];
|
|
93
|
+
if (cap !== undefined && new TextEncoder().encode(content).length > cap)
|
|
94
|
+
add(path, 'error', `Larger than ${Math.round(cap / 1024)} KB.`);
|
|
95
|
+
if (role === 'component-preview') {
|
|
96
|
+
if (!parseCardMarker(content))
|
|
97
|
+
add(path, 'warning', 'Line 1 should be the <!-- @dsCard group="…" height=N --> marker.');
|
|
98
|
+
if (PREVIEW_EMBEDS.test(content))
|
|
99
|
+
add(path, 'error', 'Previews may not use iframe, frame, object, embed, portal or noscript.');
|
|
100
|
+
for (const match of content.matchAll(/(?:src|href)\s*=\s*["'](https?:\/\/[^"']+)["']/gi)) {
|
|
101
|
+
const url = match[1];
|
|
102
|
+
if (/fonts\.(googleapis|gstatic)\.com/.test(url))
|
|
103
|
+
continue;
|
|
104
|
+
if (!/@\d+\.\d+\.\d+|\/\d+\.\d+\.\d+\//.test(url))
|
|
105
|
+
add(path, 'warning', `Pin ${url.slice(0, 80)} to an exact version.`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (path.startsWith('components/') && role === 'other') {
|
|
109
|
+
const folder = path.split('/')[1];
|
|
110
|
+
if (!COMPONENT_NAME.test(folder) && folder !== 'lib' && folder !== 'src')
|
|
111
|
+
add(path, 'warning', `components/${folder} is not a JS identifier; it is not a component.`);
|
|
112
|
+
}
|
|
113
|
+
if (role === 'font' && !/\.(woff2?|ttf|otf|ttc)$/i.test(path))
|
|
114
|
+
add(path, 'warning', 'fonts/ holds font binaries only.');
|
|
115
|
+
}
|
|
116
|
+
const { overflow } = listSections(files);
|
|
117
|
+
if (overflow.length)
|
|
118
|
+
add(overflow[0], 'info', `More than ${MAX_SECTIONS} sections; ${overflow.length} stay files.`);
|
|
119
|
+
if (!files.has('components/Cover/preview.html'))
|
|
120
|
+
add('components/Cover/preview.html', 'info', 'No cover: Overview opens with the brand book.');
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WCAG 2 contrast between color tokens.
|
|
3
|
+
*
|
|
4
|
+
* Which tokens are text and which are backgrounds is read from their names
|
|
5
|
+
* (`text-*`, `fg`, `ink`, `on-*` against `bg-*`, `surface`, `canvas`…):
|
|
6
|
+
* that is how design systems name them, and it keeps the page from showing
|
|
7
|
+
* hundreds of meaningless pairs. Parsing arbitrary CSS colors is left to
|
|
8
|
+
* the caller (a browser canvas parses every syntax); only hex is parsed here.
|
|
9
|
+
*/
|
|
10
|
+
/** An sRGB color, channels 0–255, alpha 0–1. */
|
|
11
|
+
export interface Rgb {
|
|
12
|
+
r: number;
|
|
13
|
+
g: number;
|
|
14
|
+
b: number;
|
|
15
|
+
a: number;
|
|
16
|
+
}
|
|
17
|
+
/** WCAG thresholds. */
|
|
18
|
+
export declare const CONTRAST: {
|
|
19
|
+
/** Body text. */
|
|
20
|
+
readonly text: 4.5;
|
|
21
|
+
/** Large text, borders, focus rings, icons. */
|
|
22
|
+
readonly large: 3;
|
|
23
|
+
};
|
|
24
|
+
/** Parses `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`. */
|
|
25
|
+
export declare function parseHex(value: string): Rgb | null;
|
|
26
|
+
/** Relative luminance of an opaque color. */
|
|
27
|
+
export declare function luminance({ r, g, b }: Rgb): number;
|
|
28
|
+
/** Composites a translucent color over an opaque one. */
|
|
29
|
+
export declare function over(top: Rgb, bottom: Rgb): Rgb;
|
|
30
|
+
/** Contrast ratio (1–21) of a foreground on a background; a translucent foreground is composited first. */
|
|
31
|
+
export declare function contrastRatio(foreground: Rgb, background: Rgb): number;
|
|
32
|
+
/** The rating of a ratio for body text. */
|
|
33
|
+
export declare function contrastGrade(ratio: number): 'AAA' | 'AA' | 'AA large' | 'fail';
|
|
34
|
+
/** A foreground/background pair worth checking. */
|
|
35
|
+
export interface ContrastPair {
|
|
36
|
+
foreground: string;
|
|
37
|
+
background: string;
|
|
38
|
+
/** Large text, icon or border: the 3:1 threshold applies. */
|
|
39
|
+
large: boolean;
|
|
40
|
+
}
|
|
41
|
+
/** Infers the pairs to check from token names. */
|
|
42
|
+
export declare function contrastPairs(names: readonly string[]): ContrastPair[];
|
package/src/contrast.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WCAG 2 contrast between color tokens.
|
|
3
|
+
*
|
|
4
|
+
* Which tokens are text and which are backgrounds is read from their names
|
|
5
|
+
* (`text-*`, `fg`, `ink`, `on-*` against `bg-*`, `surface`, `canvas`…):
|
|
6
|
+
* that is how design systems name them, and it keeps the page from showing
|
|
7
|
+
* hundreds of meaningless pairs. Parsing arbitrary CSS colors is left to
|
|
8
|
+
* the caller (a browser canvas parses every syntax); only hex is parsed here.
|
|
9
|
+
*/
|
|
10
|
+
/** WCAG thresholds. */
|
|
11
|
+
export const CONTRAST = {
|
|
12
|
+
/** Body text. */
|
|
13
|
+
text: 4.5,
|
|
14
|
+
/** Large text, borders, focus rings, icons. */
|
|
15
|
+
large: 3,
|
|
16
|
+
};
|
|
17
|
+
/** Parses `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`. */
|
|
18
|
+
export function parseHex(value) {
|
|
19
|
+
const match = /^#([0-9a-f]{3,8})$/i.exec(value.trim());
|
|
20
|
+
if (!match || ![3, 4, 6, 8].includes(match[1].length))
|
|
21
|
+
return null;
|
|
22
|
+
let hex = match[1];
|
|
23
|
+
if (hex.length <= 4)
|
|
24
|
+
hex = [...hex].map((c) => c + c).join('');
|
|
25
|
+
const n = (i) => parseInt(hex.slice(i, i + 2), 16);
|
|
26
|
+
return { r: n(0), g: n(2), b: n(4), a: hex.length === 8 ? n(6) / 255 : 1 };
|
|
27
|
+
}
|
|
28
|
+
/** Relative luminance of an opaque color. */
|
|
29
|
+
export function luminance({ r, g, b }) {
|
|
30
|
+
const channel = (c) => {
|
|
31
|
+
const s = c / 255;
|
|
32
|
+
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
33
|
+
};
|
|
34
|
+
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
|
|
35
|
+
}
|
|
36
|
+
/** Composites a translucent color over an opaque one. */
|
|
37
|
+
export function over(top, bottom) {
|
|
38
|
+
const a = top.a;
|
|
39
|
+
return { r: top.r * a + bottom.r * (1 - a), g: top.g * a + bottom.g * (1 - a), b: top.b * a + bottom.b * (1 - a), a: 1 };
|
|
40
|
+
}
|
|
41
|
+
/** Contrast ratio (1–21) of a foreground on a background; a translucent foreground is composited first. */
|
|
42
|
+
export function contrastRatio(foreground, background) {
|
|
43
|
+
const bg = background.a < 1 ? over(background, { r: 255, g: 255, b: 255, a: 1 }) : background;
|
|
44
|
+
const fg = foreground.a < 1 ? over(foreground, bg) : foreground;
|
|
45
|
+
const [hi, lo] = [luminance(fg), luminance(bg)].sort((x, y) => y - x);
|
|
46
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
47
|
+
}
|
|
48
|
+
/** The rating of a ratio for body text. */
|
|
49
|
+
export function contrastGrade(ratio) {
|
|
50
|
+
if (ratio >= 7)
|
|
51
|
+
return 'AAA';
|
|
52
|
+
if (ratio >= CONTRAST.text)
|
|
53
|
+
return 'AA';
|
|
54
|
+
if (ratio >= CONTRAST.large)
|
|
55
|
+
return 'AA large';
|
|
56
|
+
return 'fail';
|
|
57
|
+
}
|
|
58
|
+
const FOREGROUND = /(^|[-_.])(text|fg|foreground|ink|on|content|label|heading|body|icon|link)([-_.]|$)/i;
|
|
59
|
+
const BACKGROUND = /(^|[-_.])(bg|background|surface|canvas|paper|base|page|card|panel|fill|container)([-_.]|$)/i;
|
|
60
|
+
/** `on-primary` pairs with `primary`. */
|
|
61
|
+
const ON = /^on[-_.](.+)$/i;
|
|
62
|
+
/** Most pairs shown per theme. */
|
|
63
|
+
const MAX_PAIRS = 40;
|
|
64
|
+
/** Infers the pairs to check from token names. */
|
|
65
|
+
export function contrastPairs(names) {
|
|
66
|
+
const pairs = [];
|
|
67
|
+
const seen = new Set();
|
|
68
|
+
const push = (foreground, background) => {
|
|
69
|
+
const key = `${foreground}\u0000${background}`;
|
|
70
|
+
if (foreground === background || seen.has(key) || pairs.length >= MAX_PAIRS)
|
|
71
|
+
return;
|
|
72
|
+
seen.add(key);
|
|
73
|
+
pairs.push({ foreground, background, large: /(^|[-_.])(icon|border|ring|focus|outline|heading|display)([-_.]|$)/i.test(foreground) });
|
|
74
|
+
};
|
|
75
|
+
// `on-x` against `x` first: the most explicit pairing a system declares.
|
|
76
|
+
for (const name of names) {
|
|
77
|
+
const target = ON.exec(name)?.[1];
|
|
78
|
+
if (target && names.includes(target))
|
|
79
|
+
push(name, target);
|
|
80
|
+
}
|
|
81
|
+
const foregrounds = names.filter((name) => FOREGROUND.test(name) && !BACKGROUND.test(name) && !ON.test(name));
|
|
82
|
+
const backgrounds = names.filter((name) => BACKGROUND.test(name) && !FOREGROUND.test(name));
|
|
83
|
+
for (const fg of foregrounds)
|
|
84
|
+
for (const bg of backgrounds)
|
|
85
|
+
push(fg, bg);
|
|
86
|
+
return pairs;
|
|
87
|
+
}
|
package/src/edit.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Edits of a raw `tokens.json`, as the editor page makes them.
|
|
3
|
+
*
|
|
4
|
+
* The page edits the file the agent wrote, not the normalized model: keys
|
|
5
|
+
* this page does not understand (`meta`, a family's `note`, a token's
|
|
6
|
+
* extra fields) survive every edit. Each function returns a new object and
|
|
7
|
+
* never mutates its input, so the caller can keep the old one for undo.
|
|
8
|
+
*/
|
|
9
|
+
/** A parsed `tokens.json` (any JSON object). */
|
|
10
|
+
export type RawTokens = Record<string, unknown>;
|
|
11
|
+
/** Families whose tokens are `{name, value, usage}` lists. */
|
|
12
|
+
export type ListFamily = string;
|
|
13
|
+
/** A token entry as stored. */
|
|
14
|
+
export interface RawEntry {
|
|
15
|
+
name: string;
|
|
16
|
+
value?: unknown;
|
|
17
|
+
usage?: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
/** Parses `tokens.json`; an empty or missing file is an empty object. */
|
|
21
|
+
export declare function parseTokensFile(text: string | undefined): {
|
|
22
|
+
raw: RawTokens;
|
|
23
|
+
error: string | null;
|
|
24
|
+
};
|
|
25
|
+
/** Writes `tokens.json` the way agents are asked to: two-space indent, trailing newline. */
|
|
26
|
+
export declare function serializeTokens(raw: RawTokens): string;
|
|
27
|
+
/** The token list of a family (empty when absent or malformed). */
|
|
28
|
+
export declare function familyEntries(raw: RawTokens, family: ListFamily): RawEntry[];
|
|
29
|
+
/** The theme ids of the color family, first theme first. */
|
|
30
|
+
export declare function themeIds(raw: RawTokens): string[];
|
|
31
|
+
/** Merges fields into one entry. A field set to `undefined` is removed. */
|
|
32
|
+
export declare function updateEntry(raw: RawTokens, family: ListFamily, name: string, fields: Partial<RawEntry>): RawTokens;
|
|
33
|
+
/**
|
|
34
|
+
* Sets a color or shadow value. With a theme id, only that theme changes:
|
|
35
|
+
* a single value becomes a per-theme map (the old value stays the first
|
|
36
|
+
* theme's), and a map whose themes all agree collapses back to one value.
|
|
37
|
+
*/
|
|
38
|
+
export declare function setThemedValue(raw: RawTokens, family: 'color' | 'shadow', name: string, value: string, theme: string | null): RawTokens;
|
|
39
|
+
/** Adds a token at the end of a family (creating the family if needed). */
|
|
40
|
+
export declare function addEntry(raw: RawTokens, family: ListFamily, entry: RawEntry): RawTokens;
|
|
41
|
+
/** Removes a token. */
|
|
42
|
+
export declare function removeEntry(raw: RawTokens, family: ListFamily, name: string): RawTokens;
|
|
43
|
+
/** Renames a token; color and shadow aliases (`{old}`) follow it. */
|
|
44
|
+
export declare function renameEntry(raw: RawTokens, family: ListFamily, from: string, to: string): RawTokens;
|
|
45
|
+
/** Moves a token to `index` within its family. */
|
|
46
|
+
export declare function moveEntry(raw: RawTokens, family: ListFamily, name: string, index: number): RawTokens;
|
|
47
|
+
/** Merges fields into a type style found by name. A field set to `undefined` is removed. */
|
|
48
|
+
export declare function updateStyle(raw: RawTokens, name: string, fields: Record<string, unknown>): RawTokens;
|
|
49
|
+
/** Sets a family note (the text above a family's section). */
|
|
50
|
+
export declare function setFamilyNote(raw: RawTokens, family: ListFamily, note: string): RawTokens;
|
|
51
|
+
/** A token name not yet used in `family`, built from `stem`. */
|
|
52
|
+
export declare function freeTokenName(raw: RawTokens, family: ListFamily, stem: string): string;
|