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

Add support for different units in TextStyle.lineHeight #156

Merged
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
35 changes: 34 additions & 1 deletion packages/core/src/lib/figmaStyles/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,40 @@ describe('figmaStyles.', () => {
fontVariant: 'normal',
fontWeight: 400,
letterSpacing: 2,
lineHeight: 30,
lineHeight: '30px',
textAlign: 'left',
textDecoration: 'underline',
textTransform: 'capitalize',
verticalAlign: 'top',
},
},
]);
});

it('should parse a Text style when lineHeightUnit=FONT_SIZE_%', () => {
const node = getNode(styleNodes, 'h1');

// https://jemgold.github.io/figma-js/interfaces/typestyle.html#lineheightunit
// @ts-expect-error: "node" has style for sure
node.style.lineHeightUnit = 'FONT_SIZE_%';

const parsed = figmaStyles.parseStyles([node]);

expect(parsed).to.deep.equal([
{
styleType: 'TEXT',
visible: true,
name: 'h1',
comment: 'Page title',
originalNode: node,
style: {
fontFamily: 'Spinnaker',
fontSize: 24,
fontStyle: 'normal',
fontVariant: 'normal',
fontWeight: 400,
letterSpacing: 2,
lineHeight: '1.25',
textAlign: 'left',
textDecoration: 'underline',
textTransform: 'capitalize',
Expand Down
34 changes: 32 additions & 2 deletions packages/core/src/lib/figmaStyles/textStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,39 @@ const translateVerticalAlign = (figmaTextAlignVertical: string): FigmaExport.Ver
return map[figmaTextAlignVertical];
};

const translateLineHeight = ({
lineHeightPx,
lineHeightPercentFontSize,
lineHeightUnit,
}: Figma.TypeStyle): string => {
if (lineHeightUnit === 'FONT_SIZE_%') {
// Figma API doesn't return `lineHeightPercentFontSize`
// if lineHeightPercent is 100
return lineHeightPercentFontSize
? Math.fround(lineHeightPercentFontSize / 100).toString()
: '1';
}

// this unit is deprecated and will be
// removed in future version of Figma API
// https://www.figma.com/developers/api#files-types
if (lineHeightUnit === 'INTRINSIC_%') {
// this looks wrong at first
// but Pixels in this if-branch are intentional
// https://github.com/marcomontalbano/figma-export/pull/156#issuecomment-1931415352
return `${lineHeightPx}px`;
}

if (lineHeightUnit === 'PIXELS') {
return `${lineHeightPx}px`;
}

throw new Error(`Unknown lineHeightUnit: ${lineHeightUnit}`);
};

const createTextStyle = (textNode: Figma.Style & Figma.Text): FigmaExport.TextStyle => {
const {
fontFamily, fontWeight, fontSize, lineHeightPx, letterSpacing,
fontFamily, fontWeight, fontSize, letterSpacing,
italic, textCase, textDecoration, textAlignHorizontal, textAlignVertical,
} = textNode.style;

Expand All @@ -67,7 +97,7 @@ const createTextStyle = (textNode: Figma.Style & Figma.Text): FigmaExport.TextSt
fontWeight,
fontSize,
letterSpacing,
lineHeight: lineHeightPx,
lineHeight: translateLineHeight(textNode.style),
fontStyle: italic ? 'italic' : 'normal',
fontVariant: translateFontVariant(textCase),
textTransform: translateTextTransform(textCase),
Expand Down
2 changes: 1 addition & 1 deletion packages/output-styles-as-css/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const mockText = (visible = true): Style => ({
fontVariant: 'normal',
fontWeight: 100,
letterSpacing: 10,
lineHeight: 12,
lineHeight: '12px',
textAlign: 'left',
textDecoration: 'none',
textTransform: 'uppercase',
Expand Down
2 changes: 1 addition & 1 deletion packages/output-styles-as-css/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export = ({
text += writeVariable(getVariableName(style, 'font-variant'), `${style.style.fontVariant}`);
text += writeVariable(getVariableName(style, 'font-weight'), `${style.style.fontWeight}`);
text += writeVariable(getVariableName(style, 'letter-spacing'), `${style.style.letterSpacing}px`);
text += writeVariable(getVariableName(style, 'line-height'), `${style.style.lineHeight}px`);
text += writeVariable(getVariableName(style, 'line-height'), `${style.style.lineHeight}`);
text += writeVariable(getVariableName(style, 'text-align'), `${style.style.textAlign}`);
text += writeVariable(getVariableName(style, 'text-decoration'), `${style.style.textDecoration}`);
text += writeVariable(getVariableName(style, 'text-transform'), `${style.style.textTransform}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/output-styles-as-less/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const mockText = (visible = true): Style => ({
fontVariant: 'normal',
fontWeight: 100,
letterSpacing: 10,
lineHeight: 12,
lineHeight: '12px',
textAlign: 'left',
textDecoration: 'none',
textTransform: 'uppercase',
Expand Down
4 changes: 2 additions & 2 deletions packages/output-styles-as-less/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export = ({
font-variant: ${style.style.fontVariant};
font-weight: ${style.style.fontWeight};
letter-spacing: ${style.style.letterSpacing}px;
line-height: ${style.style.lineHeight}px;
line-height: ${style.style.lineHeight};
text-align: ${style.style.textAlign};
text-decoration: ${style.style.textDecoration};
text-transform: ${style.style.textTransform};
Expand All @@ -77,7 +77,7 @@ export = ({
text += writeVariable(style.comment, getVariableName(style, 'font-variant'), `${style.style.fontVariant}`);
text += writeVariable(style.comment, getVariableName(style, 'font-weight'), `${style.style.fontWeight}`);
text += writeVariable(style.comment, getVariableName(style, 'letter-spacing'), `${style.style.letterSpacing}px`);
text += writeVariable(style.comment, getVariableName(style, 'line-height'), `${style.style.lineHeight}px`);
text += writeVariable(style.comment, getVariableName(style, 'line-height'), `${style.style.lineHeight}`);
text += writeVariable(style.comment, getVariableName(style, 'text-align'), `${style.style.textAlign}`);
text += writeVariable(style.comment, getVariableName(style, 'text-decoration'), `${style.style.textDecoration}`);
text += writeVariable(style.comment, getVariableName(style, 'text-transform'), `${style.style.textTransform}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/output-styles-as-sass/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const mockText = (visible = true): Style => ({
fontVariant: 'normal',
fontWeight: 100,
letterSpacing: 10,
lineHeight: 12,
lineHeight: '12px',
textAlign: 'left',
textDecoration: 'none',
textTransform: 'uppercase',
Expand Down
4 changes: 2 additions & 2 deletions packages/output-styles-as-sass/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export = ({
"font-variant": ${style.style.fontVariant},
"font-weight": ${style.style.fontWeight},
"letter-spacing": ${style.style.letterSpacing}px,
"line-height": ${style.style.lineHeight}px,
"line-height": ${style.style.lineHeight},
"text-align": ${style.style.textAlign},
"text-decoration": ${style.style.textDecoration},
"text-transform": ${style.style.textTransform},
Expand Down Expand Up @@ -115,7 +115,7 @@ export = ({
text += writeVariable(
style.comment,
getVariableName(style, 'line-height'),
`${style.style.lineHeight}px`,
`${style.style.lineHeight}`,
extension,
);
text += writeVariable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const mockText = (visible = true): Style => ({
fontVariant: 'normal',
fontWeight: 100,
letterSpacing: 10,
lineHeight: 12,
lineHeight: '12px',
textAlign: 'left',
textDecoration: 'none',
textTransform: 'uppercase',
Expand Down
2 changes: 1 addition & 1 deletion packages/output-styles-as-style-dictionary/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export = ({
writeVariable(result, style.comment, getVariableName(style, 'font-variant'), `${style.style.fontVariant}`);
writeVariable(result, style.comment, getVariableName(style, 'font-weight'), `${style.style.fontWeight}`);
writeVariable(result, style.comment, getVariableName(style, 'letter-spacing'), `${style.style.letterSpacing}px`);
writeVariable(result, style.comment, getVariableName(style, 'line-height'), `${style.style.lineHeight}px`);
writeVariable(result, style.comment, getVariableName(style, 'line-height'), `${style.style.lineHeight}`);
writeVariable(result, style.comment, getVariableName(style, 'text-align'), `${style.style.textAlign}`);
writeVariable(result, style.comment, getVariableName(style, 'text-decoration'), `${style.style.textDecoration}`);
writeVariable(result, style.comment, getVariableName(style, 'text-transform'), `${style.style.textTransform}`);
Expand Down
11 changes: 9 additions & 2 deletions packages/types/src/styles/TextStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@ export type TextStyle = {
fontWeight: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
/** Font size in px */
fontSize: number;
/** Line height in px */
lineHeight: number;
/**
* Line height value with units.
*
* Examples: `"18px", "120%", "1.2"`
*
* ---
* More on units https://developer.mozilla.org/en-US/docs/Web/CSS/line-height
*/
lineHeight: string;
/** Space between characters in px */
letterSpacing: number;

Expand Down
Loading