mui / mui/material-ui

[Switch] ref attribute is not the root element

Open
#19,613 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

breaking change scope: switch type: bug
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

  • 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the ref behavior in the linked CodeSandbox, then inspect packages/material-ui/src/Switch/Switch.js around lines 162-187. Determine how the Switch ref relates to the surrounding span and SpanBase. Done means the ref exposes the width of the full Switch component, with the expected behavior covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.