Skip to content

Commit

Permalink
fix(theme): add mui v4 and v5 grid examples
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Jerolimov <[email protected]>
  • Loading branch information
christoph-jerolimov committed Dec 14, 2024
1 parent cb427cd commit e5d1a5a
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Buttons = () => {
</tr>
{colors.map(color => (
<tr key={color}>
<td>{color ?? 'undefined'}</td>
<td>{color ?? 'no color'}</td>
{variants.map(variant => (
<td key={variant}>
<Button color={color} variant={variant}>
Expand Down Expand Up @@ -74,7 +74,7 @@ const Checkboxes = () => {
</tr>
{colors.map(color => (
<tr key={color}>
<td>{color ?? 'undefined'}</td>
<td>{color ?? 'no color'}</td>
<td>
<FormControlLabel
control={<Checkbox defaultChecked color={color} />}
Expand Down
201 changes: 201 additions & 0 deletions workspaces/theme/plugins/mui4-test/src/components/GridExamples.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import Box from '@material-ui/core/Box';
import Card from '@material-ui/core/Card';
import Checkbox from '@material-ui/core/Checkbox';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Grid, { GridProps } from '@material-ui/core/Grid';

const GridContainer = (props: GridProps) => <Grid container {...props} />;

const GridItem = (props: GridProps) => (
<Grid
item
xs={12} // show 1 item per row on extra small screens
sm={6} // show 2 items per row on small screens
md={4} // show 3 items per row on medium screens
lg={3} // show 4 items per row on large screens
xl={2} // show 6 items per row on extra large screens
{...props}
/>
);

export const CommonGridExamples = () => {
const cards = Array.from({ length: 8 }, (_, i) => i + 1);
return (
<>
<h1>
Default: Grid container without spacing, an unstyled Grid item and an
unstyled Card
</h1>
<GridContainer>
{cards.map(cardContent => (
<GridItem key={cardContent}>
<Card>{cardContent}</Card>
</GridItem>
))}
</GridContainer>

<h1>
Grid container with spacing=2, an unstyled Grid item and an unstyled
Card
</h1>
<GridContainer spacing={2}>
{cards.map(cardContent => (
<GridItem key={cardContent}>
<Card>{cardContent}</Card>
</GridItem>
))}
</GridContainer>

<h1>
Grid container with spacing=4, an unstyled Grid item and a Card with
padding=2
</h1>
<GridContainer spacing={4}>
{cards.map(cardContent => (
<GridItem key={cardContent}>
<Card style={{ padding: 16 }}>{cardContent}</Card>
</GridItem>
))}
</GridContainer>
</>
);
};

const DebugGridExamples = () => {
const backgroundColors = [
'#600',
'#060',
'#800',
'#080',
'#a00',
'#0a0',
'#d00',
'#0d0',
];
return (
<>
<h1>
Grid container without spacing, an unstyled Grid item and colorized Card
</h1>
<GridContainer>
{backgroundColors.map((backgroundColor, index) => (
<GridItem key={backgroundColor}>
<Card style={{ color: '#ffffff', backgroundColor }}>
{index + 1}
</Card>
</GridItem>
))}
</GridContainer>

<h1>
Grid container with colorized Grid item to show all-sided padding, Card
position is fine
</h1>
<GridContainer>
{backgroundColors.map((backgroundColor, index) => (
<GridItem
key={backgroundColor}
style={{ color: '#ffffff', backgroundColor }}
>
<Card>{index + 1}</Card>
</GridItem>
))}
</GridContainer>

<h1>
Grid container with spacing=4, an unstyled Grid item and a colorized
Card with padding=16
</h1>
<GridContainer spacing={4}>
{backgroundColors.map((backgroundColor, index) => (
<GridItem key={backgroundColor}>
<Card style={{ color: '#ffffff', backgroundColor, padding: 16 }}>
{index + 1}
</Card>
</GridItem>
))}
</GridContainer>

<h1>
Grid container with spacing=4, an colorized Grid item to show all-sided
padding, Card position is fine
</h1>
<GridContainer spacing={4}>
{backgroundColors.map((backgroundColor, index) => (
<GridItem
key={backgroundColor}
style={{ color: '#ffffff', backgroundColor }}
>
<Card style={{ padding: 16 }}>{index + 1}</Card>
</GridItem>
))}
</GridContainer>

<h1>
Grid container without spacing and Grid item with padding=32 result in
aligned Cards! (Not in MUI v5!)
</h1>
<GridContainer>
{backgroundColors.map((backgroundColor, index) => (
<GridItem
key={backgroundColor}
style={{ color: '#ffffff', backgroundColor, padding: 32 }}
>
<Card>{index + 1}</Card>
</GridItem>
))}
</GridContainer>

<h1>
Grid container with spacing=4 and Grid item with padding=32 result also
in aligned Cards! (Not in MUI v5!)
</h1>
<GridContainer spacing={4}>
{backgroundColors.map((backgroundColor, index) => (
<GridItem
key={backgroundColor}
style={{ color: '#ffffff', backgroundColor, padding: 32 }}
>
<Card>{index + 1}</Card>
</GridItem>
))}
</GridContainer>
</>
);
};

export const GridExamples = () => {
const [showDebugExamples, setShowDebugExamples] = React.useState(false);
return (
<>
<CommonGridExamples />
<Box sx={{ pt: 4, pb: 4 }}>
<FormControlLabel
control={
<Checkbox
onChange={(_, checked) => setShowDebugExamples(checked)}
/>
}
label="Show debug examples"
/>
</Box>
{showDebugExamples && <DebugGridExamples />}
</>
);
};
40 changes: 18 additions & 22 deletions workspaces/theme/plugins/mui4-test/src/components/MUI4TestPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,32 @@ import { UserSettingsThemeToggle } from '@backstage/plugin-user-settings';

import { FormComponents } from './FormComponents';
import { PaperExamples } from './PaperExamples';
import { InlineStyles } from './InlineStyles';
import { TabExamples } from './TabExamples';
import { GridExamples } from './GridExamples';
import { InlineStyles } from './InlineStyles';

export const MUI4TestPage = () => {
return (
<Page themeId="tool">
<Header title="MUI v4 / Material UI Test Page">
<Header title="MUI v4 Test Page">
<UserSettingsThemeToggle />
</Header>
<TabbedLayout>
<TabbedLayout.Route
path="/"
title="Form components"
children={<FormComponents />}
/>
<TabbedLayout.Route
path="/paper-examples"
title="Paper examples"
children={<PaperExamples />}
/>
<TabbedLayout.Route
path="/inline-styles"
title="Inline styles"
children={<InlineStyles />}
/>
<TabbedLayout.Route
path="/tabs"
title="Tabs"
children={<TabExamples />}
/>
<TabbedLayout.Route path="/" title="Form components">
<FormComponents />
</TabbedLayout.Route>
<TabbedLayout.Route path="/papers" title="Papers">
<PaperExamples />
</TabbedLayout.Route>
<TabbedLayout.Route path="/tabs" title="Tabs">
<TabExamples />
</TabbedLayout.Route>
<TabbedLayout.Route path="/grids" title="Grids">
<GridExamples />
</TabbedLayout.Route>
<TabbedLayout.Route path="/inline-styles" title="Inline styles">
<InlineStyles />
</TabbedLayout.Route>
</TabbedLayout>
</Page>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const Checkboxes = () => {
</tr>
{colors.map(color => (
<tr key={color}>
<td>{color ?? 'undefined'}</td>
<td>{color ?? 'no color'}</td>
<td>
<FormControlLabel
control={<Checkbox defaultChecked color={color} />}
Expand Down
Loading

0 comments on commit e5d1a5a

Please sign in to comment.