Choices.js w/ DaisyUI styling #2817
Replies: 7 comments 13 replies
-
One edge case I found is that the following class .choices__inner:has(select) {
@apply select select-bordered w-full;
} limits the height and doesn't allow it to expand the selection container. EDITAdding .choices__inner:has(select) {
@apply w-full select select-bordered h-fit;
} |
Beta Was this translation helpful? Give feedback.
-
Thank you! This is awesome. My use case is selecting which groups of users have access to a specific item in my saas. |
Beta Was this translation helpful? Give feedback.
-
This is exactly what I was lookin for. Thanks! Looks great, but for some reason the edges are not curved for me. I included the css in my main css file and see all the styling in my output, yet for some reason the curves of the dropdown are not the same as for the the standard DaisyUI dropdown. Any thoughts? |
Beta Was this translation helpful? Give feedback.
-
Looked like this for me when initially installed: I had to do the following modifications to make it work: --- tmp/choices.css 2024-09-26 11:03:44
+++ app/assets/stylesheets/choices.css 2024-09-26 11:07:54
@@ -1,4 +1,10 @@
- .choices {
+.js-choices {
+ @apply max-h-12;
+}
+.js-choices option {
+ @apply hidden;
+}
+.choices {
@apply relative overflow-hidden;
}
@@ -38,7 +44,9 @@
@apply w-2 ml-0 p-2 bg-center;
background-image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjEiIGhlaWdodD0iMjEiIHZpZXdCb3g9IjAgMCAyMSAyMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48ZyBmaWxsPSIjMDAwIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiPjxwYXRoIGQ9Ik0yLjU5Mi4wNDRsMTguMzY0IDE4LjM2NC0yLjU0OCAyLjU0OEwuMDQ0IDIuNTkyeiIvPjxwYXRoIGQ9Ik0wIDE4LjM2NEwxOC4zNjQgMGwyLjU0OCAyLjU0OEwyLjU0OCAyMC45MTJ6Ii8+PC9nPjwvc3ZnPg==);
- background-size: 8px;
+ background-size: contain;
+ height: fit-content;
+ background-position: center;
}
.choices[data-type*=select-one] .choices__button:focus,.choices[data-type*=select-one] .choices__button:hover {
@@ -74,7 +82,9 @@
@apply relative inline-block w-2;
background-image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjEiIGhlaWdodD0iMjEiIHZpZXdCb3g9IjAgMCAyMSAyMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48ZyBmaWxsPSIjRkZGIiBmaWxsLXJ1bGU9ImV2ZW5vZGQiPjxwYXRoIGQ9Ik0yLjU5Mi4wNDRsMTguMzY0IDE4LjM2NC0yLjU0OCAyLjU0OEwuMDQ0IDIuNTkyeiIvPjxwYXRoIGQ9Ik0wIDE4LjM2NEwxOC4zNjQgMGwyLjU0OCAyLjU0OEwyLjU0OCAyMC45MTJ6Ii8+PC9nPjwvc3ZnPg==);
- background-size: 8px;
+ background-size: contain;
+ height: fit-content;
+ background-position: center;
}
.choices[data-type*=select-multiple] .choices__button:focus,.choices[data-type*=select-multiple] .choices__button:hover,.choices[data-type*=text] .choices__button:focus,.choices[data-type*=text] .choices__button:hover {
@@ -90,7 +100,7 @@
}
.choices:has(select) .choices__input {
- @apply inline-block text-sm border-0 px-3 ring-0 border-base-300 py-2 !ring-transparent;
+ @apply inline-block text-sm border-0 px-3 ring-0 border-base-300 py-2 !ring-transparent bg-inherit;
}
.choices:has(select[multiple]) .choices__input {
@@ -126,7 +136,7 @@
}
.choices__list--multiple .choices__item {
- @apply badge badge-primary rounded-lg mr-1 break-all box-border inline-flex inline-table;
+ @apply badge badge-secondary rounded-lg mr-1 break-all box-border inline-flex inline-table;
}
.choices__list--multiple .choices__item[data-deletable] { |
Beta Was this translation helpful? Give feedback.
-
This css looks completely broken, all rules appear empty in dev tools: |
Beta Was this translation helpful? Give feedback.
-
The multiple select input appears with awkward white background: |
Beta Was this translation helpful? Give feedback.
-
Since some time Choices.js support overriding classes in its constructor with The example which uses alpinejs: <link href="https://cdn.jsdelivr.net/npm/daisyui@4.12.10/dist/full.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com?plugins=typography"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.14.1/dist/cdn.js"></script>
<script>
daisyChoices = {
containerOuter: 'choices relative overflow-hidden rounded-lg',
containerInner: 'choices__inner inline-table px-3 py-0 min-h-1 h-1 select w-full bg-inherit pe-10 cursor-text',
input: 'choices__input pl-0 inline-block text-sm border-0 ring-0 border-base-300 px-0 py-0 !ring-transparent bg-inherit w-full',
list: 'choices__list m-0 pl-0 list-none border-0 bg-inherit',
listDropdown: 'choices__list--dropdown z-[1] absolute px-2 !bg-base-100 lg:px-4 w-full border-1 border-base-300 top-full overflow-auto break-all will-change-auto',
item: 'choices__item badge badge-primary rounded-lg mr-1 break-all box-border inline-table',
itemChoice: 'choices__item--choice cursor-pointer !block !m-auto !my-2 badge-ghost w-full h-auto hover:bg-primary hover:text-primary-content !text-base bg-base-100 border-none',
button: "choices__button -indent-[9999px] h-fit appearance-none border-0 bg-transparent cursor-pointer bg-no-repeat h-2 ml-2 bg-[length:1rem_1rem] hover:bg-[length:1.2rem_1.2rem] opacity-100 bg-center bg-[url('/static/x-mark.svg')] relative inline-block w-4",
disabledState: 'is-disabled bg-base-200 opacity-50',
openState: 'overflow-visible',
}
// Convert string to list of classes
for (var k in daisyChoices) {
daisyChoices[k] = daisyChoices[k].split(' ').filter(x => !!x)
}
</script>
<body>
<select multiple
x-init="$el.choices = new Choices($el, {classNames: daisyChoices})"
class="select hidden w-full max-w-xs"
>
<option value="1"> First </option>
<option value="2"> Second </option>
</select>
</body> |
Beta Was this translation helpful? Give feedback.
-
Hey everyone, I wanted to share what I've been working on for the past hour. I've been diving into this thing for several reasons, but primarily because I found a limited selection of visually appealing libraries like Select2, Choices.js, and Tom Select that meet my specific requirements, specifically when it is paired with Tailwind CSS. Therefore, I took it upon myself to create one that fits.
Please feel free to give it a try, and I hope it meets your expectations!
The CSS file
The CSS file (updated after the issue @Luzefiru provided below)
There are certain aspects that I haven't had the opportunity to test yet as they primarily relate to edge cases. However, if you're interested in contributing, I've prepared a checklist detailing what has been tested and what remains to be explored:
Beta Was this translation helpful? Give feedback.
All reactions