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 switch component #440

Merged
merged 9 commits into from
Aug 9, 2024
Merged

Add switch component #440

merged 9 commits into from
Aug 9, 2024

Conversation

mkernohanbc
Copy link
Contributor

@mkernohanbc mkernohanbc commented Aug 7, 2024

This PR adds a new Switch component, along with Storybook examples and docs. This PR closes #438.

A switch is a binary toggle that enables the user to set a value on or off. It is implemented using the RAC Switch component as a base.

Switch renders an <input> with the ARIA "switch" role:

Screenshot 2024-08-07 at 11 26 04
  • children slot is used to populate the text label, which automatically renders as a <label> and is associated to the switch
  • labelPosition prop can be used to position the label left or right
  • defaultSelected prop can be used to default the switch to 'on' (selected)
  • isSelected will make the switch controlled
  • isDisabled or isReadOnly can be used to lock a switch

@mkernohanbc mkernohanbc added the enhancement New feature or request label Aug 7, 2024
@mkernohanbc mkernohanbc added this to the Components v0.2.0 milestone Aug 7, 2024
@mkernohanbc mkernohanbc self-assigned this Aug 7, 2024
@mkernohanbc mkernohanbc linked an issue Aug 7, 2024 that may be closed by this pull request
@mkernohanbc mkernohanbc requested a review from ty2k August 7, 2024 18:29
}: SwitchProps) {
return (
<ReactAriaSwitch className={`bcds-react-aria-Switch`} {...props}>
{labelPosition === "left" && <>{children}</>}
Copy link
Contributor Author

@mkernohanbc mkernohanbc Aug 7, 2024

Choose a reason for hiding this comment

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

@ty2k this pair of conditionals (lines 21 and 23) for positioning the label feels like a pretty inelegant solution, if you have a better way to do this would love to hear it!

Copy link
Contributor

Choose a reason for hiding this comment

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

You could switch the flex direction of the thing like flex-direction: row and flex-direction: row-reverse based on that labelPosition prop, but this is probably the more clear solution. We can always complicate the heck out of this when we have to make all these components work for rtl instead of just ltr some day. :)

Copy link
Contributor

@ty2k ty2k left a comment

Choose a reason for hiding this comment

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

This is really great, merge away. One thing to add either in here or in another PR at some point: onChange example. Here's what I roughed out on the Vite page to make sure the thing worked as I expected:

import { useState } from "react";

import { Switch } from "@/components";

export default function SwitchPage() {
  const [isSelected, setIsSelected] = useState(false);

  return (
    <>
      <h2>Switch</h2>
      <div
        style={{
          display: "flex",
          flexDirection: "column",
          gap: "var(--layout-margin-medium",
        }}
      >
        <Switch>Label</Switch>
        <Switch labelPosition="left">Reversed label position</Switch>
        <Switch isDisabled>Disabled switch</Switch>
        <Switch labelPosition="left" defaultSelected>
          Switch on by default
        </Switch>
        <div>
          <Switch
            id="controlled-switch"
            isSelected={isSelected}
            onChange={() => setIsSelected(!isSelected)}
          >
            <pre>isSelected:</pre> {isSelected ? "true" : "false"}
          </Switch>
        </div>
      </div>
    </>
  );
}

@mkernohanbc mkernohanbc merged commit 144c083 into main Aug 9, 2024
4 checks passed
mkernohanbc added a commit that referenced this pull request Aug 13, 2024
* component setup

* roughing out

* rename ToggleButton to Switch

* first pass on styling

* styling and storybook expansion

* docs and stories

* clean up SwitchPage on vite

* fix hover styling
@mkernohanbc mkernohanbc deleted the feature/switch branch September 11, 2024 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch
2 participants