Skip to content

Commit d00a85e

Browse files
committed
Add compatibility with Select2 4.1
- Uses Bootstrap's `custom-select` implementation for uniformity - Use SVG background image for clear button - Add some lints to improve code readability Ref: https://getbootstrap.com/docs/4.6/components/forms/#select-menu Close: #49
1 parent 3d2fa98 commit d00a85e

File tree

5 files changed

+162
-178
lines changed

5 files changed

+162
-178
lines changed

.stylelintrc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
"stylelint-config-twbs-bootstrap/scss"
44
],
55
"rules": {
6-
"selector-class-pattern": "^[a-z][a-z0-9\\-_]*[a-z0-9]$"
6+
"block-closing-brace-empty-line-before": "never",
7+
"declaration-no-important": null,
8+
"rule-empty-line-before": ["always", {
9+
"except": ["first-nested"],
10+
"ignore": ["after-comment"]
11+
}],
12+
"selector-class-pattern": "^[a-z][a-z0-9\\-_]*[a-z0-9]$",
13+
"selector-no-qualifying-type": null
714
}
815
}

dist/select2-bootstrap4.css

Lines changed: 57 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@
1818

1919
.select2-container--bootstrap4 .select2-selection {
2020
width: 100%;
21+
min-height: calc(1.5em + 0.75rem + 2px);
22+
padding: 0.375rem 1.75rem 0.375rem 0.75rem;
23+
font-size: 1rem;
24+
font-weight: 400;
25+
line-height: 1.5;
26+
color: #495057;
27+
vertical-align: middle;
2128
background-color: #fff;
2229
border: 1px solid #ced4da;
2330
border-radius: 0.25rem;
24-
-webkit-transition: border-color 0.15s ease-in-out, -webkit-box-shadow 0.15s ease-in-out;
25-
transition: border-color 0.15s ease-in-out, -webkit-box-shadow 0.15s ease-in-out;
26-
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
27-
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-box-shadow 0.15s ease-in-out; }
31+
-webkit-transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, -webkit-box-shadow 0.15s ease-in-out;
32+
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, -webkit-box-shadow 0.15s ease-in-out;
33+
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
34+
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-box-shadow 0.15s ease-in-out; }
2835
@media (prefers-reduced-motion: reduce) {
2936
.select2-container--bootstrap4 .select2-selection {
3037
-webkit-transition: none;
@@ -35,11 +42,6 @@
3542
-webkit-box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
3643
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); }
3744

