Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Switch-Component #2846

Merged
merged 97 commits into from
Jul 14, 2020
Merged

Switch-Component #2846

merged 97 commits into from
Jul 14, 2020

Conversation

supreethmr
Copy link
Contributor

@supreethmr supreethmr commented Feb 16, 2020

Summary

Resolves #2520

Switch component represents toggle button that indicates whether a setting is on or off and provide users with instantaneous feedback.

Deployment:
https://terra-core-deployed-pr-2846.herokuapp.com/components/terra-switch/switch/switch

Requirements:

zeplin.

Bidirectionality:

Alignment will be mirrored in rtl orientation.

Accessibility:

Component will be keyboard navigatable

  • Component can be focused with tab
  • Pressing enter or space on the switch component will open the dropdown and focus the first option

React Props:

Prop Type Default Description
isOn boolean false Indicates Whether or not the Switch is enabled ("ON")
disabled boolean false If true, the user won't be able to toggle the switch.
labelText string '' Text of the label.
onChange function none Callback fired when the switch state is changed.


delete customProps.className;
return (
<button
Copy link
Contributor

Choose a reason for hiding this comment

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

Set an aria-label containing the labelText on the root button element.


delete customProps.className;
return (
<button
Copy link
Contributor

@dkasper-was-taken dkasper-was-taken Jul 2, 2020

Choose a reason for hiding this comment

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

Set <button> to <div>, this will resolve the focus issue with the preventDefault removed and associated css change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you referring to prevent default of mouseDown Event..?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed button to div : 22dedf6f . but we need preventDefault on both handleKeyDown and handleMouseDown to keep switch focus styles consistent in IE.

};

const handleOnMouseDown = (event) => {
event.preventDefault();
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove prevent default on mouseDown.

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 tried removing this. but if we remove focus styles starts appear for mouse interactions in IE.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just checked, yeah you are correct, that part combined with the focus is correct. Disregard this comment.

// See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Button#Clicking_and_focus
// Button on Firefox, Safari and IE running on OS X does not receive focus when clicked.
// This will set focus on the button when clicked.
sliderButton.current.focus();
Copy link
Contributor

Choose a reason for hiding this comment

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

No longer needed with associated div change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But after removing sliderButton.current.focus(); focus styles were not getting displayed in IE.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just checked, yeah you are correct IE 10 has issues.

.switch {
background-color: transparent;
border: 0;
display: flex;
Copy link
Contributor

Choose a reason for hiding this comment

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

Update to display: inline-flex.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated to inline-flex

}

// Removes dotted border of button on focus that shows up in Firefox
&::-moz-focus-inner {
Copy link
Contributor

Choose a reason for hiding this comment

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

Changing the button to a div will remove the need for this pseudo selector.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

disabled={isDisabled}
aria-checked={isChecked}
role="switch"
tabIndex="0"
Copy link
Contributor

Choose a reason for hiding this comment

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

If isDisabled is set the following attribute should not be set on the switch:

      tabIndex="0"
      onBlur={handleOnBlur}
      onClick={handleOnClick}
      onMouseDown={handleOnMouseDown}
      onKeyDown={handleOnKeyDown}

Can instead add some dependent logic like:

  let switchAttrs;
  if (!isDisabled) {
    switchAttrs = {
      tabIndex: '0',
      onBlur: handleOnBlur,
      onClick: handleOnClick,
      onMouseDown: handleOnMouseDown,
      onKeyDown: handleOnKeyDown,
    };
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added attributes variable


const labelClassNames = cx([
'label-text',
{ 'is-disabled': isDisabled },
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that there is a single entry point for the control we can remove the individual is-disabled classes from the sub-elements and instead with child/nested selectors from the parent tray's is-disabled class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but Since now we have changed button to div disabled is not happening as it was happening before when button was wrapped around all other controls. sub-elements do have theme variables for disabled styles like tray has theme variables for background-color, box-shadow and border styles. So removing these classes will enable focus styles of slider on TAB key press and hover styles on mouse hover, even when switch is disabled.

border: 0;
outline: 0;

&[data-terra-switch-show-focus-styles='true'] .slider:not(.is-disabled) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If focus is no longer possible with the isDisabled check in the javascript, we can remove this qualifiers for not(.is-disabled)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this we can remove only whole component was just a button but now after changing this to div we still need to prevent focus styles for disabled switch.

@dkasper-was-taken
Copy link
Contributor

dkasper-was-taken commented Jul 3, 2020

@supreethmr Here is a branch for reference to the proposed changes for the disabled behavior.
add-switch-component...add-switch-component-revised

There are some ways to reduce the css further, but I didn't want to mess with any of the variables.

@supreethmr
Copy link
Contributor Author

@supreethmr Here is a branch for reference to the proposed changes for the disabled behavior.
add-switch-component...add-switch-component-revised

There are some ways to reduce the css further, but I didn't want to mess with any of the variables.

Thank you for creating branch for code changes. I have updated the PR with suggested changes : 7a753a7

@mjhenkes mjhenkes changed the base branch from master to main July 7, 2020 15:55
@ryanthemanuel ryanthemanuel dismissed their stale review July 8, 2020 14:53

With Dave's approval, I'm good with this now.

@jeremyfuksa
Copy link
Contributor

I'm still good with this refactor.

@PaulSolDev
Copy link

Functional review complete. No issues identified.

@tbiethman tbiethman merged commit a035cfd into main Jul 14, 2020
@tbiethman tbiethman deleted the add-switch-component branch July 14, 2020 14:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create Switch component