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

chore(ui): make theme code ts strict #3312

Merged
merged 6 commits into from
Jan 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/ui/src/theme/createTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function createTheme(
// deepExtend is an internal Style Dictionary method
// that performs a deep merge on n objects. We could change
// this to another 3p deep merge solution too.
const mergedTheme: DefaultTheme = deepExtend([{}, DefaultTheme, theme]);
const mergedTheme = deepExtend<DefaultTheme>([{}, DefaultTheme, theme]);

// Setting up the tokens. This is similar to what Style Dictionary
// does. At the end of this, each token should have:
Expand All @@ -56,7 +56,7 @@ export function createTheme(
let cssText =
`[data-amplify-theme="${name}"] {\n` +
flattenProperties(tokens)
.map((token) => `${token.name}: ${token.value};`)
.map((token: WebDesignToken) => `${token.name}: ${token.value};`)
.join('\n') +
`\n}\n`;

Expand All @@ -74,7 +74,7 @@ export function createTheme(
setupToken,
});
const customProperties = flattenProperties(tokens)
.map((token) => `${token.name}: ${token.value};`)
.map((token: WebDesignToken) => `${token.name}: ${token.value};`)
.join('\n');
// Overrides can have a selector, media query, breakpoint, or color mode
// for creating the selector
Expand Down
27 changes: 16 additions & 11 deletions packages/ui/src/theme/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,29 @@ const SHADOW_PROPERTIES: ShadowPropertyKey[] = [
'color',
];

function referenceValue(value: string) {
function referenceValue(value?: string) {
if (!value) return '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (usesReference(value)) {
const path = value.replace(/\{|\}/g, '').replace('.value', '').split('.');
return `var(--${cssNameTransform({ path })})`;
}
return value;
}

export function cssValue(token: DesignToken<{ value: unknown }>) {
export function cssValue(token: BaseDesignToken): string | number {
const { value } = token;
if (isString(value)) {
return referenceValue(value);
}

if (isObject(value)) {
if ('offsetX' in value) {
return SHADOW_PROPERTIES.map((property) =>
if (isShadowToken(value)) {
return SHADOW_PROPERTIES.map((property) => {
return referenceValue(
// lookup property against `token` first for custom non-nested value, then lookup
// property against `value` for design token value, default to empty string
referenceValue(token[property] ?? value[property] ?? '')
).join(' ');
}
// property against `value` for design token value
isShadowToken(token) ? token[property] : value[property]
);
}).join(' ');
}

return value;
Expand All @@ -72,8 +73,12 @@ export function isDesignToken(value: unknown): value is WebDesignToken {
return isObject(value) && has(value, 'value');
}

export function isShadowToken(value: unknown): value is ShadowValue {
return isObject(value) && has(value, 'offsetX');
}

type SetupTokensProps = {
tokens: Record<string | number, any>;
tokens?: Record<string | number, any>;
path?: Array<string>;
setupToken: SetupToken;
};
Expand Down Expand Up @@ -101,7 +106,7 @@ export function setupTokens({
return setupToken({ token: tokens as BaseDesignToken, path });
}

const output = {};
const output: Record<string, any> = {};

for (const name in tokens) {
if (has(tokens, name)) {
Expand Down
21 changes: 21 additions & 0 deletions packages/ui/src/types/style-dictionary.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.