Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating palette base color token type for better type safety #222

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Updating palette base color token type for better type safety",
"packageName": "@adaptive-web/adaptive-ui",
"email": "47367562+bheston@users.noreply.github.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Updating palette base color token type for better type safety",
"packageName": "@adaptive-web/adaptive-ui-designer-core",
"email": "47367562+bheston@users.noreply.github.com",
"dependentChangeType": "patch"
}
3 changes: 2 additions & 1 deletion examples/use-adaptive-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
layerFillFixedBase
} from "@adaptive-web/adaptive-ui/reference";
import { DesignToken, FASTRadioGroup, FASTSwitch } from "@microsoft/fast-foundation";
import { Color } from '@adaptive-web/adaptive-ui';

// This must be called during initialization for the Design Tokens to be setup so the component styling is applied.
DesignToken.registerDefaultStyleTarget();
Expand All @@ -47,5 +48,5 @@ document.getElementById("accentColor").onchange = function(event) {
const value = (event.target as FASTRadioGroup).value;
console.log("accentColor change", value);
// This Design Token causes the accent palette to update, including any components styled with recipes based on that palette.
accentBaseColor.withDefault("#" + value);
accentBaseColor.withDefault(Color.parse("#" + value));
};
2 changes: 1 addition & 1 deletion packages/adaptive-ui-designer-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export {
AppliedDesignTokens,
DesignTokenValues
} from "./model.js";
export { mapReplacer, mapReviver, deserializeMap } from "./serialization.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";
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ColorRecipeParams,
contrastSwatch,
createNonCss,
createTokenColor,
createTokenColorRecipe,
createTokenColorRecipeValue,
createTokenSwatch,
Expand All @@ -11,6 +10,7 @@ import {
PaletteRGB,
StyleProperty,
stylePropertyBorderFillAll,
Swatch,
} from "@adaptive-web/adaptive-ui";
import {
blackOrWhiteDiscernibleRecipe,
Expand All @@ -25,7 +25,7 @@ import { DesignTokenResolver } from "@microsoft/fast-foundation";

// Local recipes for use in documentation files.

export const docBaseColor = createTokenColor("doc-base-color").withDefault("#E1477E");
export const docBaseColor = createTokenSwatch("doc-base-color").withDefault(Swatch.parse("#E1477E")!);

export const docPalette = createNonCss<Palette>("doc-palette").withDefault(
(resolve: DesignTokenResolver) =>
Expand Down
19 changes: 17 additions & 2 deletions packages/adaptive-ui-designer-core/src/registry/recipes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { sentenceCase } from "change-case";
import type { DesignToken } from "@microsoft/fast-foundation";
import {
Color,
densityAdjustmentUnits,
DesignTokenMetadata,
DesignTokenType,
Swatch,
TypedCSSDesignToken
} from "@adaptive-web/adaptive-ui"
import {
Expand Down Expand Up @@ -79,6 +79,7 @@ import {
labelFontFamily,
labelFontStyle,
labelFontWeight,
layerFillActiveDelta,
layerFillBaseLuminance,
layerFillFixedBase,
layerFillFixedMinus1,
Expand All @@ -89,7 +90,10 @@ import {
layerFillFixedPlus2,
layerFillFixedPlus3,
layerFillFixedPlus4,
layerFillFocusDelta,
layerFillHoverDelta,
layerFillInteractive,
layerFillRestDelta,
neutralBaseColor,
neutralFillDiscernible,
neutralFillIdeal,
Expand Down Expand Up @@ -167,6 +171,10 @@ const designTokens: DesignTokenStore = [
warningBaseColor,
criticalBaseColor,
layerFillBaseLuminance,
layerFillRestDelta,
layerFillHoverDelta,
layerFillActiveDelta,
layerFillFocusDelta,
docBaseColor,
wcagContrastLevel,
densityAdjustmentUnits,
Expand Down Expand Up @@ -195,7 +203,7 @@ const designTokens: DesignTokenStore = [
strokeStrongRestDelta,
];

const colorTokens: DesignTokenStore<Swatch> = [
const colorTokens: DesignTokenStore<Color> = [
// Layer
layerFillFixedMinus4,
layerFillFixedMinus3,
Expand All @@ -208,36 +216,43 @@ const colorTokens: DesignTokenStore<Swatch> = [
layerFillFixedPlus4,
layerFillInteractive.rest,
// Fill
accentBaseColor,
accentFillStealth.rest,
accentFillSubtle.rest,
accentFillIdeal.rest,
accentFillDiscernible.rest,
accentFillReadable.rest,
highlightBaseColor,
highlightFillStealth.rest,
highlightFillSubtle.rest,
highlightFillIdeal.rest,
highlightFillDiscernible.rest,
highlightFillReadable.rest,
criticalBaseColor,
criticalFillStealth.rest,
criticalFillSubtle.rest,
criticalFillIdeal.rest,
criticalFillDiscernible.rest,
criticalFillReadable.rest,
warningBaseColor,
warningFillStealth.rest,
warningFillSubtle.rest,
warningFillIdeal.rest,
warningFillDiscernible.rest,
warningFillReadable.rest,
successBaseColor,
successFillStealth.rest,
successFillSubtle.rest,
successFillIdeal.rest,
successFillDiscernible.rest,
successFillReadable.rest,
infoBaseColor,
infoFillStealth.rest,
infoFillSubtle.rest,
infoFillIdeal.rest,
infoFillDiscernible.rest,
infoFillReadable.rest,
neutralBaseColor,
neutralFillStealth.rest,
neutralFillSubtle.rest,
neutralFillIdeal.rest,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { type Color, modeLrgb, modeRgb, parse, type Rgb, useMode, wcagLuminance } from "culori/fn";
import { Shadow, StyleProperty } from "@adaptive-web/adaptive-ui";
import { Controller, STYLE_REMOVE } from "@adaptive-web/adaptive-ui-designer-core";
import { AppliedStyleModules, AppliedStyleValues, PluginNodeData } from "@adaptive-web/adaptive-ui-designer-core";
import { focusIndicatorNodeName, PluginNode, State, StatesState } from "@adaptive-web/adaptive-ui-designer-core";
import { AppliedStyleModules, AppliedStyleValues, Controller, focusIndicatorNodeName, PluginNode, PluginNodeData, serializeMap, State, StatesState, STYLE_REMOVE } from "@adaptive-web/adaptive-ui-designer-core";
import { FIGMA_SHARED_DATA_NAMESPACE } from "@adaptive-web/adaptive-ui-designer-figma";
import { colorToRgba, variantBooleanHelper } from "./utility.js";

Expand Down Expand Up @@ -217,11 +215,11 @@ export class FigmaPluginNode extends PluginNode {
});

if (deserializedDesignTokens.size) {
// console.log(" reconciled design tokens", this.debugInfo, deserializedDesignTokens.serialize());
// console.log(" reconciled design tokens", this.debugInfo, serializeMap(deserializedDesignTokens));
}

if (deserializedAppliedDesignTokens.size) {
// console.log(" reconciled applied design tokens", this.debugInfo, deserializedAppliedDesignTokens.serialize());
// console.log(" reconciled applied design tokens", this.debugInfo, serializeMap(deserializedAppliedDesignTokens));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class DesignTokenAdd extends FASTElement {
}

if (this.field) {
this.field.value = this.selectedDesignToken?.token.default;
this.field.value = "" + this.selectedDesignToken?.token.default;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class UIController {
source,
});
} else {
console.error("Token not found:", applied.tokenID, node.name, node.type);
console.error("Token not found:", applied.tokenID, node.name, node.type, node.id, applied.value);
}
} else { // Removed
allApplied.set(target, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ElementViewTemplate, html, repeat } from "@microsoft/fast-element";
import { twoWay } from "@microsoft/fast-element/binding/two-way.js";
import { Color } from "@adaptive-web/adaptive-ui";
import { WcagContrastLevel } from "@adaptive-web/adaptive-ui/reference";
import { ComponentType } from "../../component-type.js";
import { ControlPane } from "./control-pane.js";
Expand Down Expand Up @@ -34,7 +35,7 @@ export function controlPaneTemplate<T extends ControlPane>(): ElementViewTemplat
type="color"
value=${(x) => x.state.neutralColor}
@change=${(x, c) =>
x.state.neutralColor = c.eventTarget<HTMLInputElement>().value
x.state.neutralColor = Color.parse(c.eventTarget<HTMLInputElement>().value)!
}
/>
</div>
Expand All @@ -50,7 +51,7 @@ export function controlPaneTemplate<T extends ControlPane>(): ElementViewTemplat
type="color"
value=${(x) => x.state.accentColor}
@change=${(x, c) =>
x.state.accentColor = c.eventTarget<HTMLInputElement>().value
x.state.accentColor = Color.parse(c.eventTarget<HTMLInputElement>().value)!
}
/>
</div>
Expand All @@ -61,7 +62,7 @@ export function controlPaneTemplate<T extends ControlPane>(): ElementViewTemplat
type="color"
value=${(x) => x.state.highlightColor}
@change=${(x, c) =>
x.state.highlightColor = c.eventTarget<HTMLInputElement>().value
x.state.highlightColor = Color.parse(c.eventTarget<HTMLInputElement>().value)!
}
/>
</div>
Expand Down
15 changes: 8 additions & 7 deletions packages/adaptive-ui-explorer/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import { observable } from "@microsoft/fast-element";
import { Context } from "@microsoft/fast-element/context.js";
import { Color } from "@adaptive-web/adaptive-ui";
import { WcagContrastLevel } from "@adaptive-web/adaptive-ui/reference";
import { ComponentType } from "./component-type.js";

export const State = Context.create<State>("State");
export interface State {
componentType: ComponentType;
neutralColor: string;
accentColor: string;
highlightColor: string;
neutralColor: Color;
accentColor: Color;
highlightColor: Color;
showOnlyLayerBackgrounds: boolean;
wcagContrastLevel: WcagContrastLevel;
disabledState: boolean;
showSwatches: boolean;
}

const PLACEHOLDER_COLOR = "#ff00ff";
const PLACEHOLDER_COLOR = Color.parse("#ff00ff")!;

export class DefaultState implements State {
@observable
public componentType: ComponentType = ComponentType.backplate;

@observable
public neutralColor: string = PLACEHOLDER_COLOR;
public neutralColor: Color = PLACEHOLDER_COLOR;

@observable
public accentColor: string = PLACEHOLDER_COLOR;
public accentColor: Color = PLACEHOLDER_COLOR;

@observable
public highlightColor: string = PLACEHOLDER_COLOR;
public highlightColor: Color = PLACEHOLDER_COLOR;

@observable
public showOnlyLayerBackgrounds: boolean = true;
Expand Down
3 changes: 2 additions & 1 deletion packages/adaptive-ui/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class Color implements RelativeLuminance, CSSDirective {
static parse(color: string): Color | undefined;
get relativeLuminance(): number;
toColorString(): string;
toString: () => string;
}

// @public
Expand Down Expand Up @@ -154,7 +155,7 @@ export function createForegroundSetBySet(foregroundRecipe: TypedDesignToken<Inte
export function createNonCss<T>(name: string): DesignToken<T>;

// @public
export function createTokenColor(name: string, intendedFor?: StyleProperty | StyleProperty[]): TypedCSSDesignToken<string>;
export function createTokenColor(name: string, intendedFor?: StyleProperty | StyleProperty[]): TypedCSSDesignToken<Color>;

// @public
export function createTokenColorRecipe<T = Swatch>(baseName: string, intendedFor: StyleProperty | StyleProperty[], evaluate: ColorRecipeEvaluate<T>): TypedDesignToken<ColorRecipe<T>>;
Expand Down
5 changes: 5 additions & 0 deletions packages/adaptive-ui/src/core/color/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export class Color implements RelativeLuminance, CSSDirective {
return this.color.alpha !== undefined && this.color.alpha < 1 ? formatRgb(this.color) : formatHex(this.color);
}

/**
* {@inheritdoc Color.toColorString}
*/
public toString = this.toColorString;

/**
* Gets the contrast between this Color and another.
*
Expand Down
5 changes: 3 additions & 2 deletions packages/adaptive-ui/src/core/token-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DesignToken } from "@microsoft/fast-foundation";
import { DesignTokenType, TypedCSSDesignToken, TypedDesignToken } from "./adaptive-design-tokens.js";
import { Color } from "./color/color.js";
import { Swatch } from "./color/swatch.js";
import { StyleProperty } from "./modules/types.js";
import { Recipe, RecipeEvaluate } from "./recipes.js";
Expand Down Expand Up @@ -27,8 +28,8 @@ export const { createTyped } = TypedCSSDesignToken;
*
* @public
*/
export function createTokenColor(name: string, intendedFor?: StyleProperty | StyleProperty[]): TypedCSSDesignToken<string> {
return TypedCSSDesignToken.createTyped<string>(name, DesignTokenType.color, intendedFor);
export function createTokenColor(name: string, intendedFor?: StyleProperty | StyleProperty[]): TypedCSSDesignToken<Color> {
return TypedCSSDesignToken.createTyped<Color>(name, DesignTokenType.color, intendedFor);
}

/**
Expand Down
19 changes: 10 additions & 9 deletions packages/adaptive-ui/src/reference/palette.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { DesignTokenResolver } from "@microsoft/fast-foundation";
import { DesignTokenType } from "../core/adaptive-design-tokens.js";
import { Palette, PaletteOkhsl } from "../core/color/index.js";
import { createTokenNonCss } from "../core/token-helpers.js";
import { Color, Palette, PaletteOkhsl } from "../core/color/index.js";
import { createTokenColor, createTokenNonCss } from "../core/token-helpers.js";
import { StyleProperty } from "../core/modules/types.js";

/** @public */
export const neutralBaseColor = createTokenNonCss<string>("color.neutral.base", DesignTokenType.color).withDefault("#808080");
export const neutralBaseColor = createTokenColor("color.neutral.base", StyleProperty.backgroundFill).withDefault(Color.parse("#808080")!);

/** @public */
export const neutralPalette = createTokenNonCss<Palette>("color.neutral.palette", DesignTokenType.palette).withDefault(
Expand All @@ -13,7 +14,7 @@ export const neutralPalette = createTokenNonCss<Palette>("color.neutral.palette"
);

/** @public */
export const accentBaseColor = createTokenNonCss<string>("color.accent.base", DesignTokenType.color).withDefault("#F26C0D");
export const accentBaseColor = createTokenColor("color.accent.base", StyleProperty.backgroundFill).withDefault(Color.parse("#F26C0D")!);

/** @public */
export const accentPalette = createTokenNonCss<Palette>("color.accent.palette", DesignTokenType.palette).withDefault(
Expand All @@ -22,7 +23,7 @@ export const accentPalette = createTokenNonCss<Palette>("color.accent.palette",
);

/** @public */
export const highlightBaseColor = createTokenNonCss<string>("color.highlight.base", DesignTokenType.color).withDefault("#0DA1F2");
export const highlightBaseColor = createTokenColor("color.highlight.base", StyleProperty.backgroundFill).withDefault(Color.parse("#0DA1F2")!);

/** @public */
export const highlightPalette = createTokenNonCss<Palette>("color.highlight.palette", DesignTokenType.palette).withDefault(
Expand All @@ -31,7 +32,7 @@ export const highlightPalette = createTokenNonCss<Palette>("color.highlight.pale
);

/** @public */
export const criticalBaseColor = createTokenNonCss<string>("color.critical.base", DesignTokenType.color).withDefault("#D92635");
export const criticalBaseColor = createTokenColor("color.critical.base", StyleProperty.backgroundFill).withDefault(Color.parse("#D92635")!);

/** @public */
export const criticalPalette = createTokenNonCss<Palette>("color.critical.palette", DesignTokenType.palette).withDefault(
Expand All @@ -40,7 +41,7 @@ export const criticalPalette = createTokenNonCss<Palette>("color.critical.palett
);

/** @public */
export const warningBaseColor = createTokenNonCss<string>("color.warning.base", DesignTokenType.color).withDefault("#D526D9");
export const warningBaseColor = createTokenColor("color.warning.base", StyleProperty.backgroundFill).withDefault(Color.parse("#D526D9")!);

/** @public */
export const warningPalette = createTokenNonCss<Palette>("color.warning.palette", DesignTokenType.palette).withDefault(
Expand All @@ -49,7 +50,7 @@ export const warningPalette = createTokenNonCss<Palette>("color.warning.palette"
);

/** @public */
export const successBaseColor = createTokenNonCss<string>("color.success.base", DesignTokenType.color).withDefault("#6AD926");
export const successBaseColor = createTokenColor("color.success.base", StyleProperty.backgroundFill).withDefault(Color.parse("#6AD926")!);

/** @public */
export const successPalette = createTokenNonCss<Palette>("color.success.palette", DesignTokenType.palette).withDefault(
Expand All @@ -58,7 +59,7 @@ export const successPalette = createTokenNonCss<Palette>("color.success.palette"
);

/** @public */
export const infoBaseColor = createTokenNonCss<string>("color.info.base", DesignTokenType.color).withDefault("#0DA1F2");
export const infoBaseColor = createTokenColor("color.info.base", StyleProperty.backgroundFill).withDefault(Color.parse("#0DA1F2")!);

/** @public */
export const infoPalette = createTokenNonCss<Palette>("color.info.palette", DesignTokenType.palette).withDefault(
Expand Down
Loading