38-
.select2-container--bootstrap4.select2-container--focus.select2-container--open .select2-selection {
39-
border-bottom: none;
40-
border-bottom-right-radius: 0;
41-
border-bottom-left-radius: 0; }
42-
4345
.select2-container--bootstrap4.select2-container--open.select2-container--above .select2-selection {
4446
border-top-left-radius: 0;
4547
border-top-right-radius: 0; }
@@ -66,18 +68,13 @@ select.is-valid ~ .select2-container--bootstrap4 .select2-selection,
6668
form.was-validated select:valid ~ .select2-container--bootstrap4 .select2-selection {
6769
border-color: #28a745; }
6870

69-
.select2-container--bootstrap4 .select2-search {
70-
width: 100%; }
71-
7271
.select2-container--bootstrap4 .select2-dropdown {
7372
border-color: #ced4da;
7473
border-radius: 0; }
7574
.select2-container--bootstrap4 .select2-dropdown.select2-dropdown--below {
76-
border-top: none;
7775
border-bottom-right-radius: 0.25rem;
7876
border-bottom-left-radius: 0.25rem; }
7977
.select2-container--bootstrap4 .select2-dropdown.select2-dropdown--above {
80-
border-top: 1px solid #ced4da;
8178
border-top-left-radius: 0.25rem;
8279
border-top-right-radius: 0.25rem; }
8380
.select2-container--bootstrap4 .select2-dropdown .select2-results__option[aria-selected="true"] {
@@ -108,42 +105,26 @@ form.was-validated select:valid ~ .select2-container--bootstrap4 .select2-select
108105

109106
.select2-container--bootstrap4 .select2-selection__clear {
110107
float: right;
111-
width: .9em;
112-
height: .9em;
113-
padding-left: .15em;
114-
margin-top: .7em;
115-
margin-right: .3em;
116-
line-height: .75em;
117-
color: #f8f9fa;
118-
background-color: #c8c8c8;
108+
width: 1em;
109+
height: 1em;
110+
padding: 0;
111+
margin: 0.25em 0 0;
112+
font-weight: 700;
113+
line-height: 0.75em;
114+
color: #495057;
115+
background: #e6e6e6 url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 16 16'%3e%3cpath fill='%23495057' d='M1 12l4-4-4-4 3-3 4 4 4-4 3 3-4 4 4 4-3 3-4-4-4 4z'/%3e%3c/svg%3e") center no-repeat !important;
119116
border-radius: 100%; }
117+
.select2-container--bootstrap4 .select2-selection__clear span {
118+
display: none; }
120119
.select2-container--bootstrap4 .select2-selection__clear:hover {
121-
background-color: #afafaf; }
120+
background-color: #d9d9d9 !important; }
122121

123122
.select2-container--bootstrap4 .select2-selection--single {
124-
height: calc(1.5em + 0.75rem + 2px) !important; }
123+
background: #fff url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' width='4' height='5' viewBox='0 0 4 5'%3e%3cpath fill='%23343a40' d='M2 0L0 2h4zm0 5L0 3h4z'/%3e%3c/svg%3e") right 0.75rem center/8px 10px no-repeat; }
125124
.select2-container--bootstrap4 .select2-selection--single .select2-selection__placeholder {
126-
line-height: calc(1.5em + 0.75rem);
127125
color: #6c757d; }
128-
.select2-container--bootstrap4 .select2-selection--single .select2-selection__arrow {
129-
position: absolute;
130-
top: 50%;
131-
right: 3px;
132-
width: 20px; }
133-
.select2-container--bootstrap4 .select2-selection--single .select2-selection__arrow b {
134-
position: absolute;
135-
top: 60%;
136-
left: 50%;
137-
width: 0;
138-
height: 0;
139-
margin-top: -2px;
140-
margin-left: -4px;
141-
border-color: #343a40 transparent transparent transparent;
142-
border-style: solid;
143-
border-width: 5px 4px 0; }
144126
.select2-container--bootstrap4 .select2-selection--single .select2-selection__rendered {
145-
padding-left: 0.75rem;
146-
line-height: calc(1.5em + 0.75rem);
127+
padding: 0;
147128
color: #495057; }
148129

149130
.select2-search--dropdown .select2-search__field {
@@ -155,44 +136,49 @@ form.was-validated select:valid ~ .select2-container--bootstrap4 .select2-select
155136
color: #6c757d; }
156137

157138
.select2-container--bootstrap4 .select2-selection--multiple {
158-
min-height: calc(1.5em + 0.75rem + 2px) !important; }
139+
padding: 0 0.75rem; }
159140
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__rendered {
160-
-webkit-box-sizing: border-box;
161-
box-sizing: border-box;
162-
width: 100%;
163-
padding: 0 0.375rem;
164-
margin: 0;
165-
list-style: none; }
141+
margin: 0; }
142+
.select2-container--bootstrap4 .select2-selection--multiple .select2-search--inline {
143+
display: inline-block; }
166144
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice {
167-
float: left;
168-
padding: 0;
169-
padding-right: 0.75rem;
170-
margin-top: calc(0.375rem - 2px);
171-
margin-right: 0.375rem;
172-
color: #495057;
145+
position: relative;
146+
display: inline-block;
147+
padding: 0 5px 0 21px;
148+
margin: 0.375rem 0.375rem 0.375rem 0;
149+
color: #fff;
150+
vertical-align: top;
173151
cursor: pointer;
174-
border: 1px solid #bdc6d0;
152+
background-color: #6c757d;
175153
border-radius: 0.2rem; }
154+
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__display {
155+
padding: 0 5px; }
176156
.select2-container--bootstrap4 .select2-selection--multiple .select2-search__field {
157+
height: calc(1.5em + 0.75rem);
158+
padding-top: 0.375rem;
159+
margin: 0;
160+
line-height: 1.5;
177161
color: #495057; }
178-
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice + .select2-search {
179-
width: 0; }
180162
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__remove {
181-
float: left;
182-
padding-right: 3px;
183-
padding-left: 3px;
184-
margin-right: 1px;
185-
margin-left: 3px;
163+
position: absolute;
164+
top: 0;
165+
left: 0;
166+
padding: 0 5px;
167+
margin: 0;
186168
font-weight: 700;
187-
color: #bdc6d0; }
169+
color: rgba(255, 255, 255, 0.75);
170+
cursor: pointer;
171+
background: none;
172+
border: none;
173+
border-right: 1px solid #a1a8ae; }
188174
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__remove:hover {
189-
color: #343a40; }
175+
color: rgba(255, 255, 255, 0.85); }
176+
.select2-container--bootstrap4 .select2-selection--multiple.select2-selection--clearable {
177+
padding-right: 32px; }
190178
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__clear {
191-
position: absolute !important;
192-
top: 0;
193-
right: .7em;
194-
float: none;
195-
margin-right: 0; }
179+
position: absolute;
180+
top: 0.375rem;
181+
right: 0.75rem; }
196182

197183
.select2-container--bootstrap4.select2-container--disabled .select2-selection--multiple .select2-selection__choice {
198184
padding: 0 5px;

src/_layout.scss

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
$s2bs4-selection-clear-icon: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 16 16'><path fill='#{$custom-select-color}' d='M1 12l4-4-4-4 3-3 4 4 4-4 3 3-4 4 4 4-3 3-4-4-4 4z'/></svg>") !default;
2+
13
// basic
24
.select2-container {
35
display: block;
@@ -11,40 +13,43 @@
1113
.input-group .select2-container--bootstrap4 {
1214
flex-grow: 1;
1315
}
16+
1417
// for prepended input
1518
.input-group-prepend ~ .select2-container--bootstrap4 .select2-selection {
1619
border-top-left-radius: 0;
1720
border-bottom-left-radius: 0;
1821
}
22+
1923
// for appended input
2024
.input-group > .select2-container--bootstrap4:not(:last-child) .select2-selection {
2125
border-top-right-radius: 0;
2226
border-bottom-right-radius: 0;
2327
}
2428

2529
.select2-container--bootstrap4 {
26-
2730
// input box
2831
.select2-selection {
2932
width: 100%;
30-
background-color: $input-bg;
31-
border: $input-border-width solid $input-border-color;
32-
@include border-radius($input-border-radius);
33-
@include box-shadow($input-box-shadow);
34-
@include transition($input-transition);
33+
min-height: $custom-select-height;
34+
padding: $custom-select-padding-y ($custom-select-padding-x + $custom-select-indicator-padding) $custom-select-padding-y $custom-select-padding-x;
35+
font-family: $custom-select-font-family;
36+
@include font-size($custom-select-font-size);
37+
font-weight: $custom-select-font-weight;
38+
line-height: $custom-select-line-height;
39+
color: $custom-select-color;
40+
vertical-align: middle;
41+
background-color: $custom-select-bg;
42+
border: $custom-select-border-width solid $custom-select-border-color;
43+
@include border-radius($custom-select-border-radius, 0);
44+
@include box-shadow($custom-select-box-shadow);
45+
@include transition($custom-forms-transition);
3546
}
3647

3748
// focused input box
3849
&.select2-container--focus {
3950
.select2-selection {
40-
border-color: $input-focus-border-color;
41-
box-shadow: $input-focus-box-shadow;
42-
}
43-
44-
// when open, hide bottom border
45-
&.select2-container--open .select2-selection {
46-
border-bottom: none;
47-
@include border-bottom-radius(0);
51+
border-color: $custom-select-focus-border-color;
52+
box-shadow: $custom-select-focus-box-shadow;
4853
}
4954
}
5055

@@ -55,6 +60,7 @@
5560
@include border-top-radius(0);
5661
}
5762
}
63+
5864
&.select2-container--below {
5965
.select2-selection {
6066
@include border-bottom-radius(0);
@@ -67,10 +73,11 @@
6773
&.select2-container--disabled.select2-container--focus {
6874
.select2-selection {
6975
cursor: not-allowed;
70-
background-color: $input-disabled-bg;
71-
border-color: $input-border-color;
76+
background-color: $custom-select-disabled-bg;
77+
border-color: $custom-select-border-color;
7278
box-shadow: none;
7379
}
80+
7481
.select2-search__field {
7582
background-color: transparent;
7683
}
@@ -82,38 +89,32 @@
8289
form.was-validated select:invalid ~ & .select2-selection {
8390
border-color: $danger;
8491
}
92+
8593
select.is-valid ~ & .select2-selection,
8694
form.was-validated select:valid ~ & .select2-selection {
8795
border-color: $success;
8896
}
8997
// stylelint-enable selector-no-qualifying-type
9098

91-
// fixes placeholder width
92-
.select2-search {
93-
width: 100%;
94-
}
95-
9699
// dropdown
97100
.select2-dropdown {
98-
border-color: $input-border-color;
101+
border-color: $custom-select-border-color;
99102
border-radius: 0; // overwrite select2.css
100103

101104
// dropdown opened below
102105
&.select2-dropdown--below {
103-
border-top: none;
104-
@include border-bottom-radius($input-border-radius);
106+
@include border-bottom-radius($custom-select-border-radius);
105107
}
106108

107109
// dropdown opened above
108110
&.select2-dropdown--above {
109-
border-top: 1px solid $input-border-color;
110-
@include border-top-radius($input-border-radius);
111+
@include border-top-radius($custom-select-border-radius);
111112
}
112113

113114
// selected item
114115
.select2-results__option[aria-selected="true"] {
115-
color: color-yiq(darken($input-bg, 5%));
116-
background-color: darken($input-bg, 5%);
116+
color: color-yiq(darken($custom-select-bg, 5%));
117+
background-color: darken($custom-select-bg, 5%);
117118
}
118119
}
119120

@@ -127,13 +128,14 @@
127128
// for <optgroup>
128129
.select2-results__option[role="group"] {
129130
padding: 0;
131+
130132
.select2-results__options--nested .select2-results__option {
131133
padding-left: 1em;
132134
}
133135
}
134136

135137
.select2-results__option {
136-
padding: $input-padding-y $input-padding-x;
138+
padding: $custom-select-padding-y $custom-select-padding-x;
137139
}
138140

139141
// fixes vertical overflow
@@ -148,21 +150,25 @@
148150
color: $secondary;
149151
}
150152

151-
// all clear button
153+
// clear all button
152154
.select2-selection__clear {
153-
$bg: #c8c8c8;
154155
float: right;
155-
width: .9em;
156-
height: .9em;
157-
padding-left: .15em;
158-
margin-top: .7em;
159-
margin-right: .3em;
160-
line-height: .75em;
161-
color: $light;
162-
background-color: $bg;
156+
width: 1em;
157+
height: 1em;
158+
padding: 0;
159+
margin: subtract($custom-select-line-height * 1em, 1em) / 2 0 0;
160+
font-weight: 700;
161+
line-height: (12em / 16);
162+
color: $custom-select-color;
163+
background: darken($custom-select-bg, 10%) escape-svg($s2bs4-selection-clear-icon) center no-repeat !important; // stylelint-disable-line declaration-no-important
163164
border-radius: 100%;
165+
166+
span {
167+
display: none;
168+
}
169+
164170
&:hover {
165-
background-color: darken($bg, 10%);
171+
background-color: darken($custom-select-bg, 15%) !important; // stylelint-disable-line declaration-no-important
166172
}
167173
}
168174
}

0 commit comments

Comments
 (0)