-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat pro 1274 pinkv2 combobox #225
Open
ernstmul
wants to merge
12
commits into
v2
Choose a base branch
from
feat-PRO-1274-pinkv2-combobox
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dbd11ca
First combobox setup
ernstmul 2e954ce
Add label prop
ernstmul 616bdce
Add max height
ernstmul e4fe4ae
Pure css scroll indicator
ernstmul 490fddd
No shadow scroll indicator, but half item cutoff
ernstmul 2225fcb
Fix darkmode
ernstmul d0177a2
Cleanup
ernstmul a5b5e80
Typescript fix
ernstmul 315e244
Give more time to propagate the click, but also clear it straight awa…
ernstmul fe14ed8
Keep the actively focussed element in view
ernstmul 595b748
Use Icon component instead of hardcoded svg code
ernstmul 289c2b6
Added restprops for required support
ernstmul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<script lang="ts"> | ||
import type { ComboboxOption, ComboboxProps } from '$lib/combobox/index.js'; | ||
|
||
export let placeholder: ComboboxProps['placeholder'] = ''; | ||
export let disabled: ComboboxProps['disabled'] = false; | ||
export let options: ComboboxProps['options'] = []; | ||
export let label: ComboboxProps['label'] = ''; | ||
|
||
$: hasFocus = false; | ||
let currentActiveIndex: number | null = null; | ||
let selectedOption: ComboboxOption | null = null; | ||
let filteredOptions = options; | ||
let inputTextValue: string | null = null; | ||
|
||
function selectOption(optionIndex: number) { | ||
selectedOption = filteredOptions[optionIndex]; | ||
inputTextValue = selectedOption.value; | ||
hasFocus = false; | ||
currentActiveIndex = null; | ||
} | ||
|
||
function handleKeydown(event: KeyboardEvent) { | ||
if (hasFocus) { | ||
switch (event.key) { | ||
case 'ArrowUp': | ||
if (currentActiveIndex !== null && currentActiveIndex > 0) { | ||
currentActiveIndex--; | ||
} else if (currentActiveIndex === 0) { | ||
currentActiveIndex = null; | ||
} | ||
|
||
break; | ||
case 'ArrowDown': | ||
if (currentActiveIndex !== null && currentActiveIndex < options.length - 1) { | ||
currentActiveIndex++; | ||
} else if (currentActiveIndex === null) { | ||
currentActiveIndex = 0; | ||
} | ||
break; | ||
case 'Enter': | ||
if (currentActiveIndex !== null) { | ||
selectOption(currentActiveIndex); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<div | ||
class="combobox-container" | ||
tabindex="0" | ||
on:keydown={handleKeydown} | ||
role="combobox" | ||
aria-expanded={hasFocus} | ||
data-active-option={currentActiveIndex ? `option-${options[currentActiveIndex].key}` : ''} | ||
aria-controls="comboboxoptions" | ||
> | ||
<label | ||
><span>{label}</span> | ||
<div class="combobox-input" class:disabled> | ||
<input | ||
type="text" | ||
{placeholder} | ||
{disabled} | ||
on:focus={() => { | ||
hasFocus = true; | ||
}} | ||
on:focusout={() => { | ||
setTimeout(() => { | ||
hasFocus = false; | ||
currentActiveIndex = null; | ||
filteredOptions = options; | ||
}, 100); | ||
}} | ||
bind:value={inputTextValue} | ||
on:input={() => { | ||
filteredOptions = options.filter((option) => | ||
option.value.toLowerCase().includes(inputTextValue.toLowerCase()) | ||
); | ||
}} | ||
/> | ||
<svg | ||
width="20" | ||
height="20" | ||
viewBox="0 0 20 20" | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
fill-rule="evenodd" | ||
clip-rule="evenodd" | ||
d="M5.29289 7.29289C5.68342 6.90237 6.31658 6.90237 6.70711 7.29289L10 10.5858L13.2929 7.29289C13.6834 6.90237 14.3166 6.90237 14.7071 7.29289C15.0976 7.68342 15.0976 8.31658 14.7071 8.70711L10.7071 12.7071C10.3166 13.0976 9.68342 13.0976 9.29289 12.7071L5.29289 8.70711C4.90237 8.31658 4.90237 7.68342 5.29289 7.29289Z" | ||
/> | ||
</svg> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep we can! Didn't use that one before! Good suggestion, thanks! |
||
</div> | ||
</label> | ||
<div class="combobox-menu" class:hidden={!hasFocus || filteredOptions.length === 0}> | ||
<ul id="comboboxoptions" role="listbox" aria-label="Options"> | ||
{#each filteredOptions as option, index} | ||
<li | ||
class="option" | ||
class:active={currentActiveIndex === index} | ||
id={`option-${option.key}`} | ||
on:mouseenter={() => { | ||
currentActiveIndex = index; | ||
}} | ||
> | ||
<button | ||
on:click={() => { | ||
selectOption(index); | ||
}} | ||
> | ||
{option.value} | ||
</button> | ||
</li> | ||
{/each} | ||
</ul> | ||
</div> | ||
</div> | ||
|
||
<style lang="scss"> | ||
@use './combobox'; | ||
|
||
@include combobox.base; | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
@mixin base { | ||
label { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 6px; | ||
} | ||
|
||
.combobox-container { | ||
position: relative; | ||
} | ||
|
||
.combobox-input { | ||
display: flex; | ||
padding: var(--space-3, 6px) var(--space-4, 8px) var(--space-3, 6px) var(--space-6, 12px); | ||
align-items: center; | ||
gap: var(--space-7, 16px); | ||
align-self: stretch; | ||
border-radius: 8px; | ||
border: var(--border-width-S, 1px) solid var(--color-border-neutral, #e4e4e7); | ||
background: var(--color-bgcolor-neutral-primary, #fff); | ||
|
||
box-sizing: border-box; | ||
|
||
input { | ||
flex-grow: 1; | ||
&::placeholder { | ||
color: var(--color-fgColor-neutral-tertiary, #97979b); | ||
} | ||
} | ||
|
||
&:hover { | ||
border: var(--border-width-S, 1px) solid var(--color-border-focus, #818186); | ||
} | ||
|
||
&:focus-within { | ||
box-shadow: 0 0 0 1px var(--color-border-focus, #818186); | ||
border-color: var(--color-border-focus, #818186); | ||
|
||
svg { | ||
transform: rotate(180deg); | ||
} | ||
} | ||
|
||
&.disabled { | ||
border: var(--border-width-S, 1px) solid var(--color-border-neutral-strong, #d8d8db); | ||
background: var(--color-bgColor-neutral-tertiary, #ededf0); | ||
} | ||
|
||
svg { | ||
transform: rotate(0deg); | ||
transition: transform 0.2s; | ||
fill: var(--color-fgColor-neutral-secondary, #56565c); | ||
} | ||
} | ||
|
||
.combobox-menu { | ||
display: flex; | ||
position: absolute; | ||
width: 100%; | ||
padding: var(--gap-XXS, 4px); | ||
flex-direction: column; | ||
align-items: flex-start; | ||
gap: var(--gap-XXXS, 2px); | ||
border-radius: var(--border-radius-M, 12px); | ||
border: var(--border-width-S, 1px) solid var(--color-border-neutral, #e4e4e7); | ||
background: var(--color-bgcolor-neutral-primary, #fff); | ||
margin-top: 8px; | ||
max-height: 275px; | ||
overflow-y: scroll; | ||
|
||
/* box-shadow/neutral/S */ | ||
box-shadow: | ||
0 1px 3px 0 rgba(0, 0, 0, 0.03), | ||
0 4px 4px 0 rgba(0, 0, 0, 0.04); | ||
|
||
ul, | ||
button { | ||
width: 100%; | ||
} | ||
|
||
button { | ||
cursor: pointer; | ||
height: 100%; | ||
padding-top: var(--space-3, 6px); | ||
padding-bottom: var(--space-3, 6px); | ||
} | ||
|
||
.option { | ||
display: flex; | ||
padding: 0 var(--space-4, 8px) 0 var(--space-5, 10px); | ||
align-items: center; | ||
gap: var(--gap-S, 8px); | ||
align-self: stretch; | ||
|
||
&.active { | ||
border-radius: var(--border-radius-S, 8px); | ||
background: var(--color-overlay-neutral-hover, rgba(25, 25, 28, 0.03)); | ||
} | ||
} | ||
} | ||
.hidden { | ||
display: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { HTMLInputAttributes } from 'svelte/elements'; | ||
|
||
export type ComboboxOption = { key: string; value: string }; | ||
export type ComboboxProps = HTMLInputAttributes & { | ||
label: string; | ||
options: Array<ComboboxOption>; | ||
}; | ||
|
||
export { default as Combobox } from './Combobox.svelte'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<script context="module" lang="ts"> | ||
import type { MetaProps } from '@storybook/addon-svelte-csf'; | ||
import { Combobox } from '$lib/combobox/index.js'; | ||
|
||
export const meta: MetaProps = { | ||
title: 'Components/Combobox', | ||
component: Combobox, | ||
args: { | ||
placeholder: 'Placeholder', | ||
disabled: false, | ||
label: 'My combobox', | ||
options: [ | ||
{ key: 'RU', value: 'Russia' }, | ||
{ key: 'CA', value: 'Canada' }, | ||
{ key: 'US', value: 'United States' }, | ||
{ key: 'CN', value: 'China' }, | ||
{ key: 'BR', value: 'Brazil' }, | ||
{ key: 'AU', value: 'Australia' }, | ||
{ key: 'IN', value: 'India' }, | ||
{ key: 'AR', value: 'Argentina' }, | ||
{ key: 'KZ', value: 'Kazakhstan' }, | ||
{ key: 'DZ', value: 'Algeria' }, | ||
{ key: 'CD', value: 'Democratic Republic of the Congo' }, | ||
{ key: 'SA', value: 'Saudi Arabia' }, | ||
{ key: 'MX', value: 'Mexico' }, | ||
{ key: 'ID', value: 'Indonesia' }, | ||
{ key: 'SD', value: 'Sudan' }, | ||
{ key: 'LY', value: 'Libya' }, | ||
{ key: 'IR', value: 'Iran' }, | ||
{ key: 'MN', value: 'Mongolia' }, | ||
{ key: 'PE', value: 'Peru' }, | ||
{ key: 'TD', value: 'Chad' }, | ||
{ key: 'NE', value: 'Niger' }, | ||
{ key: 'AO', value: 'Angola' }, | ||
{ key: 'ML', value: 'Mali' }, | ||
{ key: 'ZA', value: 'South Africa' }, | ||
{ key: 'CO', value: 'Colombia' }, | ||
{ key: 'ET', value: 'Ethiopia' }, | ||
{ key: 'BO', value: 'Bolivia' }, | ||
{ key: 'MA', value: 'Morocco' }, | ||
{ key: 'EG', value: 'Egypt' }, | ||
{ key: 'TZ', value: 'Tanzania' }, | ||
{ key: 'NG', value: 'Nigeria' }, | ||
{ key: 'VE', value: 'Venezuela' }, | ||
{ key: 'PK', value: 'Pakistan' }, | ||
{ key: 'NA', value: 'Namibia' }, | ||
{ key: 'MO', value: 'Mozambique' }, | ||
{ key: 'TR', value: 'Turkey' }, | ||
{ key: 'CL', value: 'Chile' }, | ||
{ key: 'ZM', value: 'Zambia' }, | ||
{ key: 'MM', value: 'Myanmar' }, | ||
{ key: 'AF', value: 'Afghanistan' }, | ||
{ key: 'SS', value: 'South Sudan' }, | ||
{ key: 'FR', value: 'France' }, | ||
{ key: 'SO', value: 'Somalia' }, | ||
{ key: 'CF', value: 'Central African Republic' }, | ||
{ key: 'UA', value: 'Ukraine' }, | ||
{ key: 'MG', value: 'Madagascar' }, | ||
{ key: 'BW', value: 'Botswana' }, | ||
{ key: 'KE', value: 'Kenya' }, | ||
{ key: 'YE', value: 'Yemen' }, | ||
{ key: 'TH', value: 'Thailand' } | ||
] | ||
}, | ||
argTypes: { | ||
placeholder: { | ||
control: { type: 'text' } | ||
}, | ||
disabled: { | ||
control: { type: 'boolean' } | ||
}, | ||
options: { | ||
control: false | ||
} | ||
} | ||
}; | ||
</script> | ||
|
||
<script> | ||
import { Story, Template } from '@storybook/addon-svelte-csf'; | ||
</script> | ||
|
||
<Template let:args> | ||
<div style="width: 320px"> | ||
<Combobox {...args} /> | ||
</div> | ||
</Template> | ||
|
||
<Story name="Default" /> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might need the
required
state like for other inputsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added $$restProps to the input
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if the $$restProps is enough... I think we might need to show a
*
on the label too when it's required (double check with Caio)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the
$$restProps
came in handy because we extend the props fromHTMLInputAttributes
, so we get all those optional props right there. Asked Caio about the*