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: add support to Color Themes #176

Merged

Conversation

opauloh
Copy link
Contributor

@opauloh opauloh commented Oct 17, 2022

Hey all

At my team, we have apps that users can switch between Light and Dark Theme, and having the ability to control that type of token on Figma is something we have been really looking for, and this PR introduces a feature that adds support to that!

How it works: on Figma, you can create extra frames under the Color Themes at the same level as the other colors, and under these Frames, you can have color elements that will then get stored in specific object collections under the color token.

Screenshots

Example of how it would like in Figma:

color-themes-demo

Example of how it would like in code:

code

Which in a practical example you could use as follows:

// initialize with default theme
let theme = 'lightTheme';

// Primary action button with white color token and with different primary background color according with theme
let button = `<button style="background: ${colors[theme].primary}; color: ${colors.white}">Primary Action</button>`;

// Background color that changes according to the theme
let panel = `<div style=`color: ${colors[theme].emptyShade}`>Panel background</div>`

Description of the changes made

  • updated the makeColorTokens function to read up to one nested Frame and store Tokens using the frame name
  • updated docs with the Color Themes Support section
  • added test cases for Multiple Themes usage

Checklist

  • I have read the Figmagic Contribution Guideline
  • The PR title summarizes the change

@opauloh
Copy link
Contributor Author

opauloh commented Oct 26, 2022

pinging @mikaelvesavuori

@mikaelvesavuori
Copy link
Owner

Hey @opauloh! Sorry for dropping the ball on this, tons at work right now + new projects coming up.

I am generally happy about this, but the issue—and how I have addressed the need for this from others—is that it should suffice to handle this as a simple "meta object" as needed, by you as the user.

Have you verified that component generation works correctly with this? (Manually ideally, since I don't necessarily trust we have tests for that)

@opauloh
Copy link
Contributor Author

opauloh commented Oct 27, 2022

Hey @opauloh! Sorry for dropping the ball on this, tons at work right now + new projects coming up.

No worries @mikaelvesavuori! That's totally understandable; thanks for letting me know

Have you verified that component generation works correctly with this? (Manually ideally, since I don't necessarily trust we have tests for that)

Yes - Component generation doesn't support multi-themes, but it also doesn't break, let's suppose you have a component with a color that only exists on the colors -> theme frame.

image

image

In this scenario, when attempting to sync with Figma using --syncElements, Figmagic would warn about No matching token! Hard-coding to expected value

image

The component will still be created, just without a token assigned to it the color, since it currently only looks for the Colors frame and not for nested frames.

image

I think this is an acceptable behavior, but I would be happy to add support to components generation on a separate issue / PR. Because there are some nuances, for example, it would need to ensure the same color token exists across all themes, and it would be needed to extend the components to accept a theme prop as well.

for example

interface ButtonProps {
 children: any;
 theme: Theme,
  [propName: string]: {};
}

const Button: React.FC<ButtonProps> = ({ children, theme }) => (
  <ButtonStyled theme={theme} {...props}>{children ? children : "Button text"}</ButtonStyled>
);

And then CSS would be able to refer to the theme like this.

background-color: ${colors[theme]['danger']};

@mikaelvesavuori
Copy link
Owner

Hi again @opauloh! I am happy to merge this if you add a note/warning that the behavior of generated components will be as you told me above in this thread.

Consider if you have bandwidth to look at solving the issue/problem in a later PR and I will look at that as well.

@opauloh
Copy link
Contributor Author

opauloh commented Nov 1, 2022

Hi again @opauloh! I am happy to merge this if you add a note/warning that the behavior of generated components will be as you told me above in this thread.

Consider if you have bandwidth to look at solving the issue/problem in a later PR and I will look at that as well.

Notes updated! I'll open an issue soon on how to proceed with the generation of the components.

@opauloh
Copy link
Contributor Author

opauloh commented Nov 2, 2022

pinging @mikaelvesavuori

@sonarcloud
Copy link

sonarcloud bot commented Nov 2, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@mikaelvesavuori mikaelvesavuori merged commit e4f3939 into mikaelvesavuori:main Nov 2, 2022
@mikaelvesavuori
Copy link
Owner

mikaelvesavuori commented Nov 2, 2022

Hey @opauloh, this is released in 4.5.8 - https://github.com/mikaelvesavuori/figmagic/releases/tag/v4.5.8

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

2 participants