Skip to content

Commit

Permalink
[CSS-in-JS] Context and foundation (#4440)
Browse files Browse the repository at this point in the history
* poc

* tidy

* proxy set handler

* default theme; docs setup

* table colors via css-in-js

* exports

* withEuiTheme; useEuiTheme return shape; some types

* export and proxy fixes

* use without provider; some test setup

* prevent unnecessary updates

* refactor colorMode calc; move computation to provider

* utils refactor

* clean up

* compute default theme for initial context value

* snapshot update preview

* axe ignore temporary docs page

* theme: colorVis and borders, sizes update

* coupled types

* some utils tests

* start to amsterdam; name in theme obj

* Revert "table colors via css-in-js"

This reverts commit 5614b0f.

* eslintrc

* readme

Co-authored-by: cchaos <[email protected]>
  • Loading branch information
thompsongl and cchaos authored Feb 17, 2021
1 parent a0a8b42 commit 156f7fe
Show file tree
Hide file tree
Showing 24 changed files with 2,077 additions and 23 deletions.
8 changes: 7 additions & 1 deletion .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ module.exports = {
"modules": process.env.BABEL_MODULES ? process.env.BABEL_MODULES === 'false' ? false : process.env.BABEL_MODULES : "commonjs" // babel's default is commonjs
}],
["@babel/typescript", { isTSX: true, allExtensions: true }],
"@babel/react"
"@babel/react",
[
"@emotion/babel-preset-css-prop",
{
"labelFormat": "[local]"
},
],
],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
Expand Down
21 changes: 20 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* 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.
*/

const APACHE_2_0_LICENSE_HEADER = `
/*
* Licensed to Elasticsearch B.V. under one or more contributor
Expand Down Expand Up @@ -48,7 +67,7 @@ module.exports = {
'prettier/standard',
'plugin:prettier/recommended',
],
plugins: ['jsx-a11y', 'prettier', 'local', 'react-hooks'],
plugins: ['jsx-a11y', 'prettier', 'local', 'react-hooks', '@emotion'],
rules: {
'prefer-template': 'error',
'local/i18n': 'error',
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
"@elastic/charts": "^24.5.1",
"@elastic/datemath": "^5.0.3",
"@elastic/eslint-config-kibana": "^0.15.0",
"@emotion/babel-preset-css-prop": "^11.0.0",
"@emotion/eslint-plugin": "^11.0.0",
"@emotion/jest": "^11.1.0",
"@emotion/react": "^11.1.1",
"@svgr/core": "5.4.0",
"@svgr/plugin-svgo": "^4.0.3",
"@types/classnames": "^2.2.10",
Expand Down Expand Up @@ -213,6 +217,7 @@
},
"peerDependencies": {
"@elastic/datemath": "^5.0.2",
"@emotion/react": "11.x",
"@types/react": "^16.9.34",
"@types/react-dom": "^16.9.6",
"moment": "^2.13.0",
Expand Down
1 change: 1 addition & 0 deletions scripts/a11y-testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const docsPages = async (root, page) => {
`${root}#/tabular-content/data-grid-virtualization`,
`${root}#/elastic-charts/creating-charts`,
`${root}#/elastic-charts/part-to-whole-comparisons`,
`${root}#/temporary/canopy`,
];

return [
Expand Down
3 changes: 2 additions & 1 deletion scripts/jest/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"^.+\\.(js|tsx?)$": "babel-jest"
},
"snapshotSerializers": [
"<rootDir>/node_modules/enzyme-to-json/serializer"
"<rootDir>/node_modules/enzyme-to-json/serializer",
"@emotion/jest/enzyme-serializer"
]
}
13 changes: 12 additions & 1 deletion src-docs/src/components/with_theme/theme_context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import React from 'react';
import { EUI_THEMES, EUI_THEME } from '../../../../src/themes';
// @ts-ignore importing from a JS file
import { applyTheme } from '../../services';
import {
EuiThemeProvider,
EuiThemeDefault,
EuiThemeAmsterdam,
} from '../../../../src/services';

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

Expand Down Expand Up @@ -47,7 +52,13 @@ export class ThemeProvider extends React.Component<object, State> {
theme,
changeTheme: this.changeTheme,
}}>
{children}
<EuiThemeProvider
theme={
theme.includes('amsterdam') ? EuiThemeAmsterdam : EuiThemeDefault
}
colorMode={theme.includes('light') ? 'light' : 'dark'}>
{children}
</EuiThemeProvider>
</ThemeContext.Provider>
);
}
Expand Down
17 changes: 17 additions & 0 deletions src-docs/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ import { ElasticChartsCategoryExample } from './views/elastic_charts/category_ex
import { ElasticChartsSparklinesExample } from './views/elastic_charts/sparklines_example';

import { ElasticChartsPieExample } from './views/elastic_charts/pie_example';

/** ! Temporary ! */

import Canopy from './views/emotion/canopy';

