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

fix: font size syncing #142

Merged
merged 2 commits into from
Nov 24, 2021
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
10 changes: 6 additions & 4 deletions plugins/sync-font/host.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export function hostSyncFont(host) {
host.onRequest('font', () => {
const computedStyle = window.getComputedStyle(document.body);
Copy link
Member

Choose a reason for hiding this comment

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

To me, this seems like the bug right here? Like in our LMS pages, we output this:

html {
    font-size: 20px;
}

And that value changes when the user changes their font size preference in account settings. So could we just always grab it from the root and always set it on the root and ignore the body completely?

Copy link

@jameswklassen jameswklassen Nov 24, 2021

Choose a reason for hiding this comment

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

Yeah, this is what started the font syncing discussion. In Portfolio we want to reflect the font size preference inside our iFrame.

We weren't sure if others depend on the current behaviour (getting computed style from <body> rather than <html>).

If it makes sense that the style comes right from <html> (and it doesn't break existing usage), then it might be cleanest to always get the computed style from the root

const computedStyle = window.getComputedStyle(document.documentElement);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm ok with treating this as a bug. I'm less worried after thinking about the fact that teams have to upgrade to get the new version of ifrau anyways and would hopefully test and see the difference. I can make the adjust to just grab the html font size and not introduce the new sizeRoot stuff if that works for everyone.

const bodyComputedStyle = window.getComputedStyle(document.body);
const rootComputedStyle = window.getComputedStyle(document.documentElement);
const visualRedesign = document.body.classList.contains('visual-redesign');

return {
family: computedStyle.fontFamily,
size: computedStyle.fontSize,
visualRedesign: visualRedesign
family: bodyComputedStyle.fontFamily,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still using body to grab the font family. Isn't used but have it for compatibility.

size: rootComputedStyle.fontSize,
visualRedesign
};
});
}
26 changes: 15 additions & 11 deletions test/plugins/sync-font.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ chai.use(sinonChai).should();
describe('sync-font', () => {

const fontFamily = 'comic sans';
const fontSize = '20px';
let classListAdd;

beforeEach(() => {
classListAdd = sinon.spy();
global.document = {
documentElement: {
style: {
fontSize: '1pt'
fontSize
}
},
body: {
style: {
fontFamily
},
classList: {
add: classListAdd,
contains: function(className) {
Expand All @@ -31,10 +35,10 @@ describe('sync-font', () => {
}
};
global.window = {
getComputedStyle: function() {
getComputedStyle: function(element) {
return {
fontFamily: fontFamily,
fontSize: '20pt'
fontFamily: element.style.fontFamily,
fontSize: element.style.fontSize
};
}
};
Expand All @@ -46,8 +50,8 @@ describe('sync-font', () => {

beforeEach(() => {
response = {
family: 'foo',
size: '20px'
family: fontFamily,
size: fontSize
};
client = new MockClient();
request = sinon.stub(client, 'request').returns(
Expand All @@ -68,7 +72,7 @@ describe('sync-font', () => {

it('should apply font size to HTML element', (done) => {
clientSyncFont(client).then(() => {
expect(document.documentElement.style.fontSize).to.equal('20px');
expect(document.documentElement.style.fontSize).to.equal(fontSize);
done();
});
});
Expand Down Expand Up @@ -105,8 +109,8 @@ describe('sync-font', () => {
hostSyncFont(host);
const value = onRequest.args[0][1]();
expect(value).to.eql({
family: 'comic sans',
size: '20pt',
family: fontFamily,
size: fontSize,
visualRedesign: false
});
});
Expand All @@ -116,8 +120,8 @@ describe('sync-font', () => {
hostSyncFont(host);
const value = onRequest.args[0][1]();
expect(value).to.eql({
family: 'comic sans',
size: '20pt',
family: fontFamily,
size: fontSize,
visualRedesign: true
});
});
Expand Down