Skip to content

Commit

Permalink
Add semantic color tokens to BaseStyles (#1880)
Browse files Browse the repository at this point in the history
* Add new semantic color tokens to BaseStyles

* Remove unnecessary aliases

* Fix border typo

* Fix comments

* Rename color role normal to neutral

* Update special color names

* Add disabled colors

* Remove aliasing logic

All colors are now named directly as their aliases. The 4-part token structure only serves as a mental model but is omitted in code (it will be detailed in documentation). This makes things more consistent, reduces the number of variables in global styles, and aligns closely to Figma.

* Update snapshots

* Document custom properties on theme page

* Add changeset

* Add ref to Figma in docs and fix typos
  • Loading branch information
Robin Métral authored Jan 10, 2023
1 parent 65a7a6c commit 38dcc5c
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-buttons-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sumup/circuit-ui': minor
---

Added semantic color tokens as CSS custom properties to the `BaseStyles`. See the [documentation](https://circuit.sumup.com/?path=/docs/features-theme--page) for details.
108 changes: 108 additions & 0 deletions .storybook/components/ColorsTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright 2023, SumUp Ltd.
* 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 { useEffect, useState } from 'react';
import { css, ThemeProvider } from '@emotion/react';
import { light } from '@sumup/design-tokens';
import {
Anchor,
spacing,
Table,
ToastProvider,
useNotificationToast,
} from '@sumup/circuit-ui';

const getCustomProperty = (property: string) => {
return getComputedStyle(document.documentElement).getPropertyValue(property);
};

function Copy({ property }: { property: string }) {
const { setToast } = useNotificationToast();
return (
<Anchor
css={spacing({ left: 'kilo' })}
size="two"
/* https://caniuse.com/mdn-api_clipboard_writetext */
onClick={() =>
navigator.clipboard
.writeText(property)
.then(() => setToast({ body: 'Copied!', variant: 'confirm' }))
}
>
Copy
</Anchor>
);
}

const getRows = (customProperties: string[][]) => {
return customProperties.map(([property, value]) => [
{
children: (
<div
css={css`
display: flex;
`}
>
<code>{property}</code>
<Copy property={property} />
</div>
),
},
{ children: <code>{value}</code> },
{
align: 'right' as const,
children: (
<span
css={css`
display: block;
width: auto;
height: 1em;
background: var(${property});
border: 1px solid var(--cui-border-normal);
`}
/>
),
},
]);
};

function ColorsTable({ customProperties }: { customProperties: string[] }) {
const [colors, setColors] = useState<string[][]>();

useEffect(() => {
setColors(
customProperties.map((property) => [
property,
getCustomProperty(property),
]),
);
}, []);

return (
<ThemeProvider theme={light}>
<ToastProvider>
{colors && (
<Table
condensed
headers={['Property name', 'Color value', 'Preview']}
rows={getRows(colors)}
/>
)}
</ToastProvider>
</ThemeProvider>
);
}

export default ColorsTable;
1 change: 1 addition & 0 deletions .storybook/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { default as Swatch } from './Swatch';
export { default as Type } from './Type';
export { default as Spacing } from './Spacing';
export { default as MediaQueriesTable } from './MediaQueriesTable';
export { default as ColorsTable } from './ColorsTable';
export { default as BorderWidth } from './BorderWidth';
export { default as BorderRadius } from './BorderRadius';
export { default as IconSize } from './IconSize';
Expand Down
2 changes: 1 addition & 1 deletion .storybook/util/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const components = {
h4: withThemeProvider(Headline, {
as: 'h4',
size: 'four',
css: spacing({ top: 'giga' }),
css: spacing({ top: 'giga', bottom: 'mega' }),
}),
h5: withThemeProvider(SubHeadline, {
as: 'h5',
Expand Down
Loading

1 comment on commit 38dcc5c

@vercel
Copy link

@vercel vercel bot commented on 38dcc5c Jan 10, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.