diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 675f7bf53..f0b5fb4f2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -17,7 +17,15 @@ This is a guide on how to contribute to RoValra Don't rush me and don't expect it to get accepted instantly.\ If the PR has remained stale with no review for a few weeks then feel free to ask about it. -Every PR needs a clear description of what it does. Depending on the type of PR, please also include the following: +When opening a PR, please: + +* Include a clear description of what it does +* Make sure it builds +* Make sure it works properly, without significant issues +* If Artificial Intelligence was utilised in the generation or authoring of the PR, please very clearly disclose/specify what AI did, what it was used for, and whether you tested its code +* If Artificial Intelligence was utilised in the generation or authoring of the PR, please manually review and test all code generated by the AI before opening the PR + +Depending on the type of PR, please also include the following: ### New features @@ -38,6 +46,44 @@ Describe the fix in detail. Please include: PRs without screenshots for new features, or without a proper description for bug fixes, may take longer to review or be closed. +## Making UI with TSX + +If you need to make UI, you can use JSX/TSX in the extension via PReact (not to be confused with React). Keep in mind that you do **not** need the `.jsx`/`.tsx` file extension. + +The PReact API is very similar to the React API. The documentation can be found here: . + +### Creating an element + +You can create an element in two ways. + +The first way automatically gives it a parent: + +```js +import { renderElement } from './core/ui/react.ts'; +const renderedElement = renderElement(element, parent); +``` + +The second way lets you create an HTMLElement without adding it to the webpage: + +```js +import { renderElement } from './core/ui/react.ts'; +const renderedElement = renderElement(element); +``` + +Of course, standard `render()` is still available, and is used by `renderElement()`. + +You can define HTML elements like this: + +```js +
+``` + +> [!NOTE] +> Use `className` instead of `class`, and dont forget to call `renderElement(el)` before adding it to the webpage, or just call `renderElement(el, parent)` or `render(el, parent)`! + ## Adding Translations RoValra uses [i18next](https://www.i18next.com/) for translations. All translations are community made. diff --git a/build.js b/build.js index 96bb8de0e..b435ec96a 100644 --- a/build.js +++ b/build.js @@ -54,6 +54,7 @@ try { ); process.exit(1); } + //E why the hell did i make this comment? const bannerText = `/*! * ${pkg.name} v${pkg.version} @@ -79,6 +80,13 @@ const commonConfig = { js: bannerText, css: bannerText, }, + + loader: { + '.js': 'jsx' + }, + + jsx: 'automatic', + jsxImportSource: 'preact', }; function compileScssFile(inputFile, outputFile) { @@ -102,6 +110,7 @@ esbuild entryPoints: [backgroundEntryPath], outfile: 'dist/background.js', bundle: true, + inject: ['./src/shared/preact-inject.js'], }) .catch(() => process.exit(1)); @@ -192,6 +201,7 @@ esbuild banner: { js: bannerText + '\n' + dracoSource, }, + inject: ['./src/shared/preact-inject.js'], }) .catch(() => process.exit(1)); if (fs.existsSync(cssDir)) { diff --git a/package-lock.json b/package-lock.json index 100111779..c8cf6cf15 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "fzstd": "^0.1.1", "i18next": "26.5.5", "marked": "^18.1.1", + "preact": "^11.2.0", "roavatar-renderer": "^1.10.3", "roblox-bat": "0.6.5" }, @@ -1044,6 +1045,9 @@ "arm" ], "dev": true, + "libc": [ + "glibc" + ], "license": "MIT", "optional": true, "os": [ @@ -1065,6 +1069,9 @@ "arm" ], "dev": true, + "libc": [ + "musl" + ], "license": "MIT", "optional": true, "os": [ @@ -1086,6 +1093,9 @@ "arm64" ], "dev": true, + "libc": [ + "glibc" + ], "license": "MIT", "optional": true, "os": [ @@ -1107,6 +1117,9 @@ "arm64" ], "dev": true, + "libc": [ + "musl" + ], "license": "MIT", "optional": true, "os": [ @@ -1128,6 +1141,9 @@ "x64" ], "dev": true, + "libc": [ + "glibc" + ], "license": "MIT", "optional": true, "os": [ @@ -1149,6 +1165,9 @@ "x64" ], "dev": true, + "libc": [ + "musl" + ], "license": "MIT", "optional": true, "os": [ @@ -4773,6 +4792,24 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/preact": { + "version": "11.2.0", + "resolved": "https://registry.npmjs.org/preact/-/preact-11.2.0.tgz", + "integrity": "sha512-/HfHI/7N84e6GrWBJ3J280z3mLaRJiDcSvj/A+y/4AbLnND/1UlGHe1sR6oYuTGwId0nP7dqDU9OnLxMPZ7xBg==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/preact" + }, + "peerDependencies": { + "preact-render-to-string": ">=6.7.0" + }, + "peerDependenciesMeta": { + "preact-render-to-string": { + "optional": true + } + } + }, "node_modules/prelude-ls": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", diff --git a/package.json b/package.json index 66568ebdd..1fb3788b6 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "lint:firefox": "web-ext lint --source-dir dist-firefox", "package:firefox": "web-ext build --source-dir dist-firefox --artifacts-dir build --overwrite-dest", "start:firefox": "web-ext run --source-dir dist-firefox --start-url https://www.roblox.com", - "lint": "eslint ." + "lint": "eslint .", + "types": "tsc --noEmit" }, "devDependencies": { "@types/chrome": "^0.3.6", @@ -34,7 +35,8 @@ "fzstd": "^0.1.1", "i18next": "26.5.5", "marked": "^18.1.1", - "roavatar-renderer": "^1.10.3", + "preact": "^11.2.0", + "roavatar-renderer": "^1.10.3", "roblox-bat": "0.6.5" }, "overrides": { diff --git a/public/Assets/locales/en.json b/public/Assets/locales/en.json index cd5801a0f..91241eac9 100644 --- a/public/Assets/locales/en.json +++ b/public/Assets/locales/en.json @@ -1155,6 +1155,9 @@ "madeFr": "Helped make the French locale", "madeZh": "Helped make the Chinese locale", "madeVi": "Helped make the Vietnamese locale" + }, + "ui": { + "addedPreact": "Added Preact support for easier UI creation" } }, "ui": { diff --git a/src/content/core/configs/otherContributions.ts b/src/content/core/configs/otherContributions.ts index 3c7b7fc89..0bbe3b2d1 100644 --- a/src/content/core/configs/otherContributions.ts +++ b/src/content/core/configs/otherContributions.ts @@ -83,6 +83,12 @@ export const OTHER_CONTRIBUTIONS: ContributionsType = { new Contribution(650766686, "displayAppThemeUserProfile.addedToWebsite", "https://github.com/NotValra/RoValra-Website/pull/7"), // @auggeeo ] }, + UI: { + label: "User Interfaces", + contributors: [ + new Contribution(1564574922, "ui.addedPreact", "https://github.com/NotValra/RoValra/pull/419"), // @BossBoss2021 + ] + }, Locales: { label: "RoValra Languages", contributors: [ diff --git a/src/content/core/ui/overlay.js b/src/content/core/ui/overlay.js index d3fd8604e..469550b5f 100644 --- a/src/content/core/ui/overlay.js +++ b/src/content/core/ui/overlay.js @@ -1,7 +1,9 @@ // Creates roblox overlay +import { render } from 'preact'; import { createCloseButton } from './closeButton.js'; import { createAssetIcon } from './general/toast.js'; import DOMPurify from 'dompurify'; +import { renderElement } from './react.js'; export function createOverlay({ title, @@ -18,13 +20,16 @@ export function createOverlay({ const overlay = document.createElement('div'); overlay.className = 'rovalra-global-overlay'; - const content = document.createElement('div'); - content.className = 'rovalra-overlay-content'; - content.setAttribute('role', 'dialog'); + const content = renderElement(
); + content.style.maxWidth = maxWidth; content.style.maxHeight = maxHeight; content.style.display = 'flex'; content.style.flexDirection = 'column'; + if (overflowVisible) { content.style.overflow = 'visible'; } else { diff --git a/src/content/core/ui/react.ts b/src/content/core/ui/react.ts new file mode 100644 index 000000000..fbc9c96b2 --- /dev/null +++ b/src/content/core/ui/react.ts @@ -0,0 +1,20 @@ +import { render, ComponentChild, ContainerNode, createRef, isValidElement, cloneElement } from "preact"; + +export function renderElement(element: ComponentChild, parent?: ContainerNode): HTMLElement { + if (!isValidElement(element)) { + throw new Error(); + } + + const ref = createRef(); + const container = document.createElement('div'); + + render(cloneElement(element, { ref: ref }), container); + + if (!ref.current) + throw new Error("Failed to create PReact element."); + + if (parent) + parent.appendChild(ref.current!); + + return ref.current; +} diff --git a/src/shared/preact-inject.js b/src/shared/preact-inject.js new file mode 100644 index 000000000..8f7fdde38 --- /dev/null +++ b/src/shared/preact-inject.js @@ -0,0 +1,2 @@ +export { render } from 'preact'; +export { useState, useEffect, useRef, useMemo } from 'preact/hooks'; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 000000000..dc59212d1 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact", + + "allowJs": true, + "checkJs": false, + + "target": "ES2022", + "module": "NodeNext", + "moduleResolution": "NodeNext", + "lib": ["DOM", "DOM.Iterable", "ES2022"], + "esModuleInterop": true, + }, + "include": ["src/**/*"], + "exclude": ["node_modules", "dist"] +}