Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSS-in-JS] Context and foundation #4440

Merged
merged 29 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
26e5f4f
poc
thompsongl Jan 14, 2021
cdf36a9
tidy
thompsongl Jan 14, 2021
98244a2
Merge branch 'master' into emotion-poc
thompsongl Jan 14, 2021
7e13703
proxy set handler
thompsongl Jan 15, 2021
e45fb50
default theme; docs setup
thompsongl Jan 15, 2021
5614b0f
table colors via css-in-js
thompsongl Jan 15, 2021
aa1c2f3
exports
thompsongl Jan 16, 2021
38e5cf2
withEuiTheme; useEuiTheme return shape; some types
thompsongl Jan 20, 2021
da6ae0b
export and proxy fixes
thompsongl Jan 20, 2021
455b472
Merge branch 'master' into emotion-poc
thompsongl Jan 21, 2021
ada9bd4
use without provider; some test setup
thompsongl Jan 22, 2021
f7589c1
prevent unnecessary updates
thompsongl Jan 26, 2021
5b6e1cf
refactor colorMode calc; move computation to provider
thompsongl Jan 28, 2021
7ae44fa
utils refactor
thompsongl Jan 28, 2021
3d9ff01
Merge branch 'feature/css-in-js' into emotion-poc
thompsongl Jan 28, 2021
4648630
clean up
thompsongl Jan 28, 2021
25c70e7
compute default theme for initial context value
thompsongl Feb 3, 2021
66104ab
snapshot update preview
thompsongl Feb 3, 2021
28909a4
axe ignore temporary docs page
thompsongl Feb 3, 2021
875c86d
theme: colorVis and borders, sizes update
thompsongl Feb 3, 2021
0dbbcbe
Merge branch 'feature/css-in-js' into emotion-poc
thompsongl Feb 5, 2021
8af54f9
coupled types
thompsongl Feb 5, 2021
178e176
some utils tests
thompsongl Feb 5, 2021
d53dba4
Merge remote-tracking branch '2_upstream/feature/css-in-js' into emot…
Feb 9, 2021
6cefd30
start to amsterdam; name in theme obj
thompsongl Feb 10, 2021
e0a762a
Merge branch 'feature/css-in-js' into emotion-poc
thompsongl Feb 16, 2021
c84b4ee
Revert "table colors via css-in-js"
thompsongl Feb 16, 2021
18fcd48
eslintrc
thompsongl Feb 16, 2021
760aea8
readme
thompsongl Feb 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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