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

Design Tokens Manager - Scale export + theme utlis for grommet-demo-app #4593

Merged
merged 10 commits into from
Dec 16, 2024
3 changes: 3 additions & 0 deletions design-tokens-manager/src/routes/Scaler/Results.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export const Results = ({
<ScaleToolbar
direction="row-reverse"
scale={scale}
settings={{ base, contentBase, factor, steps, nearest }}
contentSizes={tshirtContent}
spacingSizes={tshirtSpacing}
open={open}
setOpen={setOpen}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,24 @@ import { Box, Button } from 'grommet';
import { Sidebar } from 'grommet-icons';
import { CopyButton } from '../../../components/CopyButton';

export const ScaleToolbar = ({ scale, open, setOpen, ...rest }) => {
export const ScaleToolbar = ({
scale: scaleProp,
settings,
contentSizes,
spacingSizes,
open,
setOpen,
...rest
}) => {
const scale = {
name: `${settings.base}-${settings.factor}-${settings.nearest}-${settings.steps}`,
description: `${settings.base}px based scale at ${settings.factor} factor. Rounded to ${settings.nearest}px grid unit with ${settings.steps} steps`,
settings: settings,
scale: scaleProp,
content: contentSizes,
spacing: spacingSizes,
};

return (
<Box gap="xsmall" flex={false} {...rest}>
<Button
Expand Down
2 changes: 1 addition & 1 deletion sandbox/grommet-app/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext, useState, useEffect, useMemo } from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { Grommet, Box } from 'grommet';
import { themes } from './theme';
import { themes } from './themes/theme';
import Sustainability from './pages/sustainability/index';
import Home from './pages/index';
import NextDashboard from './pages/next/index';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
HelpOption,
Menu,
} from 'grommet-icons';
import { themes } from '../../theme';
import { themes } from '../../themes/theme';
import { ToggleGroup } from '../ToggleGroup/ToggleGroup';

// TO DO fix animation once motion tokens are added
Expand Down
2 changes: 1 addition & 1 deletion sandbox/grommet-app/src/pages/sticker-sheet/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
} from 'grommet';
import { User, Table, List, MapLocation, Previous } from 'grommet-icons';
import { hpe } from 'grommet-theme-hpe';
import { current as hpeCurrent } from '../../theme';
import { current as hpeCurrent } from '../../themes/theme';
import ContentPane from '../../components/ContentPane';

const StyleInProgress = () => (
Expand Down
81 changes: 81 additions & 0 deletions sandbox/grommet-app/src/themes/theme-utils/scale-to-theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// scale-to-theme.js
// Utility functions to convert scale definitions and t-shirt sizes to theme objects
// Scale definition example:
// {
// name: string,
// description: string,
// settings: {
// base: integer,
// contentBase: integer,
// factor: number,
// steps: integer,
// nearest: integer,
// },
// scale: integer[];
// content: { size: string, value: integer }[];
// }

const scaleToThemeObject = ({ scale, index = 0 }) => {
const result = {};
scale.forEach((step, i) => {
const { size, value } = step;
if (index !== 0) {
result[size] =
i + index >= 0 ? `${scale[i + index].value}px` : `${scale[0].value}px`;
} else {
result[size] = `${value}px`;
}
});
return result;
};

const pageThemeObject = ({ scale, target }) => {
const max = scale.reduce(
(acc, { size, value }) => {
// Minimizing the difference between the target and the value
const test =
!acc.value || Math.abs(target - value) < Math.abs(target - acc.value);
acc.size = test ? size : acc.size;
acc.value = test ? value : acc.value;
return acc;
},
{ size: undefined, value: undefined },
);

return { width: { max: max.size } };
};

export const createTheme = ({ spacing, content }) => {
return {
global: {
breakpoints: {
xsmall: {
edgeSize: {
...scaleToThemeObject({ scale: spacing, index: -1 }),
},
size: {
...scaleToThemeObject({ scale: content, index: -1 }),
},
},
small: {
edgeSize: {
...scaleToThemeObject({ scale: spacing, index: -1 }),
},
size: {
...scaleToThemeObject({ scale: content, index: -1 }),
},
},
},
edgeSize: {
...scaleToThemeObject({ scale: spacing }),
},
size: {
...scaleToThemeObject({ scale: content }),
},
},
page: {
wide: { ...pageThemeObject({ scale: content, target: 1536 }) },
narrow: { ...pageThemeObject({ scale: content, target: 768 }) },
},
};
};
Loading