Skip to content

Commit 7a549ba

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 b06f529 commit 7a549ba

File tree

7 files changed

+180
-246
lines changed

7 files changed

+180
-246
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: 64 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@
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-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";
24+
font-size: 1rem;
25+
font-weight: 400;
26+
line-height: 1.5;
27+
color: #495057;
28+
vertical-align: middle;
2129
background-color: #fff;
2230
border: 1px solid #ced4da;
2331
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; }
32+
-webkit-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, -webkit-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;
35+
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; }
2836
@media (prefers-reduced-motion: reduce) {
2937
.select2-container--bootstrap4 .select2-selection {
3038
-webkit-transition: none;
@@ -35,11 +43,6 @@
3543
-webkit-box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
3644
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); }
3745

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-
4346
.select2-container--bootstrap4.select2-container--open.select2-container--above .select2-selection {
4447
border-top-left-radius: 0;
4548
border-top-right-radius: 0; }
@@ -66,26 +69,21 @@ select.is-valid ~ .select2-container--bootstrap4 .select2-selection,
6669
form.was-validated select:valid ~ .select2-container--bootstrap4 .select2-selection {
6770
border-color: #28a745; }
6871

69-
.select2-container--bootstrap4 .select2-search {
70-
width: 100%; }
71-
7272
.select2-container--bootstrap4 .select2-dropdown {
7373
border-color: #ced4da;
7474
border-radius: 0; }
7575
.select2-container--bootstrap4 .select2-dropdown.select2-dropdown--below {
76-
border-top: none;
7776
border-bottom-right-radius: 0.25rem;
7877
border-bottom-left-radius: 0.25rem; }
7978
.select2-container--bootstrap4 .select2-dropdown.select2-dropdown--above {
80-
border-top: 1px solid #ced4da;
8179
border-top-left-radius: 0.25rem;
8280
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; }
8681

87-
.select2-container--bootstrap4 .select2-results__option--highlighted,
88-
.select2-container--bootstrap4 .select2-results__option--highlighted.select2-results__option[aria-selected="true"] {
82+
.select2-container--bootstrap4 .select2-results__option--selected {
83+
color: #212529;
84+
background-color: #e6e6e6; }
85+
86+
.select2-container--bootstrap4 .select2-results__option--highlighted {
8987
color: #fff;
9088
background-color: #007bff; }
9189

@@ -108,42 +106,26 @@ form.was-validated select:valid ~ .select2-container--bootstrap4 .select2-select
108106

109107
.select2-container--bootstrap4 .select2-selection__clear {
110108
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;
109+
width: 1em;
110+
height: 1em;
111+
padding: 0;
112+
margin: 0.25em 0 0;
113+
font-weight: 700;
114+
line-height: 0.75em;
115+
color: #495057;
116+
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;
119117
border-radius: 100%; }
118+
.select2-container--bootstrap4 .select2-selection__clear span {
119+
display: none; }
120120
.select2-container--bootstrap4 .select2-selection__clear:hover {
121-
background-color: #afafaf; }
121+
background-color: #d9d9d9 !important; }
122122

123123
.select2-container--bootstrap4 .select2-selection--single {
124-
height: calc(1.5em + 0.75rem + 2px) !important; }
124+
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; }
125125
.select2-container--bootstrap4 .select2-selection--single .select2-selection__placeholder {
126-
line-height: calc(1.5em + 0.75rem);
127126
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; }
144127
.select2-container--bootstrap4 .select2-selection--single .select2-selection__rendered {
145-
padding-left: 0.75rem;
146-
line-height: calc(1.5em + 0.75rem);
128+
padding: 0;
147129
color: #495057; }
148130

149131
.select2-search--dropdown .select2-search__field {
@@ -155,44 +137,50 @@ form.was-validated select:valid ~ .select2-container--bootstrap4 .select2-select
155137
color: #6c757d; }
156138

157139
.select2-container--bootstrap4 .select2-selection--multiple {
158-
min-height: calc(1.5em + 0.75rem + 2px) !important; }
140+
padding: 0 0.75rem; }
159141
.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; }
142+
margin: 0; }
143+
.select2-container--bootstrap4 .select2-selection--multiple .select2-search--inline {
144+
display: inline-block; }
166145
.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;
146+
position: relative;
147+
display: inline-block;
148+
padding: 0 5px 0 21px;
149+
margin: 0.375rem 0.375rem 0.375rem 0;
150+
color: #fff;
151+
vertical-align: top;
173152
cursor: pointer;
174-
border: 1px solid #bdc6d0;
153+
background-color: #6c757d;
175154
border-radius: 0.2rem; }
155+
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__display {
156+
padding: 0 5px; }
176157
.select2-container--bootstrap4 .select2-selection--multiple .select2-search__field {
158+
height: calc(1.5em + 0.75rem);
159+
padding-top: 0.375rem;
160+
margin: 0;
161+
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";
162+
line-height: 1.5;
177163
color: #495057; }
178-
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice + .select2-search {
179-
width: 0; }
180164
.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;
165+
position: absolute;
166+
top: 0;
167+
left: 0;
168+
padding: 0 5px;
169+
margin: 0;
186170
font-weight: 700;
187-
color: #bdc6d0; }
171+
color: rgba(255, 255, 255, 0.75);
172+
cursor: pointer;
173+
background: none;
174+
border: none;
175+
border-right: 1px solid #a1a8ae; }
188176
.select2-container--bootstrap4 .select2-selection--multiple .select2-selection__choice__remove:hover {
189-
color: #343a40; }
177+
color: rgba(255, 255, 255, 0.85); }
178+
.select2-container--bootstrap4 .select2-selection--multiple.select2-selection--clearable {
179+
padding-right: 32px; }
190180
.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; }
181+
position: absolute;
182+
top: 0.375rem;
183+
right: 0.75rem; }
196184

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

0 commit comments

Comments
 (0)