Skip to content

Commit 3192b29

Browse files
committed
feat(tokens): add gray colors
1 parent 81024ab commit 3192b29

File tree

4 files changed

+70
-19
lines changed

4 files changed

+70
-19
lines changed

core/src/themes/base/default.tokens.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { generateColorSteps } from '../../utils/theme';
12
import type { DefaultTheme } from '../themes.interfaces';
23

34
import { darkTheme } from './dark.tokens';
@@ -160,4 +161,8 @@ export const defaultTheme: DefaultTheme = {
160161
xl: '2',
161162
xxl: '2.4',
162163
},
164+
165+
color: {
166+
gray: generateColorSteps('#fff', '#000'),
167+
},
163168
};

core/src/themes/native/native.theme.default.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ $text-color-step-100: var(
113113
$text-color-step-150: var(
114114
--ion-color-step-850,
115115
var(--ion-text-color-step-150, mix($background-color-value, $text-color-value, 15%))
116-
);
116+
); // 262626 gray-850
117117
$text-color-step-200: var(
118118
--ion-color-step-800,
119119
var(--ion-text-color-step-200, mix($background-color-value, $text-color-value, 20%))

core/src/themes/themes.interfaces.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { PredefinedColors } from '../interface';
12
import type { IonicConfig } from '../utils/config';
23

34
// Platform-specific theme
@@ -218,24 +219,7 @@ export type BaseTheme = {
218219
};
219220

220221
// COLOR TOKENS
221-
color?: {
222-
[key: string]: {
223-
bold: {
224-
base: string;
225-
contrast: string;
226-
foreground: string;
227-
shade: string;
228-
tint: string;
229-
};
230-
subtle: {
231-
base: string;
232-
contrast: string;
233-
foreground: string;
234-
shade: string;
235-
tint: string;
236-
};
237-
};
238-
};
222+
color?: Colors;
239223

240224
// PLATFORM SPECIFIC OVERRIDES
241225
ios?: PlatformTheme;
@@ -261,3 +245,34 @@ export type DefaultTheme = BaseTheme & {
261245

262246
config?: IonicConfig;
263247
};
248+
249+
// Semantic color value structure
250+
type SemanticColorValue = {
251+
base: string;
252+
contrast: string;
253+
foreground: string;
254+
shade: string;
255+
tint: string;
256+
};
257+
258+
// Semantic color hue value structure
259+
type SemanticHue = {
260+
bold?: SemanticColorValue;
261+
subtle?: SemanticColorValue;
262+
};
263+
264+
// Number string keys structure
265+
export type NumberStringKeys = {
266+
// Enforce keys are strings of numbers (like 50, '50', etc.)
267+
[K in number as `${K}`]?: string;
268+
};
269+
270+
// Primitive color keys
271+
type PrimitiveColors = 'gray';
272+
273+
// Colors interface
274+
type Colors = {
275+
[K in PredefinedColors]?: SemanticHue;
276+
} & {
277+
[K in PrimitiveColors]?: NumberStringKeys;
278+
};

core/src/utils/theme.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { printIonWarning } from '@utils/logging';
22

33
import type { Color, CssClassMap } from '../interface';
4+
import type { NumberStringKeys } from '../themes/themes.interfaces';
45

56
import { deepMerge } from './helpers';
67

@@ -471,3 +472,33 @@ export const mix = (baseColor: string, mixColor: string, weight: string): string
471472
const toHex = (n: number) => n.toString(16).padStart(2, '0');
472473
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
473474
};
475+
476+
/**
477+
* Generates a color scale object by mixing a light and dark color.
478+
*
479+
* This function creates ten distinct shade levels (50, 100, 150, ..., 950)
480+
* by mixing the light color into the dark color using mix percentages
481+
* that increment in steps of 5%.
482+
*
483+
* The final output is an object where keys are the shade levels (50-950)
484+
* and values are the resulting mixed hex codes.
485+
*
486+
* @param {string} lightColor - The lighter base color hex value.
487+
* @param {string} darkColor - The darker base color hex value.
488+
* @returns {NumberStringKeys} An object of color shades.
489+
*
490+
* @example
491+
* mix('#ffffff', '#000000', 5%) results in the color for key '50'
492+
* mix('#ffffff', '#000000', 95%) results in the color for key '950'
493+
*/
494+
export const generateColorSteps = (lightColor: string, darkColor: string): NumberStringKeys => {
495+
const colorSteps: NumberStringKeys = {};
496+
497+
for (let i = 50; i <= 950; i += 50) {
498+
const weight = `${i / 10}%`;
499+
500+
colorSteps[i.toString() as keyof NumberStringKeys] = mix(lightColor, darkColor, weight);
501+
}
502+
503+
return colorSteps;
504+
};

0 commit comments

Comments
 (0)