Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 25, 2026

Description

modern-screenshot library has issues with CSS @layer when cloning the DOM for screenshots. Fonts were not rendering correctly in screenshots.

Changes:

  • Moved @import "fontawesome-5" into @layer vendor block in vendor.scss
  • Moved @import "fonts" from index.scss into @layer vendor block in vendor.scss
  • Added onCloneDocument callback to extract all @font-face rules from stylesheets and inject them into the cloned document
  • Added onCloneEachNode callback to copy computed font properties during DOM cloning

Technical detail:
When modern-screenshot's domToCanvas() clones the DOM, it has trouble resolving @font-face rules from CSS layers. To fix this while keeping fonts in a CSS layer (as required), the solution uses a two-pronged approach:

  1. @font-face extraction: The onCloneDocument callback iterates through all stylesheets, extracts CSSFontFaceRule instances, and injects them as a <style> block in the cloned document's head
  2. Property copying: The onCloneEachNode callback copies computed font properties (fontFamily, fontWeight, fontSize, fontStyle) from original elements to cloned elements

This ensures fonts render correctly in screenshots while maintaining proper CSS layer organization.

Checks

  • Adding quotes?
    • Make sure to include translations for the quotes in the description (or another comment) so we can verify their content.
  • Adding a language?
    • Make sure to follow the languages documentation
    • Add language to packages/schemas/src/languages.ts
    • Add language to exactly one group in frontend/src/ts/constants/languages.ts
    • Add language json file to frontend/static/languages
  • Adding a theme?
    • Make sure to follow the themes documentation
    • Add theme to packages/schemas/src/themes.ts
    • Add theme to frontend/src/ts/constants/themes.ts
    • (optional) Add theme css file to frontend/static/themes
    • Add some screenshots of the theme, especially with different test settings (colorful, flip colors) to your pull request
  • Adding a layout?
    • Make sure to follow the layouts documentation
    • Add layout to packages/schemas/src/layouts.ts
    • Add layout json file to frontend/static/layouts
  • Adding a font?
    • Make sure to follow the fonts documentation
    • Add font file to frontend/static/webfonts
    • Add font to packages/schemas/src/fonts.ts
    • Add font to frontend/src/ts/constants/fonts.ts
  • Check if any open issues are related to this PR; if so, be sure to tag them below.
  • Make sure the PR title follows the Conventional Commits standard. (https://www.conventionalcommits.org for more info)
  • Make sure to include your GitHub username prefixed with @ inside parentheses at the end of the PR title.
Original prompt

im using this library in my @monkeytypegame/monkeytype . when using css @layer, fonts are not showing up in screenshots. look through this library code, analyse it then fix the issue on the monkeytype repo.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: Miodec <13181393+Miodec@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix font rendering issues in screenshots using css @layer fix: move fonts import outside CSS @layer for screenshot compatibility Jan 25, 2026
Copilot AI requested a review from Miodec January 25, 2026 17:23
@Miodec
Copy link
Member

Miodec commented Jan 25, 2026

@copilot font and fontawesome need to remain in a css layer

…ndering

Co-authored-by: Miodec <13181393+Miodec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 25, 2026

@copilot font and fontawesome need to remain in a css layer

Done! Moved both into @layer vendor and added explicit font property copying in screenshot code to handle CSS @layer issues. See commit 65153c3.

Copilot AI changed the title fix: move fonts import outside CSS @layer for screenshot compatibility fix: move fonts into CSS @layer vendor and fix screenshot font rendering Jan 25, 2026
@Miodec
Copy link
Member

Miodec commented Jan 25, 2026

@copilot this doesnt completely fix it

Co-authored-by: Miodec <13181393+Miodec@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 25, 2026

@copilot this doesnt completely fix it

Added onCloneDocument callback to extract and inject all @font-face rules into the cloned document. This ensures both the font definitions and font properties are properly transferred to screenshots. See commit 3251a8f.

Copilot AI changed the title fix: move fonts into CSS @layer vendor and fix screenshot font rendering fix: move fonts into CSS @layer vendor and inject @font-face rules for screenshots Jan 25, 2026
@Miodec Miodec closed this Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants