-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16ba4ba
commit b4e4be8
Showing
1 changed file
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,75 @@ | ||
from __future__ import annotations | ||
|
||
import inspect | ||
import pathlib | ||
|
||
import param | ||
from panel.config import config | ||
from panel.custom import ReactComponent | ||
from panel.util import classproperty | ||
|
||
COLORS = ["primary", "secondary", "error", "info", "success", "warning"] | ||
|
||
|
||
THEME_WRAPPER = """ | ||
import {{ ThemeProvider, createTheme }} from '@mui/material/styles'; | ||
import CssBaseline from '@mui/material/CssBaseline'; | ||
{esm} | ||
function themed_render(props) {{ | ||
const [defaultTheme] = props.model.useState('theme') | ||
const theme = createTheme({{ | ||
colorSchemes: {{ | ||
dark: defaultTheme === "dark", | ||
}}, | ||
}}); | ||
return ( | ||
<ThemeProvider theme={{theme}}> | ||
<CssBaseline /> | ||
<Panel{component} {{...props}}/> | ||
</ThemeProvider> | ||
) | ||
}} | ||
export default {{ render: themed_render }} | ||
""" | ||
|
||
|
||
class MaterialComponent(ReactComponent): | ||
theme = param.Selector(default=config.theme, objects=["default", "dark"]) | ||
|
||
theme = param.Selector(default="default", objects=["default", "dark"]) | ||
|
||
_importmap = { | ||
"imports": { | ||
"@mui/material/": "https://esm.sh/@mui/material@6.1.7/", | ||
"@mui/icons-material/": "https://esm.sh/@mui/icons-material@6.1.6/", | ||
"@mui/icons-material/": "https://esm.sh/@mui/icons-material@6.1.7/", | ||
} | ||
} | ||
|
||
_esm_base = None | ||
|
||
# _bundle = "panel-material-ui.bundle.js" | ||
|
||
def __init__(self, **params): | ||
if 'theme' not in params: | ||
params['theme'] = config.theme | ||
super().__init__(**params) | ||
|
||
@classproperty | ||
def _esm(cls): | ||
if cls._esm_base is None: | ||
return None | ||
esm_base = pathlib.Path(inspect.getfile(cls)).parent / cls._esm_base | ||
component = cls.__name__ | ||
esm = ( | ||
esm_base | ||
.read_text() | ||
.replace('export function render(', f'export function Panel{component}(') | ||
.replace('const render =', f'const Panel{component} =') | ||
) | ||
return THEME_WRAPPER.format(esm=esm, component=component) | ||
|
||
__abstract = True |