Published on

Beginners Guide: Make A Select with search With Tailwind CSS

Select with search

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Select with search ui component

Alpinejs search

Why use Tailwind CSS to create a Select with search ui component?

  • It can make the building process of Select with search ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Select with search component file.

The preview of Select with search ui component

Free download of the Select with search's source code

The source code of Select with search ui component

<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

<div class="relative max-w-sm mx-auto text-xs"
     x-data="{
            search: '',
            showSelector: false,
            selected: {1:'Chris'},
            options: [],
            clearOpts() {
                this.search = '';
                this.showSelector = false;
                this.options = []
            },
            select(id, name) {
                this.selected[id] = name;
                this.clearOpts();
                $dispatch('selected', Object.keys(this.selected));
            },
            remove(id) {
                delete this.selected[id]
                $dispatch('selected', Object.keys(this.selected));
            },
            goSearch() {
                if (this.search) {
                    this.options = {5: 'Carl', 6: 'Alex', 7: 'Bryan'};
                    this.showSelector = true;
                } else {
                    this.showSelector = false;
                }
            },
        }">
    <div class="bg-white rounded-md p-2 flex gap-1 flex-wrap" @click="$refs.search_input.focus()"
         @click.outside="showSelector=false">
        <template x-for="(name, id) in selected">
            <div class="bg-blue-200 rounded-md flex items-center">
                <div class="p-2" x-text="name"></div>
                <div @click="remove(id)"
                     class="p-2 select-none rounded-r-md cursor-pointer hover:bg-magma-orange-clear">
                    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M12.5745 1L1 12.5745" stroke="#FEAD69" stroke-width="2" stroke-linecap="round"/>
                        <path d="M1.00024 1L12.5747 12.5745" stroke="#FEAD69" stroke-width="2" stroke-linecap="round"/>
                    </svg>
                </div>
            </div>
        </template>
        <div class="flex-1">
            <input type="text" x-model="search" x-ref="search_input"
                   @input.debounce.400ms="goSearch();" placeholder="Search"
                   class="w-full border-0 focus:border-0 focus:outline-none focus:ring-0 py-1 px-0">
            <div x-show="showSelector" class="absolute left-0 bg-white z-30 w-full rounded-b-md font-medium">
                <div class="p-2 space-y-1">
                    <template x-for="(name, id) in options">
                        <div>
                            <template x-if="!selected[id]">
                                <div @click="select(id, name)"
                                     class="bg-blue-200 border-2 border-blue-200 cursor-pointer rounded-md p-2 hover:border-light-blue-1"
                                     x-text="name"></div>
                            </template>
                        </div>
                    </template>
                    <template x-if="options.length === 0">
                        <div class="text-gray-500">
                            No result
                        </div>
                    </template>
                </div>
            </div>
        </div>
    </div>
</div>

How to create a Select with search with Tailwind CSS?

Install tailwind css of verion 2.2.4

Use the script html tag to import the script of Tailwind CSS of the version 2.2.4

<script src="https://cdn.tailwindcss.com"></script>

All the unility class needed to create a Select with search component

  • relative
  • max-w-sm
  • mx-auto
  • text-xs
  • bg-white
  • p-2
  • flex
  • gap-1
  • flex-wrap
  • bg-blue-200
  • hover:bg-magma-orange-clear
  • flex-1
  • w-full
  • border-0
  • focus:border-0
  • py-1
  • px-0
  • absolute
  • left-0
  • z-30
  • border-2
  • border-blue-200
  • hover:border-light-blue-1
  • text-gray-500

24 steps to create a Select with search component with Tailwind CSS

  1. Use relative to position an element according to the normal flow of the document.

  2. Set the maximum width/height of an element using the max-w-sm utilities.

  3. Control the horizontal margin of an element to auto using the mx-auto utilities.

  4. Control the text color of an element to xs using the text-xs utilities.

  5. Control the background color of an element to white using the bg-white utilities.

  6. Control the padding on all sides of an element to 0.5rem using the p-2 utilities.

  7. Use flex to create a block-level flex container.

  8. To specify the width between columns, you can use the gap-1 utilities.

  9. Use flex to create a block-level flex container.

  10. Control the background color of an element to blue-200 using the bg-blue-200 utilities.

  11. Control the background color of an element to magma-orange-clear using the hover:bg-magma-orange-clear utilities on hover.

  12. Use flex to create a block-level flex container.

  13. Use w-full to set an element to a 100% based width.

  14. Control the border color of an element to 0rem using the border-0 utilities.

  15. Control the border color of an element to 0rem using the focus:border-0 utilities on focus.

  16. Control the vertical padding of an element to 0.25rem using the py-1 utilities.

  17. Control the horizontal padding of an element to 0rem using the px-0 utilities.

  18. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  19. Use the left-0 utilities to set the left position of a positioned element to 0rem.

  20. Control the stack order (or three-dimensional positioning) of an element to 30 in Tailwind, regardless of order it has been displayed, using the z-30 utilities.

  21. Control the border color of an element to 0.5rem using the border-2 utilities.

  22. Control the border color of an element to blue-200 using the border-blue-200 utilities.

  23. Control the border color of an element to light-blue-1 using the hover:border-light-blue-1 utilities on hover.

  24. Control the text color of an element to gray-500 using the text-gray-500 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to create a Select with search components, learn and follow along to implement your own components.