Skip to content
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

Improve Language Selector dropdowns #254

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 42 additions & 40 deletions src/components/LanguageSelector.tsx
Original file line number Diff line number Diff line change
@@ -152,9 +152,13 @@ const LanguageSelector = () => {

useEffect(() => {
if (isOpen && focusedIndex >= 0) {
const element = document.querySelector(
`.selector__item:nth-child(${focusedIndex + 1})`
) as HTMLElement;
const elements = Array.from(
document.querySelectorAll(".selector__item")
) as HTMLElement[];
const focusableElements = elements.filter(
(el) => el.getAttribute("tabIndex") !== "-1"
);
const element = focusableElements[focusedIndex];
element?.focus();
}
}, [isOpen, focusedIndex]);
@@ -186,43 +190,41 @@ const LanguageSelector = () => {
</div>
<span className="selector__arrow" />
</button>
{isOpen && (
<ul
className="selector__dropdown"
role="listbox"
onKeyDown={handleKeyDown}
tabIndex={-1}
>
{fetchedLanguages.map((lang, index) =>
lang.subLanguages.length > 0 ? (
<SubLanguageSelector
key={lang.name}
opened={openedLanguages.includes(lang)}
parentLanguage={lang}
onDropdownToggle={handleToggleSubLanguage}
handleParentSelect={handleSelect}
afterSelect={afterSelect}
/>
) : (
<li
key={lang.name}
role="option"
tabIndex={-1}
onClick={() => handleSelect(lang)}
className={`selector__item ${
language.name === lang.name ? "selected" : ""
} ${focusedIndex === index ? "focused" : ""}`}
aria-selected={language.name === lang.name}
>
<label>
<img src={lang.icon} alt="" />
<span>{lang.name}</span>
</label>
</li>
)
)}
</ul>
)}
<ul
className={`selector__dropdown ${isOpen ? "" : " hidden"}`}
role="listbox"
onKeyDown={handleKeyDown}
tabIndex={0}
>
{fetchedLanguages.map((lang, index) =>
lang.subLanguages.length > 0 ? (
<SubLanguageSelector
key={lang.name}
opened={openedLanguages.includes(lang)}
parentLanguage={lang}
onDropdownToggle={handleToggleSubLanguage}
handleParentSelect={handleSelect}
afterSelect={afterSelect}
/>
) : (
<li
key={lang.name}
role="option"
tabIndex={0}
onClick={() => handleSelect(lang)}
className={`selector__item ${
language.name === lang.name ? "selected" : ""
} ${focusedIndex === index ? "focused" : ""}`}
aria-selected={language.name === lang.name}
>
<label>
<img src={lang.icon} alt="" />
<span>{lang.name}</span>
</label>
</li>
)
)}
</ul>
</div>
);
};
38 changes: 19 additions & 19 deletions src/components/SubLanguageSelector.tsx
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ const SubLanguageSelector = ({
<>
<li
role="option"
tabIndex={-1}
tabIndex={0}
className={`selector__item ${
subLanguage === defaultSlugifiedSubLanguageName &&
language.name === parentLanguage.name
@@ -78,24 +78,24 @@ const SubLanguageSelector = ({
</label>
</li>

{opened &&
parentLanguage.subLanguages.map((sl) => (
<li
key={sl.name}
role="option"
tabIndex={-1}
className={`selector__item sublanguage__item ${
slugify(subLanguage) === slugify(sl.name) ? "selected" : ""
}`}
aria-selected={slugify(subLanguage) === slugify(sl.name)}
onClick={handleSubLanguageSelect(sl)}
>
<label>
<img src={sl.icon} alt={sl.name} />
<span>{sl.name}</span>
</label>
</li>
))}
{parentLanguage.subLanguages.map((sl) => (
<li
key={sl.name}
role="option"
tabIndex={opened ? 0 : -1}
aria-disabled={!opened}
className={`selector__item sublanguage__item ${opened ? "" : "hidden"} ${
slugify(subLanguage) === slugify(sl.name) ? "selected" : ""
}`}
aria-selected={slugify(subLanguage) === slugify(sl.name)}
onClick={handleSubLanguageSelect(sl)}
>
<label>
<img src={sl.icon} alt={sl.name} />
<span>{sl.name}</span>
</label>
</li>
))}
</>
);
};
44 changes: 40 additions & 4 deletions src/styles/main.css
Original file line number Diff line number Diff line change
@@ -400,7 +400,7 @@ abbr {
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-top: 7px solid var(--clr-text-primary); /* [1] */
transition: transform 100ms ease;
transition: transform 300ms ease;
}

.selector--open .selector__arrow {
@@ -413,7 +413,7 @@ abbr {

position: absolute;
width: 100%;
max-height: 20rem;
height: 50vh;
overflow-y: auto;

background-color: var(--clr-bg-secondary);
@@ -426,10 +426,28 @@ abbr {

box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
z-index: 1;
transition: all 300ms ease;
}

.selector__dropdown:focus-within {
border-color: var(--clr-accent);
.selector__dropdown.hidden {
border: none;
padding: 0;
height: 0;
opacity: 0;
overflow: hidden;
}

.selector__dropdown::-webkit-scrollbar {
width: 8px;
}

.selector__dropdown::-webkit-scrollbar-thumb {
background: var(--scrollbar-thumb);
border-radius: 4px;
}

.selector__dropdown::-webkit-scrollbar-thumb:hover {
background: var(--scrollbar-thumb);
}

.selector__item {
@@ -439,10 +457,19 @@ abbr {
gap: 1rem;
align-items: center;
border-radius: var(--br-md);
transition: all 300ms ease;
}

.sublanguage__item {
height: 3rem;
margin-left: 1.5rem;
transition: all 300ms ease;
}

.sublanguage__item.hidden {
height: 0;
opacity: 0;
overflow: hidden;
}

.sublanguage__button{
@@ -462,6 +489,11 @@ abbr {
transform: rotate(-90deg);
transition: transform 100ms ease;
cursor: pointer;
transition: all 200ms ease;
}

.sublanguage__arrow:hover {
border-top-color: var(--clr-accent);
}

[aria-expanded="true"] .sublanguage__arrow {
@@ -472,6 +504,10 @@ abbr {
border-top-color: var(--clr-text-tertiary);
}

.selector__item.selected .sublanguage__arrow:hover {
border-top-color: var(--clr-text-primary);
}

.selector__item label {
width: 100%;
padding: 0.25em 0.75em;