Skip to content

Commit 5ce55e8

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 - Use the same Browserlist configuration as Bootstrap 4.6 Ref: https://getbootstrap.com/docs/4.6/components/forms/#select-menu Close: ttskch#49, ttskch#60, ttskch#61
1 parent a7c1e46 commit 5ce55e8

File tree

7 files changed

+244
-252
lines changed

7 files changed

+244
-252
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: 87 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
outline: 0; }
55

66
.input-group .select2-container--bootstrap4 {
7-
-webkit-box-flex: 1;
8-
-ms-flex-positive: 1;
9-
flex-grow: 1; }
7+
-ms-flex-positive: 1;
8+
flex-grow: 1; }
109

1110
.input-group-prepend ~ .select2-container--bootstrap4 .select2-selection {
1211
border-top-left-radius: 0;
@@ -18,27 +17,25 @@
1817

1918
.select2-container--bootstrap4 .select2-selection {
2019
width: 100%;
20+
min-height: calc(1.5em + 0.75rem + 2px);
21+
padding: 0.375rem 1.75rem 0.375rem 0.75rem;
22+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
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+
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out; }
2832
@media (prefers-reduced-motion: reduce) {
2933
.select2-container--bootstrap4 .select2-selection {
30-
-webkit-transition: none;
3134
transition: none; } }
3235

3336
.select2-container--bootstrap4.select2-container--focus .select2-selection {
3437
border-color: #80bdff;
35-
-webkit-box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
36-
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); }
37-
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; }
38+
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); }
4239

4340
.select2-container--bootstrap4.select2-container--open.select2-container--above .select2-selection {
4441
border-top-left-radius: 0;
@@ -48,15 +45,12 @@
4845
border-bottom-right-radius: 0;
4946
border-bottom-left-radius: 0; }
5047

51-
.select2-container--bootstrap4.select2-container--disabled .select2-selection, .select2-container--bootstrap4.select2-container--disabled.select2-container--focus .select2-selection {
48+
.select2-container--bootstrap4.select2-container--disabled .select2-selection,
49+
.select2-container--bootstrap4.select2-container--disabled .select2-search__field {
5250
cursor: not-allowed;
5351
background-color: #e9ecef;
5452
border-color: #ced4da;
55-
-webkit-box-shadow: none;
56-
box-shadow: none; }
57-
58-
.select2-container--bootstrap4.select2-container--disabled .select2-search__field, .select2-container--bootstrap4.select2-container--disabled.select2-container--focus .select2-search__field {
59-
background-color: transparent; }
53+
box-shadow: none; }
6054

6155
select.is-invalid ~ .select2-container--bootstrap4 .select2-selection,
6256
form.was-validated select:invalid ~ .select2-container--bootstrap4 .select2-selection {
@@ -66,136 +60,127 @@ select.is-valid ~ .select2-container--bootstrap4 .select2-selection,
6660
form.was-validated select:valid ~ .select2-container--bootstrap4 .select2-selection {
6761
border-color: #28a745; }
6862

69-
.select2-container--bootstrap4 .select2-search {
70-
width: 100%; }
71-
7263
.select2-container--bootstrap4 .select2-dropdown {
7364
border-color: #ced4da;
7465
border-radius: 0; }
7566
.select2-container--bootstrap4 .select2-dropdown.select2-dropdown--below {
76-
border-top: none;
7767
border-bottom-right-radius: 0.25rem;
7868
border-bottom-left-radius: 0.25rem; }
7969
.select2-container--bootstrap4 .select2-dropdown.select2-dropdown--above {
80-
border-top: 1px solid #ced4da;
8170
border-top-left-radius: 0.25rem;
8271
border-top-right-radius: 0.25rem; }
83-
.select2-container--bootstrap4 .select2-dropdown .select2-results__option[aria-selected="true"] {
84-
color: #212529;
85-
background-color: #f2f2f2; }
86-
87-
.select2-container--bootstrap4 .select2-results__option--highlighted,
88-
.select2-container--bootstrap4 .select2-results__option--highlighted.select2-results__option[aria-selected="true"] {
89-
color: #fff;
90-
background-color: #007bff; }
91-
92-
.select2-container--bootstrap4 .select2-results__option[role="group"] {
93-
padding: 0; }
94-
.select2-container--bootstrap4 .select2-results__option[role="group"] .select2-results__options--nested .select2-results__option {
95-
padding-left: 1em; }
9672

9773
.select2-container--bootstrap4 .select2-results__option {
9874
padding: 0.375rem 0.75rem; }
75+
.select2-container--bootstrap4 .select2-results__option--selected {
76+
color: #212529;
77+
background-color: #e9ecef; }
78+
.select2-container--bootstrap4 .select2-results__option--disabled {
79+
color: #6c757d; }
80+
.select2-container--bootstrap4 .select2-results__option--highlighted {
81+
color: #fff;
82+
background-color: #007bff; }
83+
.select2-container--bootstrap4 .select2-results__option--group {
84+
padding: 0; }
85+
.select2-container--bootstrap4 .select2-results__option--group .select2-results__options--nested .select2-results__option {
86+
padding-left: 0.75rem; }
9987

10088
.select2-container--bootstrap4 .select2-results > .select2-results__options {
10189
max-height: 15em;
10290
overflow-y: auto; }
10391

10492
.select2-container--bootstrap4 .select2-results__group {
10593
display: list-item;
106-
padding: 6px;
94+
padding: 0.375rem 0.375rem;
10795
color: #6c757d; }
10896

10997
.select2-container--bootstrap4 .select2-selection__clear {
11098
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;
99+
width: 1em;
100+
height: 1em;
101+
padding: 0;
102+
margin: 0.25em 0 0;
103+
font-weight: 700;
104+
line-height: 0.75em;
105+
color: #495057;
106+
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;
119107
border-radius: 100%; }
108+
.select2-container--bootstrap4 .select2-selection__clear span {
109+
display: none; }
120110
.select2-container--bootstrap4 .select2-selection__clear:hover {
121-
background-color: #afafaf; }
111+
background-color: #d9d9d9 !important; }
112+
113+
.select2-container--bootstrap4 .select2-results__message {
114+
color: #6c757d; }
122115

123116
.select2-container--bootstrap4 .select2-selection--single {
124-
height: calc(1.5em + 0.75rem + 2px) !important; }
117+
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; }
125118
.select2-container--bootstrap4 .select2-selection--single .select2-selection__placeholder {
126-
line-height: calc(1.5em + 0.75rem);
127119
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; }
144120
.select2-container--bootstrap4 .select2-selection--single .select2-selection__rendered {
145-
padding-left: 0.75rem;
146-
line-height: calc(1.5em + 0.75rem);
121+
padding: 0;
147122
color: #495057; }
148123

149-
.select2-search--dropdown .select2-search__field {
124+
.select2-container--bootstrap4 .select2-search--dropdown .select2-search__field {
150125
padding: 0.375rem 0.75rem;
126+
color: #495057;
151127
border: 1px solid #ced4da;
152128
border-radius: 0.25rem; }
153-
154-
.select2-results__message {
155-
color: #6c757d; }
129+
.select2-container--bootstrap4 .select2-search--dropdown .select2-search__field:focus {
130+
color: #495057;
131+
background-color: #fff;
132+
border-color: #80bdff;
133+
outline: 0;
134+
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); }
156135

157136
.select2-container--bootstrap4 .select2-selection--multiple {
158-
min-height: calc(1.5em + 0.75rem + 2px) !important; }
137+
padding: 0 0.75rem; }
159138
.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; }
139+
margin: 0; }
140+
.select2-container--bootstrap4 .select2-selection--multiple .select2-search--inline {
141+
display: inline-block; }
166142
.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;
143+
position: relative;
144+
display: inline-block;
145+
padding: 0 5px 0 21px;
146+
margin: 0.375rem 0.375rem 0.375rem 0;
147+
color: #fff;
148+
vertical-align: top;
173149
cursor: pointer;
174-
border: 1px solid #bdc6d0;
150+
background-color: #6c757d;
175151
border-radius: 0.2rem; }
152+
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__display {
153+
padding: 0 5px; }
176154
.select2-container--bootstrap4 .select2-selection--multiple .select2-search__field {
155+
height: calc(1.5em + 0.75rem);
156+
padding-top: 0.375rem;
157+
margin: 0;
158+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
159+
line-height: 1.5;
177160
color: #495057; }
178-
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice + .select2-search {
179-
width: 0; }
180161
.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;
162+
position: absolute;
163+
top: 0;
164+
left: 0;
165+
padding: 0 5px;
166+
margin: 0;
186167
font-weight: 700;
187-
color: #bdc6d0; }
168+
color: rgba(255, 255, 255, 0.75);
169+
cursor: pointer;
170+
background: none;
171+
border: none;
172+
border-right: 1px solid #a1a8ae; }
188173
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__remove:hover {
189-
color: #343a40; }
174+
color: rgba(255, 255, 255, 0.85); }
175+
.select2-container--bootstrap4 .select2-selection--multiple.select2-selection--clearable {
176+
padding-right: 32px; }
190177
.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; }
178+
position: absolute;
179+
top: 0.375rem;
180+
right: 0.75rem; }
196181

197182
.select2-container--bootstrap4.select2-container--disabled .select2-selection--multiple .select2-selection__choice {
198-
padding: 0 5px;
183+
padding-left: 5px;
199184
cursor: not-allowed; }
200185
.select2-container--bootstrap4.select2-container--disabled .select2-selection--multiple .select2-selection__choice .select2-selection__choice__remove {
201186
display: none; }

0 commit comments

Comments
 (0)