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] Simplify shadow parameters #5892

Merged
merged 6 commits into from
May 16, 2022

Conversation

cchaos
Copy link
Contributor

@cchaos cchaos commented May 10, 2022

Based on a comment I made in #5891 :

So @constancecchen and I just ran into a similar issue with the font-sizing functions. We came up with what we think is a nice pattern we could re-use here.

  • All required parameters are directly passed, while any optional ones are in the final parameter as a single object.
  • If the function requires the use of any part of euiTheme it takes the whole UseEuiTheme, and always comes first
const euiMixin(
  euiTheme: UseEuiTheme;
  required: PropertyThatIsRequired;
  {
    optionalKey1?: something;
    optionalKey2?: something;
  }
) => {}

I've converted the shadow mixins to use this pattern. I think it simplifies them a lot.

I'm not considering this a breaking change since we just released these and under the Beta flag.

Checklist

  • Checked in both light and dark modes
  • [ ] Checked in mobile
  • Checked in Chrome, Safari, Edge, and Firefox
  • [ ] Props have proper autodocs and playground toggles
  • [ ] Added documentation
  • [ ] Checked Code Sandbox works for any docs examples
  • [ ] Added or updated jest and cypress tests
  • [ ] Checked for breaking changes and labeled appropriately
  • [ ] Checked for accessibility including keyboard-only and screenreader modes
  • [ ] Updated the Figma library counterpart
  • A changelog entry exists and is marked appropriately

@cchaos cchaos mentioned this pull request May 10, 2022
7 tasks
@cchaos cchaos requested a review from thompsongl May 10, 2022 20:12
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5892/

Comment on lines 26 to 27
export const euiBottomBarStyles = (_euiTheme: UseEuiTheme) => {
const { euiTheme } = _euiTheme;
Copy link
Member

@cee-chen cee-chen May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This distinction feels somewhat confusing to me and I wish we were a bit more consistent on whether we pass the entire returned context or just euiTheme between various helpers (e.g., the typography fns all pass euiTheme.euiTheme and not the parent context, but this is also because basically every single .styles.ts file deconstructs { euiTheme } automatically in its params).

I also wish we had a clearer naming convention for the context vs the actual theme data so that we don't end up essentially deconstructing _euiTheme.euiTheme 🙈 Maybe themeContext.euiTheme instead?...

@thompsongl, any thoughts here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with all those points. I wonder if it's just easiest to say, any mixin/function should accept the entirety of UseEuiTheme whether they only need the .euiTheme portion or not? It keeps them scalable in case of future needs to grab the colorMode.

As for naming we could just change to also ensure we're naming it as lowercase useEuiTheme so that it is clear.

Suggested change
export const euiBottomBarStyles = (_euiTheme: UseEuiTheme) => {
const { euiTheme } = _euiTheme;
export const euiBottomBarStyles = (useEuiTheme: UseEuiTheme) => {
const { euiTheme } = useEuiTheme;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I had mixins passing the full result of useEuiTheme originally. The cases where mixins needed colorMode were scarce so I went the other way. But I'd rather see consistency like you've suggested if working with the current mixin shape is difficult.

We should update all mixins and the createStyleHookFromMixin util with the new pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

Also I just realized we could simplify the code portion a bit too by deconstructing in the parameters.

Suggested change
export const euiBottomBarStyles = (_euiTheme: UseEuiTheme) => {
const { euiTheme } = _euiTheme;
export const euiBottomBarStyles = ({ euiTheme }: UseEuiTheme) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a commit based on those latest comments. But I didn't touch the util (that one still confuses me a bit)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not 😸

😄 But are you ok if I do it and add a bunch to this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if you want to merge this I can do a follow up. Up to you!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, go for it. I'm out for the rest of the week.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm not sure I can make a generic function that works with mixins that do have required args and mixins that do not have required args.

const euiMixin(
  euiTheme: UseEuiTheme;
  required: PropertyThatIsRequired;
  optional?: {
    optionalKey1?: something;
    optionalKey2?: something;
  }
) => {}

vs

const euiMixin(
  euiTheme: UseEuiTheme;
  optional?: {
    optionalKey1?: something;
    optionalKey2?: something;
  }
) => {}

So a couple options:

  • Only have the former and you're required to pass an empty arg when no properties are required: euiScrollBarStyles(euiThemeContext, {}, {size: 20});
  • Move the required args and optional args into a single object:
const euiMixin(
  euiTheme: UseEuiTheme;
  options: {
    requiredKey1: something;
    optionalKey1?: something;
    optionalKey2?: something;
  }
) => {}
  • Create a second version of createStyleHookFromMixin so we have one with required args and one without
  • Scrap createStyleHookFromMixin entirely and manually create hooks. (We lose the type safety/consistency that it adds to mixins; we have to just be consistent when creating new mixins).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I think my (opinionated) vote goes to

Scrap createStyleHookFromMixin entirely and manually create hooks. (We lose the type safety/consistency that it adds to mixins; we have to just be consistent when creating new mixins).

I haven't used this helper personally despite having created a few mixins and I don't super see the need for it. 🤔 It's not really that tedious to manually write a 5-line hook next to a mixin.

If we do strongly want the helper still, then my next vote goes to Move the required args and optional args into a single object

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5892/

@cchaos cchaos force-pushed the css-in-js/simplify-shadow-mixins branch from 01812c5 to 3e8d27c Compare May 11, 2022 20:34
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5892/

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5892/

@thompsongl
Copy link
Contributor

Ok I've decided not to push anything to this branch and instead have a follow-up/parallel PR that removes the util and changes all the other mixins. We have a couple different patterns going that need to switch to the new style.

Copy link
Member

@cee-chen cee-chen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR looks good to me as-is!

@cchaos
Copy link
Contributor Author

cchaos commented May 16, 2022

Cool beans, I'll merge after CI

@cchaos cchaos enabled auto-merge (squash) May 16, 2022 16:48
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5892/

@cchaos cchaos merged commit 6719905 into elastic:main May 16, 2022
@cchaos cchaos deleted the css-in-js/simplify-shadow-mixins branch May 16, 2022 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants