Skip to content

Commit

Permalink
Designer: Finished migration away from Token Definition wrapper (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
bheston authored Nov 14, 2024
1 parent 9fadfa1 commit 51defb0
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 244 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Designer: Finished migration away from Token Definition wrapper",
"packageName": "@adaptive-web/adaptive-ui-designer-core",
"email": "[email protected]",
"dependentChangeType": "patch"
}
4 changes: 2 additions & 2 deletions packages/adaptive-ui-designer-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export {
} from "./model.js";
export { mapReplacer, mapReviver, deserializeMap, serializeMap } from "./serialization.js";
export { State, StatesState, PluginNode, focusIndicatorNodeName, } from "./node.js";
export { DesignTokenDefinition, DesignTokenRegistry, FormControlId } from "./registry/design-token-registry.js";
export { nameToTitle, registerAppliableTokens, registerTokens } from "./registry/recipes.js";
export { AdaptiveDesignToken, DesignTokenRegistry } from "./registry/design-token-registry.js";
export { registerAppliableTokens, registerTokens } from "./registry/recipes.js";
89 changes: 0 additions & 89 deletions packages/adaptive-ui-designer-core/src/registry/custom-recipes.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,82 +1,42 @@
import { DesignToken, ValuesOf } from "@microsoft/fast-foundation";
import { StyleProperty } from "@adaptive-web/adaptive-ui";
import { CSSDesignToken, DesignToken } from "@microsoft/fast-foundation";
import { DesignTokenMetadata, StyleProperty } from "@adaptive-web/adaptive-ui";

export const FormControlId = {
text: "text",
color: "color",
} as const;

export type FormControlId = ValuesOf<typeof FormControlId>;

/**
* Defines a generic design token.
*/
export interface DesignTokenDefinition {
/**
* Display title for organizing design token sets.
*/
groupTitle?: string;

/**
* Display title of the design token.
*/
title: string;

/**
* Unique ID for the design token.
*/
id: string;

/**
* Target style properties for the design token.
*/
intendedFor?: StyleProperty[];

/**
* Type of form control to edit this value. Following convention from fast-tooling.
*/
formControlId?: FormControlId;

/**
* Underlying {@link DesignToken} for the plugin definition.
*/
token: DesignToken<any>;
}
export type AdaptiveDesignToken = (DesignToken<any> | CSSDesignToken<any>) & DesignTokenMetadata;