/**
* Lowercases input and replaces spaces with hyphens:
* e.g. 'GridView Example' -> 'gridview-example'
Expand Down Expand Up @@ -306,6 +311,18 @@ const createExample = (example, customTitle) => {
};

const navigation = [
{
name: 'Temporary',
items: [
createExample(
{
intro: <Canopy />,
sections: [],
},
'Canopy'
),
],
},
{
name: 'Guidelines',
items: [
Expand Down
214 changes: 214 additions & 0 deletions src-docs/src/views/emotion/canopy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
* 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 * as React from 'react';
import { css } from '@emotion/react';
import chroma from 'chroma-js';
import { EuiSpacer } from '../../../../src/components/spacer';
import { EuiIcon } from '../../../../src/components/icon';
import {
mergeDeep,
useEuiTheme,
withEuiTheme,
EuiThemeProvider,
computed,
euiThemeDefault,
buildTheme,
} from '../../../../src/services';

const View = () => {
const [theme, colorMode] = useEuiTheme();
return (
<div css={{ display: 'flex' }}>
<div>
{colorMode}
<pre>
<code>{JSON.stringify(theme, null, 2)}</code>
</pre>
</div>
<div>
<h3>
<EuiIcon
aria-hidden="true"
type="stopFilled"
size="xxl"
css={{ color: theme.colors.euiColorPrimary }}
/>
</h3>
<h3>
<EuiIcon
aria-hidden="true"
type="stopFilled"
size="xxl"
css={{ color: theme.colors.euiColorSecondary }}
/>
</h3>
<h3>
<EuiIcon
aria-hidden="true"
type="stopFilled"
size="xxl"
css={{ color: theme.colors.euiTextColor }}
/>
</h3>
</div>
</div>
);
};

const View3 = () => {
const overrides = {
colors: {
light: { euiColorPrimary: '#8A07BD' },
dark: { euiColorPrimary: '#bd07a5' },
},
};
return (
<>
<View />

<EuiSpacer />
<EuiThemeProvider overrides={overrides}>
<em>Overriding primary</em>
<View />
</EuiThemeProvider>
</>
);
};

const View2 = () => {
const overrides = {
colors: {
light: {
euiColorSecondary: computed(
['colors.euiColorPrimary'],
() => '#85e89d'
),
},
dark: { euiColorSecondary: '#f0fff4' },
},
};
return (
<>
<EuiSpacer />
<EuiThemeProvider overrides={overrides}>
<em>Overriding secondary</em>
<View />
</EuiThemeProvider>
</>
);
};

// eslint-disable-next-line react/prefer-stateless-function
class Block extends React.Component<any> {
render() {
const { theme, ...props } = this.props;
// TODO: TS autocomplete not working
const blockStyle = css`
color: ${theme.theme.colors.euiColorPrimary};
border-radius: ${theme.theme.borders.euiBorderRadiusSmall};
border: ${theme.theme.borders.euiBorderEditable};
`;
return (
<div {...props}>
<EuiIcon
aria-hidden="true"
type="stopFilled"
size="xxl"
css={blockStyle}
/>
</div>
);
}
}
const BlockWithTheme = withEuiTheme(Block);

export default () => {
// const [colorMode, setColorMode] = React.useState('light');
const toggleTheme = () => {
// setColorMode((mode) => (mode === 'light' ? 'dark' : 'light'));
};
const [overrides, setOverrides] = React.useState({});
const lightColors = () => {
setOverrides(
mergeDeep(overrides, {
colors: {
light: {
euiColorPrimary: chroma.random().hex(),
},
},
})
);
};
const darkColors = () => {
setOverrides(
mergeDeep(overrides, {
colors: {
dark: {
euiColorPrimary: chroma.random().hex(),
},
},
})
);
};

const newTheme = buildTheme(
{
...euiThemeDefault,
custom: '#000',
},
'CUSTOM'
);

return (
<>
<EuiThemeProvider
// theme={DefaultEuiTheme}
// colorMode={colorMode}
overrides={overrides}>
<button type="button" onClick={toggleTheme}>
{/* @ts-ignore strike */}
<strike>Toggle Color Mode!</strike> Use global config
</button>
<EuiSpacer />
<button type="button" onClick={lightColors}>
Randomize Light Primary!
</button>
{' '}
<button type="button" onClick={darkColors}>
Randomize Dark Primary!
</button>
<EuiSpacer />
<em>Default view</em>
<View />
<View2 />
<EuiSpacer />
<EuiThemeProvider<{ custom: string }>
theme={newTheme}
colorMode="inverse">
<em>Inverse colorMode</em>
<View3 />
<em>withEuiTheme</em>
<BlockWithTheme />
</EuiThemeProvider>
</EuiThemeProvider>
<EuiSpacer />
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ exports[`EuiAccordion behavior closes when clicked twice 1`] = `
onResize={[Function]}
>
<div>
<div
className=""
/>
<div />
</div>
</EuiResizeObserver>
</div>
Expand Down Expand Up @@ -118,9 +116,7 @@ exports[`EuiAccordion behavior opens when clicked once 1`] = `
onResize={[Function]}
>
<div>
<div
className=""
/>
<div />
</div>
</EuiResizeObserver>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ exports[`ExpandedItemActions render 1`] = `
"onClick": [Function],
}
}
className=""
enabled={true}
item={
Object {
Expand All @@ -27,7 +26,6 @@ exports[`ExpandedItemActions render 1`] = `
"render": [Function],
}
}
className=""
enabled={true}
index={1}
item={
Expand Down
Loading

0 comments on commit 156f7fe

Please sign in to comment.