Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align headers and typography in the theme #1627

Merged
merged 3 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions packages/toolpad-app/src/runtime/AppThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as React from 'react';
import { createTheme, ThemeOptions, PaletteOptions, Theme, ThemeProvider } from '@mui/material';
import { createTheme, ThemeOptions, PaletteOptions, ThemeProvider } from '@mui/material';
import * as colors from '@mui/material/colors';
import * as appDom from '../appDom';
import { AppTheme } from '../types';

export function createThemeOptions(toolpadTheme: AppTheme): ThemeOptions {
export function createToolpadTheme(toolpadTheme: AppTheme = {}): ThemeOptions {
const palette: PaletteOptions = {};
const primary = toolpadTheme['palette.primary.main'];
if (primary) {
Expand All @@ -21,14 +21,42 @@ export function createThemeOptions(toolpadTheme: AppTheme): ThemeOptions {
palette.mode = mode;
}

return { palette };
}
const theme = createTheme();

return createTheme(theme, {
typography: {
h1: {
fontSize: `3.25rem`,
fontWeight: 800,
},

h2: {
fontSize: `2.25rem`,
fontWeight: 700,
},

h3: {
fontSize: `1.75rem`,
fontWeight: 700,
},

h4: {
fontSize: `1.5rem`,
fontWeight: 700,
},

h5: {
fontSize: `1.25rem`,
fontWeight: 700,
},

export function createToolpadTheme(themeNode?: appDom.ThemeNode | null): Theme {
const options = themeNode?.theme
? createThemeOptions(appDom.fromConstPropValues(themeNode.theme))
: {};
return createTheme(options);
h6: {
fontSize: `1.15rem`,
fontWeight: 700,
},
},
palette,
});
}

export interface ThemeProviderProps {
Expand All @@ -41,7 +69,10 @@ export default function AppThemeProvider({ dom, children }: ThemeProviderProps)
const root = appDom.getApp(dom);
const { themes = [] } = appDom.getChildNodes(dom, root);
const themeNode = themes.length > 0 ? themes[0] : null;
return createToolpadTheme(themeNode);
const toolpadTheme: AppTheme = themeNode?.theme
? appDom.fromConstPropValues(themeNode.theme)
: {};
return createToolpadTheme(toolpadTheme);
}, [dom]);

return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
Expand Down
22 changes: 20 additions & 2 deletions packages/toolpad-components/src/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,28 @@ interface TextProps extends Omit<BaseProps, 'children'> {
loading?: boolean;
}

const MarkdownContainer = styled('div')({
const gutters = (marginTop: number, marginBottom: number) => ({
marginTop,
marginBottom,
'&:first-child': {
marginTop: 0,
},
'&:last-child': {
marginBottom: 0,
},
});

const MarkdownContainer = styled('div')(({ theme }) => ({
display: 'block',
[`&:empty::before`]: { content: '""', display: 'inline-block' },
});
h1: { ...theme.typography.h1, ...gutters(16, 16) },
h2: { ...theme.typography.h2, ...gutters(12, 12) },
h3: { ...theme.typography.h3, ...gutters(12, 12) },
h4: { ...theme.typography.h4, ...gutters(12, 12) },
h5: { ...theme.typography.h5, ...gutters(4, 4) },
h6: { ...theme.typography.h6, ...gutters(4, 4) },
p: { margin: 0, marginBottom: 6 },
}));

function Text({ value, markdown, href, loading, mode, sx, ...rest }: TextProps) {
switch (mode) {
Expand Down