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

Unified Styles API #3153

Open
wants to merge 29 commits into
base: dev
Choose a base branch
from
Open

Conversation

Sqaaakoi
Copy link
Contributor

Converts the managed styles API to be a unified API for all styles

The problem

Vencord does not inject styles into popout windows.
Additionally, plugins that create their own styles at runtime contain a lot of (ugly) code to add these styles.

How this solves the problem

Almost every call to document.createElement("style")1 is replaced with the unified styles API.
This API should be considered the single source of truth regarding stylesheets for everything from now on.
The underlying data structure for "managed styles" has been changed to not store references to DOM <style> elements.
Instead, DOM elements are looked up in all available documents (current document and popouts) when necessary.
When a popout is created, all enabled styles are injected to that document.
Please note that there is no "style updated" event system implemented into this API.

How to use it

The major functions that should be commonly used are createStyle2, deleteStyle, and setStyle. The existing enableStyle and disableStyle can still be used for managed styles.

I am aware that there's still a decent amount of out-of-date documentation in the code, and some things should be renamed or moved around. Please do provide feedback on this.
Some code was taken from #3080 (thank you @Masterjoona for originally developing code for CSS in popouts)

Footnotes

  1. Not used in YoutubeAdblock or ShikiCodeblocks, though the code in ShikiCodeblocks looks redundant.

  2. createStyle is also used to update styles

@@ -276,7 +276,7 @@ export const stylePlugin = {
name: "style-plugin",
setup: ({ onResolve, onLoad }) => {
onResolve({ filter: /\.css\?managed$/, namespace: "file" }, ({ path, resolveDir }) => ({
path: relative(process.cwd(), join(resolveDir, path.replace("?managed", ""))),
path: relative(process.cwd(), join(resolveDir, path.replace(/\?managed$/, ""))),
Copy link
Owner

Choose a reason for hiding this comment

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

why

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in this current state, yes it's unnecessary
i was just working on adding enabled by default styles, would there be ANY usecase for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

idc about the styles being enabled idea anymore, should this just be left as a string

@Sqaaakoi Sqaaakoi force-pushed the managed-styles-rewrite branch from 8d15983 to 330532c Compare February 2, 2025 01:00
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