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

How to position Switch label to the left of the switch? #11618

Closed
2 tasks done
kaitlynbrown opened this issue May 28, 2018 · 22 comments
Closed
2 tasks done

How to position Switch label to the left of the switch? #11618

kaitlynbrown opened this issue May 28, 2018 · 22 comments
Labels
component: switch This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. new feature New feature or request

Comments

@kaitlynbrown
Copy link

  • This is a v1.x issue (v0.x is no longer maintained).
  • I have searched the issues of this repository and believe that this is not a duplicate.

The examples here demonstrate how to add a label to various forms of input, including the Switch component.

Unfortunately, every single example on that page that includes a label has it positioned to the right of the component. I've looked through the props of FormControlLabel, and there doesn't seem to be any option that allows me to change this. In fact, looking at the code of FormControlLabel, it seems to be hard-coded his way.

I was able to work around this by setting the Label's direction to rtl (and setting the Switch's direction back to ltr):

import React from "react";
import styled from "react-emotion";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import Switch from "@material-ui/core/Switch";

const StyledLabel = styled(FormControlLabel)`
  direction: ${({ rtl }) => rtl && "rtl"};
  margin: 0;
`;

const StyledSwitch = styled(Switch)`
  direction: ltr;
`;

export default ({ label, rtl, ...props }) => (
  <StyledLabel rtl={rtl} control={<StyledSwitch {...props} />} label={label} />
);

But this feels very hacky and potentially unstable to me.

Is there any way I can have a Switch with its label on the left without resorting to rtl hacks? If so, can it please be documented? If not, please consider this a feature request.

@oliviertassinari oliviertassinari added new feature New feature or request component: switch This is the name of the generic UI component, not the React module! waiting for 👍 Waiting for upvotes labels May 29, 2018
@oliviertassinari
Copy link
Member

FormControlLabel wasn't designed with this use case in mind. You would need to write custom CSS to get this behavior.

@oliviertassinari
Copy link
Member

I have added the waiting for users upvotes tag. I'm closing the issue as I'm not sure people are looking for such abstraction. So please upvote this issue if you are. We will prioritize our effort based on the number of upvotes.

@caroe233
Copy link
Contributor

Hi,
I hope I did the correct thing to upvote by using the thumbs up emoji.
Actually, everywhere in the Material design guidelines the switches are on the right of the label, so it would be very helpful to have an easy way to do this...

@davidcoleman007
Copy link

davidcoleman007 commented Jul 2, 2018

I too would like this. imagine that you have a pane to view some page of results, and you want a select all switch at the top right. You want the label on the left to nestle nicely into the top right corner. this is totally in line with mobile first design and hey, bonus! it works on a desktop layout too!

I've +1'ed the ticket! Material-UI is a component lib, and regardless if we "think" we would do something, the option to show a form label on one side or the other is ultimately a design decision which a flexible and powerful framework should be able to offer. wink :)

@oliviertassinari oliviertassinari added good first issue Great for first contributions. Enable to learn the contribution process. and removed waiting for 👍 Waiting for upvotes labels Jul 3, 2018
@oliviertassinari
Copy link
Member

Alright, we can add a property to change the order of the control and the label:
https://github.com/mui-org/material-ui/blob/b3564a767135b933146f7fe73e3a34b14950c782/packages/material-ui/src/FormControlLabel/FormControlLabel.js#L75-L90
It should be easy to implement. In the mean time, you don't have to use the FormControlLabel component. You should be able to inverse the order in your code quite easily.

@mbrookes
Copy link
Member

mbrookes commented Jul 3, 2018

@oliviertassinari Any thoughts on prop name?

@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 3, 2018

@mbrookes Not really, maybe we could follow the Tooltip wording. It's important to have a wording that works with right to left and left to right direction.

@oliviertassinari
Copy link
Member

What about:

controlPlacement?: 'start' : 'end';

or

labelPlacement?: 'start' : 'end';

@caroe233
Copy link
Contributor

caroe233 commented Jul 3, 2018

Thanks for repoening :-)

Why not make it a boolean? e.g. "controlBeforeLabel"

@mbrookes
Copy link
Member

mbrookes commented Jul 3, 2018

Hmm... good point on LTR, although while Tooltip respects the prop wording (placement="left" is always on the left, even for RTL), it would seem more practical if the positioning was reversed for RTL, but then the naming becomes confusing. (That's a whole separate issue though). start / end work as expected.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 5, 2018

Why not make it a boolean? e.g. "controlBeforeLabel"

@caroe233 So we can accept more values, like top, bottom or center.

@mbrookes
Copy link
Member

mbrookes commented Jul 5, 2018

I haven't tried it with FormControlLabel, but Slider can potentially take two labels, so it might be interesting to try to accomadate that:

image

@davidcoleman007
Copy link

it is compatible with the spirit of a switch:

OFF --o ON
OFF o-- ON

@kaitlynbrown
Copy link
Author

Which release will this be in?

@mbrookes
Copy link
Member

@kaitlynbrown The next one. 😜

@iamkennytso
Copy link

What if we want to put labels on both sides?

@oliviertassinari
Copy link
Member

@iamkennytso I guess the best answer is: DIY. We try to focus on the most common use cases.

@benmccann
Copy link

benmccann commented Jun 26, 2019

I've used labelPlacement="start" to put the label on the left. However, it is right-aligned. Can anyone provide a pointer for left-aligning the label?

@oliviertassinari
Copy link
Member

@benmccann This would be better asked on StackOverflow. Stuff to try: text align right, flex justify end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: switch This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. new feature New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants