Files
Brandy Carney c63d07bdd0 fix(many): do not grow slotted checkboxes, radios, selects and toggles (#29501)
Issue number: resolves #29423

---------

## What is the current behavior?
I fixed a bug where icon was collapsing its width when next to a
checkbox, radio or toggle to match the styles of select in
https://github.com/ionic-team/ionic-framework/pull/29328. This caused a
regression for checkboxes, radios, and toggles when slotted inside of an
item. Our test coverage for this was not great, as the slotted inputs
test in item had so many elements that it was not apparent that this bug
was introduced. In addition, the select itself presented the same issue
before my PR and this is a regression from the v7 behavior. See the
following Codepens to see the regression:

- [Ionic v7](https://codepen.io/brandyscarney/pen/jOoPzoL)
- [Ionic v8](https://codepen.io/brandyscarney/pen/KKLpoLX)

## What is the new behavior?
- Updates the checkbox, radio, select, and toggle to reset the flex
property when slotted.
- Adds test coverage for the previous fix I did in
https://github.com/ionic-team/ionic-framework/pull/29328 where icons
were collapsing their width next to checkboxes, radios and toggles. This
was reproducible with a div and easier to see in a test so I used a div
with a background instead of an icon.
- Adds better test coverage for this fix which separates each component
(checkbox, radio, select, toggle) into their own screenshot test to make
sure the width is shrinking or expanding properly based on where it is
located in an item.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

## Other information

| Before fix
9b59138011
| After |
| ---| ---|
| ![before
fix](https://github.com/ionic-team/ionic-framework/assets/6577830/e27c6e3c-3d3a-4889-a44b-5f4a9a6ba552)
| ![after
fix](https://github.com/ionic-team/ionic-framework/assets/6577830/a05829d6-f776-4f0d-b7eb-cb8177449c90)
|

<table width="100%">
  <tr align="center">
    <td width="50%"><b>Before regression fix</b></td>
    <td width="50%"><b>After</b></td>
  </tr>
  <tr>
<td width="50%"><img alt="before-regression-fix"
src="https://github.com/ionic-team/ionic-framework/assets/6577830/bb1aea84-6c83-4fbe-96ad-855c1c9cca95"></td>
<td width="50%"><img alt="after-regression-fix"
src="https://github.com/ionic-team/ionic-framework/assets/6577830/655dab88-55a9-4961-a7fb-2a3233aa0004"></td>
  </tr>
</table>
2024-05-15 14:16:52 +00:00

293 lines
6.8 KiB
SCSS

@import "../../themes/ionic.globals";
@import "./checkbox.vars.scss";
// Checkbox
// --------------------------------------------------
:host {
/**
* @prop --size: Size of the checkbox icon
*
* @prop --checkbox-background: Background of the checkbox icon
* @prop --checkbox-background-checked: Background of the checkbox icon when checked
*
* @prop --border-color: Border color of the checkbox icon
* @prop --border-radius: Border radius of the checkbox icon
* @prop --border-width: Border width of the checkbox icon
* @prop --border-style: Border style of the checkbox icon
* @prop --border-color-checked: Border color of the checkbox icon when checked
*
* @prop --transition: Transition of the checkbox icon
*
* @prop --checkmark-color: Color of the checkbox checkmark when checked
* @prop --checkmark-width: Stroke width of the checkbox checkmark
*/
--checkbox-background-checked: #{ion-color(primary, base)};
--border-color-checked: #{ion-color(primary, base)};
--checkmark-color: #{ion-color(primary, contrast)};
--transition: none;
display: inline-block;
position: relative;
cursor: pointer;
user-select: none;
z-index: $z-index-item-input;
}
:host(.in-item) {
flex: 1 1 0;
width: 100%;
height: 100%;
}
/**
* Checkbox can be slotted
* in components such as item and
* toolbar which is why we do not
* limit the below behavior to just ion-item.
*/
:host([slot="start"]),
:host([slot="end"]) {
// Reset the flex property when the checkbox
// is slotted to avoid growing the element larger
// than its content.
flex: initial;
width: auto;
}
:host(.ion-color) {
--checkbox-background-checked: #{current-color(base)};
--border-color-checked: #{current-color(base)};
--checkmark-color: #{current-color(contrast)};
}
.checkbox-wrapper {
display: flex;
flex-grow: 1;
align-items: center;
height: inherit;
cursor: inherit;
}
.label-text-wrapper {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
:host(.in-item) .label-text-wrapper {
@include margin($checkbox-item-label-margin-top, null, $checkbox-item-label-margin-bottom, null);
}
:host(.in-item.checkbox-label-placement-stacked) .label-text-wrapper {
@include margin($checkbox-item-label-margin-top, null, $form-control-label-margin, null);
}
:host(.in-item.checkbox-label-placement-stacked) .native-wrapper {
@include margin(null, null, $checkbox-item-label-margin-bottom, null);
}
/**
* If no label text is placed into the slot
* then the element should be hidden otherwise
* there will be additional margins added.
*/
.label-text-wrapper-hidden {
display: none;
}
input {
@include visually-hidden();
}
.native-wrapper {
display: flex;
align-items: center;
}
.checkbox-icon {
@include border-radius(var(--border-radius));
position: relative;
width: var(--size);
height: var(--size);
transition: var(--transition);
border-width: var(--border-width);
border-style: var(--border-style);
border-color: var(--border-color);
background: var(--checkbox-background);
box-sizing: border-box;
}
.checkbox-icon path {
fill: none;
stroke: var(--checkmark-color);
stroke-width: var(--checkmark-width);
opacity: 0;
}
// Justify Content
// ---------------------------------------------
:host(.checkbox-justify-space-between) .checkbox-wrapper {
justify-content: space-between;
}
:host(.checkbox-justify-start) .checkbox-wrapper {
justify-content: start;
}
:host(.checkbox-justify-end) .checkbox-wrapper {
justify-content: end;
}
// Align Items
// ---------------------------------------------
:host(.checkbox-alignment-start) .checkbox-wrapper {
align-items: start;
}
:host(.checkbox-alignment-center) .checkbox-wrapper {
align-items: center;
}
// Label Placement - Start
// ----------------------------------------------------------------
/**
* Label is on the left of the checkbox in LTR and
* on the right in RTL.
*/
:host(.checkbox-label-placement-start) .checkbox-wrapper {
flex-direction: row;
}
:host(.checkbox-label-placement-start) .label-text-wrapper {
/**
* The margin between the label and
* the checkbox should be on the end
* when the label sits at the start.
*/
@include margin(null, $form-control-label-margin, null, 0);
}
// Label Placement - End
// ----------------------------------------------------------------
/**
* Label is on the right of the checkbox in LTR and
* on the left in RTL.
*/
:host(.checkbox-label-placement-end) .checkbox-wrapper {
flex-direction: row-reverse;
}
/**
* The margin between the label and
* the checkbox should be on the start
* when the label sits at the end.
*/
:host(.checkbox-label-placement-end) .label-text-wrapper {
@include margin(null, 0, null, $form-control-label-margin);
}
// Label Placement - Fixed
// ----------------------------------------------------------------
:host(.checkbox-label-placement-fixed) .label-text-wrapper {
/**
* The margin between the label and
* the checkbox should be on the end
* when the label sits at the start.
*/
@include margin(null, $form-control-label-margin, null, 0);
}
/**
* Label is on the left of the checkbox in LTR and
* on the right in RTL. Label also has a fixed width.
*/
:host(.checkbox-label-placement-fixed) .label-text-wrapper {
flex: 0 0 100px;
width: 100px;
min-width: 100px;
max-width: 200px;
}
// Label Placement - Stacked
// ----------------------------------------------------------------
/**
* Label is on top of the checkbox.
*/
:host(.checkbox-label-placement-stacked) .checkbox-wrapper {
flex-direction: column;
}
:host(.checkbox-label-placement-stacked) .label-text-wrapper {
@include transform(scale(#{$form-control-label-stacked-scale}));
/**
* The margin between the label and
* the checkbox should be on the bottom
* when the label sits at the top.
*/
@include margin(null, 0, $form-control-label-margin, 0);
/**
* Label text should not extend
* beyond the bounds of the checkbox.
*/
max-width: calc(100% / #{$form-control-label-stacked-scale});
}
:host(.checkbox-label-placement-stacked.checkbox-alignment-start) .label-text-wrapper {
@include transform-origin(start, top);
}
:host(.checkbox-label-placement-stacked.checkbox-alignment-center) .label-text-wrapper {
@include transform-origin(center, top);
}
// Checked / Indeterminate Checkbox
// ---------------------------------------------
:host(.checkbox-checked) .checkbox-icon,
:host(.checkbox-indeterminate) .checkbox-icon {
border-color: var(--border-color-checked);
background: var(--checkbox-background-checked);
}
:host(.checkbox-checked) .checkbox-icon path,
:host(.checkbox-indeterminate) .checkbox-icon path {
opacity: 1;
}
// Disabled Checkbox
// ---------------------------------------------
:host(.checkbox-disabled) {
pointer-events: none;
}