UNPKG

@llselect/core

Version:

Low-level select: a minimal, flexible, framework-agnostic replacement for the native HTML select element.

473 lines (410 loc) • 18.8 kB
/* llselect - Bootstrap 5 theme. Uses BS5 CSS custom properties so it inherits the user's Bootstrap colors / border radius / etc. Falls back to sensible defaults if BS5 vars are not present. */ /* Color variables: the light values. Every color below reads one of these, so a host page restyles the select component by redefining them in a rule that targets .llselect-root (README "Your own colors"). The dark values are in the block at the end of this file. Roles, one per line. */ .llselect-root { --llselect-fg: var(--bs-body-color, #212529); /* text inside the popup: header and footer slot content */ --llselect-control-fg: var(--llselect-fg); /* text of the trigger and of the filter input */ --llselect-fg-soft: var(--bs-secondary-color, #6c757d); /* the arrow and the group labels */ --llselect-fg-muted: var(--bs-secondary-color, #6c757d); /* the placeholder, the no-results text, the clear button at rest */ --llselect-bg: var(--bs-body-bg, #fff); /* surface of the trigger wrapper, the popup, the filter input, the pinned rows */ --llselect-bg-muted: var(--bs-tertiary-bg, #f8f9fa); /* surface of the group labels */ --llselect-trigger-wrapper-line: var(--bs-border-color, #dee2e6); /* border of the trigger wrapper */ --llselect-trigger-wrapper-hover-line: var(--bs-border-color, #dee2e6); /* border of the hovered trigger wrapper */ --llselect-popup-line: var(--bs-border-color-translucent, rgba(0, 0, 0, 0.175)); /* border of the popup */ --llselect-filter-line: var(--bs-border-color, #dee2e6); /* border of the filter input */ --llselect-divider: rgba(var(--bs-body-color-rgb, 33, 37, 41), 0.12); /* dividers before and after action rows and the choose-all row; header, footer and pinned-row edges; follows Bootstrap's text color */ --llselect-focus-line: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.5); /* border of the trigger wrapper while focus is in it or the popup is open (vanilla: an outline, while focus is in it); outline of a focused trigger button */ --llselect-filter-focus-line: var(--bs-primary, #0d6efd); /* border of the focused filter input */ --llselect-focus-ring: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.25); /* ring shadow of the trigger wrapper while focus is in it or the popup is open (vanilla: unused) */ --llselect-filter-focus-ring: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.25); /* ring shadow of the focused filter input (vanilla: unused) */ --llselect-shadow: rgba(var(--bs-body-color-rgb, 33, 37, 41), 0.15); /* color of the popup shadow */ --llselect-control-shadow: transparent; /* color of the inset shadow of the trigger wrapper (read by bootstrap-3 only) */ --llselect-row-fg: var(--llselect-fg); /* text of a row */ --llselect-row-hover-bg: var(--bs-tertiary-bg, #f8f9fa); /* surface of a hovered row */ --llselect-row-hover-fg: var(--llselect-fg); /* text of a hovered row */ --llselect-row-focused-bg: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.12); /* surface of the row that keyboard focus is on */ --llselect-row-focused-fg: var(--llselect-fg); /* text of that row */ --llselect-row-focused-hover-bg: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.18); /* surface of that row while hovered */ --llselect-row-chosen-fg: var(--bs-primary, #0d6efd); /* text of a chosen row */ --llselect-tag-bg: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.12); /* surface of a tag */ --llselect-tag-fg: var(--bs-primary, #0d6efd); /* text of a tag */ --llselect-tag-line: transparent; /* border of a tag */ --llselect-tag-remove-fg: var(--llselect-tag-fg); /* the x button of a tag */ --llselect-tag-remove-hover-bg: transparent; /* surface of the hovered x button */ --llselect-tag-remove-hover-fg: var(--llselect-tag-fg); /* the hovered x button */ --llselect-clear-hover-fg: var(--llselect-fg); /* the hovered clear button */ --llselect-clear-hover-bg: var(--bs-tertiary-bg, #f8f9fa); /* surface of the hovered clear button */ } .llselect-root { position: relative; display: inline-block; min-width: 14rem; font-family: var(--bs-font-sans-serif, system-ui, sans-serif); font-size: var(--bs-body-font-size, 1rem); color: var(--llselect-control-fg); line-height: var(--bs-body-line-height, 1.5); } /* --- trigger (matches .form-control look) --------------------- */ .llselect-trigger-wrapper { display: flex; align-items: center; padding: 0.375rem 0.75rem; border: var(--bs-border-width, 1px) solid var(--llselect-trigger-wrapper-line); border-radius: var(--bs-border-radius, 0.375rem); background-color: var(--llselect-bg); color: inherit; cursor: pointer; user-select: none; transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out; } .llselect-trigger-wrapper:hover { border-color: var(--llselect-trigger-wrapper-hover-line); } .llselect-trigger-wrapper:focus-within { border-color: var(--llselect-focus-line); box-shadow: 0 0 0 0.25rem var(--llselect-focus-ring); } .llselect-trigger-wrapper[data-state='open'] { border-color: var(--llselect-focus-line); box-shadow: 0 0 0 0.25rem var(--llselect-focus-ring); } /* The trigger takes focus; the wrapper draws the focus ring. */ .llselect-trigger { display: flex; flex: 1 1 auto; min-width: 0; outline: 0; } .llselect-trigger-content { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .llselect-trigger-wrapper[data-empty='true'] .llselect-trigger-content { color: var(--llselect-fg-muted); } .llselect-trigger-arrow { display: inline-flex; align-items: center; margin-inline-start: 0.5rem; color: var(--llselect-fg-soft); transition: transform 0.2s ease-in-out; } .llselect-trigger-arrow:empty { display: none; } .llselect-trigger-wrapper[data-state='open'] .llselect-trigger-arrow { transform: rotate(180deg); } /* An arrow that carries data-llselect-no-rotate, such as the up-down icons, stays still. Browsers without :has() still turn it. The first rule stops the slot from turning such an arrow back when it replaces a turned arrow, on a close or a rerender(). */ .llselect-trigger-arrow:has(> [data-llselect-no-rotate]) { transition: none; } .llselect-trigger-wrapper[data-state='open'] .llselect-trigger-arrow:has(> [data-llselect-no-rotate]) { transform: none; } /* --- popup (matches .dropdown-menu look) ---------------------- */ .llselect-popup { background-color: var(--llselect-bg); border: var(--bs-border-width, 1px) solid var(--llselect-popup-line); border-radius: var(--bs-border-radius, 0.375rem); box-shadow: 0 0.5rem 1rem var(--llselect-shadow); color: var(--llselect-fg); z-index: 1000; padding: 0.5rem 0; } /* --- items (matches .dropdown-item look) ---------------------- */ .llselect-popup-list-row { padding: 0.25rem 1rem; cursor: pointer; color: var(--llselect-row-fg); background-color: transparent; } .llselect-popup-list-row:hover { background-color: var(--llselect-row-hover-bg); color: var(--llselect-row-hover-fg); } .llselect-popup-list-row-focused { background-color: var(--llselect-row-focused-bg); color: var(--llselect-row-focused-fg); } .llselect-popup-list-row-focused:hover { background-color: var(--llselect-row-focused-hover-bg); } .llselect-popup-list-row[aria-selected='true'] { font-weight: 600; color: var(--llselect-row-chosen-fg); } /* --- choose-all row (multi, opt-in) ----------------------------- */ /* The default row content is just the counting text (its numbers carry the tri-state); data-chosen-state is the hook if a theme wants to draw its own indicator, createChooseAllRowContentElFn the way to add a real one. The row also carries the popup-list-row class, so hover / focused styling comes from there. Its divider is drawn by the block-edge rules under "dividers around the command rows": one line where the block above the items ends, none between the row and a command row that follows it. */ /* --- groups (Phase 10, matches .dropdown-header look) --------- */ /* Static label by default; rect-based scroll math (keyboard.ts) allows a theme to make it `position: sticky; top: 0`. */ .llselect-group-label { padding: 0.5rem 1rem; font-size: 0.875rem; font-weight: 600; color: var(--llselect-fg-soft); background-color: var(--llselect-bg-muted); } .llselect-group[data-disabled='true'] .llselect-group-label { opacity: 0.55; cursor: not-allowed; } /* --- filter input (Phase 8) ------------------------------------ */ .llselect-filter-input { flex: none; margin: 0.5rem; padding: 0.375rem 0.75rem; border: 1px solid var(--llselect-filter-line); border-radius: var(--bs-border-radius, 0.375rem); font: inherit; outline: none; background-color: var(--llselect-bg); color: var(--llselect-control-fg); } .llselect-filter-input:focus { border-color: var(--llselect-filter-focus-line); box-shadow: 0 0 0 0.25rem var(--llselect-filter-focus-ring); } /* --- no-results message ----------------------------------------- */ .llselect-popup-list-no-results { padding: 0.25rem 1rem; color: var(--llselect-fg-muted); } /* --- dividers around the command rows --------------------------- */ /* The choose-all row and the action rows form one block above the items and one below; they carry .llselect-popup-list-row too, so hover / focused / disabled styling comes from there. Exactly one line marks each block's edge with the items, drawn as the next element's border-top through sibling combinators (`:has` is above the browser floor). No line inside a block: choose-all followed by a command row reads as one group, like the header blocks of ng-select, Element Plus and Vuetify's select-all recipe. */ .llselect-choose-all-row + .llselect-popup-list-row:not(.llselect-popup-list-action-row), .llselect-choose-all-row + .llselect-group, .llselect-popup-list-action-row + .llselect-popup-list-row:not(.llselect-popup-list-action-row), .llselect-popup-list-action-row + .llselect-group, .llselect-popup-list-row:not(.llselect-popup-list-action-row):not(.llselect-choose-all-row) + .llselect-popup-list-action-row, .llselect-group + .llselect-popup-list-action-row { border-top: 1px solid var(--llselect-divider); } /* --- pinned command blocks --------------------------------------- */ /* popupListLeadingRowsPinned / popupListTrailingRowsPinned wrap a block in a presentational div that the library makes sticky. The theme gives it an opaque background (the items scroll under it) and the block's edge line, because the sibling rules above cannot see through the wrapper. The line is drawn only toward items: the library sets data-edge-to-items on every render, so two blocks that touch (nothing listed) draw no line at all. */ .llselect-popup-list-pinned-leading-rows, .llselect-popup-list-pinned-trailing-rows { background: var(--llselect-bg); z-index: 1; } .llselect-popup-list-pinned-leading-rows[data-edge-to-items='true'] { border-bottom: 1px solid var(--llselect-divider); } .llselect-popup-list-pinned-trailing-rows[data-edge-to-items='true'] { border-top: 1px solid var(--llselect-divider); } /* --- popup header / footer slots -------------------------------- */ /* Pinned above / below the list, outside the scroll area. The elements exist only when their content settings return an element; the divider matches the choose-all row. */ .llselect-popup-header { padding: 0.25rem 1rem; border-bottom: 1px solid var(--llselect-divider); } .llselect-popup-footer { padding: 0.25rem 1rem; border-top: 1px solid var(--llselect-divider); } /* --- disabled (Phase 9) ---------------------------------------- */ /* aria-disabled + data-disabled, never native `disabled`. Keep pointer events alive (no `pointer-events: none`) so hover/focus tooltips work. */ .llselect-trigger-wrapper[data-disabled='true'] { opacity: 0.55; cursor: not-allowed; } /* Embedded buttons are inert while the control is disabled: show not-allowed, not the pointer cursor their own rule sets. */ .llselect-trigger-wrapper[data-disabled='true'] .llselect-tag-remove-button, .llselect-trigger-wrapper[data-disabled='true'] .llselect-trigger-clear-button { cursor: not-allowed; } .llselect-popup-list-row-disabled { opacity: 0.45; cursor: not-allowed; } .llselect-popup-list-row-disabled:hover { background: none; } /* --- trigger buttons strip (clearable, triggerDisplay: 'tags') ---- */ /* Beside the trigger, before the arrow. The library sets `hidden` on it while nothing is chosen. */ .llselect-trigger-buttons { display: flex; align-items: center; gap: 0.5rem; margin-inline-start: 0.5rem; } .llselect-trigger-buttons[hidden] { display: none; } /* Tags mode, where the strip is a toolbar: the trigger is empty while chips show, so the chips take the free space and start where the text would. The large growth factor leaves the empty trigger next to nothing. */ .llselect-trigger-buttons[role='toolbar'] { flex: 9999 1 auto; margin-inline-start: 0; } /* A pointer press never focuses these buttons, so :focus is keyboard focus. */ .llselect-trigger-buttons button:focus { outline: 2px solid var(--llselect-focus-line); outline-offset: 1px; } /* --- tags (triggerDisplay: 'tags', chip look) ---------------- */ .llselect-tags { display: flex; flex: 1 1 auto; flex-wrap: wrap; gap: 0.25rem; } .llselect-tag { display: inline-flex; align-items: center; gap: 0.2rem; /* Logical: mirrors under dir="rtl" (text side start, x-button side end). */ padding-block: 0.1rem; padding-inline: 0.5rem 0.2rem; background-color: var(--llselect-tag-bg); color: var(--llselect-tag-fg); border-radius: var(--bs-border-radius-sm, 0.25rem); font-size: 0.85rem; line-height: 1.5; } .llselect-tag-remove-button { display: inline-flex; align-items: center; justify-content: center; width: 1.15em; height: 1.15em; padding: 0; border: none; border-radius: 0.2rem; background: transparent; color: inherit; opacity: 0.7; cursor: pointer; font: inherit; line-height: 1; } .llselect-tag-remove-button:empty::before { content: '\00d7'; font-size: 1.15em; } .llselect-tag-remove-button:hover { opacity: 1; } /* A disabled item's chip: greyed, its x inert (the JS click guard is the real block). */ .llselect-tag-disabled { opacity: 0.45; cursor: not-allowed; } .llselect-tag-disabled .llselect-tag-remove-button { cursor: not-allowed; } /* --- clear button (clearable) -------------------------------- */ /* :empty::before draws the x only when createTriggerClearButtonContentElFn gave no icon. */ .llselect-trigger-clear-button { display: inline-flex; align-items: center; justify-content: center; flex: none; width: 1.2em; height: 1.2em; padding: 0; border: none; border-radius: 0.2rem; background: transparent; color: var(--llselect-fg-muted); cursor: pointer; font: inherit; line-height: 1; } .llselect-trigger-clear-button:empty::before { content: '\00d7'; font-size: 1.2em; } .llselect-trigger-clear-button:hover { color: var(--llselect-clear-hover-fg); background-color: var(--llselect-clear-hover-bg); } /* --- dark color scheme ----------------------------------------------------- */ /* The library resolves the scheme and writes data-llselect-color-scheme on the root (DESIGN.md "Color scheme (light / dark) in the themes"); hosts never write that attribute. Variables not listed here keep their light value or their alias. */ .llselect-root[data-llselect-color-scheme='dark'] { --llselect-fg: var(--bs-body-color, #dee2e6); --llselect-fg-soft: var(--bs-secondary-color, rgba(222, 226, 230, 0.75)); --llselect-fg-muted: var(--bs-secondary-color, rgba(222, 226, 230, 0.75)); --llselect-bg: var(--bs-body-bg, #212529); --llselect-bg-muted: var(--bs-tertiary-bg, #2b3035); --llselect-trigger-wrapper-line: var(--bs-border-color, #495057); --llselect-trigger-wrapper-hover-line: var(--bs-border-color, #495057); --llselect-popup-line: var(--bs-border-color-translucent, rgba(255, 255, 255, 0.15)); --llselect-filter-line: var(--bs-border-color, #495057); --llselect-divider: rgba(var(--bs-body-color-rgb, 222, 226, 230), 0.12); --llselect-focus-line: #86b7fe; /* Bootstrap's input focus border; it keeps this value in dark mode and exposes no variable for it */ --llselect-filter-focus-line: var(--bs-link-color, #6ea8fe); --llselect-shadow: rgba(0, 0, 0, 0.5); --llselect-row-hover-bg: var(--bs-tertiary-bg, #2b3035); --llselect-row-focused-bg: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.25); --llselect-row-focused-hover-bg: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.35); --llselect-row-chosen-fg: var(--bs-link-color, #6ea8fe); --llselect-tag-bg: rgba(var(--bs-primary-rgb, 13, 110, 253), 0.25); --llselect-tag-fg: var(--bs-link-color, #6ea8fe); --llselect-clear-hover-bg: var(--bs-tertiary-bg, #2b3035); } /* Browser-painted parts, such as the popup scrollbar and a native control a host puts in the trigger or the popup, follow the resolved scheme. Never on .llselect-root: the resolver reads the computed color-scheme there. The child combinator keeps an outer select component's scheme off the trigger and the popup of a select component nested in it. */ .llselect-root[data-llselect-color-scheme='light'] > .llselect-trigger-wrapper, .llselect-root[data-llselect-color-scheme='light'] > .llselect-popup { color-scheme: light; } .llselect-root[data-llselect-color-scheme='dark'] > .llselect-trigger-wrapper, .llselect-root[data-llselect-color-scheme='dark'] > .llselect-popup { color-scheme: dark; } /* --- built-in dark marks ---------------------------------------------------- */ /* Each rule sets the CSS variable the resolver reads on the element that carries the mark; the nearest ancestor wins by inheritance. Rule order settles two marks on one element: an attribute beats a class name, and data-color-scheme beats everything. Origins: .dark Tailwind CSS and VitePress; .theme-dark Bulma 1; .dark-mode the Nuxt color-mode module; data-theme daisyUI, Pico CSS 2 and Bulma 1; data-bs-theme Bootstrap 5.3; data-color-scheme this project's pages, and any element a host marks. */ .dark, .theme-dark, .dark-mode { --llselect-color-scheme: dark; } .theme-light, .light-mode { --llselect-color-scheme: light; } [data-theme='dark'] { --llselect-color-scheme: dark; } [data-theme='light'] { --llselect-color-scheme: light; } [data-bs-theme='dark'] { --llselect-color-scheme: dark; } [data-bs-theme='light'] { --llselect-color-scheme: light; } [data-color-scheme='dark'] { --llselect-color-scheme: dark; } [data-color-scheme='light'] { --llselect-color-scheme: light; } [data-color-scheme='auto'] { --llselect-color-scheme: auto; }