Skip to content

Commit

Permalink
Merge pull request #43 from thompsongl/feature/css-in-js_themes
Browse files Browse the repository at this point in the history
Support more files, and arrays in theme
  • Loading branch information
cchaos authored Mar 16, 2021
2 parents f24ec3d + b2cd915 commit 93098f7
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 62 deletions.
3 changes: 2 additions & 1 deletion src-docs/src/components/with_theme/theme_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { applyTheme } from '../../services';
import {
EuiThemeProvider,
EuiThemeDefault,
EuiThemeAmsterdam,
// EuiThemeAmsterdam,
} from '../../../../src/services';
import { EuiThemeAmsterdam } from '../../../../src/themes/eui-amsterdam/theme';

const THEME_NAMES = EUI_THEMES.map(({ value }) => value);

Expand Down
9 changes: 9 additions & 0 deletions src/global_styling/variables/_colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import {
tint,
} from '../functions/_colors';

export const poles = {
ghost: '#FFF',
ink: '#000',
};

const textVariants = {
textPrimary: computed(['colors.primary'], ([primary]) =>
makeHighContrastColor(primary)
Expand All @@ -48,6 +53,8 @@ const textVariants = {
};

export const light_colors = {
...poles,

// Brand
primary: '#006BB4',
accent: '#DD0A73',
Expand Down Expand Up @@ -85,6 +92,8 @@ export const light_colors = {
};

export const dark_colors = {
...poles,

// Brand
primary: '#1BA9F5',
accent: '#F990C0',
Expand Down
16 changes: 9 additions & 7 deletions src/global_styling/variables/_typography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const scale = {
};

export const SCALES = keysOf(scale);
export type EuiFontScale = typeof SCALES[number];
export type EuiFontScale = keyof typeof scale;

const baseline = 4;
const lineHeightMultiplier = 1.5;
Expand Down Expand Up @@ -76,11 +76,13 @@ const fontWeight = {
bold: '700',
};

// @ts-ignore HELP needed with TS
const fontSize: {
[mapType in EuiFontScale]: string;
} = SCALES.reduce((acc, elem) => {
// @ts-ignore HELP needed with TS
type EuiFontSize = {
[mapType in EuiFontScale]: {
fontSize: string;
lineHeight: string;
};
};
const fontSize = SCALES.reduce((acc, elem) => {
acc[elem] = {
fontSize: computed(
[`${COLOR_MODE_KEY}.base`, `${COLOR_MODE_KEY}.font.scale.${elem}`],
Expand All @@ -92,7 +94,7 @@ const fontSize: {
),
};
return acc;
}, {});
}, {} as EuiFontSize);

// TODO -> MOVE TO COMPONENT
// $euiCodeFontWeightRegular: 400;
Expand Down
4 changes: 2 additions & 2 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export {
Computed,
euiThemeDefault,
EuiThemeDefault,
EuiThemeAmsterdam,
euiThemeAmsterdam,
// EuiThemeAmsterdam,
// euiThemeAmsterdam,
EuiThemeColor,
EuiThemeColorMode,
EuiThemeComputed,
Expand Down
4 changes: 1 addition & 3 deletions src/services/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,5 @@ export {
} from './types';
export {
EuiThemeDefault,
euiThemeDefault,
EuiThemeAmsterdam,
euiThemeAmsterdam,
euiThemeDefault, // EuiThemeAmsterdam, // euiThemeAmsterdam,
} from './theme';
84 changes: 37 additions & 47 deletions src/services/theme/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,32 @@ import {
light_colors,
dark_colors,
} from '../../global_styling/variables/_colors';
import {
light_colors_ams,
dark_colors_ams,
} from '../../themes/eui-amsterdam/global_styling/variables/_colors';
// import {
// light_colors_ams,
// dark_colors_ams,
// } from '../../themes/eui-amsterdam/global_styling/variables/_colors';

import { base, size } from '../../global_styling/variables/_size';

import fonts from '../../global_styling/variables/_typography';
import fonts_ams from '../../themes/eui-amsterdam/global_styling/variables/_typography';
// import fonts_ams from '../../themes/eui-amsterdam/global_styling/variables/_typography';

import { border } from '../../global_styling/variables/_borders';
import { border_ams } from '../../themes/eui-amsterdam/global_styling/variables/_borders';
// import { border_ams } from '../../themes/eui-amsterdam/global_styling/variables/_borders';

import { titles } from '../../global_styling/variables/title';
import { titles_ams } from '../../themes/eui-amsterdam/global_styling/variables/title';
// import { titles_ams } from '../../themes/eui-amsterdam/global_styling/variables/title';

/**
* Anything using `COLOR_MODE_KEY` directly, is something that should be top level, while
* anything using the `color.` key will remain under `color`
*/

// HELP: For some reason removing this causes a type error:
// TypeError: 'getOwnPropertyDescriptor' on proxy: trap reported non-configurability for property 'length' which is either non-existent or configurable in the proxy target
const poles = {
ghost: '#FFF',
ink: '#000',
};

/* DEFAULT THEME */
// TODO: All theme files need to be imported here or else they error out.
// Creation of the themes shouldn't be restricted to a particular file

export const light = {
...poles,
...light_colors,
base,
size,
Expand All @@ -67,7 +59,6 @@ export const light = {
};

export const dark = {
...poles,
...dark_colors,
base,
size,
Expand All @@ -87,34 +78,33 @@ export const EuiThemeDefault = buildTheme(euiThemeDefault, 'EUI_THEME_DEFAULT');

/* AMSTERDAM THEME */

export const amsterdam_light = {
...poles,
...light_colors_ams,
base,
size,
...fonts_ams,
border: border_ams,
titles: titles_ams,
};

export const amsterdam_dark = {
...poles,
...dark_colors_ams,
base,
size,
...fonts_ams,
border: border_ams,
titles: titles_ams,
};

export const euiThemeAmsterdam = {
[COLOR_MODE_KEY]: {
light: amsterdam_light,
dark: amsterdam_dark,
},
};

export const EuiThemeAmsterdam = buildTheme(
euiThemeAmsterdam,
'EUI_THEME_AMSTERDAM'
);
// export const amsterdam_light = {
// ...light_colors_ams,
// base,
// size,
// ...fonts_ams,
// border: border_ams,
// titles: titles_ams,
// // array: [1, 2, 3],
// };

// export const amsterdam_dark = {
// ...dark_colors_ams,
// base,
// size,
// ...fonts_ams,
// border: border_ams,
// titles: titles_ams,
// };

// export const euiThemeAmsterdam = {
// [COLOR_MODE_KEY]: {
// light: amsterdam_light,
// dark: amsterdam_dark,
// },
// };

// export const EuiThemeAmsterdam = buildTheme(
// euiThemeAmsterdam,
// 'EUI_THEME_AMSTERDAM'
// );
4 changes: 2 additions & 2 deletions src/services/theme/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const getComputed = <T = EuiThemeShape>(
over[key] instanceof Computed
? over[key].getValue(base.root, over.root, output, colorMode)
: over[key];
if (isObject(baseValue)) {
if (isObject(baseValue) && !Array.isArray(baseValue)) {
loop(baseValue, overValue ?? {}, checkExisting, newPath);
} else {
setOn(output, newPath, overValue ?? baseValue);
Expand Down Expand Up @@ -219,7 +219,7 @@ export const buildTheme = <T extends {}>(model: T, key: string) => {
const target = property === 'root' ? _target : _target.model || _target;
// @ts-ignore `string` index signature
const value = target[property];
if (typeof value === 'object' && value !== null) {
if (isObject(value) && !Array.isArray(value)) {
return new Proxy(
{
model: value,
Expand Down
5 changes: 5 additions & 0 deletions src/themes/eui-amsterdam/global_styling/variables/_colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import {
shade,
tint,
} from '../../../../global_styling/functions/_colors';
import { poles } from '../../../../global_styling/variables/_colors';

export const light_colors_ams = {
...poles,

// Brand
primary: '#07C',
accent: '#F04E98',
Expand Down Expand Up @@ -60,6 +63,8 @@ export const light_colors_ams = {
};

export const dark_colors_ams = {
...poles,

// Brand
primary: '#36A2EF',
accent: '#F68FBE',
Expand Down
60 changes: 60 additions & 0 deletions src/themes/eui-amsterdam/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you 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 { buildTheme } from '../../services/theme';
import { COLOR_MODE_KEY } from '../../services/theme/utils';
import { base, size } from '../../global_styling/variables/_size';
import {
light_colors_ams,
dark_colors_ams,
} from './global_styling/variables/_colors';
import fonts_ams from './global_styling/variables/_typography';
import { border_ams } from './global_styling/variables/_borders';
import { titles_ams } from './global_styling/variables/title';

export const amsterdam_light = {
...light_colors_ams,
base,
size,
...fonts_ams,
border: border_ams,
titles: titles_ams,
// array: [1, 2, 3],
};

export const amsterdam_dark = {
...dark_colors_ams,
base,
size,
...fonts_ams,
border: border_ams,
titles: titles_ams,
};

export const euiThemeAmsterdam = {
[COLOR_MODE_KEY]: {
light: amsterdam_light,
dark: amsterdam_dark,
},
};

export const EuiThemeAmsterdam = buildTheme(
euiThemeAmsterdam,
'EUI_THEME_AMSTERDAM'
);

0 comments on commit 93098f7

Please sign in to comment.