Skip to content

feat: make XBlock template use React and Paragon. - #560

Open
Kelketek wants to merge 3 commits into
openedx:masterfrom
open-craft:fox/react-template
Open

Kelketek wants to merge 3 commits into
openedx:masterfrom
open-craft:fox/react-template

Conversation

@Kelketek

Copy link
Copy Markdown
Contributor

Description

This pull request adds React and Paragon as well as modern build tooling to the XBlock template. This allows XBlock developers to get started with a practical, end-to-end example of React-based XBlock development, and allows them to begin using Paragon and design tokens in their blocks today.

Supporting information

https://openedx.atlassian.net/wiki/spaces/OEPM/pages/6574833665/Proposal+Update+XBlock+Examples+Templates+and+Documentation+to+be+React+and+Paragon-based

Internal ticket: https://tasks.opencraft.com/browse/BB-11326

Testing instructions

  1. uv sync
  2. uv run ./bin/workbench-make-xblock
  3. Follow the instructions on your screen
  4. Follow the instructions in the resulting README
  5. Run the build, tests, formatting, etc.
  6. Verify the block works as expected in studio and the LMS.

@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Oct 28, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @Kelketek!

This repository is currently maintained by @openedx/axim-engineering.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@tecoholic tecoholic left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Kelketek Nice. I have tested this and it works as expected. I am super impressed at how smoothly this all works. Really nice :)

I have made a few comments, kindly take a look.

@@ -0,0 +1,8 @@
import { type XBlockConfig } from "@opencraft/xblock-react-utils";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Kelketek I think we should upstream the utils package to namespace it under @openedx or make them a part of the frontend itself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@tecoholic Agreed-- that's part of the original proposal. It's a bit chicken-and-egg here-- I'm expecting we'll need a follow-up post-contribution to update the references once we move the repo in to the @openedx namespace, but I wanted to make sure that we had a working end-to-end setup that was properly inclusive before pushing that.

@@ -0,0 +1,27 @@
import type { ReactNode } from "react";
import { RuntimeContext } from "@opencraft/xblock-react-utils";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is related to the lib/context.ts. If the utils package already ships a RuntimeContext, could we either merge the config into it? Or just put the context and useXBlockConfig hook into the utils?

@@ -0,0 +1,27 @@
import type { ReactNode } from "react";
import { RuntimeContext } from "@opencraft/xblock-react-utils";
import { mockRuntime, render } from "@opencraft/xblock-react-utils/testing";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similarly, could we upstream the testing utils?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This would be part of moving this package to upstream, yes.

"oxfmt": "^0.73.0",
"oxlint": "^1.88.0",
"react-intl": "^6.8.9",
"typescript": "~5.9.3",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Kelketek Typescript 7.0 was released in July this year, which offers upward of 10x speed improvements. Can we use it here?

type XBlockRuntime,
} from "@opencraft/xblock-react-utils";
import type { {{cookiecutter.class_name}}Fields } from "@/types.ts";
import styles from "./style.scss?inline";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A comment here about why ?inline here is necessary and if all styles should be imported this way would be helpful.

Comment on lines +11 to +17
saveStudioBlock<{{cookiecutter.class_name}}Fields>({
saveUrl: studioSaveUrl,
data: saveData,
runtime,
errorMessage: "Failed to save configuration",
});
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I am conflicted by this function being a util. While working on the JSON import, I had to re-implement the "save" functionality with some custom logic and realized this utility function is useful only in the narrow context. If we were to replace this with $.post(runtime.handlerUrl(element, studio_save), saveData)... instead, we will be able to document the use of Python handler function inside React and provide an example for developers - at the expense of being concise.

Comment on lines +16 to +20
createXBlockRoot({ runtime, element, config: context.config, styles })(
<XBlockConfigContext.Provider value={context.config}>
<App {...context.data} />
</XBlockConfigContext.Provider>,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just an extension of the previous comments about contexts - should we just merge the Runtime and Config contexts as single context and just make it

createXBlockRoot({...})(<App {...data} />)

Comment on lines +9 to +10
minimumReleaseAgeExclude:
- "@opencraft/xblock-react-utils@0.1.1"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is okay for testing. Ideally we don't have anything here? Or we are make this package a target for hijackers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It probably isn't needed anymore since 0.1.1 is now old enough. However, you can't replace a version on NPM these days, so as long as the entry is known good. So, this isn't introducing a vulnerability.

Per the NPM docs:

"Once a package is published with a given name and version, that specific name and version combination can never be used again, even if it is removed with npm unpublish."

If we end up updating the package as part of this work we'll need to bump the version here, though :)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Got it. Thank you.

Comment on lines +1 to +11
/*
* The base styles of cms set html { font-size: 62.5% } to get the base font size of 1rem = 10px.
* But Paragon's CSS Variables define the text sizes in "rem" units with the expectation of 1rem = 16px.
* To fix this, we set html { font-size: 100% } to make 1rem = 16px, matching the Paragons CSS Variables.
*
* Note that this only affects things within the shadow root created for the XBlock, so it won't affect
* things outside of the XBlock editor.
*/
html {
font-size: 100%;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This should only be in the Python package's static/css/studio.css right? Possible duplicate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, that's right-- I had placed it here at one point thinking we might be able to avoid that, but it didn't seem so and I forgot to remove it. I'll clear it out.

description = "{{cookiecutter.short_name}} XBlock" # TODO: write a better description.
readme = "README.md"
# license = "UNKNOWN" # TODO: choose a license: 'AGPL-3.0-or-later' and 'Apache-2.0' are popular.
# Version used by the platform as of Verawood. Lower this if you need to support older versions,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for adding this comment.

@@ -0,0 +1,59 @@
import type { SaveStudioBlockParams } from "@opencraft/xblock-react-utils";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@tecoholic I'm still working on your notes, but I wanted to call your attention to this file I've added.

This contains a revised version of saveStudioBlock as well as a function which makes jQuery promise-based. Please give these a look over and if they look good I'll put them in a PR to xblock-react-utils.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I like the revised function, as it takes the saveUrl as a param and can be used across all the XBlock handlers now.


export const App = (data: {{cookiecutter.class_name}}Fields) => {
const { runtime } = useContext(RuntimeContext);
const studioSaveUrl = useHandlerUrl("studio_save");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Oh! Wow. There exists a useHandlerUrl hook? Nice.

}

const promise = new Promise<T>((resolve, reject) => {
$.ajax(resolvedSettings as JQueryAjaxSettings).done(resolve).fail(reject);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is nice. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). open-source-contribution PR author is not from Axim or 2U

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

3 participants