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

Button: Make button visuals overridable by sx #3683

Merged
merged 16 commits into from
Oct 11, 2023

Conversation

broccolinisoup
Copy link
Member

@broccolinisoup broccolinisoup commented Aug 29, 2023

As described in the issue, a regression introduced in #3447 where the icon colors became not overridable by the sx prop in Button.

This issue originally goes back to introducing data-attributes selectors to Button (#2792) and it came with some challenges with overriding styles with sx. At the time, we wrote a custom CSS selector to take data attributes into account while applying the sx styles. However, the new icon styles that are introduced in #3447, are not included in the custom selectors.

Note: We won't have to use this css selectors when we move to css modules ✨ but for now, it seems to be doing what we want.

All that said, I am always open to any kinds of feedback 🙏🏻

Closes https://github.com/github/primer/issues/2576

Screenshots

Before

Close issue button with its icon and the icon color is muted grey

After

Close issue button with its icon and the icon color is purple

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Changes are SSR compatible
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge

Take a look at the What we look for in reviews section of the contributing guidelines for more information on how we review PRs.

@changeset-bot
Copy link

changeset-bot bot commented Aug 29, 2023

🦋 Changeset detected

Latest commit: ff416d3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Aug 29, 2023

size-limit report 📦

Path Size
dist/browser.esm.js 104.74 KB (+0.05% 🔺)
dist/browser.umd.js 105.32 KB (+0.05% 🔺)

@github-actions github-actions bot temporarily deployed to storybook-preview-3683 August 29, 2023 05:27 Inactive
Comment on lines 81 to 88
if (props.leadingIcon) cssSelectorList.push(`& [data-component="leadingVisual"]`)
if (props.trailingIcon) cssSelectorList.push(`& [data-component="trailingVisual"]`)
if (props.trailingAction) cssSelectorList.push(`& [data-component="trailingAction"]`)
Copy link
Member Author

Choose a reason for hiding this comment

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

These lines are repetitive and I am happy to take into a loop but thought keeping this way makes them more readable. Let me know if you have any feedback!

@github-actions github-actions bot temporarily deployed to storybook-preview-3683 August 29, 2023 05:31 Inactive
@broccolinisoup broccolinisoup temporarily deployed to github-pages August 29, 2023 05:33 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 August 29, 2023 05:33 Inactive
@broccolinisoup broccolinisoup temporarily deployed to github-pages August 29, 2023 07:01 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 August 29, 2023 07:01 Inactive
@broccolinisoup broccolinisoup force-pushed the primer-2576-button-leading-visual-color branch from a89b0dd to 2d39e3f Compare August 29, 2023 07:13
@broccolinisoup broccolinisoup temporarily deployed to github-pages August 29, 2023 07:19 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 August 29, 2023 07:19 Inactive
@broccolinisoup broccolinisoup temporarily deployed to github-pages August 29, 2023 07:25 — with GitHub Actions Inactive
@broccolinisoup broccolinisoup force-pushed the primer-2576-button-leading-visual-color branch from c687771 to 2c05542 Compare August 29, 2023 07:25
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 August 29, 2023 07:26 Inactive
@broccolinisoup broccolinisoup temporarily deployed to github-pages August 29, 2023 07:31 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 August 29, 2023 07:32 Inactive
@broccolinisoup broccolinisoup marked this pull request as ready for review August 29, 2023 07:46
@broccolinisoup broccolinisoup requested review from a team and mperrotti August 29, 2023 07:46
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 August 29, 2023 07:51 Inactive
@primer primer bot temporarily deployed to github-pages August 29, 2023 07:55 Inactive
@joshblack
Copy link
Member

Thanks @broccolinisoup! Will take a look at the tests 👀

Wanted to ask you a quick question, is it possible at all to use a CSS Custom Property when color is set to coordinate this instead of the stuff we're doing in JS? Something like:

const ButtonComponent = forwardRef(({children, sx: sxProp = defaultSxProp, ...props}, forwardedRef): JSX.Element => {
  let sxStyles = sxProp

  // grap the button props that have associated data attributes in the styles
  const {block, size, leadingIcon, trailingIcon, trailingAction} = props
  const style = {};

  if (sxProp !== null && Object.keys(sxProp).length > 0) {
    sxStyles = generateCustomSxProp({block, size, leadingIcon, trailingIcon, trailingAction}, sxProp)

    if (sxProp.color) {
      style['--button-color'] = sxProp.color;
    }
  }

  return (
    <ButtonBase ref={forwardedRef} as="button" sx={sxStyles} type="button" style={style} {...props}>
      {children}
    </ButtonBase>
  )
}) as PolymorphicForwardRefComponent<'button', ButtonProps>

And then in the styles we use it like:

// ...

'[data-component="leadingVisual"], [data-component="trailingVisual"], [data-component="trailingAction"]': {
  color: `var(--button-color, ${theme?.colors.fg.muted})`,
},

// ...

Or would this not really work out the same way? Just was thinking of a simple way we could explicitly opt in things to this value but totally understand if it's not really the same thing at all as the custom sx prop seems to be doing a ton.

@broccolinisoup
Copy link
Member Author

Hey @joshblack that totally works for the color 🎉 I wasn't very happy how the generateCustomSxProp function got more complex and unreadable but couldn't think of a different way. I appreciate this so much 🙏🏻 I'll definetely update the implementation just a few things clarify around this:

  • I was thinking if we can apply the custom css prop solution you suggested to the rest of the styles that uses data attributes to simplify the custom sx prop function. However, as far as I understand, this might be a bit challenging for the rest of the styles that uses data attribute.

For example to make sure sx styles are applied properly to the size="small" button that uses data-attribute for setting styles, seems like we need to create css var for each attribute that we are overriding i.e. font-size, padding etc. Is this what you are reading as well or do I miss something here?

  • In terms of the jest tests, I was using toHaveStyle for expect but seems like that function has some issue with the css variables (some related open issues I found: issue1, issue2) - Do you have any recommendation to test these styles in jest? I am also happy to add test stories for each case for VRT. Let me know what you think!

Thanks again, I appreciate your input 🙏🏻

@broccolinisoup broccolinisoup temporarily deployed to github-pages September 18, 2023 04:02 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 September 18, 2023 04:02 Inactive
@broccolinisoup broccolinisoup temporarily deployed to github-pages September 18, 2023 08:28 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 September 18, 2023 08:28 Inactive
@broccolinisoup broccolinisoup temporarily deployed to github-pages September 18, 2023 08:38 — with GitHub Actions Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-3683 September 18, 2023 08:39 Inactive
Copy link
Member

@joshblack joshblack left a comment

Choose a reason for hiding this comment

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

Would love to see how this works out over in dotcom 🤞 Let me know if I missed anything since the last time we spoke.

@broccolinisoup
Copy link
Member Author

broccolinisoup commented Oct 11, 2023

@joshblack All looking good! Checks are green and tested in review lab to see the button that is originally reported as an issue

https://github.com/github/github/pull/291667
Screenshot from project board and an issue sidebar is open on the right side. There is a pink arrow down the bottom that points out the 'Close issue' button and the icon color is purple is expected

I'm merging this - let me know if you have any concern!

@github-actions

This comment was marked as outdated.

@broccolinisoup broccolinisoup added this pull request to the merge queue Oct 11, 2023
Merged via the queue into main with commit a84a149 Oct 11, 2023
31 checks passed
@broccolinisoup broccolinisoup deleted the primer-2576-button-leading-visual-color branch October 11, 2023 03:12
This was referenced Oct 11, 2023
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.

2 participants