fix dropdown width issue & improve ux

This commit is contained in:
tglide committed 2023-12-13 12:17:08 +00:00
1 parent 1b0c48e93b
commit 5d6967cee8
5 files changed
+232 -139

No files matched your search

+107
View File
@@ -28,6 +28,8 @@
"web-vitals": "^3.4.0"
},
"devDependencies": {
"@melt-ui/pp": "^0.1.4",
"@melt-ui/svelte": "^0.65.1",
"@playwright/test": "^1.37.1",
"@sveltejs/adapter-static": "^2.0.3",
"@sveltejs/kit": "^1.24.0",
@@ -53,6 +55,7 @@
"svelte-check": "^3.5.1",
"svelte-jester": "^2.3.2",
"svelte-preprocess": "^5.0.4",
"svelte-sequential-preprocessor": "^2.0.1",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vite": "^4.4.9",
@@ -1292,6 +1295,31 @@
"node": ">=14"
}
},
"node_modules/@floating-ui/core": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/@floating-ui/core/-/core-1.5.2.tgz",
"integrity": "sha512-Ii3MrfY/GAIN3OhXNzpCKaLxHQfJF9qvwq/kEJYdqDxeIHa01K8sldugal6TmeeXl+WMvhv9cnVzUTaFFJF09A==",
"dev": true,
"dependencies": {
"@floating-ui/utils": "^0.1.3"
}
},
"node_modules/@floating-ui/dom": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@floating-ui/dom/-/dom-1.5.3.tgz",
"integrity": "sha512-ClAbQnEqJAKCJOEbbLo5IUlZHkNszqhuxS4fHAVxRPXPya6Ysf2G8KypnYcOTpx6I8xcgF9bbHb6g/2KpbV8qA==",
"dev": true,
"dependencies": {
"@floating-ui/core": "^1.4.2",
"@floating-ui/utils": "^0.1.3"
}
},
"node_modules/@floating-ui/utils": {
"version": "0.1.6",
"resolved": "https://registry.npmjs.org/@floating-ui/utils/-/utils-0.1.6.tgz",
"integrity": "sha512-OfX7E2oUDYxtBvsuS4e/jSn4Q9Qb6DzgeYtsAdkPZ47znpoNsMgZw0+tVijiv3uGNR6dgNlty6r9rzIzHjtd/A==",
"dev": true
},
"node_modules/@humanwhocodes/config-array": {
"version": "0.11.13",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.13.tgz",
@@ -1325,6 +1353,15 @@
"integrity": "sha512-dvuCeX5fC9dXgJn9t+X5atfmgQAzUOWqS1254Gh0m6i8wKd10ebXkfNKiRK+1GWi/yTvvLDHpoxLr0xxxeslWw==",
"dev": true
},
"node_modules/@internationalized/date": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/@internationalized/date/-/date-3.5.0.tgz",
"integrity": "sha512-nw0Q+oRkizBWMioseI8+2TeUPEyopJVz5YxoYVzR0W1v+2YytiYah7s/ot35F149q/xAg4F1gT/6eTd+tsUpFQ==",
"dev": true,
"dependencies": {
"@swc/helpers": "^0.5.0"
}
},
"node_modules/@istanbuljs/load-nyc-config": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz",
@@ -1809,6 +1846,39 @@
"@jridgewell/sourcemap-codec": "^1.4.14"
}
},
"node_modules/@melt-ui/pp": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/@melt-ui/pp/-/pp-0.1.4.tgz",
"integrity": "sha512-zR+Kl3CZJPJBHW8V7YcdQCMI/dVcnW9Ct3yGbVaIywYVStVRS7F9uEDOea3xLLT2WTGodQePzPlUn53yKFu87g==",
"dev": true,
"dependencies": {
"estree-walker": "^3.0.3"
},
"engines": {
"pnpm": ">=8.6.3"
},
"peerDependencies": {
"@melt-ui/svelte": ">= 0.29.0",
"svelte": "^3.55.0 || ^4.0.0 || ^5.0.0-next.1"
}
},
"node_modules/@melt-ui/svelte": {
"version": "0.65.1",
"resolved": "https://registry.npmjs.org/@melt-ui/svelte/-/svelte-0.65.1.tgz",
"integrity": "sha512-EFLLcfT7i3ICsGmdK4i83V5+08jl6ZKFKxguvOr2zNV08cIXWwp4lIwoYLOpP6ydORrbJjiRBd9sfNJcrm5YWg==",
"dev": true,
"dependencies": {
"@floating-ui/core": "^1.3.1",
"@floating-ui/dom": "^1.4.5",
"@internationalized/date": "^3.5.0",
"dequal": "^2.0.3",
"focus-trap": "^7.5.2",
"nanoid": "^4.0.2"
},
"peerDependencies": {
"svelte": ">=3 <5"
}
},
"node_modules/@nodelib/fs.scandir": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
@@ -2081,6 +2151,15 @@
"vite": "^4.0.0"
}
},
"node_modules/@swc/helpers": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/@swc/helpers/-/helpers-0.5.3.tgz",
"integrity": "sha512-FaruWX6KdudYloq1AHD/4nU+UsMTdNE8CKyrseXWEcgjDAbvkwJg2QGPAnfIJLIWsjZOSPLOAykK6fuYp4vp4A==",
"dev": true,
"dependencies": {
"tslib": "^2.4.0"
}
},
"node_modules/@testing-library/dom": {
"version": "9.3.3",
"resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-9.3.3.tgz",
@@ -4546,6 +4625,15 @@
"integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==",
"dev": true
},
"node_modules/focus-trap": {
"version": "7.5.4",
"resolved": "https://registry.npmjs.org/focus-trap/-/focus-trap-7.5.4.tgz",
"integrity": "sha512-N7kHdlgsO/v+iD/dMoJKtsSqs5Dz/dXZVebRgJw23LDk+jMi/974zyiOYDziY2JPp8xivq9BmUGwIJMiuSBi7w==",
"dev": true,
"dependencies": {
"tabbable": "^6.2.0"
}
},
"node_modules/for-each": {
"version": "0.3.3",
"resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz",
@@ -8479,6 +8567,19 @@
"node": ">=12"
}
},
"node_modules/svelte-sequential-preprocessor": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/svelte-sequential-preprocessor/-/svelte-sequential-preprocessor-2.0.1.tgz",
"integrity": "sha512-N5JqlBni6BzElxmuFrOPxOJnjsxh1cFDACLEVKs8OHBcx8ZNRO1p5SxuQex1m3qbLzAC8G99EHeWcxGkjyKjLQ==",
"dev": true,
"dependencies": {
"svelte": "^4.0.1",
"tslib": "~2.6.0"
},
"engines": {
"node": ">=16"
}
},
"node_modules/svelte/node_modules/aria-query": {
"version": "5.3.0",
"resolved": "https://registry.npmjs.org/aria-query/-/aria-query-5.3.0.tgz",
@@ -8528,6 +8629,12 @@
"integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==",
"dev": true
},
"node_modules/tabbable": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/tabbable/-/tabbable-6.2.0.tgz",
"integrity": "sha512-Cat63mxsVJlzYvN51JmVXIgNoUokrIaT2zLclCXjRd8boZ0004U4KCs/sToJ75C6sdlByWxpYnb5Boif1VSFew==",
"dev": true
},
"node_modules/test-exclude": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/test-exclude/-/test-exclude-6.0.0.tgz",
+3
View File
@@ -40,6 +40,8 @@
"web-vitals": "^3.4.0"
},
"devDependencies": {
"@melt-ui/pp": "^0.1.4",
"@melt-ui/svelte": "^0.65.1",
"@playwright/test": "^1.37.1",
"@sveltejs/adapter-static": "^2.0.3",
"@sveltejs/kit": "^1.24.0",
@@ -65,6 +67,7 @@
"svelte-check": "^3.5.1",
"svelte-jester": "^2.3.2",
"svelte-preprocess": "^5.0.4",
"svelte-sequential-preprocessor": "^2.0.1",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"vite": "^4.4.9",
+114 -134
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import { DropList } from '$lib/components';
import { clickOnEnter } from '$lib/helpers/a11y';
import { createCombobox, melt } from '@melt-ui/svelte';
import { createEventDispatcher, onMount } from 'svelte';
import { Label } from '.';
@@ -29,17 +29,38 @@
// stretch is used inside of a flex container to give the element flex:1
export let stretch = true;
export let search = '';
// The actual selected value
export let value: Option['value'];
const {
elements: { input, menu, option },
states: { selected: innerSelected, inputValue, open },
helpers: { isHighlighted },
options: comboOptions
} = createCombobox<Option['value']>({
onSelectedChange({ next }) {
if (next) {
value = next.value;
dispatch('select', next);
}
return next;
},
preventScroll: false
});
$: {
comboOptions.disabled.set(disabled);
comboOptions.required.set(required);
}
$: inputValue.set(search);
$: search = $inputValue;
$: selectedOption = options.find((option) => option.value === value);
$: selectedOption && innerSelected.set(selectedOption);
let element: HTMLInputElement;
let hasFocus = false;
let selected: number = null;
$: if (!hasFocus) {
selected = null;
}
const dispatch = createEventDispatcher();
@@ -53,158 +74,117 @@
hasFocus = true;
}
function handleKeydown(event: KeyboardEvent) {
event.stopImmediatePropagation();
switch (event.key) {
case 'Escape':
hasFocus = false;
break;
case 'ArrowDown':
if (selected === null) {
selected = 0;
} else {
selected = (selected + 1) % options.length;
}
break;
case 'ArrowUp':
if (selected === null) {
selected = options.length - 1;
} else {
selected = (selected - 1 + options.length) % options.length;
}
break;
case 'Enter':
if (selected !== null) {
event.preventDefault();
selectOption(options[selected]);
}
break;
}
}
function selectOption(option: Option) {
value = option.value;
search = option.label;
// It's not working without this line.
if (!$$slots.output) {
element.value = search;
} else {
search = '';
}
hasFocus = false;
dispatch('select', option);
}
function clearOption() {
search = '';
value = null;
!$$slots.output && (element.value = search);
hasFocus = false;
inputValue.set('');
innerSelected.set(null);
dispatch('reset');
}
$: showClearBtn = (hasFocus && search) || value;
$: console.log(disabled);
</script>
<li
<div
class="u-position-relative form-item"
class:u-width-full-line={fullWidth}
class:u-stretch={stretch}>
<DropList
bind:show={hasFocus}
noStyle
noArrow
scrollable
placement="bottom-end"
position="static"
fullWidth={true}
fixed>
<Label {required} {hideRequired} {optionalText} hide={!showLabel} for={id} {tooltip}>
{label}
</Label>
<Label {required} {hideRequired} {optionalText} hide={!showLabel} for={id} {tooltip}>
{label}
</Label>
<div class="custom-select">
<div class="input-text-wrapper" style="--amount-of-buttons:2">
{#if $$slots.output && selectedOption}
<div
role="button"
tabindex="0"
on:keyup={clickOnEnter}
on:click={() => {
if (interactiveOutput) hasFocus = !hasFocus;
}}>
<slot name="output" option={selectedOption} />
</div>
{:else}
<input
type="text"
class="input-text"
{placeholder}
{disabled}
{required}
bind:value={search}
bind:this={element}
on:focus={() => (hasFocus = true)}
on:click={() => (hasFocus = true)}
on:input={handleInput}
on:keydown={handleKeydown} />
{/if}
<div class="custom-select">
<div class="input-text-wrapper" style="--amount-of-buttons:2">
{#if $$slots.output && selectedOption}
<div
role="button"
tabindex="0"
on:keyup={clickOnEnter}
on:click={() => {
if (interactiveOutput) hasFocus = !hasFocus;
}}>
<slot name="output" option={selectedOption} />
</div>
{:else}
<input
type="text"
class="input-text"
{placeholder}
{required}
on:input={handleInput}
bind:this={element}
use:melt={$input} />
{/if}
<div class="options-list">
{#if showClearBtn}
<button
class="options-list-button"
aria-label="clear field"
type="button"
disabled={!interactiveOutput}
on:click|preventDefault={clearOption}>
<span class="icon-x" aria-hidden="true" />
</button>
{/if}
<div class="options-list">
{#if showClearBtn}
<button
class="options-list-button"
aria-label="clear field"
type="button"
disabled={!interactiveOutput}
on:click={() => (hasFocus = !hasFocus)}>
<span class="icon-cheveron-down" aria-hidden="true" />
on:click|preventDefault={clearOption}>
<span class="icon-x" aria-hidden="true" />
</button>
</div>
{/if}
<button
class="options-list-button"
type="button"
disabled={!interactiveOutput}
on:click={() => {
hasFocus = !hasFocus;
open.set(true);
}}>
<span class="icon-cheveron-down" aria-hidden="true" />
</button>
</div>
</div>
<svelte:fragment slot="list">
{#each options as option, i}
<li class="drop-list-item">
<button
class="drop-button"
class:is-selected={selected === i}
type="button"
on:click|preventDefault={() => {
selectOption(option);
}}>
<slot {option}>
<span class="text" data-private>{option.label}</span>
</slot>
</button>
</li>
{:else}
<li class="drop-list-item">
<span class="text">There are no {name} that match your search</span>
</li>
{/each}
</svelte:fragment>
</DropList>
</li>
<div class="drop-wrapper" use:melt={$menu}>
<div class="drop is-no-arrow">
<section class="drop-section">
<ul class="drop-list">
{#each options as o}
<li class="drop-list-item">
<button
class="drop-button"
class:is-selected={$isHighlighted(o.value)}
type="button"
use:melt={$option({ value: o.value, label: o.label })}>
<slot option={o}>
<span class="text" data-private>{o.label}</span>
</slot>
</button>
</li>
{:else}
<li class="drop-list-item">
<span class="text">There are no {name} that match your search</span>
</li>
{/each}
</ul>
</section>
</div>
</div>
</div>
</div>
<style>
.form-item :global(.drop) {
translate: 0 4px;
}
.form-item :global(.drop-section) {
width: 100%;
margin-inline: initial;
max-inline-size: initial;
.drop-section {
margin-inline: unset;
/* padding-inline: unset; */
max-inline-size: unset;
}
.drop {
position: static;
inset-inline-start: unset;
inset-block-end: unset;
min-inline-size: unset;
max-inline-size: unset;
inline-size: unset;
}
</style>
@@ -37,7 +37,10 @@
$: options = ($attributes as Models.AttributeString[])
.filter(
(attr) =>
attr.type === 'string' && !attr?.array && !names?.some((name) => name === attr.key)
attr.type === 'string' &&
!attr?.array &&
!names?.some((name) => name === attr.key) &&
attr.key.toLowerCase().includes(search?.toLowerCase())
)
.map((attr) => {
return {
+4 -4
View File
@@ -1,11 +1,12 @@
import { preprocessMeltUI } from '@melt-ui/pp';
import sequence from 'svelte-sequential-preprocessor';
import adapter from '@sveltejs/adapter-static';
import preprocess from 'svelte-preprocess';
/** @type {import('@sveltejs/kit').Config} */
/** @type {import('@sveltejs/kit').Config}*/
const config = {
// Consult https://github.com/sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: preprocess(),
preprocess: sequence([preprocess(), preprocessMeltUI()]),
compilerOptions: {
accessors: process.env.VITEST
},
@@ -26,5 +27,4 @@ const config = {
}
}
};
export default config;