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

feat: resolve css variables in experiences #596

Merged
merged 2 commits into from May 16, 2024
Merged

Conversation

ryunsong-contentful
Copy link
Contributor

@ryunsong-contentful ryunsong-contentful commented May 2, 2024

Purpose

Add css variable support for Experiences for Design Tokens

Approach

Create an in memory resolved css variables values similar to design tokens. That way the ui can consume the values and store it in its immer store and thus make it accessible throughout the ui webapp

@ryunsong-contentful ryunsong-contentful requested review from a team as code owners May 2, 2024 17:04
Copy link

vercel bot commented May 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
experience-builder-test-app ✅ Ready (Inspect) Visit Preview May 13, 2024 4:02pm

Copy link
Contributor

@chasepoirier chasepoirier left a comment

Choose a reason for hiding this comment

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

LGTM 🥇

Comment on lines +238 to +244
const element = document.createElement('div');
document.body.appendChild(element);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this work with SSR? 🤔

Copy link
Collaborator

@Spring3 Spring3 May 8, 2024

Choose a reason for hiding this comment

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

I would also discourage from trying to affect the DOM from within a plain js function.

I think react would expect it to be done in the useEffect

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is compatible with SSR, @Spring3 can you explain what the difference in using the useEffect does? I'm only temporarily creating it to grab the css values and then deleting the element but am open to hearing your thoughts and follow up on your suggestion.

Comment on lines 251 to 278
if (sizing) {
const rawResolvedValues = getAllCssVariableValues(element, sizing, 'width');
Object.assign(resolvedCssVariables, rawResolvedValues);
}
if (color) {
const rawResolvedValues = getAllCssVariableValues(element, color, 'background-color');
Object.assign(resolvedCssVariables, rawResolvedValues);
}
if (borderRadius) {
const rawResolvedValues = getAllCssVariableValues(element, borderRadius, 'border-radius');
Object.assign(resolvedCssVariables, rawResolvedValues);
}
if (fontSize) {
const rawResolvedValues = getAllCssVariableValues(element, fontSize, 'font-size');
Object.assign(resolvedCssVariables, rawResolvedValues);
}
if (lineHeight) {
const rawResolvedValues = getAllCssVariableValues(element, lineHeight, 'line-height');
Object.assign(resolvedCssVariables, rawResolvedValues);
}
if (letterSpacing) {
const rawResolvedValues = getAllCssVariableValues(element, letterSpacing, 'letter-spacing');
Object.assign(resolvedCssVariables, rawResolvedValues);
}
if (textColor) {
const rawResolvedValues = getAllCssVariableValues(element, textColor, 'color');
Object.assign(resolvedCssVariables, rawResolvedValues);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be refactored to use a loop to reduce some of the repetition. For example:

const cssProperties = [
  { variable: sizing, property: 'width' },
  { variable: color, property: 'background-color' },
  { variable: borderRadius, property: 'border-radius' },
  { variable: fontSize, property: 'font-size' },
  { variable: lineHeight, property: 'line-height' },
  { variable: letterSpacing, property: 'letter-spacing' },
  { variable: textColor, property: 'color' },
];

cssProperties.forEach(({ variable, property }) => {
  if (variable) {
    const rawResolvedValues = getAllCssVariableValues(element, variable, property);
    Object.assign(resolvedCssVariables, rawResolvedValues);
  }
});

@@ -228,7 +228,7 @@ export type RecursiveDesignTokenDefinition = {
};

export type DesignTokensDefinition = {
spacing?: Record<string, string>;
spacing?: Record<string, string> | Record<string, number>;
Copy link
Contributor

Choose a reason for hiding this comment

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

This would also work here:

Suggested change
spacing?: Record<string, string> | Record<string, number>;
spacing?: Record<string, string | number>;

Copy link
Contributor

@primeinteger primeinteger left a comment

Choose a reason for hiding this comment

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

I'm still not sure if the document.createElement approach will work in SSR-land, but approving either way and we can look at that later if it comes up.

@ryunsong-contentful ryunsong-contentful merged commit 07eee5f into development May 16, 2024
9 checks passed
@ryunsong-contentful ryunsong-contentful deleted the ALT-758 branch May 16, 2024 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants