Skip to content

feat(ui-components): add states and full-width support to Button component - #45665

Merged
ahmaxed merged 4 commits into
freeCodeCamp:mainfrom
huyenltnguyen:feat/button-states
Apr 18, 2022
Merged

ahmaxed merged 4 commits into
freeCodeCamp:mainfrom
huyenltnguyen:feat/button-states

Conversation

@huyenltnguyen

@huyenltnguyen huyenltnguyen commented Apr 10, 2022 •

Copy link
Copy Markdown
Member

Checklist:

  • I have read freeCodeCamp's contribution guidelines.
  • My pull request has a descriptive title (not a vague title like Update index.md)
  • My pull request targets the main branch of freeCodeCamp.
  • I have tested these changes either locally on my machine, or GitPod.

Ref #45357

This PR adds the following support to the Button component:

  • Active state
  • Focus state
  • Disabled state (via the disabled prop)
  • Full-width usage (via the block prop)

Note: The PR can be reviewed by commits.

Screenshots

Active state

I'm adding a screenshot of the normal state for comparison:

Normal Active
Screen Shot 2022-04-11 at 01 47 14 Screen Shot 2022-04-11 at 01 21 24

Focus state

Light Dark
Primary Screen Shot 2022-04-11 at 14 46 28 Screen Shot 2022-04-11 at 14 46 35
Danger Screen Shot 2022-04-11 at 14 43 44 Screen Shot 2022-04-11 at 14 47 23
Info Screen Shot 2022-04-11 at 14 47 57 Screen Shot 2022-04-11 at 14 47 51

Disabled state

Note: Inactive/disabled elements are allowed to not meet the contrast minimum (Ref: https://www.w3.org/TR/WCAG21/#contrast-minimum)

Light Dark
Primary Screen Shot 2022-04-11 at 01 21 13 Screen Shot 2022-04-11 at 01 50 07
Danger Screen Shot 2022-04-11 at 01 48 58 Screen Shot 2022-04-11 at 01 53 24
Info Screen Shot 2022-04-11 at 01 54 07 Screen Shot 2022-04-11 at 01 53 59

Full-width button

Screen Shot 2022-04-11 at 01 28 08

@gitpod-io

gitpod-io Bot commented Apr 10, 2022

Copy link
Copy Markdown

@github-actions github-actions Bot added the scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. label Apr 10, 2022
@ghost

ghost commented Apr 10, 2022 •

Copy link
Copy Markdown

👀 Review this PR in a CodeSee Review Map

Preview the Review Map

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

@bbsmooth

bbsmooth commented Apr 10, 2022 •

Copy link
Copy Markdown
Contributor
  • I did not cover the focus state as browsers automatically add a focus ring to the element on focus, which is sufficient for a11y support.

You are correct, it is technically sufficient for now under WCAG 2.1 but it won't be under WCAG 2.2 as SC 2.4.11 is being added which will create a new minimum standard for focus appearance, which many browsers will not meet, so basically we will need to make sure we have a good custom focus indicator that can be seen in all browsers. In PR #45137 I suggested we use --blue-mid as the focus ring color as it contrasts nicely in both the light and dark themes.

WCAG 2.2 probably won't be officially released until this Fall but I don't think that should stop us from having a better focus indicator now.

Comment on lines +9 to +16
'active:before:w-full',
'active:before:h-full',
'active:before:absolute',
'active:before:inset-0',
'active:before:border-3',
'active:before:border-transparent',
'active:before:bg-gray-900',
'active:before:opacity-20',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Basically when the button is in active state, we will show an overlay using a pseudo element. I set border-3 and border-transparent classes so that the overlay doesn't cover the border of the main element.

Comment on lines +45 to +50
...(disabled
? []
: [
'hover:bg-default-background-danger-hover',
'hover:text-default-foreground-danger-hover'
])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This would be &:hover:not([aria-disabled="true"]) if we were to write in CSS. Unfortunately Tailwind doesn't allow a not selector so I had to add a condition here.

className={classes}
type={type}
onClick={handleClick}
aria-disabled={disabled}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

aria-disabled is more preferred as it allows the user to focus on the element and understand that the button is disabled. While with the disabled attribute, the button is not focusable, and non-sighted users won't know about its existence nor its disabled state.

Refs:

Comment on lines +42 to +52
gray: {
0: 'var(--gray00)',
50: 'var(--gray05)',
100: 'var(--gray10)',
150: 'var(--gray15)',
450: 'var(--gray45)',
750: 'var(--gray75)',
800: 'var(--gray80)',
850: 'var(--gray85)',
900: 'var(--gray90)'
},

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I only need gray90, but adding all of the variables in anyway, since we already did that for other colors.

Comment on lines +107 to +111
plugins: [
plugin(({ addVariant }) => {
addVariant('aria-disabled', '[aria-disabled="true"]');
})
]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Tailwind supports a lot of selectors by default (ref), but aria-disabled isn't one of them. I followed this guide to add our own modifier. This allows us to style the disabled state using aria-disabled:... classes.

And at this point, I have to say I'm very impressed with Tailwind. They support everything!!! And their documentation is very well-written.

@huyenltnguyen
huyenltnguyen marked this pull request as ready for review April 10, 2022 19:14
@huyenltnguyen
huyenltnguyen requested a review from a team April 10, 2022 19:14
@huyenltnguyen huyenltnguyen mentioned this pull request Apr 10, 2022
4 of 10 tasks
@huyenltnguyen

Copy link
Copy Markdown
Member Author

@bbsmooth Thanks a lot for the guidance!

I vaguely remember reading about this new requirement in the past, but I wasn't aware of the date it would be applied. (And it being a draft made me think it would be a while until the internet elders decide to roll it out lol.)

I have updated the PR to use our own focus ring and I have also added screenshots to the PR description. Let me know what you think 🙂

@raisedadead raisedadead added the status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. label Apr 13, 2022
@ahmaxed

ahmaxed commented Apr 18, 2022

Copy link
Copy Markdown
Member

Thanks again for the detailed notes. My only nitpick is that the disabled button looks like it is registering a click when it is not.
clickable

@ahmaxed
ahmaxed merged commit d35c24a into freeCodeCamp:main Apr 18, 2022
@huyenltnguyen
huyenltnguyen deleted the feat/button-states branch April 18, 2022 13:56
@huyenltnguyen

Copy link
Copy Markdown
Member Author

Ouch! Thanks Ahmad, that's a nice catch!

This is probably a good opportunity for new contributors to get familiar with our UI components. I'll create a issue for this 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants