Skip to content

Commit ec6d0c2

Browse files
committed
AUI: Add import capability to component anatomy definitions
1 parent d500a16 commit ec6d0c2

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/adaptive-ui/docs/api-report.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ export interface SerializableAnatomy {
509509
styleRules: SerializableStyleRule[];
510510
}
511511

512+
// @beta (undocumented)
513+
export interface SerializableAnatomyWithImports extends SerializableAnatomy {
514+
// (undocumented)
515+
imports?: string[];
516+
}
517+
512518
// @beta (undocumented)
513519
export type SerializableBooleanCondition = string;
514520

packages/adaptive-ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@microsoft/fast-foundation": "3.0.0-alpha.31",
4343
"commander": "^12.0.0",
4444
"culori": "^3.2.0",
45+
"deepmerge-ts": "^7.1.3",
4546
"glob": "^10.3.10",
4647
"matcher": "^5.0.0",
4748
"postcss": "^8.4.39",

packages/adaptive-ui/src/bin/aui.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as prettier from "prettier";
88
import { ComposableStyles, ElementStyles } from '@microsoft/fast-element';
99
import { CSSDesignToken } from "@microsoft/fast-foundation";
1010
import { Command } from 'commander';
11+
import { deepmerge } from "deepmerge-ts";
1112
import { glob } from "glob";
1213
import postcss, { type Processor} from "postcss";
1314
import postcssMergeLonghand from "postcss-merge-longhand";
@@ -19,6 +20,7 @@ import {
1920
ComponentConditions,
2021
ComponentParts,
2122
SerializableAnatomy,
23+
SerializableAnatomyWithImports,
2224
SerializableBooleanCondition,
2325
SerializableStringCondition,
2426
SerializableStyleRule,
@@ -84,11 +86,27 @@ program.command("compile-styles <glob>")
8486

8587
program.command("compile-json-anatomy <anatomyPath>")
8688
.description("Compile a stylesheet from a JSON anatomy")
87-
.action(async (path: string) => {
88-
const data = (await fsp.readFile(path)).toString();
89+
.action(async (jsonPath: string) => {
90+
const data = (await fsp.readFile(jsonPath)).toString();
8991
await import("../reference/index.js");
9092

91-
const jsonData = JSON.parse(data);
93+
let jsonData = JSON.parse(data) as SerializableAnatomyWithImports;
94+
95+
if (jsonData.imports) {
96+
for (const imp of jsonData.imports) {
97+
const impWithExt = imp.toLowerCase().endsWith(".json") ? imp : `${imp}.json`;
98+
const impFilePath = path.format({ ...path.parse(path.join(path.parse(jsonPath).dir, impWithExt)) });
99+
const impData = (await fsp.readFile(impFilePath)).toString();
100+
const impJsonData = JSON.parse(impData);
101+
// If `parts` are in the import, they are for validation/consistency of that file, but we want to use the parts
102+
// list from the main anatomy definition.
103+
// Consider extending this so imports can add their own known parts.
104+
delete impJsonData.parts;
105+
106+
jsonData = deepmerge(jsonData, impJsonData);
107+
}
108+
}
109+
92110
const compiler = new SheetCompilerImpl();
93111
const sheet = jsonToAUIStyleSheet(jsonData);
94112
const compiledSheet = compiler.compile(sheet);

packages/adaptive-ui/src/core/modules/types.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { StyleRule } from "./styles.js";
77
*
88
* @public
99
*/
10-
export type BooleanCondition = string;
10+
export type BooleanCondition = string;
1111

1212
/**
1313
* The state and selector for a multiple value condition.
@@ -152,7 +152,7 @@ export const Interactivity = {
152152
*
153153
* For instance, a form control.
154154
*/
155-
disabledAttribute: {
155+
disabledAttribute: {
156156
interactive: ":not([disabled])",
157157
disabled: "[disabled]",
158158
} as InteractivityDefinition,
@@ -162,7 +162,7 @@ export const Interactivity = {
162162
*
163163
* For instance, a form control.
164164
*/
165-
disabledClass: {
165+
disabledClass: {
166166
interactive: ":not(.disabled)",
167167
disabled: ".disabled",
168168
} as InteractivityDefinition,
@@ -172,7 +172,7 @@ export const Interactivity = {
172172
*
173173
* For instance, an `<a>` should style as plain text when it doesn't have an `href` attribute.
174174
*/
175-
hrefAttribute: {
175+
hrefAttribute: {
176176
interactive: "[href]",
177177
} as InteractivityDefinition,
178178

@@ -181,7 +181,7 @@ export const Interactivity = {
181181
*
182182
* For instance, cards or list items that are not able to be disabled.
183183
*/
184-
always: {
184+
always: {
185185
interactive: "",
186186
} as InteractivityDefinition,
187187

@@ -193,7 +193,7 @@ export const Interactivity = {
193193
* @remarks
194194
* This is an explicit value representing the default case.
195195
*/
196-
never: {
196+
never: {
197197
} as InteractivityDefinition,
198198
} as const;
199199

@@ -481,7 +481,7 @@ export type StyleRules = Array<StyleRule>;
481481
/**
482482
* @beta
483483
*/
484-
export type SerializableBooleanCondition = string;
484+
export type SerializableBooleanCondition = string;
485485

486486
/**
487487
* @beta
@@ -506,7 +506,7 @@ export interface SerializableStyleRule {
506506
/**
507507
* @beta
508508
*/
509-
export interface SerializableAnatomy{
509+
export interface SerializableAnatomy {
510510
name: string,
511511
context: string,
512512
conditions: Record<string, SerializableCondition>,
@@ -515,3 +515,10 @@ export interface SerializableAnatomy{
515515
focus?: FocusDefinition<any>,
516516
styleRules: SerializableStyleRule[]
517517
}
518+
519+
/**
520+
* @beta
521+
*/
522+
export interface SerializableAnatomyWithImports extends SerializableAnatomy {
523+
imports?: string[]
524+
}

0 commit comments

Comments
 (0)