Skip to content

Commit

Permalink
Make the source editor follow the host's dark mode (assuming it uses …
Browse files Browse the repository at this point in the history
…PatternFly)
  • Loading branch information
riccardo-forina committed Nov 15, 2024
1 parent fabb7d4 commit fb3f1a5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/ui/src/components/SourceEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function SourceEditor({
onSave: (source: string, sourceType: SourceType) => void;
}) {
const [height, setHeight] = useState<number>(0);
const [isDarkMode, setIsDarkMode] = useState(false);

const [code, setCode] = useState<string | undefined>(source);

Expand Down Expand Up @@ -55,6 +56,31 @@ export function SourceEditor({
setCode(source);
}, [source]);

useEffect(() => {
const observer = new MutationObserver((mutationList, observer) => {
for (const mutation of mutationList) {
if (mutation.type === "attributes") {
console.log(`The ${mutation.attributeName} attribute was modified.`);
if (mutation.attributeName === "class") {
setIsDarkMode(
(mutation.target as HTMLElement).classList.contains(
"pf-v6-theme-dark"
)
);
}
}
}
});
observer.observe(document.getElementsByTagName("html")[0], {
attributes: true,
subtree: false,
childList: false,
});
return () => {
observer.disconnect();
};
}, []);

const onEditorDidMount: CodeEditorProps["onEditorDidMount"] = (e) => {
editorRef.current = e;
};
Expand Down Expand Up @@ -137,6 +163,7 @@ export function SourceEditor({
height={`${height - 130}px`}
onEditorDidMount={onEditorDidMount}
emptyState={<SectionSkeleton count={5} />}
isDarkTheme={isDarkMode}
code={code}
/>
)}
Expand Down

0 comments on commit fb3f1a5

Please sign in to comment.