Skip to content

Commit

Permalink
Merge pull request #17 from redstonekasi/styles
Browse files Browse the repository at this point in the history
Extension styles
  • Loading branch information
NotNite authored Dec 9, 2023
2 parents 36cba32 + 93006e1 commit f5358c7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/core-extensions/src/moonbase/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ export const webpackModules: ExtensionWebExports["webpackModules"] = {
}
}
};

export const styles = [
".moonbase-settings > :first-child { margin-top: 0px; }"
];
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,6 @@ export default (require: typeof WebpackRequire) => {
const { Flex } = CommonComponents;
return (
<Flex className="moonbase-settings" direction={Flex.Direction.VERTICAL}>
<style>
{".moonbase-settings > :nth-child(2) { margin-top: 0px; }"}
</style>
{Object.entries(ext.manifest.settings!).map(([name, setting]) => (
<Setting ext={ext} key={name} name={name} setting={setting} />
))}
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/extension/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Logger from "../util/logger";
import { registerPatch, registerWebpackModule } from "../patch";
import calculateDependencies from "../util/dependency";
import { createEventEmitter } from "../util/event";
import { registerStyles } from "../styles";

const logger = new Logger("core/extension/loader");

Expand Down Expand Up @@ -68,6 +69,12 @@ async function loadExt(ext: DetectedExtension) {
}
}
}

if (exports.styles != null) {
registerStyles(
exports.styles.map((style, i) => `/* ${ext.id}#${i} */ ${style}`)
);
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const styles: string[] = [];

export function registerStyles(style: string[]) {
styles.push(...style);
}

export function installStyles() {
for (const style of styles) {
const el = document.createElement("style");
el.textContent = style;
document.documentElement.appendChild(el);
}
}
1 change: 1 addition & 0 deletions packages/types/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export type ExtensionWebpackModule = {
export type ExtensionWebExports = {
patches?: Patch[];
webpackModules?: Record<string, ExtensionWebpackModule>;
styles?: string[];
};

export type IdentifiedPatch = Patch & {
Expand Down
5 changes: 5 additions & 0 deletions packages/web-preload/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { loadProcessedExtensions } from "@moonlight-mod/core/extension/loader";
import { installWebpackPatcher } from "@moonlight-mod/core/patch";
import { installStyles } from "@moonlight-mod/core/styles";
import Logger from "@moonlight-mod/core/util/logger";

(async () => {
Expand All @@ -23,4 +24,8 @@ import Logger from "@moonlight-mod/core/util/logger";
} catch (e) {
logger.error("Error setting up web-preload", e);
}

window.addEventListener("DOMContentLoaded", () => {
installStyles();
});
})();

0 comments on commit f5358c7

Please sign in to comment.