Skip to content

Commit

Permalink
fix: Register Lumo global styles consecutively
Browse files Browse the repository at this point in the history
This allows Flow to control where the styles get injected and that they will not override any application rules
  • Loading branch information
Artur- committed Mar 14, 2023
1 parent 1cf96ee commit dc74dd6
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 25 deletions.
9 changes: 9 additions & 0 deletions packages/vaadin-lumo-styles/badge-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright (c) 2017 - 2023 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { badge } from './badge.js';
import { addLumoGlobalStyles } from './global.js';

addLumoGlobalStyles('badge', badge);
9 changes: 9 additions & 0 deletions packages/vaadin-lumo-styles/color-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright (c) 2017 - 2023 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { color } from './color.js';
import { addLumoGlobalStyles } from './global.js';

addLumoGlobalStyles('color', color);
5 changes: 2 additions & 3 deletions packages/vaadin-lumo-styles/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import './version.js';
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { addLumoGlobalStyles } from './global.js';

const colorBase = css`
:host {
Expand Down Expand Up @@ -80,9 +81,7 @@ const colorBase = css`
}
`;

const $tpl = document.createElement('template');
$tpl.innerHTML = `<style>${colorBase.toString().replace(':host', 'html')}</style>`;
document.head.appendChild($tpl.content);
addLumoGlobalStyles('colorBase', colorBase);

const color = css`
[theme~='dark'] {
Expand Down
18 changes: 18 additions & 0 deletions packages/vaadin-lumo-styles/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const prefix = 'lumo-';

export const addLumoGlobalStyles = (id, ...styles) => {
const styleTag = document.createElement('style');
styleTag.id = `${prefix}${id}`;
styleTag.textContent = styles
.map((style) => style.toString())
.join('\n')
.replace(':host', 'html');

// Add the styles after other "lumo-" styles so they always are added before other styles
const lastExistingLumoTag = Array.from(document.head.querySelectorAll(`style[id^='${prefix}']`)).pop();
if (!lastExistingLumoTag || !lastExistingLumoTag.nextElementSibling) {
document.head.append(styleTag);
} else {
lastExistingLumoTag.parentElement.insertBefore(styleTag, lastExistingLumoTag.nextElementSibling);
}
};
5 changes: 2 additions & 3 deletions packages/vaadin-lumo-styles/sizing.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import './version.js';
import { css } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { addLumoGlobalStyles } from './global.js';

const sizing = css`
:host {
Expand All @@ -23,8 +24,6 @@ const sizing = css`
}
`;

const $tpl = document.createElement('template');
$tpl.innerHTML = `<style>${sizing.toString().replace(':host', 'html')}</style>`;
document.head.appendChild($tpl.content);
addLumoGlobalStyles('sizing', sizing);

export { sizing };
5 changes: 2 additions & 3 deletions packages/vaadin-lumo-styles/spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import './version.js';
import { css } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { addLumoGlobalStyles } from './global.js';

const spacing = css`
:host {
Expand All @@ -31,8 +32,6 @@ const spacing = css`
}
`;

const $tpl = document.createElement('template');
$tpl.innerHTML = `<style>${spacing.toString().replace(':host', 'html')}</style>`;
document.head.appendChild($tpl.content);
addLumoGlobalStyles('spacing', spacing);

export { spacing };
5 changes: 2 additions & 3 deletions packages/vaadin-lumo-styles/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import './version.js';
import { css } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { addLumoGlobalStyles } from './global.js';

const style = css`
:host {
Expand Down Expand Up @@ -36,8 +37,6 @@ const globals = css`
}
`;

const $tpl = document.createElement('template');
$tpl.innerHTML = `<style>${style.toString().replace(':host', 'html')}$</style>`;
document.head.appendChild($tpl.content);
addLumoGlobalStyles('style', style);

export { globals, style };
6 changes: 1 addition & 5 deletions packages/vaadin-lumo-styles/test/visual/badge.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { fixtureSync } from '@vaadin/testing-helpers';
import { visualDiff } from '@web/test-runner-visual-regression';
import { badge } from '../../badge.js';

const badgeStyle = document.createElement('style');
badgeStyle.textContent = badge.cssText;
document.head.appendChild(badgeStyle);
import '../../badge-all.js';

describe('badge', () => {
it('flex-shrink', async () => {
Expand Down
9 changes: 9 additions & 0 deletions packages/vaadin-lumo-styles/typography-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright (c) 2017 - 2023 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { addLumoGlobalStyles } from './global.js';
import { typography } from './typography.js';

addLumoGlobalStyles('typography', typography);
6 changes: 2 additions & 4 deletions packages/vaadin-lumo-styles/typography.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import './version.js';
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { addLumoGlobalStyles } from './global.js';

const font = css`
:host {
Expand Down Expand Up @@ -123,9 +124,6 @@ const typography = css`
`;

registerStyles('', typography, { moduleId: 'lumo-typography' });

const $tpl = document.createElement('template');
$tpl.innerHTML = `<style>${font.toString().replace(':host', 'html')}</style>`;
document.head.appendChild($tpl.content);
addLumoGlobalStyles('font', font);

export { font, typography };
6 changes: 2 additions & 4 deletions packages/vaadin-lumo-styles/user-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import './version.js';
import { css } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { addLumoGlobalStyles } from './global.js';

const userColors = css`
:host {
Expand All @@ -28,8 +29,5 @@ const userColors = css`
}
`;

const $tpl = document.createElement('template');
$tpl.innerHTML = `<style>${userColors.toString().replace(':host', 'html')}</style>`;
document.head.appendChild($tpl.content);

addLumoGlobalStyles('user-colors', userColors);
export { userColors };
9 changes: 9 additions & 0 deletions packages/vaadin-lumo-styles/utility-global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright (c) 2017 - 2023 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { addLumoGlobalStyles } from './global.js';
import { utility } from './utility.js';

addLumoGlobalStyles('utility', utility);

0 comments on commit dc74dd6

Please sign in to comment.