export class DesignTokenRegistry {
private _entries: { [id: string]: DesignTokenDefinition } = {};
private _entries: { [id: string]: AdaptiveDesignToken } = {};

/**
* Registers a new design token definition.
* @param designToken The design token definition to register.
*/
public register(designToken: DesignTokenDefinition): void {
const { id } = designToken;
public register(designToken: AdaptiveDesignToken): void {
const { name } = designToken;

if (this.isRegistered(id)) {
if (this.isRegistered(name)) {
throw new Error(
`Design token ${id} has already been registered. You must unregister the design token before registering with that ID.`
`Design token ${name} has already been registered. You must unregister the design token before registering with that ID.`
);
} else {
this._entries[id] = designToken;
this._entries[name] = designToken;
}
}

/**
* Unregisters a design token definition.
* @param id The ID of the design token definition to unregister.
* @param name The ID of the design token definition to unregister.
*/
public unregister(id: string): void {
delete this._entries[id];
public unregister(name: string): void {
delete this._entries[name];
}

/**
* Gets a design token definition by ID.
* @param id The ID of the design token definition.
* @param name The ID of the design token definition.
*/
public get(id: string): DesignTokenDefinition | null {
if (this.isRegistered(id)) {
return this._entries[id];
public get(name: string): AdaptiveDesignToken | null {
if (this.isRegistered(name)) {
return this._entries[name];
}

return null;
Expand All @@ -85,23 +45,23 @@ export class DesignTokenRegistry {
/**
* Gets all entries in this registry.
*/
public get entries(): DesignTokenDefinition[] {
public get entries(): AdaptiveDesignToken[] {
return Object.values(this._entries);
}

/**
* Determines if the design token definition has been registered.
* @param id The ID of the design token definition.
* @param name The ID of the design token definition.
*/
public isRegistered(id: string): boolean {
return this._entries.hasOwnProperty(id);
public isRegistered(name: string): boolean {
return this._entries.hasOwnProperty(name);
}

/**
* Returns all entries that apply to a given style property type
* @param target the style property type to return entries of
*/
public find(target: StyleProperty): DesignTokenDefinition[] {
public find(target: StyleProperty): AdaptiveDesignToken[] {
return Object.values(this._entries).filter(value => value.intendedFor?.includes(target));
}
}
}
74 changes: 19 additions & 55 deletions packages/adaptive-ui-designer-core/src/registry/recipes.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import { sentenceCase } from "change-case";
import type { DesignToken } from "@microsoft/fast-foundation";
import {
Color,
densityAdjustmentUnits,
DesignTokenMetadata,
DesignTokenType,
TypedCSSDesignToken
} from "@adaptive-web/adaptive-ui"
import { densityAdjustmentUnits } from "@adaptive-web/adaptive-ui"
import {
accentBaseColor,
accentFillDiscernible,
Expand Down Expand Up @@ -154,13 +146,12 @@ import {
warningStrokeSubtle,
wcagContrastLevel,
} from "@adaptive-web/adaptive-ui/reference";
import { DesignTokenDefinition, DesignTokenRegistry, FormControlId } from "./design-token-registry.js";
import { blackOrWhiteDiscernibleRest, blackOrWhiteReadableRest, docBaseColor, docFill, docForeground } from "./custom-recipes.js";
import { AdaptiveDesignToken, DesignTokenRegistry } from "./design-token-registry.js";

/**
* A collection of DesignTokens for adding to a {@link DesignTokenRegistry}.
*/
type DesignTokenStore<T = any> = Array<DesignToken<T> & DesignTokenMetadata>;
type DesignTokenStore = Array<AdaptiveDesignToken>;

const designTokens: DesignTokenStore = [
accentBaseColor,
Expand All @@ -175,7 +166,6 @@ const designTokens: DesignTokenStore = [
layerFillHoverDelta,
layerFillActiveDelta,
layerFillFocusDelta,
docBaseColor,
wcagContrastLevel,
densityAdjustmentUnits,
densityControl.horizontalPaddingUnits,
Expand Down Expand Up @@ -203,7 +193,7 @@ const designTokens: DesignTokenStore = [
strokeStrongRestDelta,
];

const colorTokens: DesignTokenStore<Color> = [
const colorTokens: DesignTokenStore = [
// Layer
layerFillFixedMinus4,
layerFillFixedMinus3,
Expand Down Expand Up @@ -297,19 +287,14 @@ const colorTokens: DesignTokenStore<Color> = [
neutralStrokeDiscernible.rest,
neutralStrokeReadable.rest,
neutralStrokeStrong.rest,
// Custom
blackOrWhiteDiscernibleRest,
blackOrWhiteReadableRest,
docForeground,
docFill,
];

const strokeWidthTokens: DesignTokenStore = [
strokeThickness,
focusStrokeThickness,
];

const densityTokens: DesignTokenStore<string> = [
const densityTokens: DesignTokenStore = [
densityControl.horizontalPadding,
densityControl.verticalPadding,
densityControl.horizontalGap,
Expand All @@ -324,7 +309,7 @@ const densityTokens: DesignTokenStore<string> = [
densityLayer.verticalGap,
];

const cornerRadiusTokens: DesignTokenStore<string> = [
const cornerRadiusTokens: DesignTokenStore = [
cornerRadiusControl,
cornerRadiusLayer,
];
Expand Down Expand Up @@ -366,34 +351,13 @@ const effectsTokens: DesignTokenStore = [
elevationDialog,
];

export function nameToTitle(name: string): string {
const base = name.replace(/-/g, ' ').replace(/density_/, '');
return sentenceCase(base);
}

function registerStore<T>(
store: DesignTokenStore<T>,
groupTitle: string | undefined, // Phasing this out. Currently only used on the "Design Tokens" tab.
function registerStore(
store: DesignTokenStore,
registry: DesignTokenRegistry
): void {
store.forEach((token) => {
// console.log("registerStore", token);

// Handle legacy non-hierarchical format for `custom-recipes.ts`.
const title = nameToTitle(token.name.indexOf(".") > -1 ? token.name.split(".").slice(1).join("-") : token.name);
const intendedFor = (token instanceof TypedCSSDesignToken ? (token as TypedCSSDesignToken<any>).intendedFor : undefined);
const formControlId = token.type === DesignTokenType.color ? FormControlId.color : FormControlId.text;

const definition: DesignTokenDefinition = {
id: token.name,
title,
groupTitle,
intendedFor,
formControlId,
token,
};

registry.register(definition);
registry.register(token);
});
}

Expand All @@ -403,18 +367,18 @@ function registerStore<T>(
// For now we've grouped the color tokens since by default those are all recipes/derived.

export const registerTokens = (registry: DesignTokenRegistry) => {
registerStore(designTokens, "Global tokens", registry);
registerStore(designTokens, registry);
// This could be optimized, but some tokens are intended to be modified as well as applied as style properties.
registerStore(strokeWidthTokens, "Stroke width", registry);
registerStore(cornerRadiusTokens, "Corner radius", registry);
registerStore(textTokens, "Text", registry);
registerStore(effectsTokens, "Effects", registry);
registerStore(strokeWidthTokens, registry);
registerStore(cornerRadiusTokens, registry);
registerStore(textTokens, registry);
registerStore(effectsTokens, registry);
};

export const registerAppliableTokens = (registry: DesignTokenRegistry) => {
registerStore(colorTokens, undefined, registry);
registerStore(strokeWidthTokens, undefined, registry);
registerStore(densityTokens, undefined, registry);
registerStore(cornerRadiusTokens, undefined, registry);
registerStore(textTokens, undefined, registry);
registerStore(colorTokens, registry);
registerStore(strokeWidthTokens, registry);
registerStore(densityTokens, registry);
registerStore(cornerRadiusTokens, registry);
registerStore(textTokens, registry);
};
Loading

0 comments on commit 51defb0

Please sign in to comment.