Missing button icons: @font-face does not work in shadow dom / LitElement
See original GitHub issueReduced Test Case
“not reproducible with a link”
I don’t know how to do a webpack example with codepen or jsfiddle and provide some snippets instead from my reduced test case.
package.json
"scripts": {
"start": "webpack-dev-server --open"
},
"devDependencies": {
"css-loader": "^4.3.0",
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"@fullcalendar/core": "^5.3.1",
"@fullcalendar/daygrid": "^5.3.2",
"lit-element": "^2.4.0"
}
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
},
module: {
rules: [
{
test: /\.css$/i,
use: [
'css-loader'
]
},
],
},
};
import {LitElement, html, css, unsafeCSS} from 'lit-element';
import {Calendar} from '@fullcalendar/core';
import dayGridPlugin from '@fullcalendar/daygrid';
import commonStyles from '@fullcalendar/common/main.css';
import dayGridStyles from '@fullcalendar/daygrid/main.css';
export class AppRoot extends LitElement {
firstUpdated(_changedProperties) {
super.firstUpdated(_changedProperties);
const calendarEl = this.shadowRoot.getElementById('calendar');
const calendar = new Calendar(calendarEl, {
plugins: [dayGridPlugin],
});
calendar.render();
}
static get styles() {
return [
unsafeCSS(commonStyles),
unsafeCSS(dayGridStyles),
css`
#calendar {
width: 50%;
}
`
];
}
render() {
return html`
<div id="calendar"></div>
`;
}
}
customElements.define("app-root", AppRoot);
index.html
<!doctype html>
<html>
<head>
<script src="./main.js" type="module"></script>
</head>
<body>
<app-root></app-root>
</body>
</html>
Bug Description
I expect to see button icons.
Chrome shows nothing, Firefox shows squares with the unicode point.
I suspect there’s something wrong with loading the @font-face
or applying the css. When inspecting the shadow dom element, I can find the @font-face
in the styleSheets
collection.
Screenshots
What if tried so far
- https://github.com/fullcalendar/fullcalendar-example-projects/tree/master/webpack with MiniCssExtractPlugin. Problem: globally loaded css is not “visible” in shadow dom
- I understood that there should be no need to manually import css files in v5 (https://fullcalendar.io/blog/2020/02/changes-in-the-upcoming-v5). But I found no other way to deal with it. (Without manually importing styles and adding them to shadow dom, no styles get applied at all.) Maybe the following issue is somewhat similar:
- Using
css
instead ofunsafeCSS
does not show the icons either.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8
Top Results From Across the Web
@font-face doesn't work in Shadow DOM - Rob Dodson
A workaround is to pull your @font-face rules out of the stylesheet for your element, and move them to the top of your...
Read more >Icon Fonts in Shadow DOM - Stack Overflow
A random example. Sidenote: @font-face does not bleed into shadow. It's the directive setting the relation between fontname and the file where ...
Read more >How to Create a LitElement Web Component — Heart/Unheart
Web components make use of custom elements and shadow DOM, the :host selector in CSS is targeting the shadow host of the custom...
Read more >Encapsulating Style and Structure with Shadow DOM
Now that our element has a shadow DOM attached, there is no outlet for the light DOM to render. Let's start fixing these...
Read more >GSAP target not found with LitElement - GreenSock
All in all, as far as the code goes, the below class DOES work with the exception of GSAP not being able to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can also copy the “@font-face” from shadow dom (after adding commonStyles) to the outer scope:
But still hacky…
(This works also with LitElement when added to
firstUpdated()
.)Now this works also:
(shortened data url for better visibility)