Skip to content

[Switch] ref attribute is not the root element #19613

@williamscs

Description

@williamscs
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

When attempting to use the width of a Switch component, I don't get the width of the surrounding span.

Expected Behavior 🤔

When getting the offsetWidth of the Switch's ref, it should be the width of the entire component.

Steps to Reproduce 🕹

Attempt to use the ref component and retrieve the width. Set a component to marginLeft to share that width. (I've also reset the margin).

https://codesandbox.io/s/gifted-joliot-gxzni

Context 🔦

I would expect the helper text to align with the label of the switch (see codesandbox example)

I noticed that the ref was pointing to SpanBase: https://github.com/mui-org/material-ui/blob/7f4b81ffb7b76f73319b38f1b9f287f43c74e6d0/packages/material-ui/src/Switch/Switch.js#L162-L187

Perhaps the ref should be the span and an 'innerRef' would point to the SpanBase? I'm unsure of the right way forward. Maybe I'm misinterpreting what "root" should be, however I would like to get the width of the Switch itself, parent span CSS included.

Your Environment 🌎

Tech Version
Material-UI v4.9.1
React v16.12.0
Browser Chrome

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeIntroduces changes that are not backward compatible.scope: switchChanges related to the switch.type: bugIt doesn't behave as expected.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions