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

Add ToggleSwitch component #1933

Merged
merged 39 commits into from
Mar 24, 2022
Merged

Add ToggleSwitch component #1933

merged 39 commits into from
Mar 24, 2022

Conversation

mperrotti
Copy link
Contributor

Adds a toggle switch for turning settings on and off and immediately save the change.

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

Screenshots

ToggleSwitchDemo

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • 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.

@mperrotti mperrotti requested review from a team and colebemis March 8, 2022 01:37
@changeset-bot
Copy link

changeset-bot bot commented Mar 8, 2022

🦋 Changeset detected

Latest commit: bb78093

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 Minor

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 Mar 8, 2022

size-limit report 📦

Path Size
dist/browser.esm.js 65.06 KB (+2.3% 🔺)
dist/browser.umd.js 65.41 KB (+2.29% 🔺)

@mperrotti
Copy link
Contributor Author

What do we think of calling it ToggleSwitch instead of Switch? 🤔

docs/content/Switch.mdx Outdated Show resolved Hide resolved
/>
<PropsTableRow name="defaultOn" type="boolean" description="Uncontrolled - whether the switch is turned on" />
<PropsTableRow name="disabled" type="boolean" description="Whether the switch is ready for user input" />
<PropsTableRow name="isLoading" type="boolean" description="Whether the switch is ready for user input" />
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we change this to loading?

Suggested change
<PropsTableRow name="isLoading" type="boolean" description="Whether the switch is ready for user input" />
<PropsTableRow name="loading" type="boolean" description="Whether the switch is ready for user input" />

Also, is it intentional that the description is the same as the disabled prop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not intentional. Thanks for flagging that.

docs/content/Switch.mdx Outdated Show resolved Hide resolved
docs/content/Switch.mdx Outdated Show resolved Hide resolved
src/Switch.tsx Outdated Show resolved Hide resolved
src/Switch.tsx Outdated
display="inline-flex"
alignItems="center"
flexDirection={statusLabelPosition === 'start' ? 'row' : 'row-reverse'}
mb={4}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this component have a margin bottom?

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 have no idea where this came from. Removing.

<PropsTableRow name="defaultOn" type="boolean" description="Uncontrolled - whether the switch is turned on" />
<PropsTableRow name="disabled" type="boolean" description="Whether the switch is ready for user input" />
<PropsTableRow name="isLoading" type="boolean" description="Whether the switch is ready for user input" />
<PropsTableRow name="on" type="boolean" description="Whether the switch is turned on" />
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we call this prop checked instead of on to align with the <input type="checkbox"> API?

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'm good with that.

src/Switch.tsx Outdated
}, [onChange, onProp, isControlled])

return (
<Box
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this component render a visually hidden checkbox in case people plan to use this inside a form?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We intentionally used a button with role="switch" because this component should not be used in a form. The accessibility team doesn't want checkbox changes to be saved unless the user explicitly clicks a "save" or "submit" button.

cc @alliethu or @ichelsea to keep me correct

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah okay. Should we mention that in the docs?

Choose a reason for hiding this comment

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

<button role=switch> sounds like the right thing to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I'll include something in the docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@colebemis - I'm not sure where in the React docs this should go. Could I add a new heading for accessibility?

src/Switch.tsx Outdated Show resolved Hide resolved
src/Switch.tsx Outdated Show resolved Hide resolved
docs/content/Switch.mdx Outdated Show resolved Hide resolved
src/Switch.tsx Outdated Show resolved Hide resolved
Co-authored-by: Cole Bemis <colebemis@github.com>
Co-authored-by: Cole Bemis <colebemis@github.com>
Copy link
Contributor

@colebemis colebemis left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

mperrotti and others added 2 commits March 21, 2022 16:14
Co-authored-by: Cole Bemis <colebemis@github.com>
Co-authored-by: Cole Bemis <colebemis@github.com>
@mperrotti
Copy link
Contributor Author

mperrotti commented Mar 21, 2022

@colebemis - could you also have a look at primer/primitives#309 ?

It blocks this PR

Update: it's merged

@mperrotti
Copy link
Contributor Author

Help wanted: the themePreval snapshot test is failing, but I don't understand why.

@simurai
Copy link
Contributor

simurai commented Mar 22, 2022

the themePreval snapshot test is failing, but I don't understand why.

Ohh.. I think the same themePreval error happened to me too: #1961 (comment). You could try:

  1. Update the date here
  2. Then run test:update
    "test:update": "npm run test -- --updateSnapshot",
  3. Commit changes

Not quite sure why this is needed, but I assume something with re-generating some "snapshot" so it can be compared? This probably should be documented if it's needed every time primer/primitives is updated.

@mperrotti
Copy link
Contributor Author

A PR has also been opened for documenting interface guidelines: primer/design#249

@mperrotti
Copy link
Contributor Author

@simurai - that doesn't seem to have worked 🤔

Could you check my last commit and let me know if I did something wrong?

I updated the snapshots, but themePreval won't update. I even tried running rm -rf node_modules && npm i just to be sure.

@simurai
Copy link
Contributor

simurai commented Mar 24, 2022

@mperrotti that doesn't seem to have worked

Ahh, yess.. sorry. I missed the build step. So it would be:

  1. Update the date in src/theme-preval.js
  2. Run build
    "build": "./script/build",
  3. Run test:update
    "test:update": "npm run test -- --updateSnapshot",
  4. Commit changes

Here the commit fa9ff75 that seems to now pass CI (Visual testing still needs an approval).

Something weird in fa9ff75 are the changes to package-lock.json. Those changes were made by simply opening the mp/switch-component branch in a Codespace. So I thought maybe ok to commit that too. 🤷

@mperrotti
Copy link
Contributor Author

Thanks for fixing that @simurai !

@mperrotti mperrotti temporarily deployed to visual-testing March 24, 2022 17:35 Inactive
@mperrotti mperrotti merged commit ae7650f into main Mar 24, 2022
@mperrotti mperrotti deleted the mp/switch-component branch March 24, 2022 19:14
@primer-css primer-css mentioned this pull request Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants