Skip to content

Commit

Permalink
fix(fonts): prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaSimonePorceddu committed Jun 7, 2021
1 parent 68ba391 commit c97f825
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 91 deletions.
10 changes: 6 additions & 4 deletions apps/svelte-sandbox/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { lightTheme } from './theme';

loadFont({
importFontFace: true,
urls: [{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@400&display=swap'
}],
urls: [
{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@400&display=swap',
},
],
name: 'regular',
family: 'Roboto',
})
});
enableMorfeoDevTool();
resetCss();
theme.set(lightTheme);
Expand Down
8 changes: 4 additions & 4 deletions apps/web-sandbox/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export const lightTheme = {
m: '100px',
},
lineHeights: {
s: 1.7
s: 1.7,
},
letterSpacings: {
s: 1.6
s: 1.6,
},
fontSizes: {
s: '14px'
s: '14px',
},
sizes: {
s: '10px',
Expand Down Expand Up @@ -153,7 +153,7 @@ export const lightTheme = {
fontFamily: 'regular',
fontSize: 's',
lineHeight: 's',
letterSpacing: 's'
letterSpacing: 's',
},
variants: {
h1: {
Expand Down
2 changes: 1 addition & 1 deletion packages/fonts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './mountFont';
export * from './mountFont';
37 changes: 18 additions & 19 deletions packages/fonts/src/mountFont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type FontUrl = {
};

export type MountFontParams = {
name: string;
name: string;
urls: FontUrl[];
family: string;
/**
Expand All @@ -23,15 +23,15 @@ export function unmountFont(name: string) {
if (currentFontStyle) {
currentFontStyle.remove();
}
}
}

/**
* Mount a fontFace on document head style
*
*
* ---
*
*
* ### mount a google font
*
*
* ```ts
* mountFont({
* urls: [{
Expand All @@ -42,11 +42,11 @@ export function unmountFont(name: string) {
* name: 'bold',
* })
* ```
*
*
* ---
*
*
* ### mount a local font
*
*
* ```ts
* mountFont({
* urls: [
Expand All @@ -68,15 +68,11 @@ export function unmountFont(name: string) {

export function mountFont(font: MountFontParams) {
let newFontFaceContent = '';


if (font.importFontFace) {
newFontFaceContent += `
@import ${font.urls.reduce(
(_, value) => `url(${value.url})`,
'',
)}
`
@import ${font.urls.reduce((_, value) => `url(${value.url})`, '')}
`;
}

if (!font.importFontFace) {
Expand All @@ -85,7 +81,10 @@ export function mountFont(font: MountFontParams) {
font-family: ${font.family};
${font.weight ? `font-weight: ${font.weight};` : ''}
url: ${font.urls.reduce(
(acc, value) => `${acc ? acc + '\n' : acc} url('${value.url}') ${value.format ? `format('${value.format}')` : ''}`,
(acc, value) =>
`${acc ? acc + '\n' : acc} url('${value.url}') ${
value.format ? `format('${value.format}')` : ''
}`,
'',
)}
}
Expand All @@ -96,9 +95,9 @@ export function mountFont(font: MountFontParams) {
<style id="font-${font.name}">
${newFontFaceContent}
</style>
`
`;

unmountFont(font.name)
unmountFont(font.name);

document.head.innerHTML += newFontStyle
}
document.head.innerHTML += newFontStyle;
}
63 changes: 35 additions & 28 deletions packages/fonts/tests/mountFont.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,65 @@ import { mountFont, MountFontParams } from '../src';

const newImportedFontFace: MountFontParams = {
importFontFace: true,
urls: [{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap'
}],
urls: [
{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap',
},
],
name: 'bold',
family: 'Roboto',
}
};

const newFontFace: MountFontParams = {
urls: [{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap',
}],
urls: [
{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap',
},
],
name: 'bold',
family: 'Roboto',
}
};

const newFontWithFormat: MountFontParams = {
urls: [{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap',
format: 'woff'
}, {
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap',
format: 'woff2'
}],
urls: [
{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap',
format: 'woff',
},
{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap',
format: 'woff2',
},
],
name: 'bold',
family: 'Roboto',
}
};

describe('mountFont', () => {
describe('new fontFace', () => {
test('should add a style with the fontFace definition', () => {
mountFont(newFontFace)
mountFont(newFontFace);
const boldStyle = document.getElementById('font-bold');
expect(boldStyle?.textContent).toContain('@fontFace');
})
});

test('should set a style that contain font-weight', () => {
mountFont({ ...newFontFace, weight: 'bold' })
mountFont({ ...newFontFace, weight: 'bold' });
const boldStyle = document.getElementById('font-bold');
expect(boldStyle?.textContent).toContain('font-weight: bold');
})
});

test('should set a style that contain format', () => {
mountFont(newFontWithFormat)
mountFont(newFontWithFormat);
const boldStyle = document.getElementById('font-bold');
expect(boldStyle?.textContent).toContain('format(\'woff\')');
})
})
expect(boldStyle?.textContent).toContain("format('woff')");
});
});
describe('imported fontFace', () => {
test('should add a style with the fontface import', () => {
mountFont(newImportedFontFace)
mountFont(newImportedFontFace);
const boldStyle = document.getElementById('font-bold');
expect(boldStyle?.textContent).toContain('@import');
})
})
})
});
});
});
3 changes: 1 addition & 2 deletions packages/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { parsers, Property } from '@morfeo/core';
import { pseudosParses, gradientParsers } from './parsers';
import { pseudosProperties } from './properties';


pseudosProperties.forEach(property => {
parsers.add(property as Property, pseudosParses[property] as any);
});
Expand All @@ -20,4 +19,4 @@ export * from '@morfeo/core';
/** re-export of @morfeo/jss */
export * from '@morfeo/jss';
/** re-export of @morfeo/fonts */
export * from '@morfeo/fonts'
export * from '@morfeo/fonts';
24 changes: 12 additions & 12 deletions packages/web/src/utils/loadFont.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { theme, Font } from "@morfeo/core";
import { theme, Font } from '@morfeo/core';
import { MountFontParams, mountFont } from '@morfeo/fonts';

export type LoadFontParams = Omit<MountFontParams, 'name'> & { name: Font }
export type LoadFontParams = Omit<MountFontParams, 'name'> & { name: Font };
/**
* Load a fontFace on document head style and add it to the morfeo theme
*
*
* ---
*
*
* ### load a google font
*
*
* ```ts
* loadFont({
* urls: [{
Expand All @@ -19,11 +19,11 @@ export type LoadFontParams = Omit<MountFontParams, 'name'> & { name: Font }
* name: 'bold',
* })
* ```
*
*
* ---
*
*
* ### load a local font
*
*
* ```ts
* loadFont({
* urls: [
Expand All @@ -43,7 +43,7 @@ export type LoadFontParams = Omit<MountFontParams, 'name'> & { name: Font }
* ```
*/

export function loadFont(font: LoadFontParams) {
mountFont(font)
theme.setValue('fonts', font.name, font.family)
};
export function loadFont(font: LoadFontParams) {
mountFont(font);
theme.setValue('fonts', font.name, font.family);
}
46 changes: 25 additions & 21 deletions packages/web/tests/loadFont.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,41 @@ import { theme } from '@morfeo/core';

const newImportedFontFace: LoadFontParams = {
importFontFace: true,
urls: [{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap'
}],
urls: [
{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap',
},
],
name: 'regular',
family: 'Roboto',
}
};

const newFontFace: LoadFontParams = {
urls: [{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap',
}],
urls: [
{
url: 'https://fonts.googleapis.com/css2?family=Roboto:wght@700&display=swap',
},
],
name: 'bold',
family: 'Roboto',
}
};

beforeEach(() => {
theme.reset()
})
theme.reset();
});
describe('loadFont', () => {
describe('new fontFace', () => {
test('should add a font on theme', () => {
loadFont(newFontFace)
const themeFont = theme.getValue('fonts', newFontFace.name)
expect(themeFont).toBe(newFontFace.family)
})
})
loadFont(newFontFace);
const themeFont = theme.getValue('fonts', newFontFace.name);
expect(themeFont).toBe(newFontFace.family);
});
});
describe('imported fontFace', () => {
test('should add a font on theme', () => {
loadFont(newImportedFontFace)
const themeFont = theme.getValue('fonts', newImportedFontFace.name)
expect(themeFont).toBe(newImportedFontFace.family)
})
})
})
loadFont(newImportedFontFace);
const themeFont = theme.getValue('fonts', newImportedFontFace.name);
expect(themeFont).toBe(newImportedFontFace.family);
});
});
});

0 comments on commit c97f825

Please sign in to comment.