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

807 cannot set a number fontweight in typography #1197

Merged
merged 10 commits into from
Sep 1, 2022

Conversation

swordEdge
Copy link
Contributor

@swordEdge swordEdge commented Aug 19, 2022

We can't set fontWeight with a number between 100 to 900 which is the most common way to set it.

After fixing:
In Figma, there is a match rule between font family and font-weight, so every font weight doesn't work to every font family.
To test this, we can use Montserrat fontfamily which support all font weights from 100 to 900.

@vercel
Copy link

vercel bot commented Aug 19, 2022

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

Name Status Preview Updated
figma-tokens ✅ Ready (Inspect) Visit Preview Sep 1, 2022 at 1:09PM (UTC)
ft-storybook ✅ Ready (Inspect) Visit Preview Sep 1, 2022 at 1:09PM (UTC)

@swordEdge swordEdge linked an issue Aug 19, 2022 that may be closed by this pull request
@github-actions
Copy link
Contributor

github-actions bot commented Aug 19, 2022

Commit SHA:70e27aec559b28f4d6d057c053389ff202d36366

Test coverage results 🧪

Code coverage diff between base branch:next and head branch: 807-cannot-set-a-number-fontweight-in-typography 
Status File % Stmts % Branch % Funcs % Lines
🟢 total 68.19 (0) 60.04 (-0.04) 65.2 (0.08) 68.36 (0)
🟢 src/plugin/helpers.ts 80 (1.05) 93.94 (0.19) 100 (0) 80 (1.05)
🟢 src/plugin/setTextValuesOnTarget.ts 100 (3.57) 86.11 (0.82) 100 (0) 100 (3.57)
✨ 🆕 src/plugin/figmaTransforms/fontWeight.ts 36.36 30 100 36.36

@@ -29,7 +29,7 @@ export default async function setValuesOnNode(
const stylePathPrefix = prefixStylesWithThemeName && activeThemeObject
? activeThemeObject.name
: null;

console.log('values', values);
Copy link
Contributor

Choose a reason for hiding this comment

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

logs

@@ -18,11 +19,12 @@ export default async function setTextValuesOnTarget(target: TextNode | TextStyle
} = value;
const family = fontFamily?.toString() || (target.fontName !== figma.mixed ? target.fontName.family : '');
const style = fontWeight?.toString() || (target.fontName !== figma.mixed ? target.fontName.style : '');
await figma.loadFontAsync({ family, style });
const transformedValue = transformValue(style, 'fontWeights');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we change this so we

  1. First try to set font weight as it is
  2. If that fails try the mapping

Reason is that some (not a lot) fonts have weights actually named '500'

@six7
Copy link
Collaborator

six7 commented Aug 19, 2022

This works great so far!

One more thing that I feel would be really great if we could get in, as that way we'd probably be able to cover a lot more font weights

Some fonts define their weights not like SemiBold but Semi Bold (notice the space between Semi and Bold), or sometimes even DemiBold.

Could we pass in our mapping an array of possible weight names? Then we could extend that with some mappings that map in certain font weights. We'd then try to set each of them, and if there's a match, it will apply that?

In the case of 600 this would mean we'd return ['SemiBold', 'Semi Bold', 'DemiBold']? We could use an array for each of these, as I'm sure we'd need to add more possible variations to the other weights later?

@swordEdge swordEdge requested review from six7 and LiamMartens August 22, 2022 06:49
@github-actions
Copy link
Contributor

github-actions bot commented Aug 22, 2022

Commit SHA:272e6fdba9d8737c3266dc9e01d9814fba531450
Current PR reduces the test coverage percentage by 1 for some tests

Copy link
Collaborator

@six7 six7 left a comment

Choose a reason for hiding this comment

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

Nice! some minor comments

expect(textNodeMock).toEqual({ ...textNodeMock, fontName: { ...textNodeMock.fontName, style: 'Medium' } });
});

it(`can't set number fontWeight to the node if there is no match fontWeight`, async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
it(`can't set number fontWeight to the node if there is no match fontWeight`, async () => {
it(`can't set number fontWeight to the node if there is no matching fontWeight`, async () => {

@@ -35,6 +36,25 @@ describe('setTextValuesOnTarget', () => {
expect(textNodeMock).toEqual({ ...textNodeMock, fontName: { ...textNodeMock.fontName, style: 'Bold' } });
});

it('convert number fontWeight and sets to the node', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
it('convert number fontWeight and sets to the node', async () => {
it('converts a numerical fontWeight and sets to the node', async () => {


try {
await figma.loadFontAsync({ family, style });
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we move the await inside the if (fontFamily || fontWeight)? Then it will only run if required

candidateStyles.map(async (candidateStyle) => (
await figma.loadFontAsync({ family, style: candidateStyle })
.then(() => {
if (fontFamily || fontWeight) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think we don't even need this check? if we do, it should check for family and candidateStyle - as these are the properties we're setting here.

Copy link
Collaborator

@six7 six7 left a comment

Choose a reason for hiding this comment

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

Small change + can we get test coverage to pass?

candidateStyles.map(async (candidateStyle) => (
figma.loadFontAsync({ family, style: candidateStyle })
.then(() => {
if (fontFamily || candidateStyle) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We dont need to check for fontFamily here as we're in an if condition that already checks for that on line 23.

So can just be a check for candidateStyle?

@swordEdge swordEdge requested a review from six7 August 24, 2022 06:33
@six7
Copy link
Collaborator

six7 commented Aug 30, 2022

Can we still get the test coverage to pass @swordEdge ? Then I'd merge this in 👍

@six7 six7 merged commit 53e3d0e into next Sep 1, 2022
@jsamr
Copy link

jsamr commented Sep 21, 2022

@six7 Sorry to bother you! Has this fix been included in version 119?

@six7
Copy link
Collaborator

six7 commented Sep 22, 2022

We released this today with version 120 👍

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.

Cannot set a number fontWeight in typography
5 participants