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

Replace <!-- colors --> with Colors MDX component #10401

Merged
merged 6 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions polaris.shopify.com/content/design/colors.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Color plays a key role in creating the overall hierarchy of a screen. Using the

---

## Color pallette
## Color palette

The Polaris color palette is composed of 8 different colors, each with 10 unique shades. These colors are then used to create semantic tokens that style both Polaris components and custom components within the Shopify admin.

{ /* <!-- colors --> */ }
<Colors />

## Tokens

Expand Down
2 changes: 2 additions & 0 deletions polaris.shopify.com/src/components/Markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Tooltip from '../Tooltip';
import Icon from '../Icon';
import {FeaturedCardGrid} from '../FeaturedCardGrid';
import {useCopyToClipboard} from '../../utils/hooks';
import {Colors} from './components/Colors';

const CodeVisibilityContext = createContext<[boolean, (arg: boolean) => void]>([
false,
Expand Down Expand Up @@ -226,6 +227,7 @@ function Markdown<
TipBanner,
Lede,
RichCardGrid,
Colors,
Tip: ({children}) => (
<div className="tip-banner">
<div className="tip-banner__header">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@import '../../../../styles/variables.scss';

.colors {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now delete the related styles from polaris.shopify.com/src/styles/globals.scss 🎉

display: grid;
grid-template-columns: repeat(5, 1fr);
gap: var(--p-space-4);
margin-bottom: var(--p-space-8);
font-size: var(--font-size-75);
font-weight: var(--p-font-weight-semibold);

@media (min-width: $breakpointTablet) {
grid-template-columns: repeat(10, 1fr);
}

.colors-swatch {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is a CSS Module, we should be able to export the classnames and use them in the JS rather than having global CSS classes:

Suggested change
.colors {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: var(--p-space-4);
margin-bottom: var(--p-space-8);
font-size: var(--font-size-75);
font-weight: var(--p-font-weight-semibold);
@media (min-width: $breakpointTablet) {
grid-template-columns: repeat(10, 1fr);
}
.colors-swatch {
.Colors {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: var(--p-space-4);
margin-bottom: var(--p-space-8);
font-size: var(--font-size-75);
font-weight: var(--p-font-weight-semibold);
@media (min-width: $breakpointTablet) {
grid-template-columns: repeat(10, 1fr);
}
.ColorsSwatch {

position: relative;
overflow: hidden;
border-radius: var(--p-border-radius-2);
margin-bottom: var(--p-space-2);

&:before {
content: "";
display: block;
padding-bottom: 75%;
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as colorsObj from '../../../../../../polaris-tokens/dist/esm/src/colors.mjs';
import {capitalize} from '../../../../utils/various';
import './Colors.module.scss';

type ColorScale = 50 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;

type ColorValue = {
[index in ColorScale]: string;
};

interface Colors {
[key: string]: ColorValue;
}

const colors = colorsObj as unknown as Colors;

export function Colors() {
const colorOrder = [
'gray',
'green',
'teal',
'blue',
'purple',
'red',
'orange',
'yellow',
];

const colorMap = colorOrder.map((color) => {
const shades: ColorValue = colors[color] ?? [];
const swatches = Object.entries(shades)
.sort(([prevShade], [nextShade]) =>
Number(prevShade) < Number(nextShade) ? 1 : -1,
)
.map(([shade, value]) => (
<div key={value}>
<div className="colors-swatch" style={{backgroundColor: value}}></div>
<div>{shade}</div>
</div>
));

return (
<>
<h3>{capitalize(color)}</h3>
<div className="colors">{swatches}</div>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using CSS modules:

Suggested change
import './Colors.module.scss';
type ColorScale = 50 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
type ColorValue = {
[index in ColorScale]: string;
};
interface Colors {
[key: string]: ColorValue;
}
const colors = colorsObj as unknown as Colors;
export function Colors() {
const colorOrder = [
'gray',
'green',
'teal',
'blue',
'purple',
'red',
'orange',
'yellow',
];
const colorMap = colorOrder.map((color) => {
const shades: ColorValue = colors[color] ?? [];
const swatches = Object.entries(shades)
.sort(([prevShade], [nextShade]) =>
Number(prevShade) < Number(nextShade) ? 1 : -1,
)
.map(([shade, value]) => (
<div key={value}>
<div className="colors-swatch" style={{backgroundColor: value}}></div>
<div>{shade}</div>
</div>
));
return (
<>
<h3>{capitalize(color)}</h3>
<div className="colors">{swatches}</div>
import styles from './Colors.module.scss';
type ColorScale = 50 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
type ColorValue = {
[index in ColorScale]: string;
};
interface Colors {
[key: string]: ColorValue;
}
const colors = colorsObj as unknown as Colors;
export function Colors() {
const colorOrder = [
'gray',
'green',
'teal',
'blue',
'purple',
'red',
'orange',
'yellow',
];
const colorMap = colorOrder.map((color) => {
const shades: ColorValue = colors[color] ?? [];
const swatches = Object.entries(shades)
.sort(([prevShade], [nextShade]) =>
Number(prevShade) < Number(nextShade) ? 1 : -1,
)
.map(([shade, value]) => (
<div key={value}>
<div className={styles.ColorsSwatch} style={{backgroundColor: value}}></div>
<div>{shade}</div>
</div>
));
return (
<>
<h3>{capitalize(color)}</h3>
<div className={styles.Colors}>{swatches}</div>

</>
);
});

return <>{colorMap}</>;
}
40 changes: 0 additions & 40 deletions polaris.shopify.com/src/utils/markdown.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import yaml from 'js-yaml';

import * as colors from '../../../polaris-tokens/dist/esm/src/colors.mjs';

export const parseMarkdown = (inputMarkdown) => {
const readmeSections = inputMarkdown.split('---');
const frontMatterSection = readmeSections[1];
Expand Down Expand Up @@ -68,40 +66,6 @@ export const parseMarkdown = (inputMarkdown) => {
});
}

// Add some custom HTML to <!-- colors --> tags
const colorsRegex = /<!-- (colors) -->/gis;
if (markdown.match(colorsRegex)) {
markdown = markdown.replace(colorsRegex, (match) => {
const colorOrder = [
'gray',
'green',
'teal',
'blue',
'purple',
'red',
'orange',
'yellow',
];

return colorOrder.reduce((acc, color) => {
const shades = colors[color] ?? [];
const swatches = Object.entries(shades)
.sort(([prevShade], [nextShade]) =>
Number(prevShade) < Number(nextShade) ? 1 : -1,
)
.map(
([shade, value]) =>
`<div><div class="colors-swatch" style="background-color: ${value};"></div><div>${shade}</div></div>`,
)
.join('');

return `${acc}<h3>${capitalize(
color,
)}</h3><div class="colors">${swatches}</div>`;
}, '');
});
}

const out = {
frontMatter,
description,
Expand All @@ -110,7 +74,3 @@ export const parseMarkdown = (inputMarkdown) => {

return out;
};

function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
4 changes: 4 additions & 0 deletions polaris.shopify.com/src/utils/various.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ export const uppercaseFirst = (str: string): string =>

export const deslugify = (str: string): string =>
uppercaseFirst(str.replace(/-+/g, ' '));

export const capitalize = (str: string) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};