Skip to content

Commit 4ebba11

Browse files
committed
refactored carbonization/foundations/color page and ShwCarbonizationTokenPreviewColor to show actual color values in preview item
1 parent b1dae75 commit 4ebba11

File tree

7 files changed

+116
-65
lines changed

7 files changed

+116
-65
lines changed
Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
1-
import Component from '@glimmer/component';
1+
import type { TemplateOnlyComponent } from '@ember/component/template-only';
22
import { pageTitle } from 'ember-page-title';
3+
import { helper } from '@ember/component/helper';
4+
import { eq, notEq, and } from 'ember-truth-helpers';
35

46
import ShwTextH1 from 'showcase/components/shw/text/h1';
5-
import ShwCarbonizationTokenPreviewColor from 'showcase/components/shw/carbonization/token-preview-color';
7+
import ShwCarbonizationComparisonGrid from 'showcase/components/shw/carbonization/comparison-grid';
8+
import ShwCarbonizationTokenPreviewHdsColor from 'showcase/components/shw/carbonization/token-preview/hds-color';
9+
10+
import type { DesignToken } from '../../../../types/design-token';
611

712
import TOKENS_RAW from '@hashicorp/design-system-tokens/dist/docs/products/tokens.json';
813

9-
export default class ColorCarbonization extends Component {
10-
get allColorTokens() {
11-
return TOKENS_RAW.filter((token) => token.$type === 'color');
12-
}
14+
const tokenShortName = helper(function ([token]: [DesignToken]) {
15+
return token.name?.replace(/^token-/, '');
16+
});
1317

14-
<template>
15-
{{pageTitle "Color - Carbonization"}}
18+
const ColorCarbonization: TemplateOnlyComponent = <template>
19+
{{pageTitle "Color - Carbonization"}}
1620

17-
<ShwTextH1>Color - Carbonization</ShwTextH1>
21+
<ShwTextH1>Color - Carbonization</ShwTextH1>
1822

19-
<section>
20-
{{#each this.allColorTokens as |token|}}
21-
{{! @glint-ignore - we know all the tokens of type 'color' have values as 'strings' }}
22-
<ShwCarbonizationTokenPreviewColor @token={{token}} />
23-
{{/each}}
24-
</section>
25-
</template>
26-
}
23+
<section>
24+
{{#each TOKENS_RAW as |token|}}
25+
{{#if (and (eq token.$type "color") (notEq token.name undefined))}}
26+
<ShwCarbonizationComparisonGrid @label={{tokenShortName token}}>
27+
<:theming as |T|>
28+
<ShwCarbonizationTokenPreviewHdsColor
29+
@mode={{T.theme}}
30+
@tokenName={{token.name}}
31+
/>
32+
</:theming>
33+
</ShwCarbonizationComparisonGrid>
34+
{{/if}}
35+
{{/each}}
36+
</section>
37+
</template>;
38+
export default ColorCarbonization;

showcase/app/components/page-carbonization/foundations/typography.gts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import ShwTextH1 from 'showcase/components/shw/text/h1';
1010

1111
import ShwFlex from 'showcase/components/shw/flex';
1212
import ShwCarbonizationComparisonGrid from 'showcase/components/shw/carbonization/comparison-grid';
13-
import ShwCarbonizationStylePreviewTypography from 'showcase/components/shw/carbonization/style-preview-typography';
13+
import ShwCarbonizationStylePreviewTypography from 'showcase/components/shw/carbonization/style-preview/typography';
1414

1515
import { STYLES_COMBINATIONS } from 'showcase/components/page-foundations/typography/sub-sections/styles';
1616

showcase/app/components/shw/carbonization/token-preview-color/index.gts

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Component from '@glimmer/component';
2+
import style from 'ember-style-modifier';
3+
4+
import ShwLabel from 'showcase/components/shw/label';
5+
import THEMED_TOKENS_RAW from '@hashicorp/design-system-tokens/dist/docs/products/themed-tokens.json';
6+
7+
import type { HdsModes } from '@hashicorp/design-system-components/services/hds-theming';
8+
9+
import type { DesignToken } from '../../../../../types/design-token';
10+
11+
export interface ShwCarbonizationTokenPreviewColorSignature {
12+
Args: {
13+
mode: HdsModes;
14+
tokenName: string;
15+
};
16+
}
17+
18+
export default class ShwCarbonizationTokenPreviewColor extends Component<ShwCarbonizationTokenPreviewColorSignature> {
19+
get background(): string {
20+
return `var(--${this.args.tokenName})`;
21+
}
22+
23+
get value(): string {
24+
const allThemedTokensForCurrentMode:
25+
| Record<string, DesignToken>
26+
| undefined = THEMED_TOKENS_RAW[this.args.mode];
27+
const themedToken = allThemedTokensForCurrentMode?.[this.args.tokenName];
28+
29+
if (themedToken?.$value) {
30+
return themedToken.$value as string;
31+
} else {
32+
console.error(
33+
`Missing themed token for theme=${this.args.mode} and name=${this.args.tokenName}`,
34+
);
35+
return 'Undefined token value';
36+
}
37+
}
38+
39+
<template>
40+
<div class="shw-carbonization-token-preview-color-wrapper">
41+
<ShwLabel
42+
class="shw-carbonization-token-preview-color-value"
43+
>{{this.value}}</ShwLabel>
44+
<div
45+
class="shw-carbonization-token-preview-color"
46+
{{style background=this.background}}
47+
/>
48+
</div>
49+
</template>
50+
}
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1+
.shw-carbonization-token-preview-color-wrapper {
2+
position: relative;
3+
}
4+
5+
.shw-carbonization-token-preview-color-value {
6+
position: absolute;
7+
top: 4px;
8+
left: 4px;
9+
margin: 0px;
10+
padding: 2px 3px;
11+
color: var(--content-foreground-color);
12+
font-size: 0.7rem;
13+
background-color: var(--content-background-color);
14+
border-radius: 3px;
15+
}
16+
117
.shw-carbonization-token-preview-color {
218
width: 100%;
3-
height: 32px;
19+
height: 50px;
420
border-radius: 6px;
521
box-shadow: inset 0 0 1px 0 rgba(0, 0, 0, 25%);
622
}

showcase/types/design-token.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface DesignToken {
2+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3+
value?: any;
4+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5+
$value?: any;
6+
type?: string;
7+
$type?: string;
8+
$description?: string;
9+
name?: string;
10+
comment?: string;
11+
themeable?: boolean;
12+
attributes?: Record<string, unknown>;
13+
/**
14+
* When flattening tokens, DesignToken is given a key that matches the original ancestor tree e.g. `{colors.red.500}`
15+
*/
16+
key?: string;
17+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
18+
[key: string]: any;
19+
}

0 commit comments

Comments
 (0)