This React example shows how themes for both a grid and a chart can be be toggled via one button.
When using React, changing the grid's theme is quite easy, we can use a `useEffect` hook to track the change of theme:
const [lightModeActive, setLightModeActive] = useState<boolean>(true);
This can be toggled via a button, each time it's pressed, the grid is re-rendered and the `className` re-evaluated.
Since chart-themes are passed as a grid-property:
chartThemes={[lightModeActive ? 'ag-vivid' : 'ag-vivid-dark']}
useEffect(() => { const api = gridRef.current?.api; if (!api) return; if (!api.getChartRef) return; let id = api.getChartModels()[0]?.chartId; api.getChartRef(id).destroyChart(); createChart(api); }, [lightModeActive]);
Note that here we are using the following function to create the chart:
const createChart = useCallback((api: GridApi): void => { api.createRangeChart({ cellRange: { rowStartIndex: 0, rowEndIndex: 79, columns: ['country', 'gold', 'silver', 'bronze'], }, chartType: 'groupedColumn', chartContainer: chartDivRef.current, aggFunc: 'sum', }); }, []);
Pre-28
It is possible to customise AG Grid Theme by using CSS variables and change them dynamically.
Source Code (stackblitz): https://stackblitz.com/edit/ag-grid-dynamic-theme-styling-by-updating-css-variables?file=src/App.scss
docs for themes customising: https://www.ag-grid.com/javascript-data-grid/themes-customising/
How it works
In App.scss root there are custom variables.
The custom variables are assigned to corresponding ag-grid CSS variable theme parameters.
There is a function that updates the custom variables.
Simplified example
Source Code (stackblitz): https://stackblitz.com/edit/dynamic-theme-styling-by-updating-css-variables-simple-example?file=src%2FApp.scss
Comments
0 comments
Please sign in to leave a comment.