-
Notifications
You must be signed in to change notification settings - Fork 841
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] Starting simple usage docs #4558
Merged
cchaos
merged 6 commits into
elastic:feature/css-in-js
from
cchaos:feature/css-in-js_docs
Mar 3, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d6d2e76
Some initial consuming docs
c6e78c0
Added docs for overriding, color mode, and computed values
e08fb1b
Altering a few examples and creating one for custom keys
dde4eef
Merge remote-tracking branch '2_upstream/feature/css-in-js' into feat…
19dc002
Updating docs to `modify`
96cf8d9
ts-ignoring custom keys
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, { FunctionComponent, ReactNode } from 'react'; | ||
import { EuiIcon } from '../../../../src/components/icon'; | ||
import { EuiCode } from '../../../../src/components/code'; | ||
import { EuiText } from '../../../../src/components/text'; | ||
import { EuiThemeProvider, useEuiTheme } from '../../../../src/services'; | ||
|
||
const Box: FunctionComponent<{ children: ReactNode }> = ({ children }) => { | ||
const [theme] = useEuiTheme(); | ||
|
||
return ( | ||
<EuiText | ||
css={{ | ||
background: theme.colors.euiFocusBackgroundColor, | ||
padding: theme.sizes.euiSizeXL, | ||
color: theme.colors.euiColorPrimaryText, | ||
}}> | ||
<p> | ||
<EuiIcon type="stopFilled" color={theme.colors.euiColorPrimary} />{' '} | ||
{children} | ||
</p> | ||
</EuiText> | ||
); | ||
}; | ||
|
||
export default () => { | ||
const primaryOverrides = { | ||
colors: { | ||
light: { | ||
euiColorPrimary: '#db1dde', | ||
}, | ||
dark: { | ||
euiColorPrimary: '#e378e4', | ||
}, | ||
}, | ||
}; | ||
|
||
return ( | ||
<div> | ||
<EuiThemeProvider modify={primaryOverrides}> | ||
<Box> | ||
The <EuiCode>euiColorPrimary</EuiCode> color has been changed to{' '} | ||
<EuiCode>#db1dde</EuiCode> (<EuiCode>#e378e4</EuiCode> for dark mode) | ||
and so the calculated values of <EuiCode>euiColorPrimaryText</EuiCode>{' '} | ||
and <EuiCode>euiFocusBackgroundColor</EuiCode> have also been updated. | ||
</Box> | ||
</EuiThemeProvider> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import { EuiSpacer } from '../../../../src/components/spacer'; | ||
import { EuiIcon } from '../../../../src/components/icon'; | ||
import { useEuiTheme } from '../../../../src/services'; | ||
|
||
export default () => { | ||
const [theme] = useEuiTheme(); | ||
return ( | ||
<div> | ||
<p> | ||
<EuiIcon | ||
type="stopFilled" | ||
size="xxl" | ||
css={{ color: theme.colors.euiColorPrimary }} | ||
/>{' '} | ||
This primary color will adjust based on the light or dark theme value | ||
</p> | ||
<EuiSpacer /> | ||
<div | ||
css={{ | ||
background: theme.colors.euiColorLightShade, | ||
padding: theme.sizes.euiSizeXL, | ||
}}> | ||
<p> | ||
The padding of this box is passed as a raw unit translated to pixels | ||
</p> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import { css } from '@emotion/react'; | ||
import { withEuiTheme } from '../../../../src/services'; | ||
import { EuiIcon } from '../../../../src/components/icon'; | ||
|
||
// eslint-disable-next-line react/prefer-stateless-function | ||
class Block extends React.Component<any> { | ||
render() { | ||
const { theme } = this.props; | ||
|
||
const divStyle = css` | ||
background: ${theme.theme.colors.euiColorLightShade}; | ||
// This way of providing sizing values doesn't output correctly | ||
padding: ${theme.theme.sizes.euiSizeXL}; | ||
border-radius: ${theme.theme.borders.euiBorderRadius}; | ||
`; | ||
|
||
return ( | ||
<div css={divStyle}> | ||
<p> | ||
<EuiIcon type="faceSad" /> This box has it's background, padding, | ||
and border-radius controlled by custom css | ||
</p> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export const ConsumingHOC = withEuiTheme(Block); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React, { FunctionComponent, ReactNode } from 'react'; | ||
import { tint, shade } from '../../../../src/services/theme/theme'; | ||
import { EuiIcon } from '../../../../src/components/icon'; | ||
import { EuiCode } from '../../../../src/components/code'; | ||
import { EuiText } from '../../../../src/components/text'; | ||
import { | ||
computed, | ||
EuiThemeProvider, | ||
useEuiTheme, | ||
} from '../../../../src/services'; | ||
|
||
const Box: FunctionComponent<{ children: ReactNode }> = ({ children }) => { | ||
const [theme] = useEuiTheme(); | ||
|
||
return ( | ||
<EuiText | ||
css={{ | ||
// @ts-ignore Needs to be fixed in types | ||
background: theme.colors.customColorPrimaryHighlight, | ||
padding: theme.sizes.euiSizeXL, | ||
// @ts-ignore Needs to be fixed in types | ||
color: theme.colors.customColorPrimaryText, | ||
}}> | ||
<p> | ||
<EuiIcon | ||
type="stopFilled" | ||
// @ts-ignore Needs to be fixed in types | ||
color={theme.colors.customColorPrimary} | ||
/>{' '} | ||
{children} | ||
</p> | ||
</EuiText> | ||
); | ||
}; | ||
|
||
export default () => { | ||
const primaryOverrides = { | ||
colors: { | ||
light: { | ||
customColorPrimary: 'rgb(29, 222, 204)', | ||
customColorPrimaryHighlight: computed( | ||
['colors.customColorPrimary'], | ||
([customColorPrimary]) => tint(customColorPrimary, 0.8) | ||
), | ||
// Need a global contrast function | ||
customColorPrimaryText: computed( | ||
['colors.customColorPrimary'], | ||
([customColorPrimary]) => shade(customColorPrimary, 0.8) | ||
), | ||
}, | ||
dark: { | ||
customColorPrimary: 'rgb(29, 222, 204)', | ||
customColorPrimaryHighlight: computed( | ||
['colors.customColorPrimary'], | ||
([customColorPrimary]) => shade(customColorPrimary, 0.8) | ||
), | ||
// Need a global contrast function | ||
customColorPrimaryText: computed( | ||
['colors.customColorPrimary'], | ||
([customColorPrimary]) => tint(customColorPrimary, 0.8) | ||
), | ||
}, | ||
}, | ||
}; | ||
|
||
return ( | ||
<div> | ||
<EuiThemeProvider modify={primaryOverrides}> | ||
<Box> | ||
A new key of <EuiCode>customColorPrimary</EuiCode> has been added as | ||
<EuiCode>rgb(29, 222, 204)</EuiCode>. | ||
<br /> | ||
<br /> | ||
There is also two new computed color keys create off of this for | ||
better contrast. | ||
</Box> | ||
</EuiThemeProvider> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { FunctionComponent, ReactNode } from 'react'; | ||
import { EuiIcon } from '../../../../src/components/icon'; | ||
import { EuiSpacer } from '../../../../src/components/spacer'; | ||
import { EuiThemeProvider, useEuiTheme } from '../../../../src/services'; | ||
|
||
const Box: FunctionComponent<{ children: ReactNode }> = ({ children }) => { | ||
const [theme] = useEuiTheme(); | ||
|
||
return ( | ||
<div | ||
css={{ | ||
background: theme.colors.euiColorLightShade, | ||
padding: theme.sizes.euiSizeXL, | ||
color: theme.colors.euiTextColor, | ||
}}> | ||
<p>{children}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default () => { | ||
return ( | ||
<div> | ||
<EuiThemeProvider colorMode="light"> | ||
<Box> | ||
<EuiIcon type="faceHappy" /> The colors of this box are will always be | ||
in <strong>light</strong> mode | ||
</Box> | ||
</EuiThemeProvider> | ||
<EuiSpacer /> | ||
<EuiThemeProvider colorMode="dark"> | ||
<Box> | ||
<EuiIcon type="faceHappy" /> The colors of this box are will always be | ||
in <strong>dark</strong> mode | ||
</Box> | ||
</EuiThemeProvider> | ||
<EuiSpacer /> | ||
<EuiThemeProvider colorMode="inverse"> | ||
<Box> | ||
<EuiIcon type="faceHappy" /> The colors of this box are the opposite ( | ||
<strong>inverse</strong>) of the current color mode | ||
</Box> | ||
</EuiThemeProvider> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { FunctionComponent, ReactNode } from 'react'; | ||
import { EuiCode } from '../../../../src/components/code'; | ||
import { EuiThemeProvider, useEuiTheme } from '../../../../src/services'; | ||
|
||
const Box: FunctionComponent<{ children: ReactNode }> = ({ children }) => { | ||
const [theme] = useEuiTheme(); | ||
|
||
return ( | ||
<div | ||
css={{ | ||
background: theme.colors.euiColorLightShade, | ||
padding: theme.sizes.euiSizeXL, | ||
}}> | ||
<p>{children}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
export default () => { | ||
const overrides = { | ||
colors: { | ||
light: { euiColorLightShade: '#d3e6df' }, | ||
dark: { euiColorLightShade: '#394c4b' }, | ||
}, | ||
}; | ||
|
||
return ( | ||
<div> | ||
<EuiThemeProvider modify={overrides}> | ||
<Box> | ||
The background of this box is using the locally overridden value of{' '} | ||
<EuiCode>theme.colors.euiColorLightShade</EuiCode> | ||
</Box> | ||
</EuiThemeProvider> | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to ignore these because TS complains that the custom keys don't exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to tell
useEuiTheme
that the custom keys exist:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to do that on creation of the modifications instead of usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, no. A given
useEuiTheme
doesn't/can't have enough knowledge about which provider it's accessing to be able to infer any extensions. This is mostly a limitation of how React context works.If we want typed, autocomplete-ready theme variables for the base EUI theme, then we have to do this extra step to get extensions to work also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd imagine that if an app is planning on extending the theme and wants to use it in many places, they could create a wrapped hook to hide the details:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
K, then I'm just going to leave the ts comments in for now to see if we can address a bit better.