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

refactor: improve language select style #77

Merged
merged 1 commit into from
Nov 27, 2024
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
31 changes: 30 additions & 1 deletion src/components/LanguageSelector.astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ const alternateLinks = Object.fromEntries(
getLocaleUrl(Astro.url.pathname, locale),
]),
);
const selectedWidthCalcId =
"selected-width-calc-" + Math.random().toString(36).substring(7);
const { class: className, ...rest } = Astro.props;
---

<astro-language-selector data-links={JSON.stringify(alternateLinks)}>
<astro-language-selector
data-links={JSON.stringify(alternateLinks)}
data-width-calc-id={selectedWidthCalcId}
>
<select class={className} {...rest}>
{
Object.entries(localeDisplay).map(([value, display]) => (
Expand All @@ -27,6 +32,14 @@ const { class: className, ...rest } = Astro.props;
}
</select>
</astro-language-selector>
<select
id={selectedWidthCalcId}
class={className}
{...rest}
style="visibility: hidden; position: absolute; top: -9999px; pointer-events: none; user-select: none;"
>
<option></option>
</select>

<style>
.option {
Expand All @@ -43,12 +56,28 @@ const { class: className, ...rest } = Astro.props;
connectedCallback() {
const select = this.querySelector("select");
const links = JSON.parse(this.dataset.links ?? "{}");
const selectedWidthCalcId = this.dataset.widthCalcId;

// Select tag cannot have dynamic width that fits its content without JavaScript.
const calcWidth = () => {
if (!select || !selectedWidthCalcId) return;
const selected = select.querySelector("option:checked");
const calc = document.getElementById(selectedWidthCalcId);
const calcOption = calc?.querySelector("option");
if (selected && calc && calcOption) {
calcOption.textContent = selected.textContent;
select.style.width = `${calc.offsetWidth}px`;
}
};

calcWidth();

if (select) {
select.addEventListener("change", () => {
const locale = select.value;
const url = links[locale];
window.location.href = url;
calcWidth();
});
}
}
Expand Down