The appearance of a Tom-select with the bootstrap 5 theme sets it apart from a bootstrap 5.1.3 styled select

Premise

In order to ensure consistent display of selects across Windows, Linux, and Mac in my project, I have implemented the following combination:

I have configured SCSS files for bootstrap and tom-select.

The frontend packages are managed using the Yarn package manager.

I utilize SCSS for customizing Bootstrap variables to create different themes within the site.

My setup with Symfony and Webpack Encore involves separate handling of SCSS and JS (CSS is not bundled with JavaScript). The CSS is compiled from SCSS that includes tom-select SCSS.

The webpage contains CSS (Bootstrap + customized themes + tom-select) and JS (bootstrap and tom-select independently).

Problem

  1. The select displays with a smaller than default box-shadow in Bootstrap 5.
  2. The height of the select element is +2px larger than default in Bootstrap 5.
  3. The dropdown of the select element starts further away from its field compared to default in Bootstrap 5.

Issues number 2 and 3 are minor inconveniences, but the first one is considered a significant problem.

Illustration

https://i.sstatic.net/jTvFe.png

Things I have tried

  1. Attempted to redefine the tom-select variable $input-focus-width, but it did not resolve the issue;
  2. Experimented with different arrangements of includes in the main SCSS override, but the problems persisted;
  3. Introduced additional CSS rules (see Update 1)

Code

Main SCSS override

@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";
@import "~bootstrap/scss/mixins";

$input-border-color: $gray-500;
$form-select-border-color: $gray-500;
$input-group-addon-border-color: $gray-500;
<input-focus-width: 0.25rem; /* has no effect on tom-select */

@import "~bootstrap/scss/bootstrap";
@import "~tom-select/src/scss/tom-select.bootstrap5";

Main SCSS imports override

@import "./main_override";

body
{
    background: #f0ede8 !important;
}

/* other styles follow */

TWIG template (page that includes compiled CSS)

{% extends 'base_admin.html.twig' %}

{% block title %}New firm{% endblock %}

{% block stylesheets %}
    {{ parent() }}
{% endblock %}

{% block javascripts %}
    {{ parent() }}

    {{ encore_entry_script_tags('tom_select') }}
{% endblock %}

{% block main_content %}
    <div class="wrapper">
        <h3 class="fix text-center">{% trans from 'menu' %}menu.firm.new{% endtrans %}</h3>

        <div class="container">
            <div class="inner-container">
                <div class="box-list clearfix">
                    {% if form is defined %}
                        {{ form_start(form, {'attr': {'id': 'form-firm-create', 'novalidate': 'novalidate', 'autocomplete': 'off'}, 'method': 'POST'}) }}
                            <div class="field-group">
                                {{ form_row(form.isSpecial, {'label_attr': {'class': 'form-group-label'}, 'attr': {'class': 'form-control'}}) }}
                            </div>
                            <div class="box-btn form-group text-center box-submit">
                                <a class="btn btn-secondary me-1" href="{{ path('firm_list') }}">
                                    <i class="fas fa-long-arrow-left"></i>
                                    <span>{%- trans from 'firms' %}firms.goToList{% endtrans -%}</span>
                                </a>
                                {{ form_widget(form.firmCreate) }}
                            </div>
                        {{ form_end(form) }}
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock %}

JavaScript executed on the page

"use strict";

import TomSelect from "tom-select/dist/js/tom-select.complete.min.js";

$(function()
{
    new TomSelect("#firm_create_isSpecial", {
        allowEmpty: false,
        create: false,
        sortField: {
            direction: "asc",
            field: "text"
        }
    });
});

Conclusion

What am I missing?

Is there an error in my configuration?

Thank you for any ideas and suggestions.

Update 1

By adding the following rules, I achieved a uniform width box-shadow and correct inner border color, although a slight flash occurs when clicking due to a 2px increase in width to match the control's proper width.

Added CSS rules

.ts-wrapper.form-select .ts-control,
.ts-wrapper.form-select.focus .ts-control
{
	border: 1px solid #fff !important;
}

.ts-wrapper.form-select.single.input-active .ts-control
{
	border: 1px solid #ccc !important;
}

.ts-wrapper.form-select.single.input-active.focus .ts-control
{
	border: 1px solid #86b7fe !important;
}

.my-ts-select.focus .ts-control,
.my-ts-select:focus .ts-control
{
	border: 1px solid #86b7fe !important;
	box-shadow: 0 0 0 .25rem #b7d5ff !important;
	outline: 0;
	width: calc(100% + 2px);
	margin-left: -1px;
}

.ts-control
{
	border: none !important;
}

I believe the provided CSS rules can be optimized. Feel free to share your suggestions.

Update 2

After making more modifications to the CSS, I found that removing the class form-select from the rendered field aligns the tom-select control with bootstrap styling without additional adjustments!

Answer №1

To ensure that tom-select appears with Bootstrap styling, you can utilize JavaScript to remove the 'form-select' class before configuring the field.

"use strict";

import TomSelect from "tom-select/dist/js/tom-select.complete.min.js";

window.addEventListener('load', (event) =>
{
    // eliminate form-select class from tom-select element
    document.getElementById("test_is_special").classList.remove('form-select');

    // initiate tom-select
    new TomSelect("#test_is_special", {
        allowEmpty: false,
        create: false,
        sortField: {
            direction: "asc",
            field: "text"
        }
    });
});

Answer №2

Upon switching to version 2.0.1 of tom-select, I encountered the same issue.

However, after updating both the JS and CSS files to version 2.0.3, the problem has been resolved.

Now, I am able to utilize form-control as well as form-control-sm, seamlessly aligning with other inputs and selects in BS5.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The jQuery click event not triggering on ASP.NET control

I am currently developing a C#/Asp application that utilizes jQuery. I am trying to implement a specific function that should be called every time a click event is triggered on an element with a class of "loadingInProgress". However, the code snippet belo ...

Retrieving the value of an array from a JSON data structure

I am working with the object shown below to extract the desired output. The result will be a new object that represents the final output. var data = { Customer: { Name: "emp1", Departments: [ {Departme ...

What is the best way to ensure that the circle is perfectly centered inside the box?

Currently delving into the world of game programming, I've been struggling with this exercise. I can't seem to figure out why the circle won't stop in the center of the box within the update function. What am I missing? var canvas = documen ...

Are there any other options besides using the React Material-UI makeStyles() function for styling class Components?

While experimenting with the makeStyles() function in Material-UI's React library, I encountered a specific error message: The use of hooks is limited to the body of a function component. Below is a snippet of the code that triggered this error: ...

The list of lists is giving an error: "Cannot read property 'name' of undefined."

What am I doing wrong here? let items = [{ name: 'client1' }, { name: 'client2' }, { name: "client3"}]; for (let i = 0; i < items.length; i++) { if (items[i]['name'].includes(self.autocomplete)) { self.box += '<l ...

Adding a nested data structure in a Meteor application

In my mongo collection, I have a document named exam. // meteor:PRIMARY> db.exam.find() { "_id" : "RLvWTcsrbRXJeTqdB", "examschoolid" : "5FF2JRddZdtTHuwkx", "examsubjects" : [ { "subject" : "Z4eLrwGwqG4pw4HKX" }, ...

Can you guide me on how to export a named function using module.exports?

I have a script file for my discord bot that includes a specific command and a function with parsing logic that I want to reuse in my main index.js // File: ./commands/scrumPrompt.js // The function const extractDeets = function (f, scrum) { let items ...

What is the best way to automatically create a PDF file that includes interactive JavaScript charts?

My goal is to create a word document or PDF containing charts from various JavaScript chart libraries. I've successfully implemented these libraries on websites, but now I want to include them in my documents as well. My initial attempt involved usin ...

execute php sql after a certain delay

Currently, I am working with PHP, SQL, and JavaScript to develop a friend request system. In this system, if User-1 sends a friend request to User-2, User-2 must accept the request within 1 hour. If not, the request will be automatically denied or removed. ...

Retrieve information using AngularJS only when it is not yet defined

Currently, I am using $http in a controller to retrieve data and display it to the user. However, once the data is fetched for the first time, I do not want to fetch it again when moving between different tabs or controllers. My expertise lies in web dev ...

Ways to prevent browser scrolling on smartphones and tablets

Having a simple HTML file with an iframe embedded, I have been attempting to prevent my browser from scrolling in both documents. However, this solution works intermittently and does not provide consistent results. I have tried applying various event list ...

The issue with onclientclick in Asp.Net button

I am experiencing a peculiar problem that I cannot seem to solve. The issue revolves around a button that I have implemented using the following code: <asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save" OnClientClick="return Confi ...

"An error occurs when trying to trigger a .click() event within a list element

There is a list that can contain either plain text or a link. If there is a link present, the entire list element should be clickable. When attempting to execute the following code: if ($('.link').length) { $('li[data-contains-link]' ...

HTML image vanishes once external jQuery link is incorporated

Although I have added an external link to my JavaScript file (<script type="text/javascript" src="script.js">) in the header of my HTML document, the image on my webpage mysteriously vanishes. HTML: <HTML> <HEAD> <link rel="st ...

Is it possible to redirect a URL with a readyState of 4?

Is it possible to redirect to a page when the readyState is equal to 4? //load the send form if (sendRequest) { sendRequest.open("POST", urlRequest, true); sendRequest.setRequestHeader("Content-Type", "application/x-www-form-urlenc ...

What is the best way to secure a folder and its contents utilizing sessions for added protection?

Currently, I am developing a project that involves assigning each user a dedicated folder for uploading files. However, there is a security concern where any user could potentially access files by simply typing in the path and filename. I am exploring met ...

When refreshing the page, redux-persist clears the state

I have integrated redux-persist into my Next.js project. The issue I am facing is that the state is getting saved in localStorage when the store is updated, but it gets reset every time the page changes. I suspect the problem lies within one of the reducer ...

Transforming a redux form onSubmit function into a promise-based structure

One of my goals is to promisify the onSubmit handling in my submitForm for redux form. You can find a similar example here. submitForm = () => { return this.props.submituserForm() .then(() => { console.log('test') }) ...

Dynamically insert <td> elements into <tr> element using both jQuery and JavaScript

I am facing an issue with adding a new table data (td) element dynamically to the first table row (tr) in my JavaScript code. Here is the original table before adding the new td element: <table> <tbody> <tr> <t ...

Create a variety of unique objects on the canvas without any repetition or unwanted overlapping

Is there a way to generate objects on a map in a HTML5 Canvas without them overlapping or occupying the same space? I tried checking inside an array to see if the next 20 values are already taken to prevent overlapping, but it didn't work as expected ...