Skip to content

Commit

Permalink
Handle bound var theme toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhlongviolin1 committed Dec 23, 2024
1 parent cacd400 commit c660dc4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
30 changes: 24 additions & 6 deletions frontend/taipy-gui/src/components/Taipy/ThemeToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import ToggleButtonGroup from "@mui/material/ToggleButtonGroup";
import WbSunny from "@mui/icons-material/WbSunny";
import Brightness3 from "@mui/icons-material/Brightness3";

import { TaipyActiveProps, getCssSize } from "./utils";
import { TaipyActiveProps, TaipyChangeProps, getCssSize } from "./utils";
import { TaipyContext } from "../../context/taipyContext";
import { createThemeAction } from "../../context/taipyReducers";
import { useClassNames } from "../../utils/hooks";
import { createSendUpdateAction, createThemeAction } from "../../context/taipyReducers";
import { useClassNames, useModule } from "../../utils/hooks";
import { getLocalStorageValue } from "../../context/utils";
import { getComponentClassName } from "./TaipyStyle";

interface ThemeToggleProps extends TaipyActiveProps {
interface ThemeToggleProps extends TaipyActiveProps, TaipyChangeProps {
style?: SxProps;
label?: string;
width?: string | number;
Expand All @@ -53,11 +53,29 @@ const ThemeToggle = (props: ThemeToggleProps) => {
const { id, label = "Mode", style = emptyStyle, active = true } = props;
const { state, dispatch } = useContext(TaipyContext);

const module = useModule();

const className = useClassNames(props.libClassName, props.dynamicClassName, props.className);

const updateBoundValue = useCallback(
(val: PaletteMode) => {
if (!props.updateVarName) {
return;
}
dispatch(
createSendUpdateAction(props.updateVarName, val === "dark", module, props.onChange, props.propagate),
);
},
[dispatch, props.updateVarName, props.onChange, props.propagate, module],
);

useEffect(() => {
updateBoundValue(state.theme.palette.mode);
}, [state.theme.palette.mode, updateBoundValue]);

const changeMode = useCallback(
(evt: MouseEvent, mode: PaletteMode) => mode !== null && dispatch(createThemeAction(mode === "dark")),
[dispatch]
[dispatch],
);

useEffect(() => {
Expand All @@ -72,7 +90,7 @@ const ThemeToggle = (props: ThemeToggleProps) => {
props.width
? ({ ...boxSx, ...style, width: getCssSize(props.width) } as SxProps)
: ({ ...boxSx, ...style } as SxProps),
[style, props.width]
[style, props.width],
);

return (
Expand Down
2 changes: 1 addition & 1 deletion taipy/gui/viselements.json
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@
"name": "value",
"default_property": true,
"type": "dynamic(Any)",
"doc": "Bound to the selection value."
"doc": "Bound to the selection value. If this is used for theme toggling, it will return a boolean indicating whether the current theme is set to dark or not."
},
{
"name": "lov",
Expand Down

0 comments on commit c660dc4

Please sign in to comment.