Skip to content

Implement ToggleButton component #48424

Closed

Description

@rajgaur98

Current Specs

  • Border width: 3px
  • Variants (Extended from the Button component):
    • Default
    • Primary (This is only being used)
    • Link
  • Sizes (Extended from the Button component):
    • Large
    • Small (This is only being used)
    • Extra Small
  • States:
    • On (checked)
    • Off (unchecked)
    • Hovered
    • Disables
  • Input Types
    • Checkbox
    • Radio
  • Can be used as a block (Extended from the Button component)
  • Supports text + icon
  • Supports Dark & Light mode
  • Has a checkmark icon if in On state

Screenshots

Usage Light Theme Dark Theme
Checked/On image image
Unchecked/Off image image
"On" Hovered image image
"Off" Hovered image image
"On" Focused image image
"Off" Focused image image

Implementation Plan

Props

Prop Type Required Default Note
size large, medium, small No medium
variant primary, danger No primary
checked true, false No false This replaces the class (active or not-active)
disabled true, false No false
type radio, checkbox Yes I am not sure if we need "checkbox", do we? If not, we can remove this prop
onChange React.ChangeEvent No
value string, number, boolean No

Implmentation

Planning to use Headless UI as recommended in #44668

Activity

  1. rajgaur98 commented on Nov 10, 2022

    @rajgaur98
    ContributorAuthor

    Hi @ahmadabdolsaheb , if everything looks good may I start with the implementation of the above?

  2. ahmaxed commented on Nov 15, 2022

    @ahmaxed
    Member

    Yes, please proceed with a pr.

  3. rajgaur98 commented on Nov 15, 2022

    @rajgaur98
    ContributorAuthor

    @ahmadabdolsaheb The tailwind styles are not being applied to the custom components by default (I am running npm run develop:client command). Other CSS is applying perfectly, but not the tailwind classes.

    Then, I thought to include the base.css stylesheet to the component file directly. But its giving this error
    TypeError: Cannot read properties of undefined (reading 'config') at getTailwindConfig

    I searched on the web a bit but didn't find any solution. What am I doing wrong?

  4. ahmaxed commented on Nov 17, 2022

    @ahmaxed
    Member

    That is expected. you can ignore that message for now or create an empty postcss.config.js file in the freecodecamp's root directory.

  5. bbsmooth commented on Dec 27, 2022

    @bbsmooth
    Contributor

    @ahmadabdolsaheb @rajgaur98 I finally had a chance to look at the current PR for this and I do have a few accessibility concerns that I will elaborate on later this week. But first I'd like to ask where this component would be used in the interface? I'm assuming it would be used for the various toggles on the Settings page? Are there any other places FCC is using toggles?

  6. ahmaxed commented on Jan 3, 2023

    @ahmaxed
    Member

    @bbsmooth, for now only the settings page uses toggles.

  7. bbsmooth commented on Jan 3, 2023

    @bbsmooth
    Contributor

    @ahmadabdolsaheb Thank you.

    We have two types of toggles on the Settings page. Some of them perform an action immediately when toggled, such as the Night Mode toggle. These should use button elements and should be grouped in a fieldset with a legend describing what they toggle. They should also use the aria-pressed attribute on each button to indicate to screen reader users which one is currently active (one will always be false and one will always be true).

    The other type of toggle is triggered when the Save button is clicked. These should use radio button inputs. They should also be grouped in a fieldset with a legend describing what they toggle.

    Is this ToggleButton component intended to be used for both of these types of toggles? Or will there be a separate component for the radio button toggles?

  8. rajgaur98 commented on Jan 5, 2023

    @rajgaur98
    ContributorAuthor

    The other type of toggle is triggered when the Save button is clicked. These should use radio button inputs. They should also be grouped in a fieldset with a legend describing what they toggle.

    @bbsmooth , I couldn't find the second type of toggle that you mentioned. Could you please attach a screenshot? Thanks!

  9. bbsmooth commented on Jan 5, 2023

    @bbsmooth
    Contributor

    @rajgaur98 None of the toggles are currently using button elements on the Settings page. My point is that some of them should be using button elements, specifically, the toggles that change a setting immediately and don't need the Save button to be activated.

    So what I'm asking here is if this component is going to have the option to use either button elements or radio button inputs? Or is this component only going to use button elements and we will create another toggle component that uses radio button inputs?

    Also, issue #48913 was just opened and is suggesting we change all of the toggles on the Settings page to activate immediately and thus not require the use of the Save button. This would mean that there would be no need for the radio button input toggle (at least at this time for the Settings page). But I still think we can decide now whether this component is meant to accommodate both options or just one.

  10. bbsmooth commented on Jan 5, 2023

    @bbsmooth
    Contributor

    I have created a codepen with an accessible working example of a two button toggle group using buttons:

    Codepen: Accessible Toggle Group Using Buttons

    I'm not implying you need to follow this exactly. I just wanted to give a clear example of how this should work. It really is quite minimal in implementation (at least as far as the HTML is concerned). Just two buttons toggling the aria-pressed attribute (one is always set to true and the other to false). The real accessibility work is done in the fieldset/legend (which is out of scope for this component).

  11. rajgaur98 commented on Jan 5, 2023

    @rajgaur98
    ContributorAuthor

    I have created a codepen with an accessible working example of a two button toggle group using buttons:

    Codepen: Accessible Toggle Group Using Buttons

    I'm not implying you need to follow this exactly. I just wanted to give a clear example of how this should work. It really is quite minimal in implementation (at least as far as the HTML is concerned). Just two buttons toggling the aria-pressed attribute (one is always set to true and the other to false). The real accessibility work is done in the fieldset/legend (which is out of scope for this component).

    I think i got it. So, should I move forward with using roles button and not radio/switch?

    And also, the fieldset will be useful in the ToggleButtonGroup component and not this ToggleButton component?

  12. bbsmooth commented on Jan 5, 2023

    @bbsmooth
    Contributor

    OK, I apologize, I think I may be confused about what this component really is. And I think we might not be using the terminology the same way. I was assuming that ToggleButton was referring to all of the toggles currently on the Settings page. Is this correct? Or if not, which of the toggles on the Settings page does ToggleButton refer to? Or does it not refer to any of the toggles on the Settings page and is just a building block in order to create these toggles?

    The reason these distinctions are important is because even though all of the toggles on the Settings page basically look the same and are currently coded the same, they are in fact not all the same. There are actually two types of toggles on the Settings page: those that only take effect when the Save button is clicked and those that take effect immediately when the toggle is changed. This is an important distinction for accessibility and thus they must be coded differently.

  13. rajgaur98 commented on Jan 6, 2023

    @rajgaur98
    ContributorAuthor

    OK, I apologize, I think I may be confused about what this component really is. And I think we might not be using the terminology the same way. I was assuming that ToggleButton was referring to all of the toggles currently on the Settings page. Is this correct? Or if not, which of the toggles on the Settings page does ToggleButton refer to? Or does it not refer to any of the toggles on the Settings page and is just a building block in order to create these toggles?

    The reason these distinctions are important is because even though all of the toggles on the Settings page basically look the same and are currently coded the same, they are in fact not all the same. There are actually two types of toggles on the Settings page: those that only take effect when the Save button is clicked and those that take effect immediately when the toggle is changed. This is an important distinction for accessibility and thus they must be coded differently.

    This implementation was intended to replace all the ToggleButtons on the settings page. And when coming up about the implementation, I didn't know the distinction you mentioned regarding accessibility.

    I can think of two options:

    • Change all the toggles on the settings page to follow one implementation (preferably role=button?).
    • Or pass the role radio/button to the ToggleButton

    I don't trust my judgement here on accessibility, so please suggest what should be done

  14. bbsmooth commented on Jan 6, 2023

    @bbsmooth
    Contributor

    There is an open issue about the toggles on the Settings page. I think we'll have to wait to see how that turns out before we can answer these questions. But at least we have a clear understanding of what this component is now. If ultimately the Settings page remains with two different types of toggle functionality then we will either need to create two separate components (one for toggles using button elements and one for toggles using radio inputs) or, as you suggest, pass in a type to this component so it can spit out the appropriate HTML based on the type.

    Also, I created a codepen for toggles using radio buttons. Again, just to give an idea of how these would work.

  15. rajgaur98 commented on Jan 7, 2023

    @rajgaur98
    ContributorAuthor

    There is an open issue about the toggles on the Settings page. I think we'll have to wait to see how that turns out before we can answer these questions. But at least we have a clear understanding of what this component is now. If ultimately the Settings page remains with two different types of toggle functionality then we will either need to create two separate components (one for toggles using button elements and one for toggles using radio inputs) or, as you suggest, pass in a type to this component so it can spit out the appropriate HTML based on the type.

    Also, I created a codepen for toggles using radio buttons. Again, just to give an idea of how these would work.

    Thanks for the examples! Learned new things about accessibility.

  16. bbsmooth commented on Jan 11, 2023

    @bbsmooth
    Contributor

    It looks like we will need both types of toggles on the Settings page: those that use buttons and those that use radio button inputs (as demonstrated in my codepen links above). Whether this should be done as one ToggleButton component which can handle either type or split into two separate components I'll leave for others to decide.

    Also, going by PR #48757, I think we need a three option toggle, so it would probably be a good idea to make sure the component can take two or three values (or perhaps don't put a limit on it?).

  17. rajgaur98 commented on Jan 12, 2023

    @rajgaur98
    ContributorAuthor

    It looks like we will need both types of toggles on the Settings page: those that use buttons and those that use radio button inputs (as demonstrated in my codepen links above). Whether this should be done as one ToggleButton component which can handle either type or split into two separate components I'll leave for others to decide.

    https://headlessui.com/react/switch component does not offer a type prop, so it is not be possible to change the HTML wrt the type prop. But we can use https://headlessui.com/react/radio-group for the type radio and an HTML button for the type button. I am also okay with implementing these both in HTML and not headless UI

    Also, going by PR #48757, I think we need a three option toggle, so it would probably be a good idea to make sure the component can take two or three values (or perhaps don't put a limit on it?).

    In #48567 we are taking the value as strings, so this should work. And the role of handling multiple ToggleButtons would be carries out by the ToggleButtonGroup when it is implemented

  18. BalanaguYashwanth commented on Feb 9, 2023

    @BalanaguYashwanth
    Contributor

    Hello @bbsmooth @ahmadabdolsaheb,

    Can you guys review @rajgaur98 PR #48567 and close it. So that developer can save time to look into other PRs

  19. ahmaxed commented on Feb 17, 2023

    @ahmaxed
    Member

    Thank you @BalanaguYashwanth for the reminder. All prs are in the queue and are being reviewed based on priority.
    As a result, tagging users would not speed up the review process.

  20. BalanaguYashwanth commented on Feb 17, 2023

    @BalanaguYashwanth
    Contributor

    ok

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions