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

v5: Floating labels #30449

Merged
merged 15 commits into from Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bundlewatch.config.json
Expand Up @@ -30,7 +30,7 @@
},
{
"path": "./dist/css/bootstrap.min.css",
"maxSize": "21.5 kB"
"maxSize": "21.6 kB"
},
{
"path": "./dist/js/bootstrap.bundle.js",
Expand Down
1 change: 1 addition & 0 deletions scss/_forms.scss
Expand Up @@ -5,5 +5,6 @@
@import "forms/form-check";
@import "forms/form-file";
@import "forms/form-range";
@import "forms/floating-labels";
@import "forms/input-group";
@import "forms/validation";
10 changes: 8 additions & 2 deletions scss/_variables.scss
Expand Up @@ -648,7 +648,7 @@ $input-bg: $white !default;
$input-disabled-bg: $gray-200 !default;
$input-disabled-border-color: null !default;

$input-color: $gray-700 !default;
$input-color: $body-color !default;
mdo marked this conversation as resolved.
Show resolved Hide resolved
$input-border-color: $gray-400 !default;
$input-border-width: $input-btn-border-width !default;
$input-box-shadow: $box-shadow-inset !default;
Expand Down Expand Up @@ -733,7 +733,6 @@ $input-group-addon-color: $input-color !default;
$input-group-addon-bg: $gray-200 !default;
$input-group-addon-border-color: $input-border-color !default;


$form-select-padding-y: $input-padding-y !default;
$form-select-padding-x: $input-padding-x !default;
$form-select-font-family: $input-font-family !default;
Expand Down Expand Up @@ -824,6 +823,13 @@ $form-file-padding-x-lg: $input-padding-x-lg !default;
$form-file-font-size-lg: $input-font-size-lg !default;
$form-file-height-lg: $input-height-lg !default;

$form-floating-height: add(3.75rem, $input-height-border) !default;
$form-floating-padding-x: $input-padding-x !default;
$form-floating-padding-y: 1rem !default;
$form-floating-input-padding-t: 1.75rem !default;
$form-floating-input-padding-b: .5rem !default;
$form-floating-label-opacity: .65 !default;
$form-floating-label-transform: scale(.85) translateY(-.5rem) translateX(.15rem) !default;

// Form validation

Expand Down
85 changes: 85 additions & 0 deletions scss/forms/_floating-labels.scss
@@ -0,0 +1,85 @@
// stylelint-disable selector-no-vendor-prefix

.form-floating {
position: relative;

> .form-control,
> .form-select {
height: $form-floating-height;
padding: $form-floating-padding-y $form-floating-padding-x;
}

> label {
position: absolute;
top: 0;
left: 0;
height: 100%; // allow textareas
padding: $form-floating-padding-y $form-floating-padding-x;
pointer-events: none;
border: $input-border-width solid transparent; // Required for aligning label's text with the input as it affects inner box model
transform-origin: 0 0;
@include transition(opacity .1s ease-in-out, transform .1s ease-in-out);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this value be variable, as we do with $input-transition?
Apart from that, seems fine!

Copy link
Member

@ffoodd ffoodd Oct 27, 2020

Choose a reason for hiding this comment

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

Trying this on both Firefox & Chromium on Ubuntu, I can see a little font rendering jump (more obvious on Firefox). I guess it can be related to the font-stack I updated recently—already mentionned a related bug in #31955 by @MartijnCuppens.

That's unrelated to the PR I guess, I'd need more testing on those using our old font-stack and compare other possibilities… Opening an issue to track this. Edit: #31991

}

// stylelint-disable no-duplicate-selectors
> .form-control {
&::placeholder {
color: transparent;
}

&:focus,
&:not(:placeholder-shown) {
padding-top: $form-floating-input-padding-t;
mdo marked this conversation as resolved.
Show resolved Hide resolved
padding-bottom: $form-floating-input-padding-b;
}
// Duplicated because `:-webkit-autofill` invalidates other selectors when grouped
&:-webkit-autofill {
padding-top: $form-floating-input-padding-t;
padding-bottom: $form-floating-input-padding-b;
}
}

> .form-select {
padding-top: $form-floating-input-padding-t;
padding-bottom: $form-floating-input-padding-b;
}

> .form-control:focus,
> .form-control:not(:placeholder-shown),
> .form-select {
~ label {
opacity: $form-floating-label-opacity;
transform: $form-floating-label-transform;
}
}
// Duplicated because `:-webkit-autofill` invalidates other selectors when grouped
> .form-control:-webkit-autofill {
~ label {
opacity: $form-floating-label-opacity;
transform: $form-floating-label-transform;
}
}
// stylelint-enable no-duplicate-selectors
}


//
// Fallback for classic Edge
//

@supports (-ms-ime-align: auto) {
.form-floating {
display: flex;
flex-direction: column-reverse;
}
.form-floating > label {
position: static;
padding: 0;
margin-bottom: calc(#{$form-floating-padding-y} / 2); // stylelint-disable-line function-disallowed-list
border: 0;
@include transition(none);
}
.form-floating > .form-control::-ms-input-placeholder {
color: $input-placeholder-color;
}
}
2 changes: 1 addition & 1 deletion scss/forms/_form-select.scss
Expand Up @@ -6,7 +6,7 @@
.form-select {
display: block;
width: 100%;
height: $form-select-height;
min-height: $form-select-height;
mdo marked this conversation as resolved.
Show resolved Hide resolved
padding: $form-select-padding-y ($form-select-padding-x + $form-select-indicator-padding) $form-select-padding-y $form-select-padding-x;
font-family: $form-select-font-family;
@include font-size($form-select-font-size);
Expand Down
101 changes: 0 additions & 101 deletions site/content/docs/5.0/examples/floating-labels/floating-labels.css

This file was deleted.

35 changes: 0 additions & 35 deletions site/content/docs/5.0/examples/floating-labels/index.html

This file was deleted.

104 changes: 104 additions & 0 deletions site/content/docs/5.0/forms/floating-labels.md
@@ -0,0 +1,104 @@
---
layout: docs
title: Floating labels
description: Create beautifully simple form labels that float over your input fields.
group: forms
toc: true
---

## Example

Wrap a pair of `<input class="form-control">` and `<label>` elements in `.form-floating` to enable floating labels with Bootstrap's textual form fields. A `placeholder` is required on each `<input>` as our method of CSS-only floating labels uses the `:placeholder-shown` pseudo-element. Also note that the `<input>` must come first so we can utilize a sibling selector (e.g., `~`).

This approach works in the new Microsoft Edge built on Chromium and gracefully degrades on older versions.

{{< example >}}
<div class="form-floating mb-3">
<input type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
<label for="floatingInput">Email address</label>
</div>
<div class="form-floating">
<input type="password" class="form-control" id="floatingPassword" placeholder="Password">
<label for="floatingPassword">Password</label>
</div>
{{< /example >}}

When there's a `value` already defined, `<label>`s will automatically adjust to their floated position.

{{< example >}}
<form class="form-floating">
<input type="email" class="form-control" id="floatingInputValue" placeholder="name@example.com" value="test@example.com">
<label for="floatingInputValue">Input with value</label>
</form>
{{< /example >}}

Form validation styles also work as expected.

{{< example >}}
<form class="form-floating">
<input type="email" class="form-control is-invalid" id="floatingInputInvalid" placeholder="name@example.com" value="test@example.com">
<label for="floatingInputInvalid">Invalid input</label>
</form>
{{< /example >}}

## Textareas

By default, `<textarea>`s with `.form-control` will be the same height as `<input>`s.

{{< example >}}
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea"></textarea>
<label for="floatingTextarea">Comments</label>
</div>
{{< /example >}}

To set a custom height on your `<textarea>`, do not use the `rows` attribute. Instead, set an explicit `height` (either inline or via custom CSS).

{{< example >}}
<div class="form-floating">
<textarea class="form-control" placeholder="Leave a comment here" id="floatingTextarea2" style="height: 100px"></textarea>
<label for="floatingTextarea2">Comments</label>
</div>
{{< /example >}}

## Selects

Other than `.form-control`, floating labels only available on `.form-select`s. They work in the same way, but unlike `<input>`s, they'll always show the `<label>` in its floated state.

{{< example >}}
<div class="form-floating">
<select class="form-select" id="floatingSelect" aria-label="Floating label select example">
<option selected>Open this select menu</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<label for="floatingSelect">Works with selects</label>
</div>
{{< /example >}}

## Layout

When working with the Bootstrap grid system, be sure to place form elements within column classes.

{{< example >}}
<div class="row g-2">
<div class="col-md">
<div class="form-floating">
<input type="email" class="form-control" id="floatingInputGrid" placeholder="name@example.com" value="mdo@example.com">
<label for="floatingInputGrid">Email address</label>
</div>
</div>
<div class="col-md">
<div class="form-floating">
<select class="form-select" id="floatingSelectGrid" aria-label="Floating label select example">
<option selected>Open this select menu</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<label for="floatingSelectGrid">Works with selects</label>
</div>
</div>
</div>
{{< /example >}}
2 changes: 2 additions & 0 deletions site/content/docs/5.0/forms/overview.md
Expand Up @@ -18,6 +18,8 @@ sections:
description: Replace browser default range inputs with our custom version.
- title: Input group
description: Attach labels and buttons to your inputs for increased semantic value.
- title: Floating labels
description: Create beautifully simple form labels that float over your input fields.
- title: Layout
description: Create inline, horizontal, or complex grid-based layouts with your forms.
- title: Validation
Expand Down