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

[material-ui][Checkbox] Asterisk placement aligned correctly #39721

Merged
merged 3 commits into from
Nov 8, 2023

Conversation

axelbostrom
Copy link
Contributor

@axelbostrom axelbostrom commented Nov 2, 2023

@mui-bot
Copy link

mui-bot commented Nov 2, 2023

Netlify deploy preview

https://deploy-preview-39721--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 926377b

@danilo-leal danilo-leal added component: checkbox This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material design This is about UI or UX design, please involve a designer labels Nov 2, 2023
Copy link

@asdotdev asdotdev left a comment

Choose a reason for hiding this comment

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

This fixes asterisk placement. There is no need for flex so using Stack is unnecessary.

But div can be used in place of the span as label text and the asterisk is already wrapped in span.
Because using span inside span is not good, the Stack component also uses div.

@mj12albert
Copy link
Member

@axelbostrom I suggest just overriding the display property on the Stack instead to lessen the impact of the change (e.g. someone could be targeting classes on that Stack for styling)

diff --git a/packages/mui-material/src/FormControlLabel/FormControlLabel.js b/packages/mui-material/src/FormControlLabel/FormControlLabel.js
index fa0d4cf32c..be38944c23 100644
--- a/packages/mui-material/src/FormControlLabel/FormControlLabel.js
+++ b/packages/mui-material/src/FormControlLabel/FormControlLabel.js
@@ -166,7 +166,7 @@ const FormControlLabel = React.forwardRef(function FormControlLabel(inProps, ref
     >
       {React.cloneElement(control, controlProps)}
       {required ? (
-        <Stack direction="row" alignItems="center">
+        <Stack direction="row" alignItems="center" sx={{ display: 'block' }}>
           {label}
           <AsteriskComponent ownerState={ownerState} aria-hidden className={classes.asterisk}>
             &thinsp;{'*'}

@mj12albert
Copy link
Member

Because using span inside span is not good

I don't think there is anything inherently wrong with spans inside spans 🤔

@mj12albert mj12albert self-assigned this Nov 3, 2023
@axelbostrom
Copy link
Contributor Author

@mj12albert Right, but I feel like using stack for the asterisk is very uncessesary, since there is no need for horizontal or vertical aligntment for the asterisk. So yeah either a div or span inside span. I don't think there is an issue with a span inside a span either, but not sure.

@asdotdev
Copy link

asdotdev commented Nov 3, 2023

I don't think there is anything inherently wrong with spans inside spans 🤔

Yes, you are right there is nothing wrong with doing it. It's just less semantic.

The span element is a generic inline container for phrasing content, which does not inherently represent anything. It should be used only when no other semantic element is appropriate.

@mj12albert
Copy link
Member

I feel like using stack for the asterisk is very uncessesary

You are right that is is unnecessary, but to remove it entirely now is a bigger change (that I'd like to avoid) than updating a style

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Nov 3, 2023

Just a quick note: Stack was introduced in #37831 to address the asterisk placement issue with different label positions in #37281. Removing it might affect #37281.

Edit: I should have added visual regression tests in that PR to explain the purpose of the Stack component. This way, the CI would fail when the Stack component is removed.

@axelbostrom
Copy link
Contributor Author

@ZeeshanTamboli ok I will look into this this weekend or on monday.

Anyway, what is the best solution using span stack or div?

@mj12albert
Copy link
Member

what is the best solution using span stack or div

Let's keep using the Stack and try to fix the issue by adjusting the styles alone to avoid breaking changes

@axelbostrom axelbostrom reopened this Nov 6, 2023
@axelbostrom
Copy link
Contributor Author

This change ensures that the asterisk correctly appears at the end of the label text within the FormControlLabel component, even when the label spans multiple lines, but uses nested spans but keeps the stack component.

Copy link

@asdotdev asdotdev left a comment

Choose a reason for hiding this comment

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

Current changes add unnecessary HTML elements.
Screenshot 2023-11-06 at 8 22 14 PM

Simply this change can work.

- <Stack direction="row" alignItems="center">
-     <span>
+ <Stack display="block">
            {label}
            <AsteriskComponent ownerState={ownerState} aria-hidden className={classes.asterisk}>
                &thinsp;{'*'}
            </AsteriskComponent>
-     </span>
  </Stack>
Screenshot 2023-11-06 at 8 24 35 PM

Copy link
Member

@mj12albert mj12albert left a comment

Choose a reason for hiding this comment

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

We should go with @amitabh-sahu's suggestion, it's highly preferable we don't alter the structure to avoid breaking changes

axelbostrom and others added 2 commits November 8, 2023 08:24
Co-authored-by: Albert Yu <albert@albertyu.co>
Signed-off-by: Axel Boström <56017794+axelbostrom@users.noreply.github.com>
Co-authored-by: Albert Yu <albert@albertyu.co>
Signed-off-by: Axel Boström <56017794+axelbostrom@users.noreply.github.com>
Copy link
Member

@mj12albert mj12albert left a comment

Choose a reason for hiding this comment

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

@axelbostrom Looks good ~ thanks for spending the time to work on this ✨

@mj12albert
Copy link
Member

BTW I've added a sandbox to the description and checked out @ZeeshanTamboli's comment:

Screenshot 2023-11-08 at 4 46 19 PM

The changes did not break different labelPlacements

@mj12albert mj12albert merged commit 6a05585 into mui:master Nov 8, 2023
20 checks passed
@axelbostrom
Copy link
Contributor Author

Thanks for the help! Not the most code I have written but a great learning experience :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: checkbox This is the name of the generic UI component, not the React module! design This is about UI or UX design, please involve a designer package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[material-ui][Checkbox] Odd placement of asterisk next to required Checkbox label longer than one line
6 participants