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

fix(select): pre-selected option correctly floats label #2125

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/mdc-select/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ style dependencies for both the mdc-list and mdc-menu for this component to func
```html
<div class="mdc-select" role="listbox">
<div class="mdc-select__surface" tabindex="0">
<div class="mdc-select__label">Pick a Food Group</div>
<div class="mdc-select__label mdc-select__label--float-above">Pick a Food Group</div>
<div class="mdc-select__selected-text"></div>
<div class="mdc-select__bottom-line"></div>
</div>
Expand Down Expand Up @@ -129,6 +129,13 @@ style dependencies for both the mdc-list and mdc-menu for this component to func
</div>
```

#### Preventing [FOUC](https://en.wikipedia.org/wiki/Flash_of_unstyled_content)
Copy link
Contributor

Choose a reason for hiding this comment

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

This section should be removed. It doesn't really address the FOUC issue. It seems like this section duplicates the Select with pre-selected option section example, and they should be merged under that section.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, looks like I fat-fingered the markdown here - meant to nest the Preventing FOUC section under the Select with pre-selected option.

Should I fix the nesting so that it provides context to the Select with pre-selected option section, or just remove the Preventing FOUC section altogether?

Copy link
Contributor

Choose a reason for hiding this comment

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

Change it to

When dealing with the select component that has pre-selected values, you'll want to ensure that you
render `mdc-select__label` with the `mdc-select__label--float-above` modifier class and the selected option with `aria-selected`. This will
ensure that the label moves out of the way of the select's value and prevents a Flash Of
Un-styled Content (**FOUC**).

so that it more closely matches the text field explanation for pre-filled values. Also, it should be above the example on Line 99.


Because `MDCSelect` updates its UI based on the values it reads in when it is instantiated, there is
potential for an incorrect first render before the script containing the `MDCSelect` initialization
logic executes. To avoid this, you can manually add the `mdc-select__label--float-above` class to the
label element whenever you are initializing the component with an option pre-selected

#### Disabled select

```html
Expand Down
8 changes: 3 additions & 5 deletions packages/mdc-select/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ export default class MDCSelectFoundation extends MDCFoundation {
};
this.cancelHandler_ = () => {
this.close_();

if (this.selectedIndex_ === -1) {
this.adapter_.removeClassFromLabel(cssClasses.LABEL_FLOAT_ABOVE);
}
};
}

Expand Down Expand Up @@ -157,6 +153,9 @@ export default class MDCSelectFoundation extends MDCFoundation {
if (this.selectedIndex_ >= 0) {
selectedTextContent = this.adapter_.getTextForOptionAtIndex(this.selectedIndex_).trim();
this.adapter_.setAttrForOptionAtIndex(this.selectedIndex_, 'aria-selected', 'true');
this.adapter_.addClassToLabel(cssClasses.LABEL_FLOAT_ABOVE);
} else {
this.adapter_.removeClassFromLabel(cssClasses.LABEL_FLOAT_ABOVE);
}
this.adapter_.setSelectedTextContent(selectedTextContent);
}
Expand Down Expand Up @@ -214,7 +213,6 @@ export default class MDCSelectFoundation extends MDCFoundation {
const focusIndex = this.selectedIndex_ < 0 ? 0 : this.selectedIndex_;

this.setMenuStylesForOpenAtIndex_(focusIndex);
this.adapter_.addClassToLabel(cssClasses.LABEL_FLOAT_ABOVE);
this.adapter_.addClassToBottomLine(cssClasses.BOTTOM_LINE_ACTIVE);
this.adapter_.addClass(OPEN);
this.animationRequestId_ = requestAnimationFrame(() => {
Expand Down