Skip to content

Instantly share code, notes, and snippets.

@tecoholic
Last active April 3, 2025 04:57
Show Gist options
  • Save tecoholic/094b86944be692a165780a33b58380b4 to your computer and use it in GitHub Desktop.
Save tecoholic/094b86944be692a165780a33b58380b4 to your computer and use it in GitHub Desktop.
Unit page header slot with buttons to scroll to the corresponding XBlock
import {useState} from "react";
import {PLUGIN_OPERATIONS, DIRECT_PLUGIN} from '@openedx/frontend-plugin-framework';
import {Button, Sheet} from '@openedx/paragon';
import {useIframe} from "CourseAuthoring/course-unit/context/hooks";
const UnitPageSidebar = ({verticalBlocks}) => {
const [show, setShow] = useState(false);
const {sendMessageToIframe} = useIframe();
return (
<>
<Button onClick={() => setShow(!show)}>Sidebar</Button>
<Sheet
position="right"
show={show}
blocking={false}
onClose={() => setShow(false)}
>
{verticalBlocks.map((block) => (
<Button
key={block.id}
className="my-2"
block onClick={
() => sendMessageToIframe("scrollToXBlock", {locator: block.id})
}>
{block.name}
</Button>
))}
</Sheet>
</>
)
}
// Load environment variables from .env file
const config = {
...process.env,
pluginSlots: {
course_unit_header_actions_slot: {
keepDefault: true,
plugins: [
{
op: PLUGIN_OPERATIONS.Insert,
widget: {
id: 'unit-analytics',
type: DIRECT_PLUGIN,
priority: 51,
RenderWidget: UnitPageSidebar,
}
},
]
},
}
};
export default config;
diff --git i/cms/static/js/views/pages/container.js w/cms/static/js/views/pages/container.js
index 684bd0ab7d..07fdb580c1 100644
--- i/cms/static/js/views/pages/container.js
+++ w/cms/static/js/views/pages/container.js
@@ -169,6 +169,9 @@ function($, _, Backbone, gettext, BasePage,
case 'addXBlock':
this.createComponent(this, xblockElement, data);
break;
+ case 'scrollToXBlock':
+ document.getElementById(data.payload.locator).scrollIntoView({behavior: "smooth"});
+ break;
default:
console.warn('Unhandled message type:', data.type);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment