Open zagoorland opened 1 month ago
loadTheme()
does not inject the theme's CSS into the page, it simply returns a Promise that resolves with the theme's CSS. You need to add the CSS returned to a <style>
element. This could be achieved with an example like this:
export default function CodeEditor({ language, theme }: CodeEditorProps) {
const [themeCss, setCss] = useState<string | null>(null)
useEffect(() => {
let cancelled = false
loadTheme(theme).then(css => {
if (!cancelled) setCss(css)
})
return () => cancelled = true
}, [theme])
return (
themeCss && <main className="grow overflow-auto github">
<style>{themeCss}</style>
<Editor
language={language}
value="const foo = 'bar'"
style={{
height: "100%",
fontSize: "16px",
}}
>
{(editor) => <BasicSetup editor={editor} />}
</Editor>
</main>
)
}
Note that you'll get multiple <style>
elements with themes on the page if the CodeEditor
component is used more than once. If this is the case, you should move the theme and the <style>
element to a higher component that's only used once per page.
The editor also won't be visible before the theme loads. There are ways around this, but they're cumbersome. You can render the editor even if themeCss
is null, but this will result in an FOUC before the theme loads. It's up to you to decide what's best.
Hope this helps!
It helped! It works perfect now :) There's no way I would solve it by myself, there's no much in the docs about usage of loadTheme function. Please, consider adding some example with select element and useTheme :)
Thank you :)
There is an example showing the loadTheme
function at the bottom of the readme and the function is documented with JSDoc, which you should be able to read in your IDE.
Please, consider adding some example with select element and useTheme :)
Adding a useTheme
hook to prism-react-editor/themes
is a good idea. I will consider this.
Hey. I'm struggling to implement theme switcher using this library. Here's my code snippet:
Using this example styles are not working at all :(
Any tips how to get it working? It would be nice to have some example inside readme :)
Cheers!